本文整理汇总了Python中mypy.subtypes.is_equivalent函数的典型用法代码示例。如果您正苦于以下问题:Python is_equivalent函数的具体用法?Python is_equivalent怎么用?Python is_equivalent使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_equivalent函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: 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_equivalent(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)
示例2: is_similar_callables
def is_similar_callables(t: CallableType, s: CallableType) -> bool:
"""Return True if t and s are equivalent and have identical numbers of
arguments, default arguments and varargs.
"""
return (len(t.arg_types) == len(s.arg_types) and t.min_args == s.min_args
and t.is_var_arg == s.is_var_arg and is_equivalent(t, s))
示例3: check_method_type
def check_method_type(functype, itype, context, msg):
for item in functype.items():
if not item.arg_types or item.arg_kinds[0] != ARG_POS:
# No positional first (self) argument.
msg.invalid_method_type(item, context)
else:
# Check that self argument has type 'any' or valid instance type.
selfarg = item.arg_types[0]
if not subtypes.is_equivalent(selfarg, itype):
msg.invalid_method_type(item, context)
示例4: check_method_type
def check_method_type(functype: FunctionLike, itype: Instance,
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)
else:
# Check that self argument has type 'Any' or valid instance type.
selfarg = item.arg_types[0]
if not subtypes.is_equivalent(selfarg, itype):
msg.invalid_method_type(item, context)
示例5: visit_callable_type
def visit_callable_type(self, t: CallableType) -> Type:
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = meet_similar_callables(t, self.s)
if isinstance(result.ret_type, UninhabitedType):
# Return a plain None or <uninhabited> instead of a weird function.
return self.default(self.s)
return result
else:
return self.default(self.s)
示例6: visit_overloaded
def visit_overloaded(self, t: Overloaded) -> Type:
# This is more complex than most other cases. Here are some
# examples that illustrate how this works.
#
# First let's define a concise notation:
# - Cn are callable types (for n in 1, 2, ...)
# - Ov(C1, C2, ...) is an overloaded type with items C1, C2, ...
# - Callable[[T, ...], S] is written as [T, ...] -> S.
#
# We want some basic properties to hold (assume Cn are all
# unrelated via Any-similarity):
#
# join(Ov(C1, C2), C1) == C1
# join(Ov(C1, C2), Ov(C1, C2)) == Ov(C1, C2)
# join(Ov(C1, C2), Ov(C1, C3)) == C1
# join(Ov(C2, C2), C3) == join of fallback types
#
# The presence of Any types makes things more interesting. The join is the
# most general type we can get with respect to Any:
#
# join(Ov([int] -> int, [str] -> str), [Any] -> str) == Any -> str
#
# We could use a simplification step that removes redundancies, but that's not
# implemented right now. Consider this example, where we get a redundancy:
#
# join(Ov([int, Any] -> Any, [str, Any] -> Any), [Any, int] -> Any) ==
# Ov([Any, int] -> Any, [Any, int] -> Any)
#
# TODO: Consider more cases of callable subtyping.
result = [] # type: List[CallableType]
s = self.s
if isinstance(s, FunctionLike):
# The interesting case where both types are function types.
for t_item in t.items():
for s_item in s.items():
if is_similar_callables(t_item, s_item):
if is_equivalent(t_item, s_item):
result.append(combine_similar_callables(t_item, s_item))
elif is_subtype(t_item, s_item):
result.append(s_item)
if result:
# TODO: Simplify redundancies from the result.
if len(result) == 1:
return result[0]
else:
return Overloaded(result)
return join_types(t.fallback, s.fallback)
elif isinstance(s, Instance) and s.type.is_protocol:
call = unpack_callback_protocol(s)
if call:
return join_types(t, call)
return join_types(t.fallback, s)
示例7: visit_callable_type
def visit_callable_type(self, t: CallableType) -> Type:
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = meet_similar_callables(t, self.s)
if isinstance(result.ret_type, UninhabitedType):
# Return a plain None or <uninhabited> instead of a weird function.
return self.default(self.s)
return result
elif isinstance(self.s, Instance) and self.s.type.is_protocol:
call = unpack_callback_protocol(self.s)
if call:
return meet_types(t, call)
return self.default(self.s)
示例8: visit_typeddict_type
def visit_typeddict_type(self, t: TypedDictType) -> Type:
if isinstance(self.s, TypedDictType):
for (_, l, r) in self.s.zip(t):
if not is_equivalent(l, r):
return self.default(self.s)
items = OrderedDict([
(item_name, s_item_type or t_item_type)
for (item_name, s_item_type, t_item_type) in self.s.zipall(t)
])
mapping_value_type = join_type_list(list(items.values()))
fallback = self.s.create_anonymous_fallback(value_type=mapping_value_type)
return TypedDictType(items, fallback)
else:
return self.default(self.s)
示例9: visit_callable_type
def visit_callable_type(self, t: CallableType) -> Type:
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = join_similar_callables(t, self.s)
if any(isinstance(tp, (NoneTyp, UninhabitedType)) for tp in result.arg_types):
# We don't want to return unusable Callable, attempt fallback instead.
return join_types(t.fallback, self.s)
return result
elif isinstance(self.s, Overloaded):
# Switch the order of arguments to that we'll get to visit_overloaded.
return join_types(t, self.s)
else:
return join_types(t.fallback, self.s)
示例10: visit_typeddict_type
def visit_typeddict_type(self, t: TypedDictType) -> Type:
if isinstance(self.s, TypedDictType):
items = OrderedDict([
(item_name, s_item_type)
for (item_name, s_item_type, t_item_type) in self.s.zip(t)
if (is_equivalent(s_item_type, t_item_type) and
(item_name in t.required_keys) == (item_name in self.s.required_keys))
])
mapping_value_type = join_type_list(list(items.values()))
fallback = self.s.create_anonymous_fallback(value_type=mapping_value_type)
# We need to filter by items.keys() since some required keys present in both t and
# self.s might be missing from the join if the types are incompatible.
required_keys = set(items.keys()) & t.required_keys & self.s.required_keys
return TypedDictType(items, required_keys, fallback)
elif isinstance(self.s, Instance):
return join_types(self.s, t.fallback)
else:
return self.default(self.s)
示例11: visit_callable_type
def visit_callable_type(self, t: CallableType) -> Type:
if isinstance(self.s, CallableType) and is_similar_callables(t, self.s):
if is_equivalent(t, self.s):
return combine_similar_callables(t, self.s)
result = meet_similar_callables(t, self.s)
if isinstance(result.ret_type, UninhabitedType):
# Return a plain None or <uninhabited> instead of a weird function.
return self.default(self.s)
return result
elif isinstance(self.s, TypeType) and t.is_type_obj() and not t.is_generic():
# In this case we are able to potentially produce a better meet.
res = meet_types(self.s.item, t.ret_type)
if not isinstance(res, (NoneType, UninhabitedType)):
return TypeType.make_normalized(res)
return self.default(self.s)
elif isinstance(self.s, Instance) and self.s.type.is_protocol:
call = unpack_callback_protocol(self.s)
if call:
return meet_types(t, call)
return self.default(self.s)
示例12: visit_typeddict_type
def visit_typeddict_type(self, t: TypedDictType) -> Type:
if isinstance(self.s, TypedDictType):
for (name, l, r) in self.s.zip(t):
if (not is_equivalent(l, r) or
(name in t.required_keys) != (name in self.s.required_keys)):
return self.default(self.s)
item_list = [] # type: List[Tuple[str, Type]]
for (item_name, s_item_type, t_item_type) in self.s.zipall(t):
if s_item_type is not None:
item_list.append((item_name, s_item_type))
else:
# at least one of s_item_type and t_item_type is not None
assert t_item_type is not None
item_list.append((item_name, t_item_type))
items = OrderedDict(item_list)
mapping_value_type = join_type_list(list(items.values()))
fallback = self.s.create_anonymous_fallback(value_type=mapping_value_type)
required_keys = t.required_keys | self.s.required_keys
return TypedDictType(items, required_keys, fallback)
else:
return self.default(self.s)
示例13: check_override
def check_override(self, override, original, name, supertype, node):
"""Check a method override with given signatures.
Arguments:
override: The signature of the overriding method.
original: The signature of the original supertype method.
name: The name of the subtype. This and the next argument are
only used for generating error messages.
supertype: The name of the supertype.
"""
if (isinstance(override, Overloaded) or
isinstance(original, Overloaded) or
len((override).arg_types) !=
len((original).arg_types) or
(override).min_args !=
(original).min_args):
if not is_subtype(override, original):
self.msg.signature_incompatible_with_supertype(
name, supertype, node)
return
else:
# Give more detailed messages for the common case of both
# signatures having the same number of arguments and no
# intersection types.
coverride = override
coriginal = original
for i in range(len(coverride.arg_types)):
if not is_equivalent(coriginal.arg_types[i],
coverride.arg_types[i]):
self.msg.argument_incompatible_with_supertype(
i + 1, name, supertype, node)
if not is_subtype(coverride.ret_type, coriginal.ret_type):
self.msg.return_type_incompatible_with_supertype(
name, supertype, node)
示例14: NotImplementedError
# Found a getter or a setter.
raise NotImplementedError()
# Could not find the member.
if is_super:
msg.undefined_in_superclass(name, node)
return Any()
else:
return msg.has_no_member(itype, name, node)
SymNode lookup_member_var_or_accessor(TypeInfo info, str name, bool is_lvalue):
"""Find the attribute/accessor node that refers to a member of a type."""
if is_lvalue:
return info.get_var_or_setter(name)
else:
return info.get_var_or_getter(name)
void check_method_type(FunctionLike functype, Instance itype, Context context,
MessageBuilder msg):
for item in functype.items():
if not item.arg_types or item.arg_kinds[0] != ARG_POS:
# No positional first (self) argument.
msg.invalid_method_type(item, context)
else:
# Check that self argument has type 'any' or valid instance type.
selfarg = item.arg_types[0]
if not subtypes.is_equivalent(selfarg, itype):
msg.invalid_method_type(item, context)
示例15: check_type_equivalency
def check_type_equivalency(self, t1, t2, node, msg=messages.INCOMPATIBLE_TYPES):
"""Generate an error if the types are not equivalent. The
dynamic type is equivalent with all types.
"""
if not is_equivalent(t1, t2):
self.fail(msg, node)