FastAPI Depends() DI flagged by Flake8 Bugbear Linting

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
adam-olemacommented, 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
6reactions
ShaneNolancommented, May 10, 2022

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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Flake8 throws B008 fastapi data type definitions - Stack Overflow
One solution is to to update your flake8 configuration (e.g. in your setup.cfg file) to ignore this rule for the Depends(.
Read more >
Dependencies - First Steps - FastAPI - tiangolo
FastAPI has a very powerful but intuitive Dependency Injection system. ... from typing import Union from fastapi import Depends, FastAPI app = FastAPI()...
Read more >
The Ultimate FastAPI Tutorial Part 11 - Dependency Injection ...
Practical Section 1 - Using DI in FastAPI. To begin, I'll flag which files have changed in the accompanying example code repo app...
Read more >
ftp1.nluug.nl/os/Linux/distr/opensuse/tumbleweed/r...
shutdownOutput() should wait for writes in progress * Test + Research ... under some circumstances * repo: remove ldns leftovers * depends: remove...
Read more >
Dependency Injection in FastAPI - Level Up Coding
Dependency injection is a concept in programming where an object receive other objects that it depends on. The other objects are called ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found