Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

DimitriPapadopoulos
Copy link
Contributor

No description provided.

Copy link
Collaborator

@max-sixty max-sixty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the advantage of this?

@DimitriPapadopoulos
Copy link
Contributor Author

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

TCH001 Move application import into a type-checking block
TCH002 Move third-party import into a type-checking block
TCH003 Move standard library import into a type-checking block
@max-sixty
Copy link
Collaborator

Yes, though these are very cheap imports.

I'm also cognizant of #9581...

@DimitriPapadopoulos
Copy link
Contributor Author

DimitriPapadopoulos commented Nov 7, 2024

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
Copy link
Collaborator

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...

@DimitriPapadopoulos DimitriPapadopoulos marked this pull request as draft November 10, 2024 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants