flake8-type-checking icon indicating copy to clipboard operation
flake8-type-checking copied to clipboard

`singledispatch` and `singledispatchmethod` lead to false positives for TCH001-003

Open antazoey opened this issue 1 year ago • 3 comments

I have

type-checking-sqlalchemy-enabled = False

but it doesn't seem to affect

from sqlalchemy.sql.expression import Insert, Select

    @singledispatchmethod
    def _cache_update_clause(self, query: QueryType) -> Insert:

flake8 will tell me Insert should be in a TYPE_CHECKING block however it is required at Runtime.

There is a similar thing with

from sqlalchemy.engine import CursorResult

antazoey avatar Oct 29 '24 14:10 antazoey

@antazoey This actually looks like an issue with @singledispatchmethod not with the SQLAlchemy integration, since singledispatch needs to be able to inspect the signature at runtime so it can do the dynamic dispatch based on argument types.

This definitely seems like something we should support though, considering it's part of the standard library. Feel free to open a PR if you want to take a crack at this.

Daverball avatar Oct 29 '24 15:10 Daverball

That being said SQLAlchemy's @declared_attr may be affected too, the SQLAlchemy plugin doesn't currently handle it and there are probably some @declared_attr that require runtime access to some parts of the return annotation.

Daverball avatar Oct 29 '24 15:10 Daverball

@Daverball Yeah, I actually left that out too, a fuller code snippet:

    @singledispatchmethod
    def _cache_update_clause(self, query: QueryType) -> Insert:

shows the decorator usage. edit: updated the description

This definitely seems like something we should support though, considering it's part of the standard library. Feel free to open a PR if you want to take a crack at this.

I can take a look once I have time!

antazoey avatar Oct 29 '24 16:10 antazoey