@@ -62,7 +62,7 @@ private string doctest_in_scope(Scope scope) {
62
62
}
63
63
64
64
pragma [ noinline]
65
- private string typehint_annotation_in_file ( File file ) {
65
+ private string typehint_annotation_in_module ( Module module_scope ) {
66
66
exists ( StrConst annotation |
67
67
annotation = any ( Arguments a ) .getAnAnnotation ( ) .getASubExpression * ( )
68
68
or
@@ -71,7 +71,7 @@ private string typehint_annotation_in_file(File file) {
71
71
annotation = any ( FunctionExpr f ) .getReturns ( ) .getASubExpression * ( )
72
72
|
73
73
annotation .pointsTo ( Value:: forString ( result ) ) and
74
- file = annotation .getLocation ( ) . getFile ( )
74
+ annotation .getEnclosingModule ( ) = module_scope
75
75
)
76
76
}
77
77
@@ -84,17 +84,19 @@ private string typehint_comment_in_file(File file) {
84
84
)
85
85
}
86
86
87
- predicate imported_module_used_in_typehint ( Import imp ) {
88
- exists ( string modname , File file |
89
- imp .getAName ( ) .getAsname ( ) .( Name ) .getId ( ) = modname and
90
- file = imp .getScope ( ) .( Module ) .getFile ( )
87
+ /** Holds if the imported alias `name` from `imp` is used in a typehint (in the same file as `imp`) */
88
+ predicate imported_alias_used_in_typehint ( Import imp , Variable name ) {
89
+ imp .getAName ( ) .getAsname ( ) .( Name ) .getVariable ( ) = name and
90
+ exists ( File file , Module module_scope |
91
+ module_scope = imp .getEnclosingModule ( ) and
92
+ file = module_scope .getFile ( )
91
93
|
92
94
// Look for type hints containing the patterns:
93
95
// # type: …name…
94
- typehint_comment_in_file ( file ) .regexpMatch ( "# type:.*" + modname + ".*" )
96
+ typehint_comment_in_file ( file ) .regexpMatch ( "# type:.*" + name . getId ( ) + ".*" )
95
97
or
96
98
// Type hint is inside a string annotation, as needed for forward references
97
- typehint_annotation_in_file ( file ) .regexpMatch ( ".*\\b" + modname + "\\b.*" )
99
+ typehint_annotation_in_module ( module_scope ) .regexpMatch ( ".*\\b" + name . getId ( ) + "\\b.*" )
98
100
)
99
101
}
100
102
@@ -114,7 +116,7 @@ predicate unused_import(Import imp, Variable name) {
114
116
// Assume that opaque `__all__` includes imported module
115
117
not all_not_understood ( imp .getEnclosingModule ( ) ) and
116
118
not imported_module_used_in_doctest ( imp ) and
117
- not imported_module_used_in_typehint ( imp ) and
119
+ not imported_alias_used_in_typehint ( imp , name ) and
118
120
// Only consider import statements that actually point-to something (possibly an unknown module).
119
121
// If this is not the case, it's likely that the import statement never gets executed.
120
122
imp .getAName ( ) .getValue ( ) .pointsTo ( _)
0 commit comments