-
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
Improve detection of TYPE_CHECKING
blocks imported from typing_extensions
or _typeshed
#8429
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
It looks like at ruff/crates/ruff_linter/src/importer/mod.rs Lines 135 to 139 in 1e173f7
typing_extensions.TYPE_CHECKING block is used before inserting the typing.TYPE_CHECKING block to actually solve the issue.
|
|
return true; | ||
} | ||
|
||
if is_typing_extension(target) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to remove this behavior in which we check if the member is part of typing_extensions
. We could retain it... but that list may vary over time, so hard-coding it makes Ruff less future-proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm what's the effect of this then? Is it a breaking change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not breaking, it's more lax (the net effect is that we consider all imports from typing_extensions
to be treated equivalently to typing
, rather than the allow-list that we encoded of specific members). But let's back it out.
@zanieb - I decided to finish this one up -- feel free to review and merge. |
e7056c8
to
7a2c13d
Compare
7a2c13d
to
545bd30
Compare
if matches!( | ||
call_path.as_slice(), | ||
["typing" | "_typeshed" | "typing_extensions", target] | ||
["typing" | "_typeshed" | "typing_extensions", _target] | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi using matches!
really broke things because target
here isn't what you wanted — it's capturing second item in the slice not using the provided target
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 545bd30 — not sure how else to write it
typing_extensions.TYPE_CHECKING
to detection of type checking blockstyping_extensions
typing_extensions
TYPE_CHECKING
blocks imported from typing_extensions
or _typeshed
42665cb
to
0e0e738
Compare
Improves detection of types imported fromtyping_extensions
. Removes the hard-coded list of supported types intyping_extensions
; instead assuming all types could be imported fromtyping
,_typeshed
, ortyping_extensions
.The typing extensions package appears to re-export types even if they do not need modification.Adds detection of
if typing_extensions.TYPE_CHECKING
blocks. Avoids inserting a newif TYPE_CHECKING
block andfrom typing import TYPE_CHECKING
iftyping_extensions.TYPE_CHECKING
is used (closes #8427)