当前位置: 首页>>代码示例>>Python>>正文


Python CallableType.is_type_obj方法代码示例

本文整理汇总了Python中mypy.types.CallableType.is_type_obj方法的典型用法代码示例。如果您正苦于以下问题:Python CallableType.is_type_obj方法的具体用法?Python CallableType.is_type_obj怎么用?Python CallableType.is_type_obj使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mypy.types.CallableType的用法示例。


在下文中一共展示了CallableType.is_type_obj方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: is_callable_subtype

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
def is_callable_subtype(left: CallableType, right: CallableType, ignore_return: bool = False) -> bool:
    """Is left a subtype of right?"""
    # TODO: Support named arguments, **args, etc.
    # Non-type cannot be a subtype of type.
    if right.is_type_obj() and not left.is_type_obj():
        return False
    if right.variables:
        # Subtyping is not currently supported for generic function as the supertype.
        return False
    if left.variables:
        # Apply generic type variables away in left via type inference.
        left = unify_generic_callable(left, right)
        if left is None:
            return False

    # Check return types.
    if not ignore_return and not is_subtype(left.ret_type, right.ret_type):
        return False

    # Check argument types.
    if left.min_args > right.min_args:
        return False
    if left.is_var_arg:
        return is_var_arg_callable_subtype_helper(left, right)
    if right.is_var_arg:
        return False
    if len(left.arg_types) < len(right.arg_types):
        return False
    for i in range(len(right.arg_types)):
        if not is_subtype(right.arg_types[i], left.arg_types[i]):
            return False
    return True
开发者ID:JamesTFarrington,项目名称:mypy,代码行数:34,代码来源:subtypes.py

示例2: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, t: CallableType) -> Type:
     if isinstance(self.s, CallableType) and is_similar_callables(
             t, cast(CallableType, self.s)):
         return combine_similar_callables(t, cast(CallableType, self.s))
     elif t.is_type_obj() and is_subtype(self.s, t.fallback):
         return t.fallback
     elif (t.is_type_obj() and isinstance(self.s, Instance) and
           cast(Instance, self.s).type == t.fallback):
         return t.fallback
     else:
         return self.default(self.s)
开发者ID:JamesTFarrington,项目名称:mypy,代码行数:13,代码来源:join.py

示例3: is_callable_subtype

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
def is_callable_subtype(left: CallableType, right: CallableType,
                        ignore_return: bool = False) -> bool:
    """Is left a subtype of right?"""
    # TODO: Support named arguments, **args, etc.
    # Non-type cannot be a subtype of type.
    if right.is_type_obj() and not left.is_type_obj():
        return False

    # A callable L is a subtype of a generic callable R if L is a
    # subtype of every type obtained from R by substituting types for
    # the variables of R. We can check this by simply leaving the
    # generic variables of R as type variables, effectively varying
    # over all possible values.

    # It's okay even if these variables share ids with generic
    # type variables of L, because generating and solving
    # constraints for the variables of L to make L a subtype of R
    # (below) treats type variables on the two sides as independent.

    if left.variables:
        # Apply generic type variables away in left via type inference.
        left = unify_generic_callable(left, right, ignore_return=ignore_return)
        if left is None:
            return False

    # Check return types.
    if not ignore_return and not is_subtype(left.ret_type, right.ret_type):
        return False

    if right.is_ellipsis_args:
        return True

    # Check argument types.
    if left.min_args > right.min_args:
        return False
    if left.is_var_arg:
        return is_var_arg_callable_subtype_helper(left, right)
    if right.is_var_arg:
        return False
    if len(left.arg_types) < len(right.arg_types):
        return False
    for i in range(len(right.arg_types)):
        if not is_subtype(right.arg_types[i], left.arg_types[i]):
            return False
    return True
开发者ID:bdarnell,项目名称:mypy,代码行数:47,代码来源:subtypes.py

示例4: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, typ: CallableType) -> SnapshotItem:
     # FIX generics
     return ('CallableType',
             snapshot_types(typ.arg_types),
             snapshot_type(typ.ret_type),
             tuple(typ.arg_names),
             tuple(typ.arg_kinds),
             typ.is_type_obj(),
             typ.is_ellipsis_args)
开发者ID:greatmazinger,项目名称:mypy,代码行数:11,代码来源:astdiff.py

示例5: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, left: CallableType) -> bool:
     # FIX generics
     if isinstance(self.right, CallableType):
         cright = cast(CallableType, self.right)
         return (is_same_type(left.ret_type, cright.ret_type) and
                 is_same_types(left.arg_types, cright.arg_types) and
                 left.arg_names == cright.arg_names and
                 left.arg_kinds == cright.arg_kinds and
                 left.is_type_obj() == cright.is_type_obj())
     else:
         return False
开发者ID:JamesTFarrington,项目名称:mypy,代码行数:13,代码来源:sametypes.py

示例6: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, left: CallableType) -> bool:
     right = self.right
     if isinstance(right, CallableType):
         return is_callable_subtype(left, right)
     elif isinstance(right, Overloaded):
         return all(is_subtype(left, item) for item in right.items())
     elif is_named_instance(right, 'builtins.object'):
         return True
     elif (is_named_instance(right, 'builtins.type') and
           left.is_type_obj()):
         return True
     else:
         return False
开发者ID:noisecapella,项目名称:mypy,代码行数:15,代码来源:subtypes.py

