@@ -2055,8 +2055,11 @@ def add_symbol(self, name: str, node: SymbolTableNode,
2055
2055
existing .node != node .node ) and existing .kind != UNBOUND_IMPORTED :
2056
2056
# Modules can be imported multiple times to support import
2057
2057
# of multiple submodules of a package (e.g. a.x and a.y).
2058
- if not (existing .type and node .type and is_same_type (existing .type , node .type )):
2059
- # Only report an error if the symbol collision provides a different type.
2058
+ ok = False
2059
+ # Only report an error if the symbol collision provides a different type.
2060
+ if existing .type and node .type and is_same_type (existing .type , node .type ):
2061
+ ok = True
2062
+ if not ok :
2060
2063
self .name_already_defined (name , context )
2061
2064
self .globals [name ] = node
2062
2065
@@ -2222,11 +2225,24 @@ def process_nested_classes(self, outer_def: ClassDef) -> None:
2222
2225
self .process_nested_classes (node )
2223
2226
2224
2227
def visit_import_from (self , node : ImportFrom ) -> None :
2228
+ # We can't bind module names during the first pass, as the target module might be
2229
+ # unprocessed. However, we add dummy unbound imported names to the symbol table so
2230
+ # that we at least know that the name refers to a module.
2225
2231
for name , as_name in node .names :
2226
2232
imported_name = as_name or name
2227
2233
if imported_name not in self .sem .globals :
2228
2234
self .sem .add_symbol (imported_name , SymbolTableNode (UNBOUND_IMPORTED , None ), node )
2229
2235
2236
+ def visit_import (self , node : Import ) -> None :
2237
+ # This is similar to visit_import_from -- see the comment there.
2238
+ for id , as_id in node .ids :
2239
+ imported_id = as_id or id
2240
+ if imported_id not in self .sem .globals :
2241
+ self .sem .add_symbol (imported_id , SymbolTableNode (UNBOUND_IMPORTED , None ), node )
2242
+ else :
2243
+ # If the previous symbol is a variable, this should take precedence.
2244
+ self .sem .globals [imported_id ] = SymbolTableNode (UNBOUND_IMPORTED , None )
2245
+
2230
2246
def visit_for_stmt (self , s : ForStmt ) -> None :
2231
2247
self .analyze_lvalue (s .index )
2232
2248
0 commit comments