• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python subtypes.is_equivalent函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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)
开发者ID:JdeH,项目名称:Transcrypt,代码行数:28,代码来源:checkmember.py


示例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))
开发者ID:FaithBradley200,项目名称:mypy,代码行数:7,代码来源:join.py


示例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)
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:10,代码来源:checkmember.py


示例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)
开发者ID:philippmayrth,项目名称:mypy,代码行数:11,代码来源:checkmember.py


示例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)
开发者ID:greatmazinger,项目名称:mypy,代码行数:11,代码来源:meet.py


示例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)
开发者ID:chadrik,项目名称:mypy,代码行数:52,代码来源:join.py


示例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)
开发者ID:chadrik,项目名称:mypy,代码行数:14,代码来源:meet.py


示例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)
开发者ID:alexandrul,项目名称:mypy,代码行数:14,代码来源:meet.py


示例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)
开发者ID:greatmazinger,项目名称:mypy,代码行数:14,代码来源:join.py


示例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)
开发者ID:greatmazinger,项目名称:mypy,代码行数:18,代码来源:join.py


示例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)
开发者ID:python,项目名称:mypy,代码行数:20,代码来源:meet.py


示例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)
开发者ID:chadrik,项目名称:mypy,代码行数:21,代码来源:meet.py


示例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)
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:37,代码来源:checker.py


示例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)
开发者ID:SRiikonen,项目名称:mypy,代码行数:30,代码来源:checkmember.py


示例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)
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:6,代码来源:checker.py



注:本文中的mypy.subtypes.is_equivalent函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python subtypes.is_subtype函数代码示例发布时间:2022-05-27
下一篇:
Python trigger.make_trigger函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap