pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.63k stars 1.09k forks source link

Enforce ruff/flake8-type-checking rules (TCH) #9728

Open DimitriPapadopoulos opened 2 weeks ago

DimitriPapadopoulos commented 2 weeks ago

These imports will only be imported during type checking, not at run time.

max-sixty commented 2 weeks ago

Yes, though these are very cheap imports.

I'm also cognizant of #9581...

DimitriPapadopoulos commented 2 weeks ago

I was not aware of runtime type checking tools. Perhaps we need to keep the current status quo until we make sure TCH rules do the right thing, taking into account runtime typecheckers.

Not sure they currently do the right thing, at least by default. From TCH001:

Unused imports add a performance overhead at runtime, and risk creating import cycles. If an import is only used in typing-only contexts, it can instead be imported conditionally under an if TYPE_CHECKING: block to minimize runtime overhead.

If lint.flake8-type-checking.quote-annotations is set to true, annotations will be wrapped in quotes if doing so would enable the corresponding import to be moved into an if TYPE_CHECKING: block.

If a class requires that type annotations be available at runtime (as is the case for Pydantic, SQLAlchemy, and other libraries), consider using the lint.flake8-type-checking.runtime-evaluated-base-classes and lint.flake8-type-checking.runtime-evaluated-decorators settings to mark them as such.

max-sixty commented 2 weeks ago

OK, I reckon given the uncertainty & low cost of these imports, let's push this off until the ecosystem has had a chance to coalesce on an option...