示例7: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, left: CallableType) -> bool:
     right = self.right
     if isinstance(right, CallableType):
         return is_callable_subtype(left, right)
     elif isinstance(right, Overloaded):
         return all(is_subtype(left, item, self.check_type_parameter)
                    for item in right.items())
     elif isinstance(right, Instance):
         return is_subtype(left.fallback, right)
     elif isinstance(right, TypeType):
         # This is unsound, we don't check the __init__ signature.
         return left.is_type_obj() and is_subtype(left.ret_type, right.item)
     else:
         return False
开发者ID:edreamleo,项目名称:mypy,代码行数:16,代码来源:subtypes.py

示例8: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 def visit_callable_type(self, left: CallableType) -> bool:
     right = self.right
     if isinstance(right, CallableType):
         return is_callable_subtype(
             left, right,
             ignore_pos_arg_names=False,
             use_proper_subtype=True)
     elif isinstance(right, Overloaded):
         return all(is_proper_subtype(left, item)
                    for item in right.items())
     elif isinstance(right, Instance):
         return is_proper_subtype(left.fallback, right)
     elif isinstance(right, TypeType):
         # This is unsound, we don't check the __init__ signature.
         return left.is_type_obj() and is_proper_subtype(left.ret_type, right.item)
     return False
开发者ID:greatmazinger,项目名称:mypy,代码行数:18,代码来源:subtypes.py

示例9: visit_callable_type

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
 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,代码行数:22,代码来源:meet.py

示例10: is_callable_subtype

# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import is_type_obj [as 别名]
def is_callable_subtype(left: CallableType, right: CallableType,
                        ignore_return: bool = False,
                        ignore_pos_arg_names: bool = False) -> bool:
    """Is left a subtype of right?"""

    # If either function is implicitly typed, ignore positional arg names too
    if left.implicit or right.implicit:
        ignore_pos_arg_names = True

    # Non-type cannot be a subtype of type.
    if right.is_type_obj() and not left.is_type_obj():
        return False

    # A callable L is a subtype of a generic callable R if L is a
    # subtype of every type obtained from R by substituting types for
    # the variables of R. We can check this by simply leaving the
    # generic variables of R as type variables, effectively varying
    # over all possible values.

    # It's okay even if these variables share ids with generic
    # type variables of L, because generating and solving
    # constraints for the variables of L to make L a subtype of R
    # (below) treats type variables on the two sides as independent.

    if left.variables:
        # Apply generic type variables away in left via type inference.
        left = unify_generic_callable(left, right, ignore_return=ignore_return)
        if left is None:
            return False

    # Check return types.
    if not ignore_return and not is_subtype(left.ret_type, right.ret_type):
        return False

    if right.is_ellipsis_args:
        return True

    right_star_type = None   # type: Optional[Type]
    right_star2_type = None  # type: Optional[Type]

    # Match up corresponding arguments and check them for compatibility. In
    # every pair (argL, argR) of corresponding arguments from L and R, argL must
    # be "more general" than argR if L is to be a subtype of R.

    # Arguments are corresponding if they either share a name, share a position,
    # or both. If L's corresponding argument is ambiguous, L is not a subtype of
    # R.

    # If left has one corresponding argument by name and another by position,
    # consider them to be one "merged" argument (and not ambiguous) if they're
    # both optional, they're name-only and position-only respectively, and they
    # have the same type.  This rule allows functions with (*args, **kwargs) to
    # properly stand in for the full domain of formal arguments that they're
    # used for in practice.

    # Every argument in R must have a corresponding argument in L, and every
    # required argument in L must have a corresponding argument in R.
    done_with_positional = False
    for i in range(len(right.arg_types)):
        right_kind = right.arg_kinds[i]
        if right_kind in (ARG_STAR, ARG_STAR2, ARG_NAMED, ARG_NAMED_OPT):
            done_with_positional = True
        right_required = right_kind in (ARG_POS, ARG_NAMED)
        right_pos = None if done_with_positional else i

        right_arg = FormalArgument(
            right.arg_names[i],
            right_pos,
            right.arg_types[i],
            right_required)

        if right_kind == ARG_STAR:
            right_star_type = right_arg.typ
            # Right has an infinite series of optional positional arguments
            # here.  Get all further positional arguments of left, and make sure
            # they're more general than their corresponding member in this
            # series.  Also make sure left has its own inifite series of
            # optional positional arguments.
            if not left.is_var_arg:
                return False
            j = i
            while j < len(left.arg_kinds) and left.arg_kinds[j] in (ARG_POS, ARG_OPT):
                left_by_position = left.argument_by_position(j)
                assert left_by_position is not None
                # This fetches the synthetic argument that's from the *args
                right_by_position = right.argument_by_position(j)
                assert right_by_position is not None
                if not are_args_compatible(left_by_position, right_by_position,
                                           ignore_pos_arg_names):
                    return False
                j += 1
            continue

        if right_kind == ARG_STAR2:
            right_star2_type = right_arg.typ
            # Right has an infinite set of optional named arguments here.  Get
            # all further named arguments of left and make sure they're more
            # general than their corresponding member in this set.  Also make
            # sure left has its own infinite set of optional named arguments.
            if not left.is_kw_arg:
#.........这里部分代码省略.........
开发者ID:alexandrul,项目名称:mypy,代码行数:103,代码来源:subtypes.py


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