FastAPI Depends() DI flagged by Flake8 Bugbear Linting
See original GitHub issueFlake8 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:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
For local usage, from setup.cfg, you can do:
Anyone using a
setup.cfgflake8 configuration can use theextend-immutable-callskeyword to allowDepends().