-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support an allowlist of function calls in argument defaults for B008 #3762
Comments
Yeah perhaps we add that to the list of |
It could be a good first issue. |
I think it's probably worth doing that and adding an allowlist. Looking at our codebase, we have some custom functions we'd want to allow while still benefiting from flagging this in general. |
…`datetime.timedelta()` in a function argument defaults for B008 ref: astral-sh#3762
Okay, I opened #3764 adding a few more immutable functions. I can work on another PR adding a setting if you're open to that! |
Oh wait, reading the code, it looks like B008 might already support |
Oh wait, you're right... |
Hmm and I don't see it used in https://github.com/charliermarsh/ruff/blob/main/crates/ruff/src/rules/flake8_bugbear/rules/mutable_argument_default.rs ? Is the doc saying it's used for B006 wrong and it's actually used for B008? |
Yeah something's a bit off between these. |
@rouge8 |
`pathlib.Path()` is immutable. ref: astral-sh#3762
Good catch! Added it in #3794 |
Why not |
So at this point should it be possible to use # test.py
from typing import TypeAlias
MyTypeAlias: TypeAlias = str
def func(
x: str = str("default"),
y: str = MyTypeAlias("default"),
):
return x + y # ruff.toml
[flake8-bugbear]
extend-immutable-calls = ["MyTypeAlias"] And running test.py:9:14: B008 Do not perform function call `MyTypeAlias` in argument defaults
Found 1 error. |
It should work for B008 if you use a fully-qualified path to the symbol, rather than the string name. For example: [flake8-bugbear]
extend-immutable-calls = ["path.to.module.MyTypeAlias"] Unfortunately, that won't work for symbols defined in the file in which they're used -- they have to be imported from another file. It's just a limitation which we'll resolve eventually (#5486). |
Unrelatedly, looking back at this issue, I see a few touch-ups we can do:
|
…ts with immutable annotations (#6784) Extends #6781 Part of #3762 <!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary <!-- What's the purpose of the change? What does it do, and why? --> Allows calls in argument defaults if the argument is annotated as an immutable type to avoid false positives. ## Test Plan <!-- How was it tested? --> Snapshots
It would be great if B008 supported an allowlist of function calls, similar to
extend-immutable-calls
for B006. We do a lot of this in our code which as far as I know has no subtle bugs:Or maybe it's worth hardcoding
Decimal
as an exception?The text was updated successfully, but these errors were encountered: