[flake8-type-checking
] TC004 is too strict for SQLAlchemy's Mapped
#16412
Labels
bug
Something isn't working
Summary
Given something like:
https://play.ruff.rs/da4c76c4-92d0-4083-99c1-022152398a50
Ruff will flag the import inside the
TYPE_CHECKING
block, which very well may be required, due to circular dependencies between the two models.Additionally
runtime-required-base-classes
is technically too strict forsqlalchemy.orm.BaseModel
, since only the annotations that use aMapped
class are runtime required and furthermore only non-model types are runtime required, any forward references to other models can be resolved during mapping.We could handle the latter using an additional setting
runtime-required-generic-subscripts
. But the former would still be a problem.To handle the former we would need something like
runtime-ambiguous
instead ofruntime-required
, so we emit neitherTC001-003
norTC004
for uses in that context, since the use will have to be considered neither runtime-only nor typing-only.But turning two settings, into six settings doesn't seem particularly elegant either. Maybe someone can think of a more elegant way to structure these settings.
I think this is the final major hurdle for covering all the use-cases the flake8 plugin currently covers (beyond implementing the currently still missing
TC009
rule).Eventually type inference could let us do something more targeted for SQLAlchemy that knows exactly which symbols aren't runtime-required, but that's still a long way off and not as flexible for other libraries that may do something similar.
Version
0.9.7
The text was updated successfully, but these errors were encountered: