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

TC does not detect imports used only for type annotations #16529

Closed
devon-research opened this issue Mar 6, 2025 · 2 comments
Closed

TC does not detect imports used only for type annotations #16529

devon-research opened this issue Mar 6, 2025 · 2 comments
Labels
question Asking for support or clarification

Comments

@devon-research
Copy link

devon-research commented Mar 6, 2025

Summary

It seems that some of the TC rules are not working correctly. For example, if I have this file:

from typing import NamedTuple

t: NamedTuple | None = None

and I run

ruff check --isolated --select=TC

then I get that all checks pass.

My understanding is that this should yield a TC003 error. Am I misunderstanding the intended behavior?

Version

0.9.4

@MichaReiser
Copy link
Member

Types imported from the typing module are excempt from moving into TYPE_CHECKING by default because you still have to import the typing module to get the TYPE_CHECKING variable at runtime. So the benefit of moving types imported from typing is marginal. See Charlie's response here #9197 (comment)

However, you can remove typing from the excempt-module list and ruff will start enforcing moving typing imports in the TYPE_CHECKING block

[lint]
select = [ "TC" ]

[lint.flake8-type-checking]
exempt-modules = []

See https://play.ruff.rs/9ccf9a06-534c-441a-8101-02056076b8a6

@MichaReiser MichaReiser added the question Asking for support or clarification label Mar 6, 2025
@devon-research
Copy link
Author

Ah, I see. Thanks!

I additionally had this confusion come up outside the typing module, but I realize now from the issue you linked that the behavior I was seeing is explained by the strict = false default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Asking for support or clarification
Projects
None yet
Development

No branches or pull requests

2 participants