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

extend-immutable-calls: how to configure? #212

Closed
DavidVujic opened this issue Dec 6, 2021 · 10 comments · Fixed by #218
Closed

extend-immutable-calls: how to configure? #212

DavidVujic opened this issue Dec 6, 2021 · 10 comments · Fixed by #218

Comments

@DavidVujic
Copy link

I haven't been able to make the recently released feature extend-immutable-calls to work.

Have I configured it wrong?

I have verified that flake8 recognizes the config, by setting the line-length to 10 and verified flake8 warnings about line length.

flake8 --version

4.0.1 (flake8-bugbear: 21.11.29, mccabe: 0.6.1, pycodestyle: 2.8.0, pyflakes: 2.4.0) CPython 3.10.0 on
Darwin

example.py

import fastapi


def example_fn(data: str = fastapi.Header(None)):
    pass

setup.cfg

[flake8]
max-line-length = 120
extend-immutable-calls = ["fastapi.Header"]
flake8 --show-source

./example.py:4:28: B008 Do not perform function calls in argument defaults.  The call is performed only once at function definition time. All calls to your function will reuse the result of that definition-time function call.  If this is intended, assign the function call to a module-level variable and use that variable as a default value.
def example_fn(data: str = fastapi.Header(None)):
                           ^
@aberres
Copy link

aberres commented Jan 17, 2022

Same problem here - I tried to silence pathlib.Path.

@sco1
Copy link
Contributor

sco1 commented Feb 1, 2022

You want:

[flake8]
max-line-length = 120
extend-immutable-calls = fastapi.Header

@cooperlees
Copy link
Collaborator

Do we feel we should add anything more to the README here? I'd accept a PR if so. Otherwise do we close?

@sco1
Copy link
Contributor

sco1 commented Feb 2, 2022

Even though it's just ConfigParser doing it's thing, it wouldn't hurt to add an example to the README (see also: #215).

I'm happy to include in #218 if you feel it's appropriate.

@aberres
Copy link

aberres commented Feb 2, 2022

Ah, great thanks.

And the matching happens by name, right?

So in the following case I would need extend-immutable-calls = Path to silence the warning.

from pathlib import Path

def foo(bar=Path()):
   ...

@aberres
Copy link

aberres commented Feb 2, 2022

And as we are here already: Would it make sense to add a hint to this parameter to the error message of B008?

@DavidVujic
Copy link
Author

Great, thank you for clarifying!

I had thought that the values should be defined as a (Python) array. Now, when typing something like this (for more than one fastAPI thing to allow), everything works as expected:

extend-immutable-calls = fastapi.Header, fastapi.Depends

@cooperlees
Copy link
Collaborator

Please at least add to the README to make it clearer. The message, I’d you can make a sane suggestion in code please do too. Thanks!

@copdips
Copy link

copdips commented Feb 9, 2023

Great, thank you for clarifying!

I had thought that the values should be defined as a (Python) array. Now, when typing something like this (for more than one fastAPI thing to allow), everything works as expected:

extend-immutable-calls = fastapi.Header, fastapi.Depends

You can use list format like this:

extend-immutable-calls =
    fastapi.Header,
    fastapi.Depends,

@npip99
Copy link

npip99 commented Apr 10, 2024

If anyone is using pyproject.toml,

[tool.ruff.lint.flake8-bugbear]
extend-immutable-calls = [
    # Whitelisted default arguments
    "fastapi.Header",
    "fastapi.Depends",
]

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 a pull request may close this issue.

6 participants