@@ -62,7 +62,7 @@ private string doctest_in_scope(Scope scope) {
6262}
6363
6464pragma [ noinline]
65- private string typehint_annotation_in_file ( File file ) {
65+ private string typehint_annotation_in_module ( Module module_scope ) {
6666 exists ( StrConst annotation |
6767 annotation = any ( Arguments a ) .getAnAnnotation ( ) .getASubExpression * ( )
6868 or
@@ -71,7 +71,7 @@ private string typehint_annotation_in_file(File file) {
7171 annotation = any ( FunctionExpr f ) .getReturns ( ) .getASubExpression * ( )
7272 |
7373 annotation .pointsTo ( Value:: forString ( result ) ) and
74- file = annotation .getLocation ( ) . getFile ( )
74+ annotation .getEnclosingModule ( ) = module_scope
7575 )
7676}
7777
@@ -84,17 +84,19 @@ private string typehint_comment_in_file(File file) {
8484 )
8585}
8686
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 ( )
9193 |
9294 // Look for type hints containing the patterns:
9395 // # type: …name…
94- typehint_comment_in_file ( file ) .regexpMatch ( "# type:.*" + modname + ".*" )
96+ typehint_comment_in_file ( file ) .regexpMatch ( "# type:.*" + name . getId ( ) + ".*" )
9597 or
9698 // 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.*" )
98100 )
99101}
100102
@@ -114,7 +116,7 @@ predicate unused_import(Import imp, Variable name) {
114116 // Assume that opaque `__all__` includes imported module
115117 not all_not_understood ( imp .getEnclosingModule ( ) ) and
116118 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
118120 // Only consider import statements that actually point-to something (possibly an unknown module).
119121 // If this is not the case, it's likely that the import statement never gets executed.
120122 imp .getAName ( ) .getValue ( ) .pointsTo ( _)
0 commit comments