Skip to content

Refactoring: Remove attribute mod_id from SymbolTableNode #4077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2246,8 +2246,6 @@ class SymbolTableNode:
# AST node of definition (FuncDef/Var/TypeInfo/Decorator/TypeVarExpr,
# or None for a bound type variable).
node = None # type: Optional[SymbolNode]
# Module id (e.g. "foo.bar") or None
mod_id = '' # type: Optional[str]
# If this not None, override the type of the 'node' attribute.
type_override = None # type: Optional[mypy.types.Type]
# For generic aliases this stores the (qualified) names of type variables.
Expand All @@ -2269,7 +2267,6 @@ class SymbolTableNode:
def __init__(self,
kind: int,
node: Optional[SymbolNode],
mod_id: Optional[str] = None,
typ: 'Optional[mypy.types.Type]' = None,
module_public: bool = True,
normalized: bool = False,
Expand All @@ -2279,7 +2276,6 @@ def __init__(self,
self.kind = kind
self.node = node
self.type_override = typ
self.mod_id = mod_id
self.module_hidden = module_hidden
self.module_public = module_public
self.normalized = normalized
Expand Down Expand Up @@ -2309,8 +2305,8 @@ def type(self) -> 'Optional[mypy.types.Type]':

def __str__(self) -> str:
s = '{}/{}'.format(node_kinds[self.kind], short_type(self.node))
if self.mod_id is not None:
s += ' ({})'.format(self.mod_id)
if isinstance(self.node, SymbolNode):
s += ' ({})'.format(self.node.fullname())
# Include declared type of variables and functions.
if self.type is not None:
s += ' : {}'.format(self.type)
Expand Down
38 changes: 17 additions & 21 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ def visit_file(self, file_node: MypyFile, fnam: str, options: Options,

with experiments.strict_optional_set(options.strict_optional):
if 'builtins' in self.modules:
self.globals['__builtins__'] = SymbolTableNode(
MODULE_REF, self.modules['builtins'], self.cur_mod_id)
self.globals['__builtins__'] = SymbolTableNode(MODULE_REF,
self.modules['builtins'])

for name in implicit_module_attrs:
v = self.globals[name].node
Expand Down Expand Up @@ -1409,7 +1409,7 @@ def add_submodules_to_parent_modules(self, id: str, module_public: bool) -> None
if parent_mod and child not in parent_mod.names:
child_mod = self.modules.get(id)
if child_mod:
sym = SymbolTableNode(MODULE_REF, child_mod, parent,
sym = SymbolTableNode(MODULE_REF, child_mod,
module_public=module_public)
parent_mod.names[child] = sym
id = parent
Expand All @@ -1418,7 +1418,7 @@ def add_module_symbol(self, id: str, as_id: str, module_public: bool,
context: Context, module_hidden: bool = False) -> None:
if id in self.modules:
m = self.modules[id]
self.add_symbol(as_id, SymbolTableNode(MODULE_REF, m, self.cur_mod_id,
self.add_symbol(as_id, SymbolTableNode(MODULE_REF, m,
module_public=module_public,
module_hidden=module_hidden), context)
else:
Expand All @@ -1438,7 +1438,7 @@ def visit_import_from(self, imp: ImportFrom) -> None:
if not node or node.kind == UNBOUND_IMPORTED:
mod = self.modules.get(possible_module_id)
if mod is not None:
node = SymbolTableNode(MODULE_REF, mod, import_id)
node = SymbolTableNode(MODULE_REF, mod)
self.add_submodules_to_parent_modules(possible_module_id, True)
elif possible_module_id in self.missing_modules:
missing = True
Expand All @@ -1456,7 +1456,7 @@ def visit_import_from(self, imp: ImportFrom) -> None:
else:
name = id
ast_node = Var(name, type=typ)
symbol = SymbolTableNode(GDEF, ast_node, name)
symbol = SymbolTableNode(GDEF, ast_node)
self.add_symbol(name, symbol, imp)
return
if node and node.kind != UNBOUND_IMPORTED and not node.module_hidden:
Expand All @@ -1474,7 +1474,6 @@ def visit_import_from(self, imp: ImportFrom) -> None:
module_public = not self.is_stub_file or as_id is not None
module_hidden = not module_public and possible_module_id not in self.modules
symbol = SymbolTableNode(node.kind, node.node,
self.cur_mod_id,
node.type_override,
module_public=module_public,
normalized=node.normalized,
Expand Down Expand Up @@ -1530,8 +1529,7 @@ def normalize_type_alias(self, node: SymbolTableNode,
node = self.lookup_qualified(collections_type_aliases[fullname], ctx)
normalized = True
if normalized:
node = SymbolTableNode(node.kind, node.node,
node.mod_id, node.type_override,
node = SymbolTableNode(node.kind, node.node, node.type_override,
normalized=True, alias_tvars=node.alias_tvars)
return node

Expand Down Expand Up @@ -1576,7 +1574,6 @@ def visit_import_all(self, i: ImportAll) -> None:
name, existing_symbol, node, i):
continue
self.add_symbol(name, SymbolTableNode(node.kind, node.node,
self.cur_mod_id,
node.type_override,
normalized=node.normalized,
alias_tvars=node.alias_tvars), i)
Expand All @@ -1597,7 +1594,7 @@ def add_unknown_symbol(self, name: str, context: Context, is_import: bool = Fals
any_type = AnyType(TypeOfAny.from_error)
var.type = any_type
var.is_suppressed_import = is_import
self.add_symbol(name, SymbolTableNode(GDEF, var, self.cur_mod_id), context)
self.add_symbol(name, SymbolTableNode(GDEF, var), context)

#
# Statements
Expand Down Expand Up @@ -1831,8 +1828,7 @@ def analyze_lvalue(self, lval: Lvalue, nested: bool = False,
lval.is_def = True
lval.kind = GDEF
lval.fullname = v._fullname
self.globals[lval.name] = SymbolTableNode(GDEF, v,
self.cur_mod_id)
self.globals[lval.name] = SymbolTableNode(GDEF, v)
elif isinstance(lval.node, Var) and lval.is_def:
# Since the is_def flag is set, this must have been analyzed
# already in the first pass and added to the symbol table.
Expand Down Expand Up @@ -2276,7 +2272,7 @@ def check_namedtuple(self, node: Expression, var_name: str = None) -> Optional[T
info = self.build_namedtuple_typeinfo(name, items, types, {})
# Store it as a global just in case it would remain anonymous.
# (Or in the nearest class if there is one.)
stnode = SymbolTableNode(GDEF, info, self.cur_mod_id)
stnode = SymbolTableNode(GDEF, info)
if self.type:
self.type.names[name] = stnode
else:
Expand Down Expand Up @@ -2535,7 +2531,7 @@ def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[Ty
info = self.build_typeddict_typeinfo(name, items, types, required_keys)
# Store it as a global just in case it would remain anonymous.
# (Or in the nearest class if there is one.)
stnode = SymbolTableNode(GDEF, info, self.cur_mod_id)
stnode = SymbolTableNode(GDEF, info)
if self.type:
self.type.names[name] = stnode
else:
Expand Down Expand Up @@ -2767,7 +2763,7 @@ class A(enum.Enum):
info = self.build_enum_call_typeinfo(name, items, fullname)
# Store it as a global just in case it would remain anonymous.
# (Or in the nearest class if there is one.)
stnode = SymbolTableNode(GDEF, info, self.cur_mod_id)
stnode = SymbolTableNode(GDEF, info)
if self.type:
self.type.names[name] = stnode
else:
Expand Down Expand Up @@ -3851,7 +3847,7 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
typ = UnboundType(t)
v = Var(name, typ)
v._fullname = self.sem.qualified_name(name)
self.sem.globals[name] = SymbolTableNode(GDEF, v, self.sem.cur_mod_id)
self.sem.globals[name] = SymbolTableNode(GDEF, v)

for d in defs:
d.accept(self)
Expand Down Expand Up @@ -3886,7 +3882,7 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
for name, typ in literal_types:
v = Var(name, typ)
v._fullname = self.sem.qualified_name(name)
self.sem.globals[name] = SymbolTableNode(GDEF, v, self.sem.cur_mod_id)
self.sem.globals[name] = SymbolTableNode(GDEF, v)

del self.sem.options

Expand Down Expand Up @@ -3920,7 +3916,7 @@ def visit_func_def(self, func: FuncDef) -> None:
sem.check_no_global(func.name(), func)
else:
if at_module:
sem.globals[func.name()] = SymbolTableNode(GDEF, func, sem.cur_mod_id)
sem.globals[func.name()] = SymbolTableNode(GDEF, func)
# Also analyze the function body (in case there are conditional imports).
sem.function_stack.append(func)
sem.errors.push_function(func.name())
Expand All @@ -3936,7 +3932,7 @@ def visit_overloaded_func_def(self, func: OverloadedFuncDef) -> None:
self.sem.check_no_global(func.name(), func, True)
func._fullname = self.sem.qualified_name(func.name())
if kind == GDEF:
self.sem.globals[func.name()] = SymbolTableNode(kind, func, self.sem.cur_mod_id)
self.sem.globals[func.name()] = SymbolTableNode(kind, func)
if func.impl:
impl = func.impl
# Also analyze the function body (in case there are conditional imports).
Expand Down Expand Up @@ -3969,7 +3965,7 @@ def visit_class_def(self, cdef: ClassDef) -> None:
info.set_line(cdef.line, cdef.column)
cdef.info = info
if kind == GDEF:
self.sem.globals[cdef.name] = SymbolTableNode(kind, info, self.sem.cur_mod_id)
self.sem.globals[cdef.name] = SymbolTableNode(kind, info)
self.process_nested_classes(cdef)

def process_nested_classes(self, outer_def: ClassDef) -> None:
Expand Down
1 change: 0 additions & 1 deletion mypy/server/astdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def is_similar_node_shallow(n: SymbolTableNode, m: SymbolTableNode) -> bool:
# tvar_def
# type_override
if (n.kind != m.kind
or n.mod_id != m.mod_id
or n.module_public != m.module_public):
return False
if type(n.node) != type(m.node): # noqa
Expand Down
30 changes: 22 additions & 8 deletions test-data/unit/semanal-symtable.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ x = 1
[out]
__main__:
SymbolTable(
x : Gdef/Var (__main__))
x : Gdef/Var (__main__.x))

[case testFuncDef]
def f(): pass
[out]
__main__:
SymbolTable(
f : Gdef/FuncDef (__main__))
f : Gdef/FuncDef (__main__.f))

[case testEmptyClassDef]
class c: pass
[out]
__main__:
SymbolTable(
c : Gdef/TypeInfo (__main__))
c : Gdef/TypeInfo (__main__.c))

[case testImport]
import m
Expand All @@ -32,10 +32,10 @@ x = 1
[out]
__main__:
SymbolTable(
m : ModuleRef/MypyFile (__main__))
m : ModuleRef/MypyFile (m))
m:
SymbolTable(
x : Gdef/Var (m))
x : Gdef/Var (m.x))

[case testImportFromModule]
from m import x
Expand All @@ -45,8 +45,22 @@ y = 1
[out]
__main__:
SymbolTable(
x : Gdef/TypeInfo (__main__))
x : Gdef/TypeInfo (m.x))
m:
SymbolTable(
x : Gdef/TypeInfo (m)
y : Gdef/Var (m))
x : Gdef/TypeInfo (m.x)
y : Gdef/Var (m.y))

[case testImportAs]
from m import x as xx
[file m.py]
class x: pass
y = 1
[out]
__main__:
SymbolTable(
xx : Gdef/TypeInfo (m.x))
m:
SymbolTable(
x : Gdef/TypeInfo (m.x)
y : Gdef/Var (m.y))