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)
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)
def add_property(cls_node: TypeInfo, ans_cls_node: TypeInfo, prop_node: Expression, api: SemanticAnalyzerPluginInterface) -> None: """Add a property.""" if not isinstance(prop_node, StrExpr): api.fail('Keyword must be a string literal', prop_node, code=VALID_TYPE) return prop_name = prop_node.value try: ans_type = ans_cls_node[prop_name].type except KeyError: api.fail( f'Attribute `{prop_name}` does not exist in ' f'{ans_cls_node.name} or its parents', prop_node, code=ATTR_DEFINED) return prop_type = get_transformed_type(cls_node, ans_type, prop_node, api) if prop_type is None: return if not has_default(cls_node, prop_node, api) and prop_type is not None: prop_type = make_optional(prop_type) new_prop = Var(prop_name, api.anal_type(prop_type)) new_prop.info = cls_node new_prop.is_initialized_in_class = True new_prop.is_property = True cls_node.names[prop_name] = SymbolTableNode(MDEF, new_prop)
def add_new_node_to_model_class(self, name: str, typ: Instance) -> None: var = Var(name=name, type=typ) var.info = typ.type var._fullname = self.model_classdef.info.fullname() + '.' + name var.is_inferred = True var.is_initialized_in_class = True self.model_classdef.info.names[name] = SymbolTableNode(MDEF, var)
def add_field_to_new_typeinfo(var: Var, is_initialized_in_class: bool = False, is_property: bool = False) -> None: var.info = new_typeinfo var.is_initialized_in_class = is_initialized_in_class var.is_property = is_property var._fullname = new_typeinfo.fullname() + '.' + var.name() new_typeinfo.names[var.name()] = SymbolTableNode(MDEF, var)
def create_new_var(self, name: str, typ: Instance) -> Var: # type=: type of the variable itself var = Var(name=name, type=typ) # var.info: type of the object variable is bound to var.info = self.model_classdef.info var._fullname = self.model_classdef.info.fullname() + '.' + name var.is_initialized_in_class = True var.is_inferred = True return var
def add_new_sym_for_info(info: TypeInfo, *, name: str, sym_type: MypyType) -> None: # type=: type of the variable itself var = Var(name=name, type=sym_type) # var.info: type of the object variable is bound to var.info = info var._fullname = info.fullname + "." + name var.is_initialized_in_class = True var.is_inferred = True info.names[name] = SymbolTableNode(MDEF, var, plugin_generated=True)
def add_new_node_to_model_class(self, name: str, typ: Instance) -> None: # type=: type of the variable itself var = Var(name=name, type=typ) # var.info: type of the object variable is bound to var.info = self.model_classdef.info var._fullname = self.model_classdef.info.fullname() + '.' + name var.is_inferred = True var.is_initialized_in_class = True self.model_classdef.info.names[name] = SymbolTableNode( MDEF, var, plugin_generated=True)
def analyse_lvalue(self, lval, nested=False, add_defs=False): if isinstance(lval, NameExpr): n = lval nested_global = not self.locals and self.block_depth > 0 and not self.type if (add_defs or nested_global) and n.name not in self.globals: # Define new global name. v = Var(n.name) v._full_name = self.qualified_name(n.name) v.is_ready = False # Type not inferred yet n.node = v n.is_def = True n.kind = GDEF n.full_name = v._full_name self.globals[n.name] = SymbolTableNode(GDEF, v, self.cur_mod_id) elif isinstance(n.node, Var) and n.is_def: v = n.node self.module_names[v.name()] = SymbolTableNode(GDEF, v, self.cur_mod_id) elif self.locals and n.name not in self.locals[-1] and n.name not in self.global_decls[-1]: # Define new local name. v = Var(n.name) n.node = v n.is_def = True n.kind = LDEF self.add_local(v, n) elif not self.locals and (self.type and n.name not in self.type.vars): # Define a new attribute. v = Var(n.name) v.info = self.type v.is_initialized_in_class = True n.node = v n.is_def = True self.type.vars[n.name] = v else: # Bind to an existing name. lval.accept(self) elif isinstance(lval, MemberExpr): if not add_defs: self.analyse_member_lvalue(lval) elif isinstance(lval, IndexExpr): if not add_defs: lval.accept(self) elif isinstance(lval, ParenExpr): self.analyse_lvalue((lval).expr, nested, add_defs) elif (isinstance(lval, TupleExpr) or isinstance(lval, ListExpr)) and not nested: items = (lval).items for i in items: self.analyse_lvalue(i, True, add_defs) else: self.fail("Invalid assignment target", lval)
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_property = node.is_property new.set_line(node.line) self.var_map[node] = new return new
def _adjust_interface_function( self, api: SemanticAnalyzerPluginInterface, class_info: TypeInfo, func_def: FuncDef, ) -> Statement: if func_def.arg_names and func_def.arg_names[0] == "self": # reveal the common mistake of leaving "self" arguments in the # interface api.fail("Interface methods should not have 'self' argument", func_def) else: selftype = Instance(class_info, [], line=class_info.line, column=class_info.column) selfarg = Argument(Var("self", None), selftype, None, ARG_POS) if isinstance(func_def.type, CallableType): func_def.type.arg_names.insert(0, "self") func_def.type.arg_kinds.insert(0, ARG_POS) func_def.type.arg_types.insert(0, selftype) func_def.arg_names.insert(0, "self") func_def.arg_kinds.insert(0, ARG_POS) func_def.arguments.insert(0, selfarg) func_def.is_abstract = True if func_def.name() in ("__getattr__", "__getattribute__"): # These special methods cannot be decorated. Likely a mypy bug. return func_def func_def.is_decorated = True var = Var(func_def.name(), func_def.type) var.is_initialized_in_class = True var.info = func_def.info var.set_line(func_def.line) decor = Decorator(func_def, [], var) return decor