mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-18 01:20:24 +00:00
[flake8-bugbear
] Implement mutable-contextvar-default (B039) (#12113)
## Summary Implement mutable-contextvar-default (B039) which was added to flake8-bugbear in https://github.com/PyCQA/flake8-bugbear/pull/476. This rule is similar to [mutable-argument-default (B006)](https://docs.astral.sh/ruff/rules/mutable-argument-default) and [function-call-in-default-argument (B008)](https://docs.astral.sh/ruff/rules/function-call-in-default-argument), except that it checks the `default` keyword argument to `contextvars.ContextVar`. ``` B039.py:19:26: B039 Do not use mutable data structures for ContextVar defaults | 18 | # Bad 19 | ContextVar("cv", default=[]) | ^^ B039 20 | ContextVar("cv", default={}) 21 | ContextVar("cv", default=list()) | = help: Replace with `None`; initialize with `.set()` after checking for `None` ``` In the upstream flake8-plugin, this rule is written expressly as a corollary to B008 and shares much of its logic. Likewise, this implementation reuses the logic of the Ruff implementation of B008, namelyf765d19402/crates/ruff_linter/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs (L104-L106)
andf765d19402/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs (L106)
Thus, this rule deliberately replicates B006's and B008's heuristics. For example, this rule assumes that all functions are mutable unless otherwise qualified. If improvements are to be made to B039 heuristics, they should probably be made to B006 and B008 as well (whilst trying to match the upstream implementation). This rule does not have an autofix as it is unknown where the ContextVar next used (and it might not be within the same file). Closes #12054 ## Test Plan `cargo nextest run`
This commit is contained in:
parent
85ede4a88c
commit
d80a9d9ce9
11 changed files with 314 additions and 0 deletions
|
@ -1233,6 +1233,7 @@ impl<'a> SemanticModel<'a> {
|
|||
"_typeshed" => self.seen.insert(Modules::TYPESHED),
|
||||
"builtins" => self.seen.insert(Modules::BUILTINS),
|
||||
"collections" => self.seen.insert(Modules::COLLECTIONS),
|
||||
"contextvars" => self.seen.insert(Modules::CONTEXTVARS),
|
||||
"dataclasses" => self.seen.insert(Modules::DATACLASSES),
|
||||
"datetime" => self.seen.insert(Modules::DATETIME),
|
||||
"django" => self.seen.insert(Modules::DJANGO),
|
||||
|
@ -1820,6 +1821,7 @@ bitflags! {
|
|||
const TYPESHED = 1 << 16;
|
||||
const DATACLASSES = 1 << 17;
|
||||
const BUILTINS = 1 << 18;
|
||||
const CONTEXTVARS = 1 << 19;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue