Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def visit_var(self, node: Var) -> Var:
# Note that a Var must be transformed to a Var.
if node in self.var_map:
return self.var_map[node]
new = Var(node.name(), self.optional_type(node.type))
new.line = node.line
new._fullname = node._fullname
new.info = node.info
new.is_self = node.is_self
new.is_ready = node.is_ready
new.is_initialized_in_class = node.is_initialized_in_class
new.is_staticmethod = node.is_staticmethod
new.is_classmethod = node.is_classmethod
new.is_property = node.is_property
new.set_line(node.line)
self.var_map[node] = new
return new
return_type if return_type is not None else AnyType(),
None)
func_def = FuncDef(n.name,
args,
self.as_block(n.body, n.lineno),
func_type)
if is_coroutine:
# A coroutine is also a generator, mostly for internal reasons.
func_def.is_generator = func_def.is_coroutine = True
if func_type is not None:
func_type.definition = func_def
func_type.line = n.lineno
if n.decorator_list:
var = Var(func_def.name())
var.is_ready = False
var.set_line(n.decorator_list[0].lineno)
func_def.is_decorated = True
func_def.set_line(n.lineno + len(n.decorator_list))
func_def.body.set_line(func_def.get_line())
return Decorator(func_def, self.visit_list(n.decorator_list), var)
else:
return func_def
# already in the first pass and added to the symbol table.
assert lval.node.name() in self.globals
elif (self.is_func_scope() and lval.name not in self.locals[-1] and
lval.name not in self.global_decls[-1] and
lval.name not in self.nonlocal_decls[-1]):
# Define new local name.
v = Var(lval.name)
lval.node = v
lval.is_def = True
lval.kind = LDEF
lval.fullname = lval.name
self.add_local(v, lval)
elif not self.is_func_scope() and (self.type and
lval.name not in self.type.names):
# Define a new attribute within class body.
v = Var(lval.name)
v.info = self.type
v.is_initialized_in_class = True
v.set_line(lval)
lval.node = v
lval.is_def = True
lval.kind = MDEF
lval.fullname = lval.name
self.type.names[lval.name] = SymbolTableNode(MDEF, v)
else:
# Bind to an existing name.
if explicit_type:
self.name_already_defined(lval.name, lval)
lval.accept(self)
self.check_lvalue_validity(lval.node, lval)
elif isinstance(lval, MemberExpr):
if not add_global:
msg: MessageBuilder,
report_type: Type = None,
chk: 'mypy.checker.TypeChecker' = None) -> Type:
"""Analyse attribute access that does not target a method.
This is logically part of analyze_member_access and the arguments are
similar.
"""
# It was not a method. Try looking up a variable.
v = lookup_member_var_or_accessor(info, name, is_lvalue)
vv = v
if isinstance(vv, Decorator):
# The associated Var node of a decorator contains the type.
v = vv.var
if isinstance(v, Var):
return analyze_var(name, v, itype, info, node, is_lvalue, msg, not_ready_callback)
elif isinstance(v, FuncDef):
assert False, "Did not expect a function"
elif not v and name not in ['__getattr__', '__setattr__']:
if not is_lvalue:
method = info.get_method('__getattr__')
if method:
typ = map_instance_to_supertype(itype, method.info)
getattr_type = expand_type_by_instance(
method_type_with_fallback(method, builtin_type('builtins.function')), typ)
if isinstance(getattr_type, CallableType):
return getattr_type.ret_type
if itype.type.fallback_to_any:
return AnyType()
. def set$name*(self: C, name; Any) -> None:
. self.name! = {typ name}
"""
lvalue = MemberExpr(self_expr(), name, direct=True)
name_expr = NameExpr(name)
rvalue = coerce(name_expr, typ, AnyType(), self.tf.type_context())
ret = AssignmentStmt([lvalue], rvalue)
wrapper_name = 'set$' + name + self.tf.dynamic_suffix()
selft = self_type(self.tf.type_context())
sig = Callable([selft, AnyType()],
[nodes.ARG_POS, nodes.ARG_POS],
[None, None],
Void(), False)
return FuncDef(wrapper_name,
[Var('self'), Var(name)],
[nodes.ARG_POS, nodes.ARG_POS],
[None, None],
Block([ret]), sig)
for n in range(num_slots(base)):
cargs.append(NameExpr(tvar_arg_name(n + 1)))
for n in range(num_slots(base)):
cargs.append(NameExpr(tvar_arg_name(n + 1, BOUND_VAR)))
c = CallExpr(s, cargs, [nodes.ARG_POS] * len(cargs))
cdefs.append(ExpressionStmt(c))
# Create initialization of the wrapped object.
cdefs.append(AssignmentStmt([MemberExpr(
self_expr(),
self.object_member_name(info),
direct=True)],
NameExpr('__o')))
# Build constructor arguments.
args = [Var('self'), Var('__o')]
init = [None, None] # type: List[Node]
for alt in [False, BOUND_VAR]:
for n in range(nslots):
args.append(Var(tvar_arg_name(n + 1, alt)))
init.append(None)
nargs = nslots * 2 + 2
fdef = FuncDef('__init__',
args,
[nodes.ARG_POS] * nargs,
init,
Block(cdefs),
Callable([AnyType()] * nargs,
[nodes.ARG_POS] * nargs, [None] * nargs,
Void(),
def add_method(funcname: str,
ret: Type,
args: List[Argument],
is_classmethod: bool = False,
is_new: bool = False,
) -> None:
if is_classmethod or is_new:
first = [Argument(Var('_cls'), TypeType.make_normalized(selftype), None, ARG_POS)]
else:
first = [Argument(Var('_self'), selftype, None, ARG_POS)]
args = first + args
types = [arg.type_annotation for arg in args]
items = [arg.variable.name for arg in args]
arg_kinds = [arg.kind for arg in args]
assert None not in types
signature = CallableType(cast(List[Type], types), arg_kinds, items, ret,
function_type)
signature.variables = [tvd]
func = FuncDef(funcname, args, Block([]))
func.info = info
func.is_class = is_classmethod
func.type = set_callable_name(signature, func)
func._fullname = info.fullname + '.' + funcname
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)
vars = [Var(item, typ) for item, typ in zip(items, types)]
for var in vars:
add_field(var, is_property=True)
tuple_of_strings = TupleType([strtype for _ in items], basetuple_type)
add_field(Var('_fields', tuple_of_strings), is_initialized_in_class=True)
add_field(Var('_field_types', dictype), is_initialized_in_class=True)
add_field(Var('_field_defaults', dictype), is_initialized_in_class=True)
add_field(Var('_source', strtype), is_initialized_in_class=True)
add_field(Var('__annotations__', ordereddictype), is_initialized_in_class=True)
add_field(Var('__doc__', strtype), is_initialized_in_class=True)
tvd = TypeVarDef('NT', 'NT', -1, [], info.tuple_type)
selftype = TypeVarType(tvd)
def add_method(funcname: str,
ret: Type,
args: List[Argument],
name: Optional[str] = None,
is_classmethod: bool = False,
is_new: bool = False,
) -> None:
if is_classmethod or is_new:
first = [Argument(Var('cls'), TypeType.make_normalized(selftype), None, ARG_POS)]
else:
def add_method(funcname: str,
ret: Type,
args: List[Argument],
name: Optional[str] = None,
is_classmethod: bool = False,
is_new: bool = False,
) -> None:
if is_classmethod or is_new:
first = [Argument(Var('cls'), TypeType.make_normalized(selftype), None, ARG_POS)]
else:
first = [Argument(Var('self'), selftype, None, ARG_POS)]
args = first + args
types = [arg.type_annotation for arg in args]
items = [arg.variable.name() for arg in args]
arg_kinds = [arg.kind for arg in args]
assert None not in types
signature = CallableType(cast(List[Type], types), arg_kinds, items, ret,
function_type)
signature.variables = [tvd]
func = FuncDef(funcname, args, Block([]))
func.info = info
func.is_class = is_classmethod
func.type = set_callable_name(signature, func)
func._fullname = info.fullname() + '.' + funcname
func.type = set_callable_name(signature, func)
func.is_class = is_classmethod
func.is_static = is_staticmethod
func._fullname = get_fullname(info) + "." + name
func.line = info.line
# NOTE: we would like the plugin generated node to dominate, but we still
# need to keep any existing definitions so they get semantically analyzed.
if name in info.names:
# Get a nice unique name instead.
r_name = get_unique_redefinition_name(name, info.names)
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