本文整理汇总了Python中mypy.nodes.SymbolTable类的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable类的具体用法?Python SymbolTable怎么用?Python SymbolTable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SymbolTable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_symbol_table
def visit_symbol_table(self, symtab: SymbolTable) -> None:
# Copy the items because we may mutate symtab.
for key, value in list(symtab.items()):
cross_ref = value.cross_ref
if cross_ref is not None: # Fix up cross-reference.
del value.cross_ref
if cross_ref in self.modules:
value.node = self.modules[cross_ref]
else:
stnode = lookup_qualified_stnode(self.modules, cross_ref,
self.quick_and_dirty)
if stnode is not None:
value.node = stnode.node
value.type_override = stnode.type_override
if (self.quick_and_dirty and value.kind == TYPE_ALIAS and
stnode.type_override is None):
value.type_override = Instance(stale_info(self.modules), [])
value.alias_tvars = stnode.alias_tvars or []
elif not self.quick_and_dirty:
assert stnode is not None, "Could not find cross-ref %s" % (cross_ref,)
else:
# We have a missing crossref in quick mode, need to put something
value.node = stale_info(self.modules)
if value.kind == TYPE_ALIAS:
value.type_override = Instance(stale_info(self.modules), [])
else:
if isinstance(value.node, TypeInfo):
# TypeInfo has no accept(). TODO: Add it?
self.visit_type_info(value.node)
elif value.node is not None:
value.node.accept(self)
if value.type_override is not None:
value.type_override.accept(self.type_fixer)
示例2: compute_all_mros
def compute_all_mros(symtab: SymbolTable, modules: Dict[str, MypyFile]) -> None:
for key, value in symtab.items():
if value.kind in (LDEF, MDEF, GDEF) and isinstance(value.node, TypeInfo):
info = value.node
info.calculate_mro()
assert info.mro, "No MRO calculated for %s" % (info.fullname(),)
compute_all_mros(info.names, modules)
示例3: dump_typeinfos_recursive
def dump_typeinfos_recursive(self, names: SymbolTable) -> List[str]:
a = []
for name, node in sorted(names.items(), key=lambda x: x[0]):
if isinstance(node.node, TypeInfo):
a.extend(self.dump_typeinfo(node.node))
a.extend(self.dump_typeinfos_recursive(node.node.names))
return a
示例4: anal_defs
def anal_defs(self, defs, fnam, mod_id):
"""Perform the first analysis pass.
Resolve the full names of definitions and construct type info
structures, but do not resolve inter-definition references
such as base classes.
"""
self.cur_mod_id = mod_id
self.errors.set_file(fnam)
self.globals = SymbolTable()
self.global_decls = [set()]
# Add implicit definition of '__name__'.
name_def = VarDef([Var("__name__", Any())], True)
defs.insert(0, name_def)
for d in defs:
if isinstance(d, AssignmentStmt):
self.anal_assignment_stmt(d)
elif isinstance(d, FuncDef):
self.anal_func_def(d)
elif isinstance(d, OverloadedFuncDef):
self.anal_overloaded_func_def(d)
elif isinstance(d, TypeDef):
self.anal_type_def(d)
elif isinstance(d, VarDef):
self.anal_var_def(d)
elif isinstance(d, ForStmt):
self.anal_for_stmt(d)
# Add implicit definition of 'None' to builtins, as we cannot define a
# variable with a None type explicitly.
if mod_id == "builtins":
none_def = VarDef([Var("None", NoneTyp())], True)
defs.append(none_def)
self.anal_var_def(none_def)
示例5: replacement_map_from_symbol_table
def replacement_map_from_symbol_table(
old: SymbolTable, new: SymbolTable, prefix: str) -> Dict[SymbolNode, SymbolNode]:
"""Create a new-to-old object identity map by comparing two symbol table revisions.
Both symbol tables must refer to revisions of the same module id. The symbol tables
are compared recursively (recursing into nested class symbol tables), but only within
the given module prefix. Don't recurse into other modules accessible through the symbol
table.
"""
replacements = {} # type: Dict[SymbolNode, SymbolNode]
for name, node in old.items():
if (name in new and (node.kind == MDEF
or node.node and get_prefix(node.node.fullname()) == prefix)):
new_node = new[name]
if (type(new_node.node) == type(node.node) # noqa
and new_node.node and node.node and
new_node.node.fullname() == node.node.fullname() and
new_node.kind == node.kind):
replacements[new_node.node] = node.node
if isinstance(node.node, TypeInfo) and isinstance(new_node.node, TypeInfo):
type_repl = replacement_map_from_symbol_table(
node.node.names,
new_node.node.names,
prefix)
replacements.update(type_repl)
return replacements
示例6: replace_nodes_in_symbol_table
def replace_nodes_in_symbol_table(symbols: SymbolTable,
replacements: Dict[SymbolNode, SymbolNode]) -> None:
for name, node in symbols.items():
if node.node:
if node.node in replacements:
new = replacements[node.node]
old = node.node
replace_object_state(new, old)
node.node = new
if isinstance(node.node, Var):
# Handle them here just in case these aren't exposed through the AST.
# TODO: Is this necessary?
fixup_var(node.node, replacements)
示例7: find_symbol_tables_recursive
def find_symbol_tables_recursive(prefix: str, symbols: SymbolTable) -> Dict[str, SymbolTable]:
"""Find all nested symbol tables.
Args:
prefix: Full name prefix (used for return value keys and to filter result so that
cross references to other modules aren't included)
symbols: Root symbol table
Returns a dictionary from full name to corresponding symbol table.
"""
result = {}
result[prefix] = symbols
for name, node in symbols.items():
if isinstance(node.node, TypeInfo) and node.node.fullname().startswith(prefix + '.'):
more = find_symbol_tables_recursive(prefix + '.' + name, node.node.names)
result.update(more)
return result
示例8: replacement_map_from_symbol_table
def replacement_map_from_symbol_table(
old: SymbolTable, new: SymbolTable, prefix: str) -> Dict[SymbolNode, SymbolNode]:
replacements = {} # type: Dict[SymbolNode, SymbolNode]
for name, node in old.items():
if (name in new and (node.kind == MDEF
or node.node and get_prefix(node.node.fullname()) == prefix)):
new_node = new[name]
if (type(new_node.node) == type(node.node) # noqa
and new_node.node and node.node and
new_node.node.fullname() == node.node.fullname() and
new_node.kind == node.kind):
replacements[new_node.node] = node.node
if isinstance(node.node, TypeInfo) and isinstance(new_node.node, TypeInfo):
type_repl = replacement_map_from_symbol_table(
node.node.names,
new_node.node.names,
prefix)
replacements.update(type_repl)
return replacements
示例9: snapshot_symbol_table
def snapshot_symbol_table(name_prefix: str, table: SymbolTable) -> Dict[str, SnapshotItem]:
"""Create a snapshot description that represents the state of a symbol table.
The snapshot has a representation based on nested tuples and dicts
that makes it easy and fast to find differences.
Only "shallow" state is included in the snapshot -- references to
things defined in other modules are represented just by the names of
the targets.
"""
result = {} # type: Dict[str, SnapshotItem]
for name, symbol in table.items():
node = symbol.node
# TODO: cross_ref?
fullname = node.fullname() if node else None
common = (fullname, symbol.kind, symbol.module_public)
if symbol.kind == MODULE_REF:
# This is a cross-reference to another module.
# If the reference is busted because the other module is missing,
# the node will be a "stale_info" TypeInfo produced by fixup,
# but that doesn't really matter to us here.
result[name] = ('Moduleref', common)
elif symbol.kind == TVAR:
assert isinstance(node, TypeVarExpr)
result[name] = ('TypeVar',
node.variance,
[snapshot_type(value) for value in node.values],
snapshot_type(node.upper_bound))
elif isinstance(symbol.node, TypeAlias):
result[name] = ('TypeAlias',
symbol.node.alias_tvars,
symbol.node.normalized,
symbol.node.no_args,
snapshot_optional_type(symbol.node.target))
else:
assert symbol.kind != UNBOUND_IMPORTED
if node and get_prefix(node.fullname()) != name_prefix:
# This is a cross-reference to a node defined in another module.
result[name] = ('CrossRef', common)
else:
result[name] = snapshot_definition(node, common)
return result
示例10: replace_nodes_in_symbol_table
def replace_nodes_in_symbol_table(symbols: SymbolTable,
replacements: Dict[SymbolNode, SymbolNode]) -> None:
for name, node in symbols.items():
if node.node:
if node.node in replacements:
new = replacements[node.node]
new.__dict__ = node.node.__dict__
node.node = new
# TODO: Other node types
if isinstance(node.node, Var) and node.node.type:
node.node.type.accept(TypeReplaceVisitor(replacements))
node.node.info = cast(TypeInfo, replacements.get(node.node.info,
node.node.info))
else:
# TODO: Other node types
if isinstance(node.node, Var) and node.node.type:
node.node.type.accept(TypeReplaceVisitor(replacements))
override = node.type_override
if override:
override.accept(TypeReplaceVisitor(replacements))
示例11: visit_symbol_table
def visit_symbol_table(self, symtab: SymbolTable) -> None:
# Copy the items because we may mutate symtab.
for key, value in list(symtab.items()):
cross_ref = value.cross_ref
if cross_ref is not None: # Fix up cross-reference.
del value.cross_ref
if cross_ref in self.modules:
value.node = self.modules[cross_ref]
else:
stnode = lookup_qualified_stnode(self.modules, cross_ref)
assert stnode is not None, "Could not find cross-ref %s" % (cross_ref,)
value.node = stnode.node
value.type_override = stnode.type_override
else:
if isinstance(value.node, TypeInfo):
# TypeInfo has no accept(). TODO: Add it?
self.visit_type_info(value.node)
elif value.node is not None:
value.node.accept(self)
if value.type_override is not None:
value.type_override.accept(self.type_fixer)
示例12: add_type_promotion
def add_type_promotion(info: TypeInfo, module_names: SymbolTable, options: Options) -> None:
"""Setup extra, ad-hoc subtyping relationships between classes (promotion).
This includes things like 'int' being compatible with 'float'.
"""
defn = info.defn
promote_target = None # type: Optional[Type]
for decorator in defn.decorators:
if isinstance(decorator, CallExpr):
analyzed = decorator.analyzed
if isinstance(analyzed, PromoteExpr):
# _promote class decorator (undocumented feature).
promote_target = analyzed.type
if not promote_target:
promotions = (TYPE_PROMOTIONS_PYTHON3 if options.python_version[0] >= 3
else TYPE_PROMOTIONS_PYTHON2)
if defn.fullname in promotions:
target_sym = module_names.get(promotions[defn.fullname])
# With test stubs, the target may not exist.
if target_sym:
target_info = target_sym.node
assert isinstance(target_info, TypeInfo)
promote_target = Instance(target_info, [])
defn.info._promote = promote_target
示例13: visit_symbol_table
def visit_symbol_table(self, symtab: SymbolTable) -> None:
# Copy the items because we may mutate symtab.
for key, value in list(symtab.items()):
cross_ref = value.cross_ref
if cross_ref is not None: # Fix up cross-reference.
value.cross_ref = None
if cross_ref in self.modules:
value.node = self.modules[cross_ref]
else:
stnode = lookup_qualified_stnode(self.modules, cross_ref,
self.allow_missing)
if stnode is not None:
value.node = stnode.node
elif not self.allow_missing:
assert stnode is not None, "Could not find cross-ref %s" % (cross_ref,)
else:
# We have a missing crossref in allow missing mode, need to put something
value.node = missing_info(self.modules)
else:
if isinstance(value.node, TypeInfo):
# TypeInfo has no accept(). TODO: Add it?
self.visit_type_info(value.node)
elif value.node is not None:
value.node.accept(self)
示例14: analyze_symbol_table
def analyze_symbol_table(self, names: SymbolTable) -> None:
"""Analyze types in symbol table nodes only (shallow)."""
for node in names.values():
if isinstance(node.node, TypeAlias):
self.analyze(node.node.target, node.node)
示例15: analyze_symbol_table
def analyze_symbol_table(self, names: SymbolTable) -> None:
"""Analyze types in symbol table nodes only (shallow)."""
for node in names.values():
if node.type_override:
self.analyze(node.type_override, node)