Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flake8 throws B008 fastapi data type definitions

I have the following code, which processes a POST request via fastapi. However, flake8 keeps throwing the B008 Do not perform function calls in argument defaults. The call is performed only once at function definition time error.

@app.post("/predict", status_code=200)
def predict(
    page_no: int = Form(...), dimensions: list = Form(...), image: UploadFile = File(...)
) -> Dict[str, int]:

Is there a way to fix these warnings, or work around them?

like image 357
Dawny33 Avatar asked Jan 19 '26 18:01

Dawny33


1 Answers

One solution is to to update your flake8 configuration (e.g. in your setup.cfg file) to ignore this rule for the Depends(...) usage like this:

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

As of Dec 2022, you have to list the names manually, as the flake8-bugbear plugin doesn't know about the actual types (it's an AST parser), there is an open issue about it.

See more:

  • https://github.com/tiangolo/fastapi/issues/1522
  • https://github.com/PyCQA/flake8-bugbear/issues/252
like image 173
Elias Dorneles Avatar answered Jan 21 '26 06:01

Elias Dorneles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!