@@ -43,6 +43,10 @@ def get_line(self) -> int: pass
43
43
UNBOUND_TVAR = 4 # type: int
44
44
BOUND_TVAR = 5 # type: int
45
45
TYPE_ALIAS = 6 # type: int
46
+ # Placeholder for a name imported via 'from ... import'. Second phase of
47
+ # semantic will replace this the actual imported reference. This is
48
+ # needed so that we can detect whether a name has been imported during
49
+ UNBOUND_IMPORTED = 7 # type: int
46
50
47
51
48
52
LITERAL_YES = 2
@@ -171,6 +175,16 @@ def is_package_init_file(self) -> bool:
171
175
class ImportBase (Node ):
172
176
"""Base class for all import statements."""
173
177
is_unreachable = False
178
+ # If an import replaces existing definitions, we construct dummy assignment
179
+ # statements that assign the imported names to the names in the current scope,
180
+ # for type checking purposes. Example:
181
+ #
182
+ # x = 1
183
+ # from m import x <-- add assignment representing "x = m.x"
184
+ assignments = None # type: List[AssignmentStmt]
185
+
186
+ def __init__ (self ) -> None :
187
+ self .assignments = []
174
188
175
189
176
190
class Import (ImportBase ):
@@ -179,6 +193,7 @@ class Import(ImportBase):
179
193
ids = None # type: List[Tuple[str, Optional[str]]] # (module id, as id)
180
194
181
195
def __init__ (self , ids : List [Tuple [str , Optional [str ]]]) -> None :
196
+ super ().__init__ ()
182
197
self .ids = ids
183
198
184
199
def accept (self , visitor : NodeVisitor [T ]) -> T :
@@ -191,6 +206,7 @@ class ImportFrom(ImportBase):
191
206
names = None # type: List[Tuple[str, Optional[str]]] # Tuples (name, as name)
192
207
193
208
def __init__ (self , id : str , relative : int , names : List [Tuple [str , Optional [str ]]]) -> None :
209
+ super ().__init__ ()
194
210
self .id = id
195
211
self .names = names
196
212
self .relative = relative
@@ -203,6 +219,7 @@ class ImportAll(ImportBase):
203
219
"""from m import *"""
204
220
205
221
def __init__ (self , id : str , relative : int ) -> None :
222
+ super ().__init__ ()
206
223
self .id = id
207
224
self .relative = relative
208
225
@@ -219,12 +236,13 @@ class FuncBase(SymbolNode):
219
236
# If method, reference to TypeInfo
220
237
info = None # type: TypeInfo
221
238
is_property = False
239
+ _fullname = None # type: str # Name with module prefix
222
240
223
241
@abstractmethod
224
242
def name (self ) -> str : pass
225
243
226
244
def fullname (self ) -> str :
227
- return self .name ()
245
+ return self ._fullname
228
246
229
247
def is_method (self ) -> bool :
230
248
return bool (self .info )
@@ -238,7 +256,6 @@ class OverloadedFuncDef(FuncBase):
238
256
"""
239
257
240
258
items = None # type: List[Decorator]
241
- _fullname = None # type: str
242
259
243
260
def __init__ (self , items : List ['Decorator' ]) -> None :
244
261
self .items = items
@@ -247,9 +264,6 @@ def __init__(self, items: List['Decorator']) -> None:
247
264
def name (self ) -> str :
248
265
return self .items [1 ].func .name ()
249
266
250
- def fullname (self ) -> str :
251
- return self ._fullname
252
-
253
267
def accept (self , visitor : NodeVisitor [T ]) -> T :
254
268
return visitor .visit_overloaded_func_def (self )
255
269
@@ -345,7 +359,6 @@ class FuncDef(FuncItem):
345
359
This is a non-lambda function defined using 'def'.
346
360
"""
347
361
348
- _fullname = None # type: str # Name with module prefix
349
362
is_decorated = False
350
363
is_conditional = False # Defined conditionally (within block)?
351
364
is_abstract = False
@@ -363,9 +376,6 @@ def __init__(self,
363
376
def name (self ) -> str :
364
377
return self ._name
365
378
366
- def fullname (self ) -> str :
367
- return self ._fullname
368
-
369
379
def accept (self , visitor : NodeVisitor [T ]) -> T :
370
380
return visitor .visit_func_def (self )
371
381
@@ -1650,11 +1660,19 @@ def __str__(self) -> str:
1650
1660
1651
1661
1652
1662
class SymbolTableNode :
1653
- # LDEF/GDEF/MDEF/UNBOUND_TVAR/TVAR/...
1663
+ # Kind of node. Possible values:
1664
+ # - LDEF: local definition (of any kind)
1665
+ # - GDEF: global (module-level) definition
1666
+ # - MDEF: class member definition
1667
+ # - UNBOUND_TVAR: TypeVar(...) definition, not bound
1668
+ # - TVAR: type variable in a bound scope (generic function / generic clas)
1669
+ # - MODULE_REF: reference to a module
1670
+ # - TYPE_ALIAS: type alias
1671
+ # - UNBOUND_IMPORTED: temporary kind for imported names
1654
1672
kind = None # type: int
1655
1673
# AST node of definition (FuncDef/Var/TypeInfo/Decorator/TypeVarExpr,
1656
1674
# or None for a bound type variable).
1657
- node = None # type: SymbolNode
1675
+ node = None # type: Optional[ SymbolNode]
1658
1676
# Type variable id (for bound type variables only)
1659
1677
tvar_id = 0
1660
1678
# Module id (e.g. "foo.bar") or None
0 commit comments