-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[TCH] moving typing
imports into TYPE_CHECKING
block does not work as intended
#9197
Comments
typing
imports into TYPE_CHECKING
block are not always movedtyping
imports into TYPE_CHECKING
block does not work as intended
We definitely shouldn't panic here, but can I ask why you want put your |
As far as I can tell this works as expected according to the documentation. $ ruff check source.py --fix --unsafe-fixes --diff
--- source.py
+++ source.py
@@ -1,9 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING
-from typing import Final
if TYPE_CHECKING:
+ from typing import Final
from collections.abc import Sequence
Const: Final[Sequence[str]] = ['a']
Would fix 1 error. ... however, per the documentation you need to also add the
In case you're wondering why my example is different from yours, and has Final on its own import line -- without that change, it panicked again. I also had to remove |
On general principle:
|
It's as eli said, it's to avoid binding the name. Also it's to reduce the amount of imports at the top of the file for clarity purposes (I can collapse the TYPE_CHECKING block).
Ah sorry about that, I think it's 'ALL' that I needed to put; I'll edit the issue shortly.
Oh I am also sorry about this. I usually carefully read the docs before complaining but I think it passed under the radar. However, with that enabled, I also have a linter panic in my case as yopu said and this is probably related to #9196. For instance, the following linter-panicks from __future__ import annotations
from typing import TYPE_CHECKING, Final
if TYPE_CHECKING:
from collections.abc import Sequence
Const: Final[Sequence[str]] = ['a'] with [tool.ruff.lint.flake8-type-checking]
exempt-modules = []
strict = true So I agree that it works as intended but with 'strict' it's a linter panic. I'll update the title issue as well or we can move the discussion to #9196 if it's better for you. |
## Summary If you remove `typing` from `exempt-modules`, we tend to panic, since we try to add `TYPE_CHECKING` to `from typing import ...` statements while concurrently attempting to remove other members from that import. This PR adds special-casing for typing imports to avoid such panics. Closes #5331 Closes #9196. Closes #9197.
This is a follow-up of #9196 (actually #9196 happened when I was trying to figure out this issue).
Consider the following configuration:
Invoking
on
has no effect. However, the
Final
annotation should not be required because offrom __future__ import annotations
. More precisely, here's what I would expect:I tried to find related issues but most of them were lacking
from __future__ import annotations
and thus annotations were treated as required at runtime. However, in my case, I really want theFinal
import to be moved inside aTYPE_CHECKING
block, whenceAs such, I think the bug is related to
flake8-type-checking
itself.Environment
The text was updated successfully, but these errors were encountered: