File tree Expand file tree Collapse file tree 2 files changed +23
-4
lines changed
Expand file tree Collapse file tree 2 files changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -14,14 +14,21 @@ pub(crate) struct Collector<'a> {
1414 string_imports : StringImports ,
1515 /// The collected imports from the Python AST.
1616 imports : Vec < CollectedImport > ,
17+ /// Whether to detect type checking imports
18+ type_checking_imports : bool ,
1719}
1820
1921impl < ' a > Collector < ' a > {
20- pub ( crate ) fn new ( module_path : Option < & ' a [ String ] > , string_imports : StringImports ) -> Self {
22+ pub ( crate ) fn new (
23+ module_path : Option < & ' a [ String ] > ,
24+ string_imports : StringImports ,
25+ type_checking_imports : bool ,
26+ ) -> Self {
2127 Self {
2228 module_path,
2329 string_imports,
2430 imports : Vec :: new ( ) ,
31+ type_checking_imports,
2532 }
2633 }
2734
@@ -91,10 +98,18 @@ impl<'ast> SourceOrderVisitor<'ast> for Collector<'_> {
9198 }
9299 }
93100 }
101+ Stmt :: If ( ast:: StmtIf { test, .. } ) => {
102+ // Skip TYPE_CHECKING blocks if not requested
103+ if !self . type_checking_imports && is_type_checking_condition ( test) {
104+ // Don't traverse the body - skip these imports entirely
105+ return ;
106+ }
107+ // Otherwise, traverse normally
108+ walk_stmt ( self , stmt) ;
109+ }
94110 Stmt :: FunctionDef ( _)
95111 | Stmt :: ClassDef ( _)
96112 | Stmt :: While ( _)
97- | Stmt :: If ( _)
98113 | Stmt :: With ( _)
99114 | Stmt :: Match ( _)
100115 | Stmt :: Try ( _)
Original file line number Diff line number Diff line change @@ -39,8 +39,12 @@ impl ModuleImports {
3939 package. and_then ( |package| to_module_path ( package. as_std_path ( ) , path. as_std_path ( ) ) ) ;
4040
4141 // Collect the imports.
42- let imports =
43- Collector :: new ( module_path. as_deref ( ) , string_imports) . collect ( parsed. syntax ( ) ) ;
42+ let imports = Collector :: new (
43+ module_path. as_deref ( ) ,
44+ string_imports,
45+ type_checking_imports,
46+ )
47+ . collect ( parsed. syntax ( ) ) ;
4448
4549 // Resolve the imports.
4650 let mut resolved_imports = ModuleImports :: default ( ) ;
You can’t perform that action at this time.
0 commit comments