本文整理汇总了Python中mypy.subtypes.is_subtype函数的典型用法代码示例。如果您正苦于以下问题:Python is_subtype函数的具体用法?Python is_subtype怎么用?Python is_subtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_subtype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: join_instances
def join_instances(t: Instance, s: Instance, allow_interfaces: bool,
basic: BasicTypes) -> Type:
"""Calculate the join of two instance types.
If allow_interfaces is True, also consider interface-type results for
non-interface types.
Return ErrorType if the result is ambiguous.
"""
if t.type == s.type:
# Simplest case: join two types with the same base type (but
# potentially different arguments).
if is_subtype(t, s):
# Compatible; combine type arguments.
args = [] # type: List[Type]
for i in range(len(t.args)):
args.append(join_types(t.args[i], s.args[i], basic))
return Instance(t.type, args)
else:
# Incompatible; return trivial result object.
return basic.object
elif t.type.bases and is_subtype(t, s):
return join_instances_via_supertype(t, s, allow_interfaces, basic)
elif s.type.bases:
return join_instances_via_supertype(s, t, allow_interfaces, basic)
elif allow_interfaces:
return join_instances_as_interface(t, s, basic)
else:
return basic.object
示例2: join_instances
def join_instances(t: Instance, s: Instance) -> Type:
"""Calculate the join of two instance types.
If allow_interfaces is True, also consider interface-type results for
non-interface types.
Return ErrorType if the result is ambiguous.
"""
if t.type == s.type:
# Simplest case: join two types with the same base type (but
# potentially different arguments).
if is_subtype(t, s) or is_subtype(s, t):
# Compatible; combine type arguments.
args = [] # type: List[Type]
for i in range(len(t.args)):
args.append(join_types(t.args[i], s.args[i]))
return Instance(t.type, args)
else:
# Incompatible; return trivial result object.
return object_from_instance(t)
elif t.type.bases and is_subtype_ignoring_tvars(t, s):
return join_instances_via_supertype(t, s)
else:
# Now t is not a subtype of s, and t != s. Now s could be a subtype
# of t; alternatively, we need to find a common supertype. This works
# in of the both cases.
return join_instances_via_supertype(s, t)
示例3: test_is_proper_subtype_and_subtype_literal_types
def test_is_proper_subtype_and_subtype_literal_types(self) -> None:
fx = self.fx
lit1 = LiteralType(1, fx.a)
lit2 = LiteralType("foo", fx.d)
lit3 = LiteralType("bar", fx.d)
assert_true(is_proper_subtype(lit1, fx.a))
assert_false(is_proper_subtype(lit1, fx.d))
assert_false(is_proper_subtype(fx.a, lit1))
assert_true(is_proper_subtype(fx.uninhabited, lit1))
assert_false(is_proper_subtype(lit1, fx.uninhabited))
assert_true(is_proper_subtype(lit1, lit1))
assert_false(is_proper_subtype(lit1, lit2))
assert_false(is_proper_subtype(lit2, lit3))
assert_true(is_subtype(lit1, fx.a))
assert_false(is_subtype(lit1, fx.d))
assert_false(is_subtype(fx.a, lit1))
assert_true(is_subtype(fx.uninhabited, lit1))
assert_false(is_subtype(lit1, fx.uninhabited))
assert_true(is_subtype(lit1, lit1))
assert_false(is_subtype(lit1, lit2))
assert_false(is_subtype(lit2, lit3))
assert_false(is_proper_subtype(lit1, fx.anyt))
assert_false(is_proper_subtype(fx.anyt, lit1))
assert_true(is_subtype(lit1, fx.anyt))
assert_true(is_subtype(fx.anyt, lit1))
示例4: visit_instance
def visit_instance(self, t: Instance) -> Type:
if isinstance(self.s, Instance):
si = self.s
if t.type == si.type:
if is_subtype(t, self.s) or is_subtype(self.s, t):
# Combine type arguments. We could have used join below
# equivalently.
args = [] # type: List[Type]
for i in range(len(t.args)):
args.append(self.meet(t.args[i], si.args[i]))
return Instance(t.type, args)
else:
if experiments.STRICT_OPTIONAL:
return UninhabitedType()
else:
return NoneTyp()
else:
if is_subtype(t, self.s):
return t
elif is_subtype(self.s, t):
# See also above comment.
return self.s
else:
if experiments.STRICT_OPTIONAL:
return UninhabitedType()
else:
return NoneTyp()
elif isinstance(self.s, TypeType):
return meet_types(t, self.s)
else:
return self.default(self.s)
示例5: array_constructor_callback
def array_constructor_callback(ctx: 'mypy.plugin.FunctionContext') -> Type:
"""Callback to provide an accurate signature for the ctypes.Array constructor."""
# Extract the element type from the constructor's return type, i. e. the type of the array
# being constructed.
et = _get_array_element_type(ctx.default_return_type)
if et is not None:
allowed = _autoconvertible_to_cdata(et, ctx.api)
assert len(ctx.arg_types) == 1, \
"The stub of the ctypes.Array constructor should have a single vararg parameter"
for arg_num, (arg_kind, arg_type) in enumerate(zip(ctx.arg_kinds[0], ctx.arg_types[0]), 1):
if arg_kind == nodes.ARG_POS and not is_subtype(arg_type, allowed):
ctx.api.msg.fail(
'Array constructor argument {} of type "{}"'
' is not convertible to the array element type "{}"'
.format(arg_num, arg_type, et),
ctx.context)
elif arg_kind == nodes.ARG_STAR:
ty = ctx.api.named_generic_type("typing.Iterable", [allowed])
if not is_subtype(arg_type, ty):
ctx.api.msg.fail(
'Array constructor argument {} of type "{}"'
' is not convertible to the array element type "Iterable[{}]"'
.format(arg_num, arg_type, et),
ctx.context)
return ctx.default_return_type
示例6: join_simple
def join_simple(declaration: Type, s: Type, t: Type) -> Type:
"""Return a simple least upper bound given the declared type."""
if isinstance(s, AnyType):
return s
if isinstance(s, NoneTyp) and not isinstance(t, Void):
return t
if isinstance(s, ErasedType):
return t
if is_subtype(s, t):
return t
if is_subtype(t, s):
return s
if isinstance(declaration, UnionType):
return UnionType.make_simplified_union([s, t])
value = t.accept(TypeJoinVisitor(s))
if value is None:
# XXX this code path probably should be avoided.
# It seems to happen when a line (x = y) is a type error, and
# it's not clear that assuming that x is arbitrary afterward
# is a good idea.
return declaration
if declaration is None or is_subtype(value, declaration):
return value
return declaration
示例7: meet_simple_away
def meet_simple_away(s: Type, t: Type) -> Type:
if isinstance(s, UnionType):
return UnionType.make_simplified_union([x for x in s.items
if not is_subtype(x, t)])
elif not isinstance(s, AnyType) and is_subtype(s, t):
return Void()
else:
return s
示例8: assert_simple_join
def assert_simple_join(self, s: Type, t: Type, join: Type) -> None:
result = join_types(s, t)
actual = str(result)
expected = str(join)
assert_equal(actual, expected,
'join({}, {}) == {{}} ({{}} expected)'.format(s, t))
assert_true(is_subtype(s, result),
'{} not subtype of {}'.format(s, result))
assert_true(is_subtype(t, result),
'{} not subtype of {}'.format(t, result))
示例9: assert_simple_meet
def assert_simple_meet(self, s: Type, t: Type, meet: Type) -> None:
result = meet_types(s, t)
actual = str(result)
expected = str(meet)
assert_equal(actual, expected,
'meet({}, {}) == {{}} ({{}} expected)'.format(s, t))
assert_true(is_subtype(result, s),
'{} not subtype of {}'.format(result, s))
assert_true(is_subtype(result, t),
'{} not subtype of {}'.format(result, t))
示例10: assert_simple_meet
def assert_simple_meet(self, s, t, meet):
result = meet_types(s, t)
actual = str(result)
expected = str(meet)
assert_equal(actual, expected,
'meet({}, {}) == {{}} ({{}} expected)'.format(s, t))
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(result, s),
'{} not subtype of {}'.format(result, s))
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(result, t),
'{} not subtype of {}'.format(result, t))
示例11: assert_simple_join
def assert_simple_join(self, s, t, join):
result = join_types(s, t)
actual = str(result)
expected = str(join)
assert_equal(actual, expected,
'join({}, {}) == {{}} ({{}} expected)'.format(s, t))
if not isinstance(s, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(s, result),
'{} not subtype of {}'.format(s, result))
if not isinstance(t, ErrorType) and not isinstance(result, ErrorType):
assert_true(is_subtype(t, result),
'{} not subtype of {}'.format(t, result))
示例12: visit_literal_type
def visit_literal_type(self, t: LiteralType) -> Type:
if isinstance(self.s, LiteralType) and self.s == t:
return t
elif isinstance(self.s, Instance) and is_subtype(t.fallback, self.s):
return t
else:
return self.default(self.s)
示例13: check_exception_type
def check_exception_type(self, info, context):
t = Instance(info, [])
if is_subtype(t, self.named_type('builtins.BaseException')):
return t
else:
self.fail(messages.INVALID_EXCEPTION_TYPE, context)
return Any()
示例14: check_self_arg
def check_self_arg(functype: FunctionLike,
dispatched_arg_type: Type,
is_classmethod: bool,
context: Context, name: str,
msg: MessageBuilder) -> None:
"""For x.f where A.f: A1 -> T, check that meet(type(x), A) <: A1 for each overload.
dispatched_arg_type is meet(B, A) in the following example
def g(x: B): x.f
class A:
f: Callable[[A1], None]
"""
# TODO: this is too strict. We can return filtered overloads for matching definitions
for item in functype.items():
if not item.arg_types or item.arg_kinds[0] not in (ARG_POS, ARG_STAR):
# No positional first (self) argument (*args is okay).
msg.no_formal_self(name, item, context)
else:
selfarg = item.arg_types[0]
if is_classmethod:
dispatched_arg_type = TypeType.make_normalized(dispatched_arg_type)
if not subtypes.is_subtype(dispatched_arg_type, erase_to_bound(selfarg)):
msg.incompatible_self_argument(name, dispatched_arg_type, item,
is_classmethod, context)
示例15: check_method_type
def check_method_type(functype: FunctionLike, itype: Instance, is_classmethod: bool,
context: Context, msg: MessageBuilder) -> None:
for item in functype.items():
if not item.arg_types or item.arg_kinds[0] not in (ARG_POS, ARG_STAR):
# No positional first (self) argument (*args is okay).
msg.invalid_method_type(item, context)
elif not is_classmethod:
# Check that self argument has type 'Any' or valid instance type.
selfarg = item.arg_types[0]
# If this is a method of a tuple class, correct for the fact that
# we passed to typ.fallback in analyze_member_access. See #1432.
if isinstance(selfarg, TupleType):
selfarg = selfarg.fallback
if not subtypes.is_subtype(selfarg, itype):
msg.invalid_method_type(item, context)
else:
# Check that cls argument has type 'Any' or valid class type.
# (This is sufficient for the current treatment of @classmethod,
# but probably needs to be revisited when we implement Type[C]
# or advanced variants of it like Type[<args>, C].)
clsarg = item.arg_types[0]
if isinstance(clsarg, CallableType) and clsarg.is_type_obj():
if not subtypes.is_equivalent(clsarg.ret_type, itype):
msg.invalid_class_method_type(item, context)
else:
if not subtypes.is_equivalent(clsarg, AnyType()):
msg.invalid_class_method_type(item, context)