Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if func.name() in sem.globals:
# Already defined in this module.
original_sym = sem.globals[func.name()]
if original_sym.kind == UNBOUND_IMPORTED:
# Ah this is an imported name. We can't resolve them now, so we'll postpone
# this until the main phase of semantic analysis.
return
original_def = original_sym.node
if sem.is_conditional_func(original_def, func):
# Conditional function definition -- multiple defs are ok.
func.original_def = cast(FuncDef, original_def)
else:
# Report error.
sem.check_no_global(func.name(), func)
else:
sem.globals[func.name()] = SymbolTableNode(GDEF, func, sem.cur_mod_id)
if is_classmethod or is_staticmethod:
func.is_decorated = True
v = Var(name, func.type)
v.info = info
v._fullname = func._fullname
if is_classmethod:
v.is_classmethod = True
dec = Decorator(func, [NameExpr("classmethod")], v)
else:
v.is_staticmethod = True
dec = Decorator(func, [NameExpr("staticmethod")], v)
dec.line = info.line
sym = SymbolTableNode(MDEF, dec)
else:
sym = SymbolTableNode(MDEF, func)
sym.plugin_generated = True
info.names[name] = sym
info.defn.defs.body.append(func)
def add_field(var: Var, is_initialized_in_class: bool = False,
is_property: bool = False) -> None:
var.info = info
var.is_initialized_in_class = is_initialized_in_class
var.is_property = is_property
var._fullname = '%s.%s' % (info.fullname(), var.name())
info.names[var.name()] = SymbolTableNode(MDEF, var)
with state.strict_optional_set(options.strict_optional):
# Add implicit definitions of module '__name__' etc.
for name, t in implicit_module_attrs.items():
# unicode docstrings should be accepted in Python 2
if name == '__doc__':
if self.pyversion >= (3, 0):
typ = UnboundType('__builtins__.str') # type: Type
else:
typ = UnionType([UnboundType('__builtins__.str'),
UnboundType('__builtins__.unicode')])
else:
assert t is not None, 'type should be specified for {}'.format(name)
typ = UnboundType(t)
v = Var(name, typ)
v._fullname = self.sem.qualified_name(name)
self.sem.globals[name] = SymbolTableNode(GDEF, v)
for i, d in enumerate(defs):
d.accept(self)
if isinstance(d, AssertStmt) and assert_will_always_fail(d, options):
# We've encountered an assert that's always false,
# e.g. assert sys.platform == 'lol'. Truncate the
# list of statements. This mutates file.defs too.
del defs[i + 1:]
break
# Add implicit definition of literals/keywords to builtins, as we
# cannot define a variable with them explicitly.
if mod_id == 'builtins':
literal_types = [
('None', NoneType()),
# reveal_type is a mypy-only function that gives an error with
def process_nested_classes(self, outer_def: ClassDef) -> None:
for node in outer_def.defs.body:
if isinstance(node, ClassDef):
node.info = TypeInfo(SymbolTable(), node)
if outer_def.fullname:
node.info._fullname = outer_def.fullname + '.' + node.info.name()
else:
node.info._fullname = node.info.name()
symbol = SymbolTableNode(MDEF, node.info)
outer_def.info.names[node.name] = symbol
self.process_nested_classes(node)
def add_var_to_class(name: str, typ: Type, info: TypeInfo) -> None:
"""Add a variable with given name and type to the symbol table of a class.
This also takes care about setting necessary attributes on the variable node.
"""
var = Var(name)
var.info = info
var._fullname = fullname(info) + '.' + name
var.type = typ
info.names[name] = SymbolTableNode(MDEF, var)
def visit_import(self, node: Import) -> None:
node.is_top_level = True
# This is similar to visit_import_from -- see the comment there.
for id, as_id in node.ids:
imported_id = as_id or id
if imported_id not in self.sem.globals:
self.sem.add_symbol(imported_id, SymbolTableNode(UNBOUND_IMPORTED, None), node)
else:
# If the previous symbol is a variable, this should take precedence.
self.sem.globals[imported_id] = SymbolTableNode(UNBOUND_IMPORTED, None)
info.names[r_name] = info.names[name]
if is_classmethod or is_staticmethod:
func.is_decorated = True
v = Var(name, func.type)
v.info = info
v._fullname = func._fullname
if is_classmethod:
v.is_classmethod = True
dec = Decorator(func, [NameExpr("classmethod")], v)
else:
v.is_staticmethod = True
dec = Decorator(func, [NameExpr("staticmethod")], v)
dec.line = info.line
sym = SymbolTableNode(MDEF, dec)
else:
sym = SymbolTableNode(MDEF, func)
sym.plugin_generated = True
info.names[name] = sym
info.defn.defs.body.append(func)