本文整理汇总了Python中mypy.types.Instance类的典型用法代码示例。如果您正苦于以下问题:Python Instance类的具体用法?Python Instance怎么用?Python Instance使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Instance类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: visit_instance
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)
开发者ID:sixolet,项目名称:mypy,代码行数:55,代码来源:typeanal.py
示例2: visit_instance
def visit_instance(self, inst: Instance) -> None:
# TODO: Combine Instances that are exactly the same?
type_ref = inst.type_ref
if type_ref is None:
return # We've already been here.
inst.type_ref = None
inst.type = lookup_qualified_typeinfo(self.modules, type_ref, self.quick_and_dirty)
# TODO: Is this needed or redundant?
# Also fix up the bases, just in case.
for base in inst.type.bases:
if base.type is NOT_READY:
base.accept(self)
for a in inst.args:
a.accept(self)
if inst.final_value is not None:
inst.final_value.accept(self)
开发者ID:mananpal1997,项目名称:mypy,代码行数:16,代码来源:fixup.py
示例3: visit_instance
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]
elif info.defn.type_vars:
# Check type argument values.
for arg, TypeVar in zip(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, 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)
开发者ID:matthiaskramm,项目名称:mypy,代码行数:47,代码来源:typeanal.py
示例4: visit_instance
def visit_instance(self, inst: Instance) -> None:
# TODO: Combine Instances that are exactly the same?
type_ref = inst.type_ref
if type_ref is None:
return # We've already been here.
del inst.type_ref
node = lookup_qualified(self.modules, type_ref, self.quick_and_dirty)
if isinstance(node, TypeInfo):
inst.type = node
# TODO: Is this needed or redundant?
# Also fix up the bases, just in case.
for base in inst.type.bases:
if base.type is NOT_READY:
base.accept(self)
else:
# Looks like a missing TypeInfo in quick mode, put something there
assert self.quick_and_dirty, "Should never get here in normal mode"
inst.type = stale_info(self.modules)
for a in inst.args:
a.accept(self)
开发者ID:sixolet,项目名称:mypy,代码行数:20,代码来源:fixup.py
示例5: visit_instance
def visit_instance(self, inst: Instance) -> None:
# TODO: Combine Instances that are exactly the same?
type_ref = inst.type_ref
if type_ref is None:
return # We've already been here.
del inst.type_ref
node = lookup_qualified(self.modules, type_ref)
if isinstance(node, TypeInfo):
inst.type = node
# TODO: Is this needed or redundant?
# Also fix up the bases, just in case.
for base in inst.type.bases:
if base.type is None:
base.accept(self)
for a in inst.args:
a.accept(self)
开发者ID:cocoatomo,项目名称:mypy,代码行数:16,代码来源:fixup.py
示例6: visit_instance
def visit_instance(self, t: Instance) -> None:
info = t.type
if len(t.args) != len(info.type_vars):
if len(t.args) == 0:
# Implicit 'Any' type arguments.
# TODO remove <Type> below
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)
for arg in t.args:
arg.accept(self)
开发者ID:bogdan-kulynych,项目名称:mypy,代码行数:22,代码来源:typeanal.py
示例7: visit_instance
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)
elif info.defn.type_vars:
# Check type argument values.
for arg, TypeVar in zip(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, t)
for arg in t.args:
arg.accept(self)
开发者ID:o11c,项目名称:mypy,代码行数:37,代码来源:typeanal.py
示例8: visit_instance
def visit_instance(self, typ: Instance) -> None:
typ.type = self.fixup(typ.type)
for arg in typ.args:
arg.accept(self)
if typ.last_known_value:
typ.last_known_value.accept(self)
开发者ID:python,项目名称:mypy,代码行数:6,代码来源:astmerge.py
示例9: visit_instance
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)
开发者ID:greatmazinger,项目名称:mypy,代码行数:67,代码来源:typeanal.py
示例10: visit_unbound_type
#.........这里部分代码省略.........
elif fullname == 'typing.ClassVar':
if self.nesting_level > 0:
self.fail('Invalid type: ClassVar nested inside other type', t)
if len(t.args) == 0:
return AnyType(TypeOfAny.from_omitted_generics, line=t.line, column=t.column)
if len(t.args) != 1:
self.fail('ClassVar[...] must have at most one type argument', t)
return AnyType(TypeOfAny.from_error)
item = self.anal_type(t.args[0])
if isinstance(item, TypeVarType) or get_type_vars(item):
self.fail('Invalid type: ClassVar cannot be generic', t)
return AnyType(TypeOfAny.from_error)
return item
elif fullname in ('mypy_extensions.NoReturn', 'typing.NoReturn'):
return UninhabitedType(is_noreturn=True)
elif sym.kind == TYPE_ALIAS:
override = sym.type_override
all_vars = sym.alias_tvars
assert override is not None
an_args = self.anal_array(t.args)
if all_vars is not None:
exp_len = len(all_vars)
else:
exp_len = 0
act_len = len(an_args)
if exp_len > 0 and act_len == 0:
# Interpret bare Alias same as normal generic, i.e., Alias[Any, Any, ...]
assert all_vars is not None
return set_any_tvars(override, all_vars, t.line, t.column)
if exp_len == 0 and act_len == 0:
return override
if act_len != exp_len:
self.fail('Bad number of arguments for type alias, expected: %s, given: %s'
% (exp_len, act_len), t)
return set_any_tvars(override, all_vars or [],
t.line, t.column, implicit=False)
assert all_vars is not None
return replace_alias_tvars(override, all_vars, an_args, t.line, t.column)
elif not isinstance(sym.node, TypeInfo):
name = sym.fullname
if name is None:
name = sym.node.name()
if isinstance(sym.node, Var) and isinstance(sym.node.type, AnyType):
# Something with an Any type -- make it an alias for Any in a type
# context. This is slightly problematic as it allows using the type 'Any'
# as a base class -- however, this will fail soon at runtime so the problem
# is pretty minor.
return AnyType(TypeOfAny.from_unimported_type)
# Allow unbound type variables when defining an alias
if not (self.aliasing and sym.kind == TVAR and
(not self.tvar_scope or self.tvar_scope.get_binding(sym) is None)):
if (not self.third_pass and not self.in_dynamic_func and
not (isinstance(sym.node, (FuncDef, Decorator)) or
isinstance(sym.node, Var) and sym.node.is_ready) and
not (sym.kind == TVAR and tvar_def is None)):
if t.args and not self.global_scope:
self.fail('Unsupported forward reference to "{}"'.format(t.name), t)
return AnyType(TypeOfAny.from_error)
return ForwardRef(t)
self.fail('Invalid type "{}"'.format(name), t)
if self.third_pass and sym.kind == TVAR:
self.note_func("Forward references to type variables are prohibited", t)
return t
info = sym.node # type: TypeInfo
if len(t.args) > 0 and info.fullname() == 'builtins.tuple':
fallback = Instance(info, [AnyType(TypeOfAny.special_form)], t.line)
return TupleType(self.anal_array(t.args), fallback, t.line)
else:
# Analyze arguments and construct Instance type. The
# number of type arguments and their values are
# checked only later, since we do not always know the
# valid count at this point. Thus we may construct an
# Instance with an invalid number of type arguments.
instance = Instance(info, self.anal_array(t.args), t.line, t.column)
instance.from_generic_builtin = sym.normalized
tup = info.tuple_type
if tup is not None:
# The class has a Tuple[...] base class so it will be
# represented as a tuple type.
if t.args:
self.fail('Generic tuple types not supported', t)
return AnyType(TypeOfAny.from_error)
return tup.copy_modified(items=self.anal_array(tup.items),
fallback=instance)
td = info.typeddict_type
if td is not None:
# The class has a TypedDict[...] base class so it will be
# represented as a typeddict type.
if t.args:
self.fail('Generic TypedDict types not supported', t)
return AnyType(TypeOfAny.from_error)
# Create a named TypedDictType
return td.copy_modified(item_types=self.anal_array(list(td.items.values())),
fallback=instance)
return instance
else:
if self.third_pass:
self.fail('Invalid type "{}"'.format(t.name), t)
return AnyType(TypeOfAny.from_error)
return AnyType(TypeOfAny.special_form)
开发者ID:greatmazinger,项目名称:mypy,代码行数:101,代码来源:typeanal.py
示例11: visit_instance
def visit_instance(self, typ: Instance) -> None:
typ.type = self.fixup(typ.type)
for arg in typ.args:
arg.accept(self)
开发者ID:greatmazinger,项目名称:mypy,代码行数:4,代码来源:astmerge.py
注:本文中的mypy.types.Instance类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论