本文整理汇总了Python中mypy.types.Instance.from_generic_builtin方法的典型用法代码示例。如果您正苦于以下问题:Python Instance.from_generic_builtin方法的具体用法?Python Instance.from_generic_builtin怎么用?Python Instance.from_generic_builtin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.types.Instance
的用法示例。
在下文中一共展示了Instance.from_generic_builtin方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_unbound_type
# 需要导入模块: from mypy.types import Instance [as 别名]
# 或者: from mypy.types.Instance import from_generic_builtin [as 别名]
def visit_unbound_type(self, t: UnboundType) -> Type:
if t.optional:
t.optional = False
# We don't need to worry about double-wrapping Optionals or
# wrapping Anys: Union simplification will take care of that.
return make_optional_type(self.visit_unbound_type(t))
sym = self.lookup(t.name, t, suppress_errors=self.third_pass) # type: ignore
if sym is not None:
if sym.node is None:
# UNBOUND_IMPORTED can happen if an unknown name was imported.
if sym.kind != UNBOUND_IMPORTED:
self.fail('Internal error (node is None, kind={})'.format(sym.kind), t)
return AnyType(TypeOfAny.special_form)
fullname = sym.node.fullname()
hook = self.plugin.get_type_analyze_hook(fullname)
if hook:
return hook(AnalyzeTypeContext(t, t, self))
if (fullname in nongen_builtins and t.args and
not sym.normalized and not self.allow_unnormalized):
self.fail(no_subscript_builtin_alias(fullname), t)
if self.tvar_scope:
tvar_def = self.tvar_scope.get_binding(sym)
else:
tvar_def = None
if self.warn_bound_tvar and sym.kind == TVAR and tvar_def is not None:
self.fail('Can\'t use bound type variable "{}"'
' to define generic alias'.format(t.name), t)
return AnyType(TypeOfAny.from_error)
elif sym.kind == TVAR and tvar_def is not None:
if len(t.args) > 0:
self.fail('Type variable "{}" used with arguments'.format(
t.name), t)
return TypeVarType(tvar_def, t.line)
elif fullname == 'builtins.None':
return NoneTyp()
elif fullname == 'typing.Any' or fullname == 'builtins.Any':
return AnyType(TypeOfAny.explicit)
elif fullname == 'typing.Tuple':
if len(t.args) == 0 and not t.empty_tuple_index:
# Bare 'Tuple' is same as 'tuple'
if self.options.disallow_any_generics and not self.is_typeshed_stub:
self.fail(messages.BARE_GENERIC, t)
typ = self.named_type('builtins.tuple', line=t.line, column=t.column)
typ.from_generic_builtin = True
return typ
if len(t.args) == 2 and isinstance(t.args[1], EllipsisType):
# Tuple[T, ...] (uniform, variable-length tuple)
instance = self.named_type('builtins.tuple', [self.anal_type(t.args[0])])
instance.line = t.line
return instance
return self.tuple_type(self.anal_array(t.args))
elif fullname == 'typing.Union':
items = self.anal_array(t.args)
return UnionType.make_union(items)
elif fullname == 'typing.Optional':
if len(t.args) != 1:
self.fail('Optional[...] must have exactly one type argument', t)
return AnyType(TypeOfAny.from_error)
item = self.anal_type(t.args[0])
return make_optional_type(item)
elif fullname == 'typing.Callable':
return self.analyze_callable_type(t)
elif fullname == 'typing.Type':
if len(t.args) == 0:
any_type = AnyType(TypeOfAny.from_omitted_generics,
line=t.line, column=t.column)
return TypeType(any_type, line=t.line, column=t.column)
if len(t.args) != 1:
self.fail('Type[...] must have exactly one type argument', t)
item = self.anal_type(t.args[0])
return TypeType.make_normalized(item, line=t.line)
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)
#.........这里部分代码省略.........