本文整理汇总了Python中mypy.types.CallableType.copy_modified方法的典型用法代码示例。如果您正苦于以下问题:Python CallableType.copy_modified方法的具体用法?Python CallableType.copy_modified怎么用?Python CallableType.copy_modified使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mypy.types.CallableType
的用法示例。
在下文中一共展示了CallableType.copy_modified方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_callable_type
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def visit_callable_type(self, t: CallableType) -> Type:
return t.copy_modified(
arg_types=self.anal_array(t.arg_types),
ret_type=t.ret_type.accept(self),
fallback=t.fallback or self.builtin_type("builtins.function"),
variables=self.anal_var_defs(t.variables),
)
示例2: get_guesses
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def get_guesses(self, is_method: bool, base: CallableType, defaults: List[Optional[Type]],
callsites: List[Callsite]) -> List[CallableType]:
"""Compute a list of guesses for a function's type.
This focuses just on the argument types, and doesn't change the provided return type.
"""
options = self.get_args(is_method, base, defaults, callsites)
return [base.copy_modified(arg_types=list(x)) for x in itertools.product(*options)]
示例3: apply_generic_arguments
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def apply_generic_arguments(callable: CallableType, types: List[Type],
msg: MessageBuilder, context: Context) -> Type:
"""Apply generic type arguments to a callable type.
For example, applying [int] to 'def [T] (T) -> T' results in
'def (int) -> int'.
Note that each type can be None; in this case, it will not be applied.
"""
tvars = callable.variables
if len(tvars) != len(types):
msg.incompatible_type_application(len(tvars), len(types), context)
return AnyType()
# Check that inferred type variable values are compatible with allowed
# values and bounds. Also, promote subtype values to allowed values.
types = types[:]
for i, type in enumerate(types):
values = callable.variables[i].values
if values and type:
if isinstance(type, AnyType):
continue
if isinstance(type, TypeVarType) and type.values:
# Allow substituting T1 for T if every allowed value of T1
# is also a legal value of T.
if all(any(is_same_type(v, v1) for v in values)
for v1 in type.values):
continue
for value in values:
if mypy.subtypes.is_subtype(type, value):
types[i] = value
break
else:
msg.incompatible_typevar_value(callable, i + 1, type, context)
upper_bound = callable.variables[i].upper_bound
if type and not mypy.subtypes.satisfies_upper_bound(type, upper_bound):
msg.incompatible_typevar_value(callable, i + 1, type, context)
# Create a map from type variable id to target type.
id_to_type = {} # type: Dict[TypeVarId, Type]
for i, tv in enumerate(tvars):
if types[i]:
id_to_type[tv.id] = types[i]
# Apply arguments to argument types.
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]
# The callable may retain some type vars if only some were applied.
remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]
return callable.copy_modified(
arg_types=arg_types,
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
)
示例4: class_callable
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
special_sig: Optional[str]) -> CallableType:
"""Create a type object type based on the signature of __init__."""
variables = [] # type: List[TypeVarDef]
variables.extend(info.defn.type_vars)
variables.extend(init_type.variables)
callable_type = init_type.copy_modified(
ret_type=fill_typevars(info), fallback=type_type, name=None, variables=variables,
special_sig=special_sig)
c = callable_type.with_name(info.name())
return c
示例5: visit_callable_type
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def visit_callable_type(self, t: CallableType) -> Type:
if self.check_recursion(t):
return AnyType(TypeOfAny.from_error)
arg_types = [tp.accept(self) for tp in t.arg_types]
ret_type = t.ret_type.accept(self)
variables = t.variables.copy()
for v in variables:
if v.upper_bound:
v.upper_bound = v.upper_bound.accept(self)
if v.values:
v.values = [val.accept(self) for val in v.values]
return t.copy_modified(arg_types=arg_types, ret_type=ret_type, variables=variables)
示例6: visit_callable_type
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def visit_callable_type(self, t: CallableType, nested: bool = True) -> Type:
# Every Callable can bind its own type variables, if they're not in the outer scope
with self.tvar_scope_frame():
if self.aliasing:
variables = t.variables
else:
variables = self.bind_function_type_variables(t, t)
ret = t.copy_modified(arg_types=self.anal_array(t.arg_types, nested=nested),
ret_type=self.anal_type(t.ret_type, nested=nested),
fallback=t.fallback or self.named_type('builtins.function'),
variables=self.anal_var_defs(variables))
return ret
示例7: apply_generic_arguments
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def apply_generic_arguments(callable: CallableType, types: List[Type],
msg: MessageBuilder, context: Context) -> Type:
"""Apply generic type arguments to a callable type.
For example, applying [int] to 'def [T] (T) -> T' results in
'def [-1:int] (int) -> int'. Here '[-1:int]' is an implicit bound type
variable.
Note that each type can be None; in this case, it will not be applied.
"""
tvars = callable.variables
if len(tvars) != len(types):
msg.incompatible_type_application(len(tvars), len(types), context)
return AnyType()
# Check that inferred type variable values are compatible with allowed
# values. Also, promote subtype values to allowed values.
types = types[:]
for i, type in enumerate(types):
values = callable.variables[i].values
if values and type:
if isinstance(type, AnyType):
continue
for value in values:
if mypy.subtypes.is_subtype(type, value):
types[i] = value
break
else:
msg.incompatible_typevar_value(callable, i + 1, type, context)
# Create a map from type variable id to target type.
id_to_type = {} # type: Dict[int, Type]
for i, tv in enumerate(tvars):
if types[i]:
id_to_type[tv.id] = types[i]
# Apply arguments to argument types.
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]
bound_vars = [(tv.id, id_to_type[tv.id])
for tv in tvars
if tv.id in id_to_type]
# The callable may retain some type vars if only some were applied.
remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]
return callable.copy_modified(
arg_types=arg_types,
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
bound_vars=callable.bound_vars + bound_vars,
)
示例8: class_callable
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance) -> CallableType:
"""Create a type object type based on the signature of __init__."""
variables = [] # type: List[TypeVarDef]
for i, tvar in enumerate(info.defn.type_vars):
variables.append(TypeVarDef(tvar.name, i + 1, tvar.values, tvar.upper_bound,
tvar.variance))
initvars = init_type.variables
variables.extend(initvars)
callable_type = init_type.copy_modified(
ret_type=self_type(info), fallback=type_type, name=None, variables=variables)
c = callable_type.with_name('"{}"'.format(info.name()))
return convert_class_tvars_to_func_tvars(c, len(initvars))
示例9: combine_similar_callables
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def combine_similar_callables(t: CallableType, s: CallableType) -> CallableType:
arg_types = [] # type: List[Type]
for i in range(len(t.arg_types)):
arg_types.append(join_types(t.arg_types[i], s.arg_types[i]))
# TODO kinds and argument names
# The fallback type can be either 'function' or 'type'. The result should have 'type' as
# fallback only if both operands have it as 'type'.
if t.fallback.type.fullname() != 'builtins.type':
fallback = t.fallback
else:
fallback = s.fallback
return t.copy_modified(arg_types=arg_types,
ret_type=join_types(t.ret_type, s.ret_type),
fallback=fallback,
name=None)
示例10: meet_similar_callables
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def meet_similar_callables(t: CallableType, s: CallableType) -> CallableType:
from mypy.join import join_types
arg_types = [] # type: List[Type]
for i in range(len(t.arg_types)):
arg_types.append(join_types(t.arg_types[i], s.arg_types[i]))
# TODO in combine_similar_callables also applies here (names and kinds)
# The fallback type can be either 'function' or 'type'. The result should have 'function' as
# fallback only if both operands have it as 'function'.
if t.fallback.type.fullname() != 'builtins.function':
fallback = t.fallback
else:
fallback = s.fallback
return t.copy_modified(arg_types=arg_types,
ret_type=meet_types(t.ret_type, s.ret_type),
fallback=fallback,
name=None)
示例11: visit_callable_type
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def visit_callable_type(self, t: CallableType) -> Type:
return t.copy_modified(arg_types=self.expand_types(t.arg_types), ret_type=t.ret_type.accept(self))
示例12: apply_generic_arguments
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def apply_generic_arguments(callable: CallableType, orig_types: Sequence[Optional[Type]],
msg: MessageBuilder, context: Context,
skip_unsatisfied: bool = False) -> CallableType:
"""Apply generic type arguments to a callable type.
For example, applying [int] to 'def [T] (T) -> T' results in
'def (int) -> int'.
Note that each type can be None; in this case, it will not be applied.
If `skip_unsatisfied` is True, then just skip the types that don't satisfy type variable
bound or constraints, instead of giving an error.
"""
tvars = callable.variables
assert len(tvars) == len(orig_types)
# Check that inferred type variable values are compatible with allowed
# values and bounds. Also, promote subtype values to allowed values.
types = list(orig_types)
for i, type in enumerate(types):
assert not isinstance(type, PartialType), "Internal error: must never apply partial type"
values = callable.variables[i].values
if type is None:
continue
if values:
if isinstance(type, AnyType):
continue
if isinstance(type, TypeVarType) and type.values:
# Allow substituting T1 for T if every allowed value of T1
# is also a legal value of T.
if all(any(is_same_type(v, v1) for v in values)
for v1 in type.values):
continue
matching = []
for value in values:
if mypy.subtypes.is_subtype(type, value):
matching.append(value)
if matching:
best = matching[0]
# If there are more than one matching value, we select the narrowest
for match in matching[1:]:
if mypy.subtypes.is_subtype(match, best):
best = match
types[i] = best
else:
if skip_unsatisfied:
types[i] = None
else:
msg.incompatible_typevar_value(callable, type, callable.variables[i].name,
context)
else:
upper_bound = callable.variables[i].upper_bound
if not mypy.subtypes.is_subtype(type, upper_bound):
if skip_unsatisfied:
types[i] = None
else:
msg.incompatible_typevar_value(callable, type, callable.variables[i].name,
context)
# Create a map from type variable id to target type.
id_to_type = {} # type: Dict[TypeVarId, Type]
for i, tv in enumerate(tvars):
typ = types[i]
if typ:
id_to_type[tv.id] = typ
# Apply arguments to argument types.
arg_types = [expand_type(at, id_to_type) for at in callable.arg_types]
# The callable may retain some type vars if only some were applied.
remaining_tvars = [tv for tv in tvars if tv.id not in id_to_type]
return callable.copy_modified(
arg_types=arg_types,
ret_type=expand_type(callable.ret_type, id_to_type),
variables=remaining_tvars,
)
示例13: visit_callable_type
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def visit_callable_type(self, t: CallableType) -> Type:
return t.copy_modified(arg_types=self.translate_types(t.arg_types),
ret_type=t.ret_type.accept(self),
variables=self.translate_variables(t.variables))
示例14: update_callable_implicit_bounds
# 需要导入模块: from mypy.types import CallableType [as 别名]
# 或者: from mypy.types.CallableType import copy_modified [as 别名]
def update_callable_implicit_bounds(
t: CallableType, arg_types: List[Tuple[int, Type]]) -> CallableType:
# FIX what if there are existing bounds?
return t.copy_modified(bound_vars=arg_types)