Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextvar mutable or call default #476

Merged
merged 4 commits into from
Jun 25, 2024

Conversation

jakkdl
Copy link
Contributor

@jakkdl jakkdl commented Jun 23, 2024

fixes #475

Not the prettiest extension of FunctionDefDefaultsVisitor, and left e.g. B006.mutable_literals as bound to B006 and the naming of b008_extend_immutable_calls. I'll fix those later unless we want to reuse B006/B008 for ContextVars, or maybe just a couple comments saying that B039 also uses them.

Also adding 3.13 to CI to check for any problems there

Copy link
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. This is a nice bug to ignore which fits in with out things we highlight. Many thanks!

@@ -9,7 +9,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13-dev"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to take it as we've lucked out. We should do this in dedicated PRs in future in case there is fallout to deal with.

@cooperlees
Copy link
Collaborator

P.s. Let's make isort happy

@jakkdl
Copy link
Contributor Author

jakkdl commented Jun 25, 2024

Added some comments to note where b006/b008 stuff also applies to b039, and fixed isort. Ready to merge unless @Zac-HD wants to have opinions

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - thanks, @jakkdl for implementation and @cooperlees for review 🙏

@Zac-HD Zac-HD merged commit 188eab8 into PyCQA:main Jun 25, 2024
7 checks passed
@jakkdl jakkdl deleted the contextvar_mutable_or_call_default branch June 26, 2024 14:10
charliermarsh pushed a commit to astral-sh/ruff that referenced this pull request Jul 1, 2024
## 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)](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,
namely


https://github.com/astral-sh/ruff/blob/f765d194028ef0ebd01ef0a21e30732d4d5ff184/crates/ruff_linter/src/rules/flake8_bugbear/rules/function_call_in_argument_default.rs#L104-L106

and 


https://github.com/astral-sh/ruff/blob/f765d194028ef0ebd01ef0a21e30732d4d5ff184/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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: expand B006 and B008 to also cover ContextVar defaults
3 participants