[flake8-bugbear
] Implement mutable-contextvar-default (B039)
#12113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Implement mutable-contextvar-default (B039) which was added to flake8-bugbear in PyCQA/flake8-bugbear#476.
This rule is similar to mutable-argument-default (B006) and function-call-in-default-argument (B008), except that it checks the
default
keyword argument tocontextvars.ContextVar
.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, namely
ruff/crates/ruff_linter/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs
Lines 104 to 106 in f765d19
and
ruff/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs
Line 106 in f765d19
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