mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-16 16:40:19 +00:00
Improve detection of TYPE_CHECKING
blocks imported from typing_extensions
or _typeshed
(#8429)
~Improves detection of types imported from `typing_extensions`. Removes the hard-coded list of supported types in `typing_extensions`; instead assuming all types could be imported from `typing`, `_typeshed`, or `typing_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 new `if TYPE_CHECKING` block and `from typing import TYPE_CHECKING` if `typing_extensions.TYPE_CHECKING` is used (closes https://github.com/astral-sh/ruff/issues/8427) --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
parent
9d167a1f5c
commit
565ddebb15
16 changed files with 194 additions and 9 deletions
|
@ -315,10 +315,7 @@ pub fn is_type_checking_block(stmt: &ast::StmtIf, semantic: &SemanticModel) -> b
|
|||
}
|
||||
|
||||
// Ex) `if typing.TYPE_CHECKING:`
|
||||
if semantic
|
||||
.resolve_call_path(test)
|
||||
.is_some_and(|call_path| matches!(call_path.as_slice(), ["typing", "TYPE_CHECKING"]))
|
||||
{
|
||||
if semantic.match_typing_expr(test, "TYPE_CHECKING") {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -200,6 +200,14 @@ impl<'a> SemanticModel<'a> {
|
|||
false
|
||||
}
|
||||
|
||||
/// Return an iterator over the set of `typing` modules allowed in the semantic model.
|
||||
pub fn typing_modules(&self) -> impl Iterator<Item = &str> {
|
||||
["typing", "_typeshed", "typing_extensions"]
|
||||
.iter()
|
||||
.copied()
|
||||
.chain(self.typing_modules.iter().map(String::as_str))
|
||||
}
|
||||
|
||||
/// Create a new [`Binding`] for a builtin.
|
||||
pub fn push_builtin(&mut self) -> BindingId {
|
||||
self.bindings.push(Binding {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue