8000 Update documentation of SymbolTableNode (#4080) · python/mypy@5d25713 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d25713

Browse files
JukkaLilevkivskyi
authored andcommitted
Update documentation of SymbolTableNode (#4080)
1 parent b1f61fb commit 5d25713

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

mypy/nodes.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,19 +2233,53 @@ def __getattribute__(self, attr: str) -> None:
22332233

22342234

22352235
class SymbolTableNode:
2236+
"""Description of a name binding in a symbol table.
2237+
2238+
These are only used as values in module (global), function (local)
2239+
and class symbol tables (see SymbolTable). The name that is bound is
2240+
the key in SymbolTable.
2241+
2242+
Symbol tables don't contain direct references to AST nodes primarily
2243+
because there can be multiple symbol table references to a single
2244+
AST node (due to imports and aliases), and different references can
2245+
behave differently. This class describes the unique properties of
2246+
each reference.
2247+
2248+
The most fundamental attributes are 'kind' and 'node'. The 'node'
2249+
attribute defines the AST node that the name refers to.
2250+
2251+
For many bindings, including those targeting variables, functions
2252+
and classes, the kind is one of LDEF, GDEF or MDEF, depending on the
2253+
scope of the definition. These three kinds can usually be used
2254+
interchangeably and the difference between local, global and class
2255+
scopes is mostly descriptive, with no semantic significance.
2256+
However, some tools that consume mypy ASTs may care about these so
2257+
they should be correct.
2258+
2259+
A few definitions get special kinds, including type variables (TVAR),
2260+
imported modules and module aliases (MODULE_REF), and type aliases
2261+
(TYPE_ALIAS).
2262+
2263+
Type aliases are very special and have additional attributes that
2264+
are only used for them ('type_override', 'alias_tvars' at least).
2265+
"""
2266+
# TODO: This is a mess. Refactor!
2267+
# TODO: Describe how type aliases work.
2268+
22362269
# Kind of node. Possible values:
2237-
# - LDEF: local definition (of any kind)
2270+
# - LDEF: local definition
22382271
# - GDEF: global (module-level) definition
22392272
# - MDEF: class member definition
2240-
# - TVAR: TypeVar(...) definition
2273+
# - TVAR: TypeVar(...) definition in any scope
22412274
# - MODULE_REF: reference to a module
22422275
# - TYPE_ALIAS: type alias
2243-
# - UNBOUND_IMPORTED: temporary kind for imported names
2276+
# - UNBOUND_IMPORTED: temporary kind for imported names (we don't know the final kind yet)
22442277
kind = None # type: int
2245-
# AST node of definition (FuncDef/Var/TypeInfo/Decorator/TypeVarExpr,
2278+
# AST node of definition (among others, this can be FuncDef/Var/TypeInfo/TypeVarExpr/MypyFile,
22462279
# or None for a bound type variable).
22472280
node = None # type: Optional[SymbolNode]
2248-
# If this not None, override the type of the 'node' attribute.
2281+
# If this not None, override the type of the 'node' attribute. This is only used for
2282+
# type aliases.
22492283
type_override = None # type: Optional[mypy.types.Type]
22502284
# For generic aliases this stores the (qualified) names of type variables.
22512285
# (For example see testGenericAliasWithTypeVarsFromDifferentModules.)
@@ -2258,7 +2292,9 @@ class SymbolTableNode:
22582292
# For deserialized MODULE_REF nodes, the referenced module name;
22592293
# for other nodes, optionally the name of the referenced object.
22602294
cross_ref = None # type: Optional[str]
2261-
# Was this node created by normalіze_type_alias?
2295+
# Used to distinguish between 'typing.List' and 'builtins.list'. This is
2296+
# True when the former has been normalized to the latter, and it allow us
2297+
# to reject 'list[str]' and similar.
22622298
normalized = False # type: bool
22632299
# Was this defined by assignment to self attribute?
22642300
implicit = False # type: bool

0 commit comments

Comments
 (0)
0