Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

FastAPI Depends() DI flagged by Flake8 Bugbear Linting #1522

Closed
shawnwall opened this issue Jun 5, 2020 · 6 comments
Closed

FastAPI Depends() DI flagged by Flake8 Bugbear Linting #1522

shawnwall opened this issue Jun 5, 2020 · 6 comments

Comments

@shawnwall
Copy link

Flake8 Bugbear is a linting tool that points out potential bugs and design problems in applications. While doing linting on my FastAPI project, bugbear calls out:

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.

... for any Depends() usage in a method signature, such as:

async def login(response: Response,
                request: Request,
                form_data:
                OAuth2PasswordRequestForm = Depends())

I'm not flagging this as a bug, but just wanted to raise it to get your design opinion, as apparently someone out there thinks the Depends() pattern is not very pythonic.

@phy25
Copy link

phy25 commented Jun 6, 2020

It is felt that these lints don’t belong in the main Python tools as they are very opinionated and do not have a PEP or standard behind them. Due to flake8 being designed to be extensible, the original creators of these lints believed that a plugin was the best route. This has resulted in better development velocity for contributors and adaptive deployment for flake8 users.

https://pypi.org/project/flake8-bugbear/

@tiangolo
Copy link
Member

tiangolo commented Jun 7, 2020

Thanks for the help here @phy25 ! 👏 🙇

Yep, I think that should answer the question ✔️

@github-actions
Copy link
Contributor

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

@ghost
Copy link

ghost commented Oct 30, 2021

For those who want to keep bugbear but not disable the checks entirely for every line that uses Depends, the following small plugin will amend bugbear's rules to recognize Depends as immutable, Unfortunately, BugBear does not provide a simpler configuration process.



from typing import Any

import bugbear

class Extension(object):
    name = "bugbear_fastapi_depends"
    version = 1

    def __init__(self, noqa: Any) -> None:
        pass

bugbear.B008.immutable_calls.update([
    "fastapi.Depends",
    "fastapi.params.Depends",
    "Depends",
])

For local usage, from setup.cfg, you can do:

[flake8:local-plugins]
extension =
     BB8F = module.of.my.extension:Extension

@ShaneNolan
Copy link

Anyone using a setup.cfg flake8 configuration can use the extend-immutable-calls keyword to allow Depends().

[flake8]
...
extend-immutable-calls = fastapi.Depends, fastapi.params.Depends

@phouverneyuff
Copy link

Using ruff, I had set this on pyproject.toml

[tool.ruff.flake8-bugbear]
extend-immutable-calls = ["fastapi.Depends", "fastapi.params.Depends", "fastapi.Query", "fastapi.params.Query"]

@tiangolo tiangolo added question Question or problem reviewed labels Feb 23, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@github-actions github-actions bot removed the answered label Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7463 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

5 participants