flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

B008: extend-immutable-calls does not work with imported function

Open Tishka17 opened this issue 3 years ago • 5 comments

I have small framework, which has Depends function simialr to fastapi feature. In my projects I want flake8-bubear to ignore only this funcion, but not any other function with same name. I've tried to set full link in flake config, but it does not work. I know that it is possible to always use full function name, but it looks dirty:

Example 1. Correct behavior - no B008 In config:

extend-immutable-calls=myapp.api.lambda_handler.depends.Depends

In code

import myapp.api.lambda_handler.depends

@app.handler
def get_data(
        session: Session = myapp.api.lambda_handler.depends.Depends(),
) -> HttpResponse[ListResponse[AA]]:

Example 2. Incorrect behavior - B008 In config:

extend-immutable-calls=myapp.api.lambda_handler.depends.Depends

In code

from myapp.api.lambda_handler.depends import Depends

@app.handler
def get_data(
        session: Session = Depends(),
) -> HttpResponse[ListResponse[AA]]:

Example 3. Incorrect behavior - no B008

extend-immutable-calls=Depends
from myapp.api.lambda_handler.depends import Depends

@app.handler
def get_data(
        session: Session = Depends(),
) -> HttpResponse[ListResponse[AA]]:
    ...

def Depends(): 
    return []

@app.handler
def put_data(
        session: Session = Depends(),
) -> HttpResponse[ListResponse[AA]]:

Tishka17 avatar Apr 04 '22 07:04 Tishka17

Typer uses typer.Optional, and typer.Argument It would be nice to not have to disable B008

janrito avatar Sep 22 '22 14:09 janrito

Sorry I never responded here. I don't totally understand what bugbear should do here. We're an AST parsing linter, so if someone can use the AST to be smarter here and not cause any regressions I'm open to this.

I am also open to adding gates with extra installs if needed - e.g. detect that typer is installed and take them into account. I don't know how feasible this is tho.

cooperlees avatar Sep 22 '22 19:09 cooperlees

I don't really know what the right answer here is. There doesn't seem to be a way to annotate a function as immutable—doesn't look like there is going to be one anytime soon either https://github.com/python/typing/issues/646

Is there no way we can canonicalise a method's name?

janrito avatar Oct 07 '22 16:10 janrito

same case with from fastapi import Depends

foxale avatar Aug 24 '23 13:08 foxale

case with from fastapi import Header

Fred7b avatar Nov 23 '23 15:11 Fred7b