本文整理汇总了Python中mypy.types.Instance.invalid方法的典型用法代码示例。如果您正苦于以下问题:Python Instance.invalid方法的具体用法?Python Instance.invalid怎么用?Python Instance.invalid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.types.Instance
的用法示例。
在下文中一共展示了Instance.invalid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_instance
# 需要导入模块: from mypy.types import Instance [as 别名]
# 或者: from mypy.types.Instance import invalid [as 别名]
def visit_instance(self, t: Instance) -> None:
info = t.type
if info.replaced or info.tuple_type:
self.indicator['synthetic'] = True
# Check type argument count.
if len(t.args) != len(info.type_vars):
if len(t.args) == 0:
from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
if (self.options.disallow_any_generics and
not self.is_typeshed_stub and
from_builtins):
alternative = nongen_builtins[t.type.fullname()]
self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
# Insert implicit 'Any' type arguments.
if from_builtins:
# this 'Any' was already reported elsewhere
any_type = AnyType(TypeOfAny.special_form,
line=t.line, column=t.column)
else:
any_type = AnyType(TypeOfAny.from_omitted_generics,
line=t.line, column=t.column)
t.args = [any_type] * len(info.type_vars)
return
# Invalid number of type parameters.
n = len(info.type_vars)
s = '{} type arguments'.format(n)
if n == 0:
s = 'no type arguments'
elif n == 1:
s = '1 type argument'
act = str(len(t.args))
if act == '0':
act = 'none'
self.fail('"{}" expects {}, but {} given'.format(
info.name(), s, act), t)
# Construct the correct number of type arguments, as
# otherwise the type checker may crash as it expects
# things to be right.
t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
t.invalid = True
elif info.defn.type_vars:
# Check type argument values. This is postponed to the end of semantic analysis
# since we need full MROs and resolved forward references.
for tvar in info.defn.type_vars:
if (tvar.values
or not isinstance(tvar.upper_bound, Instance)
or tvar.upper_bound.type.fullname() != 'builtins.object'):
# Some restrictions on type variable. These can only be checked later
# after we have final MROs and forward references have been resolved.
self.indicator['typevar'] = True
for arg in t.args:
arg.accept(self)
if info.is_newtype:
for base in info.bases:
base.accept(self)
示例2: visit_instance
# 需要导入模块: from mypy.types import Instance [as 别名]
# 或者: from mypy.types.Instance import invalid [as 别名]
def visit_instance(self, t: Instance) -> None:
info = t.type
# Check type argument count.
if len(t.args) != len(info.type_vars):
if len(t.args) == 0:
# Insert implicit 'Any' type arguments.
t.args = [AnyType()] * len(info.type_vars)
return
# Invalid number of type parameters.
n = len(info.type_vars)
s = '{} type arguments'.format(n)
if n == 0:
s = 'no type arguments'
elif n == 1:
s = '1 type argument'
act = str(len(t.args))
if act == '0':
act = 'none'
self.fail('"{}" expects {}, but {} given'.format(
info.name(), s, act), t)
# Construct the correct number of type arguments, as
# otherwise the type checker may crash as it expects
# things to be right.
t.args = [AnyType() for _ in info.type_vars]
t.invalid = True
elif info.defn.type_vars:
# Check type argument values.
for (i, arg), TypeVar in zip(enumerate(t.args), info.defn.type_vars):
if TypeVar.values:
if isinstance(arg, TypeVarType):
arg_values = arg.values
if not arg_values:
self.fail('Type variable "{}" not valid as type '
'argument value for "{}"'.format(
arg.name, info.name()), t)
continue
else:
arg_values = [arg]
self.check_type_var_values(info, arg_values,
TypeVar.values, i + 1, t)
if not satisfies_upper_bound(arg, TypeVar.upper_bound):
self.fail('Type argument "{}" of "{}" must be '
'a subtype of "{}"'.format(
arg, info.name(), TypeVar.upper_bound), t)
for arg in t.args:
arg.accept(self)
示例3: visit_instance
# 需要导入模块: from mypy.types import Instance [as 别名]
# 或者: from mypy.types.Instance import invalid [as 别名]
def visit_instance(self, t: Instance) -> None:
info = t.type
if info.replaced or info.tuple_type:
self.indicator['synthetic'] = True
# Check type argument count.
if len(t.args) != len(info.type_vars):
if len(t.args) == 0:
from_builtins = t.type.fullname() in nongen_builtins and not t.from_generic_builtin
if (self.options.disallow_any_generics and
not self.is_typeshed_stub and
from_builtins):
alternative = nongen_builtins[t.type.fullname()]
self.fail(messages.IMPLICIT_GENERIC_ANY_BUILTIN.format(alternative), t)
# Insert implicit 'Any' type arguments.
if from_builtins:
# this 'Any' was already reported elsewhere
any_type = AnyType(TypeOfAny.special_form,
line=t.line, column=t.column)
else:
any_type = AnyType(TypeOfAny.from_omitted_generics,
line=t.line, column=t.column)
t.args = [any_type] * len(info.type_vars)
return
# Invalid number of type parameters.
n = len(info.type_vars)
s = '{} type arguments'.format(n)
if n == 0:
s = 'no type arguments'
elif n == 1:
s = '1 type argument'
act = str(len(t.args))
if act == '0':
act = 'none'
self.fail('"{}" expects {}, but {} given'.format(
info.name(), s, act), t)
# Construct the correct number of type arguments, as
# otherwise the type checker may crash as it expects
# things to be right.
t.args = [AnyType(TypeOfAny.from_error) for _ in info.type_vars]
t.invalid = True
elif info.defn.type_vars:
# Check type argument values.
# TODO: Calling is_subtype and is_same_types in semantic analysis is a bad idea
for (i, arg), tvar in zip(enumerate(t.args), info.defn.type_vars):
if tvar.values:
if isinstance(arg, TypeVarType):
arg_values = arg.values
if not arg_values:
self.fail('Type variable "{}" not valid as type '
'argument value for "{}"'.format(
arg.name, info.name()), t)
continue
else:
arg_values = [arg]
self.check_type_var_values(info, arg_values, tvar.name, tvar.values, i + 1, t)
# TODO: These hacks will be not necessary when this will be moved to later stage.
arg = self.resolve_type(arg)
bound = self.resolve_type(tvar.upper_bound)
if not is_subtype(arg, bound):
self.fail('Type argument "{}" of "{}" must be '
'a subtype of "{}"'.format(
arg, info.name(), bound), t)
for arg in t.args:
arg.accept(self)
if info.is_newtype:
for base in info.bases:
base.accept(self)