本文整理汇总了Python中mypy.messages.MessageBuilder.cant_assign_to_classvar方法的典型用法代码示例。如果您正苦于以下问题:Python MessageBuilder.cant_assign_to_classvar方法的具体用法?Python MessageBuilder.cant_assign_to_classvar怎么用?Python MessageBuilder.cant_assign_to_classvar使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.messages.MessageBuilder
的用法示例。
在下文中一共展示了MessageBuilder.cant_assign_to_classvar方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: analyze_var
# 需要导入模块: from mypy.messages import MessageBuilder [as 别名]
# 或者: from mypy.messages.MessageBuilder import cant_assign_to_classvar [as 别名]
def analyze_var(name: str, var: Var, itype: Instance, info: TypeInfo, node: Context,
is_lvalue: bool, msg: MessageBuilder, original_type: Type,
not_ready_callback: Callable[[str, Context], None], *,
chk: 'mypy.checker.TypeChecker') -> Type:
"""Analyze access to an attribute via a Var node.
This is conceptually part of analyze_member_access and the arguments are similar.
itype is the class object in which var is dedined
original_type is the type of E in the expression E.var
"""
# Found a member variable.
itype = map_instance_to_supertype(itype, var.info)
typ = var.type
if typ:
if isinstance(typ, PartialType):
return handle_partial_attribute_type(typ, is_lvalue, msg, var)
t = expand_type_by_instance(typ, itype)
if is_lvalue and var.is_property and not var.is_settable_property:
# TODO allow setting attributes in subclass (although it is probably an error)
msg.read_only_property(name, info, node)
if is_lvalue and var.is_classvar:
msg.cant_assign_to_classvar(name, node)
result = t
if var.is_initialized_in_class and isinstance(t, FunctionLike) and not t.is_type_obj():
if is_lvalue:
if var.is_property:
if not var.is_settable_property:
msg.read_only_property(name, info, node)
else:
msg.cant_assign_to_method(node)
if not var.is_staticmethod:
# Class-level function objects and classmethods become bound methods:
# the former to the instance, the latter to the class.
functype = t
# Use meet to narrow original_type to the dispatched type.
# For example, assume
# * A.f: Callable[[A1], None] where A1 <: A (maybe A1 == A)
# * B.f: Callable[[B1], None] where B1 <: B (maybe B1 == B)
# * x: Union[A1, B1]
# In `x.f`, when checking `x` against A1 we assume x is compatible with A
# and similarly for B1 when checking agains B
dispatched_type = meet.meet_types(original_type, itype)
check_self_arg(functype, dispatched_type, var.is_classmethod, node, name, msg)
signature = bind_self(functype, original_type, var.is_classmethod)
if var.is_property:
# A property cannot have an overloaded type => the cast is fine.
assert isinstance(signature, CallableType)
result = signature.ret_type
else:
result = signature
else:
if not var.is_ready:
not_ready_callback(var.name(), node)
# Implicit 'Any' type.
result = AnyType(TypeOfAny.special_form)
fullname = '{}.{}'.format(var.info.fullname(), name)
hook = chk.plugin.get_attribute_hook(fullname)
if hook:
result = hook(AttributeContext(original_type, result, node, chk))
return result