本文整理汇总了Python中mypy.nodes.Var.info方法的典型用法代码示例。如果您正苦于以下问题:Python Var.info方法的具体用法?Python Var.info怎么用?Python Var.info使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.nodes.Var
的用法示例。
在下文中一共展示了Var.info方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_field
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
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)
示例2: add_method
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
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
if is_classmethod:
v = Var(funcname, func.type)
v.is_classmethod = True
v.info = info
v._fullname = func._fullname
dec = Decorator(func, [NameExpr('classmethod')], v)
info.names[funcname] = SymbolTableNode(MDEF, dec)
else:
info.names[funcname] = SymbolTableNode(MDEF, func)
示例3: add_magic_hook
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def add_magic_hook(ctx) -> None:
info = ctx.cls.info
str_type = ctx.api.named_type_or_none('builtins.str', [])
assert str_type is not None
var = Var('__magic__', str_type)
var.info = info
info.names['__magic__'] = SymbolTableNode(MDEF, var)
示例4: analyse_member_lvalue
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def analyse_member_lvalue(self, lval):
lval.accept(self)
if self.is_init_method and isinstance(lval.expr, NameExpr):
node = (lval.expr).node
if isinstance(node, Var) and (node).is_self and lval.name not in self.type.vars:
lval.is_def = True
v = Var(lval.name)
v.info = self.type
v.is_ready = False
lval.def_var = v
self.type.vars[lval.name] = v
示例5: build_enum_call_typeinfo
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def build_enum_call_typeinfo(self, name: str, items: List[str], fullname: str) -> TypeInfo:
base = self.api.named_type_or_none(fullname)
assert base is not None
info = self.api.basic_new_typeinfo(name, base)
info.metaclass_type = info.calculate_metaclass_type()
info.is_enum = True
for item in items:
var = Var(item)
var.info = info
var.is_property = True
var._fullname = '{}.{}'.format(self.api.qualified_name(name), item)
info.names[item] = SymbolTableNode(MDEF, var)
return info
示例6: analyse_lvalue
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
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)
示例7: _make_frozen
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def _make_frozen(ctx: 'mypy.plugin.ClassDefContext', attributes: List[Attribute]) -> None:
"""Turn all the attributes into properties to simulate frozen classes."""
for attribute in attributes:
if attribute.name in ctx.cls.info.names:
# This variable belongs to this class so we can modify it.
node = ctx.cls.info.names[attribute.name].node
assert isinstance(node, Var)
node.is_property = True
else:
# This variable belongs to a super class so create new Var so we
# can modify it.
var = Var(attribute.name, ctx.cls.info[attribute.name].type)
var.info = ctx.cls.info
var._fullname = '%s.%s' % (ctx.cls.info.fullname(), var.name())
ctx.cls.info.names[var.name()] = SymbolTableNode(MDEF, var)
var.is_property = True
示例8: visit_var
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
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
示例9: visit_var
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def visit_var(self, v: Var) -> None:
if self.current_info is not None:
v.info = self.current_info
if v.type is not None:
v.type.accept(self.type_fixer)
示例10: analyze_member_var_access
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def analyze_member_var_access(name: str,
itype: Instance,
info: TypeInfo,
mx: MemberContext) -> Type:
"""Analyse attribute access that does not target a method.
This is logically part of analyze_member_access and the arguments are similar.
original_type is the type of E in the expression E.var
"""
# It was not a method. Try looking up a variable.
v = lookup_member_var_or_accessor(info, name, mx.is_lvalue)
vv = v
if isinstance(vv, Decorator):
# The associated Var node of a decorator contains the type.
v = vv.var
if isinstance(vv, TypeInfo):
# If the associated variable is a TypeInfo synthesize a Var node for
# the purposes of type checking. This enables us to type check things
# like accessing class attributes on an inner class.
v = Var(name, type=type_object_type(vv, mx.builtin_type))
v.info = info
if isinstance(vv, TypeAlias) and isinstance(vv.target, Instance):
# Similar to the above TypeInfo case, we allow using
# qualified type aliases in runtime context if it refers to an
# instance type. For example:
# class C:
# A = List[int]
# x = C.A() <- this is OK
typ = instance_alias_type(vv, mx.builtin_type)
v = Var(name, type=typ)
v.info = info
if isinstance(v, Var):
implicit = info[name].implicit
# An assignment to final attribute is always an error,
# independently of types.
if mx.is_lvalue and not mx.chk.get_final_context():
check_final_member(name, info, mx.msg, mx.context)
return analyze_var(name, v, itype, info, mx, implicit=implicit)
elif isinstance(v, FuncDef):
assert False, "Did not expect a function"
elif (not v and name not in ['__getattr__', '__setattr__', '__getattribute__'] and
not mx.is_operator):
if not mx.is_lvalue:
for method_name in ('__getattribute__', '__getattr__'):
method = info.get_method(method_name)
# __getattribute__ is defined on builtins.object and returns Any, so without
# the guard this search will always find object.__getattribute__ and conclude
# that the attribute exists
if method and method.info.fullname() != 'builtins.object':
function = function_type(method, mx.builtin_type('builtins.function'))
bound_method = bind_self(function, mx.original_type)
typ = map_instance_to_supertype(itype, method.info)
getattr_type = expand_type_by_instance(bound_method, typ)
if isinstance(getattr_type, CallableType):
result = getattr_type.ret_type
# Call the attribute hook before returning.
fullname = '{}.{}'.format(method.info.fullname(), name)
hook = mx.chk.plugin.get_attribute_hook(fullname)
if hook:
result = hook(AttributeContext(mx.original_type, result,
mx.context, mx.chk))
return result
else:
setattr_meth = info.get_method('__setattr__')
if setattr_meth and setattr_meth.info.fullname() != 'builtins.object':
setattr_func = function_type(setattr_meth, mx.builtin_type('builtins.function'))
bound_type = bind_self(setattr_func, mx.original_type)
typ = map_instance_to_supertype(itype, setattr_meth.info)
setattr_type = expand_type_by_instance(bound_type, typ)
if isinstance(setattr_type, CallableType) and len(setattr_type.arg_types) > 0:
return setattr_type.arg_types[-1]
if itype.type.fallback_to_any:
return AnyType(TypeOfAny.special_form)
# Could not find the member.
if mx.is_super:
mx.msg.undefined_in_superclass(name, mx.context)
return AnyType(TypeOfAny.from_error)
else:
if mx.chk and mx.chk.should_suppress_optional_error([itype]):
return AnyType(TypeOfAny.from_error)
return mx.msg.has_no_attr(mx.original_type, itype, name, mx.context)
示例11: analyze_member_var_access
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo,
node: Context, is_lvalue: bool, is_super: bool,
builtin_type: Callable[[str], Instance],
not_ready_callback: Callable[[str, Context], None],
msg: MessageBuilder,
original_type: Type,
chk: 'mypy.checker.TypeChecker') -> Type:
"""Analyse attribute access that does not target a method.
This is logically part of analyze_member_access and the arguments are similar.
original_type is the type of E in the expression E.var
"""
# 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(vv, TypeInfo):
# If the associated variable is a TypeInfo synthesize a Var node for
# the purposes of type checking. This enables us to type check things
# like accessing class attributes on an inner class.
v = Var(name, type=type_object_type(vv, builtin_type))
v.info = info
if isinstance(v, Var):
return analyze_var(name, v, itype, info, node, is_lvalue, msg,
original_type, not_ready_callback, chk=chk)
elif isinstance(v, FuncDef):
assert False, "Did not expect a function"
elif not v and name not in ['__getattr__', '__setattr__', '__getattribute__']:
if not is_lvalue:
for method_name in ('__getattribute__', '__getattr__'):
method = info.get_method(method_name)
# __getattribute__ is defined on builtins.object and returns Any, so without
# the guard this search will always find object.__getattribute__ and conclude
# that the attribute exists
if method and method.info.fullname() != 'builtins.object':
function = function_type(method, builtin_type('builtins.function'))
bound_method = bind_self(function, original_type)
typ = map_instance_to_supertype(itype, method.info)
getattr_type = expand_type_by_instance(bound_method, typ)
if isinstance(getattr_type, CallableType):
return getattr_type.ret_type
else:
setattr_meth = info.get_method('__setattr__')
if setattr_meth and setattr_meth.info.fullname() != 'builtins.object':
setattr_func = function_type(setattr_meth, builtin_type('builtins.function'))
bound_type = bind_self(setattr_func, original_type)
typ = map_instance_to_supertype(itype, setattr_meth.info)
setattr_type = expand_type_by_instance(bound_type, typ)
if isinstance(setattr_type, CallableType) and len(setattr_type.arg_types) > 0:
return setattr_type.arg_types[-1]
if itype.type.fallback_to_any:
return AnyType(TypeOfAny.special_form)
# Could not find the member.
if is_super:
msg.undefined_in_superclass(name, node)
return AnyType(TypeOfAny.from_error)
else:
if chk and chk.should_suppress_optional_error([itype]):
return AnyType(TypeOfAny.from_error)
return msg.has_no_attr(original_type, itype, name, node)
示例12: fixup_var
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def fixup_var(node: Var, replacements: Dict[SymbolNode, SymbolNode]) -> None:
if node.type:
node.type.accept(TypeReplaceVisitor(replacements))
node.info = cast(TypeInfo, replacements.get(node.info, node.info))
示例13: visit_var
# 需要导入模块: from mypy.nodes import Var [as 别名]
# 或者: from mypy.nodes.Var import info [as 别名]
def visit_var(self, node: Var) -> None:
node.info = self.fixup(node.info)
self.fixup_type(node.type)
super().visit_var(node)