File tree 6 files changed +67
-12
lines changed
resources/test/fixtures/pyflakes
6 files changed +67
-12
lines changed Original file line number Diff line number Diff line change @@ -92,3 +92,10 @@ def b(self) -> None:
92
92
case 0 ,:
93
93
import x
94
94
import y
95
+
96
+
97
+ # Test: access a sub-importation via an alias.
98
+ import foo .bar as bop
99
+ import foo .bar .baz
100
+
101
+ print (bop .baz .read_csv ("test.csv" ))
Original file line number Diff line number Diff line change @@ -70,3 +70,13 @@ def main():
70
70
71
71
def requests_mock (requests_mock : rm .Mocker ):
72
72
print (rm .ANY )
73
+
74
+
75
+ import sklearn .base
76
+ import mlflow .sklearn
77
+
78
+
79
+ def f ():
80
+ import sklearn
81
+
82
+ mlflow
Original file line number Diff line number Diff line change @@ -151,6 +151,8 @@ F401_0.py:93:16: F401 [*] `x` imported but unused
151
151
92 92 | case 0 ,:
152
152
93 | - import x
153
153
94 93 | import y
154
+ 95 94 |
155
+ 96 95 |
154
156
155
157
F401_0 .py :94 :16 : F401 [* ] ` y` imported but unused
156
158
|
@@ -166,5 +168,27 @@ F401_0.py:94:16: F401 [*] `y` imported but unused
166
168
92 92 | case 0 ,:
167
169
93 93 | import x
168
170
94 | - import y
171
+ 95 94 |
172
+ 96 95 |
173
+ 97 96 | # Test : access a sub - importation via an alias .
174
+
175
+ F401_0 .py :99 :8 : F401 [* ] ` foo.bar.baz` imported but unused
176
+ |
177
+ 97 | # Test : access a sub - importation via an alias .
178
+ 98 | import foo .bar as bop
179
+ 99 | import foo .bar .baz
180
+ | ^^^^^^^^^^^ F401
181
+ 100 |
182
+ 101 | print (bop .baz .read_csv (" test.csv" ))
183
+ |
184
+ = help : Remove unused import : ` foo.bar.baz`
185
+
186
+ ℹ Fix
187
+ 96 96 |
188
+ 97 97 | # Test : access a sub - importation via an alias .
189
+ 98 98 | import foo .bar as bop
190
+ 99 | - import foo .bar .baz
191
+ 100 99 |
192
+ 101 100 | print (bop .baz .read_csv (" test.csv" ))
169
193
170
194
Original file line number Diff line number Diff line change @@ -130,16 +130,18 @@ impl<'a> Visitor<'a> for TypeVarReferenceVisitor<'a> {
130
130
fn visit_expr ( & mut self , expr : & ' a Expr ) {
131
131
match expr {
132
132
Expr :: Name ( name) if name. ctx . is_load ( ) => {
133
- let Some ( Stmt :: Assign ( StmtAssign { value, .. } ) ) =
134
- self . semantic . lookup_symbol ( name. id . as_str ( ) )
135
- . and_then ( |binding_id| {
136
- self . semantic
137
- . binding ( binding_id)
138
- . source
139
- . map ( |node_id| self . semantic . statement ( node_id) )
140
- } ) else {
141
- return ;
142
- } ;
133
+ let Some ( Stmt :: Assign ( StmtAssign { value, .. } ) ) = self
134
+ . semantic
135
+ . lookup_symbol ( name. id . as_str ( ) )
136
+ . and_then ( |binding_id| {
137
+ self . semantic
138
+ . binding ( binding_id)
139
+ . source
140
+ . map ( |node_id| self . semantic . statement ( node_id) )
141
+ } )
142
+ else {
143
+ return ;
144
+ } ;
143
145
144
146
match value. as_ref ( ) {
145
147
Expr :: Subscript ( ExprSubscript {
Original file line number Diff line number Diff line change @@ -585,7 +585,7 @@ impl<'a> Imported<'a> for FromImport<'a> {
585
585
}
586
586
587
587
/// A wrapper around an import [`BindingKind`] that can be any of the three types of imports.
588
- #[ derive( Debug , Clone ) ]
588
+ #[ derive( Debug , Clone , is_macro :: Is ) ]
589
589
pub enum AnyImport < ' a > {
590
590
Import ( & ' a Import < ' a > ) ,
591
591
SubmoduleImport ( & ' a SubmoduleImport < ' a > ) ,
Original file line number Diff line number Diff line change @@ -590,14 +590,26 @@ impl<'a> SemanticModel<'a> {
590
590
// print(pa.csv.read_csv("test.csv"))
591
591
// ```
592
592
let import = self . bindings [ binding_id] . as_any_import ( ) ?;
593
+ if !import. is_import ( ) {
594
+ return None ;
595
+ }
596
+
597
+ // Grab, e.g., `pyarrow` from `import pyarrow as pa`.
593
598
let call_path = import. call_path ( ) ;
594
599
let segment = call_path. last ( ) ?;
595
600
if * segment == symbol {
596
601
return None ;
597
602
}
598
603
604
+ // Locate the submodule import (e.g., `pyarrow.csv`) that `pa` aliases.
599
605
let binding_id = self . scopes [ scope_id] . get ( segment) ?;
600
- if !self . bindings [ binding_id] . kind . is_submodule_import ( ) {
606
+ let submodule = & self . bindings [ binding_id] . as_any_import ( ) ?;
607
+ if !submodule. is_submodule_import ( ) {
608
+ return None ;
609
+ }
610
+
611
+ // Ensure that the submodule import and the aliased import are from the same module.
612
+ if import. module_name ( ) != submodule. module_name ( ) {
601
613
return None ;
602
614
}
603
615
You can’t perform that action at this time.
0 commit comments