本文整理汇总了Python中mypy.sametypes.is_same_type函数的典型用法代码示例。如果您正苦于以下问题:Python is_same_type函数的具体用法?Python is_same_type怎么用?Python is_same_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_same_type函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: visit_intersection
def visit_intersection(self, t):
# TODO Obsolete; target overload types instead?
# Only support very rudimentary meets between intersection types.
if is_same_type(self.s, t):
return self.s
else:
return self.default(self.s)
开发者ID:kivipe,项目名称:mypy,代码行数:7,代码来源:meet.py
示例2: check_type_var_values
def check_type_var_values(self, type: TypeInfo, actuals: List[Type],
valids: List[Type], context: Context) -> None:
for actual in actuals:
if (not isinstance(actual, AnyType) and
not any(is_same_type(actual, value) for value in valids)):
self.fail('Invalid type argument value for "{}"'.format(
type.name()), context)
开发者ID:AXGKl,项目名称:Transcrypt,代码行数:7,代码来源:typeanal.py
示例3: is_proper_subtype
def is_proper_subtype(t: Type, s: Type) -> bool:
"""Check if t is a proper subtype of s?
For proper subtypes, there's no need to rely on compatibility due to
Any types. Any instance type t is also a proper subtype of t.
"""
# FIX tuple types
if isinstance(t, Instance):
if isinstance(s, Instance):
if not t.type.has_base(s.type.fullname()):
return False
def check_argument(left: Type, right: Type, variance: int) -> bool:
if variance == COVARIANT:
return is_proper_subtype(left, right)
elif variance == CONTRAVARIANT:
return is_proper_subtype(right, left)
else:
return sametypes.is_same_type(left, right)
# Map left type to corresponding right instances.
t = map_instance_to_supertype(t, s.type)
return all(check_argument(ta, ra, tvar.variance) for ta, ra, tvar in
zip(t.args, s.args, s.type.defn.type_vars))
return False
else:
return sametypes.is_same_type(t, s)
开发者ID:alexandrul,项目名称:mypy,代码行数:28,代码来源:subtypes.py
示例4: check_argument
def check_argument(left: Type, right: Type, variance: int) -> bool:
if variance == COVARIANT:
return is_proper_subtype(left, right)
elif variance == CONTRAVARIANT:
return is_proper_subtype(right, left)
else:
return sametypes.is_same_type(left, right)
开发者ID:alexandrul,项目名称:mypy,代码行数:7,代码来源:subtypes.py
示例5: add_symbol
def add_symbol(self, name: str, node: SymbolTableNode,
context: Context) -> None:
# NOTE: This is closely related to SemanticAnalyzerPass2.add_symbol. Since both methods
# will be called on top-level definitions, they need to co-operate. If you change
# this, you may have to change the other method as well.
if self.sem.is_func_scope():
assert self.sem.locals[-1] is not None
if name in self.sem.locals[-1]:
# Flag redefinition unless this is a reimport of a module.
if not (node.kind == MODULE_REF and
self.sem.locals[-1][name].node == node.node):
self.sem.name_already_defined(name, context)
self.sem.locals[-1][name] = node
else:
assert self.sem.type is None # Pass 1 doesn't look inside classes
existing = self.sem.globals.get(name)
if (existing
and (not isinstance(node.node, MypyFile) or existing.node != node.node)
and existing.kind != UNBOUND_IMPORTED
and not isinstance(existing.node, ImportedName)):
# Modules can be imported multiple times to support import
# of multiple submodules of a package (e.g. a.x and a.y).
ok = False
# Only report an error if the symbol collision provides a different type.
if existing.type and node.type and is_same_type(existing.type, node.type):
ok = True
if not ok:
self.sem.name_already_defined(name, context)
elif not existing:
self.sem.globals[name] = node
开发者ID:sixolet,项目名称:mypy,代码行数:30,代码来源:semanal_pass1.py
示例6: is_proper_subtype
def is_proper_subtype(t: Type, s: Type) -> bool:
"""Check if t is a proper subtype of s?
For proper subtypes, there's no need to rely on compatibility due to
Any types. Any instance type t is also a proper subtype of t.
"""
# FIX tuple types
if isinstance(t, Instance):
if isinstance(s, Instance):
if not t.type.has_base(s.type.fullname()):
return False
t = map_instance_to_supertype(t, s.type)
return all(sametypes.is_same_type(x, y)
for x, y in zip(t.args, s.args))
return False
else:
return sametypes.is_same_type(t, s)
开发者ID:FlorianLudwig,项目名称:mypy,代码行数:17,代码来源:subtypes.py
示例7: apply_generic_arguments
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,
)
开发者ID:AXGKl,项目名称:Transcrypt,代码行数:56,代码来源:applytype.py
示例8: visit_typeddict_type
def visit_typeddict_type(self, left: TypedDictType) -> bool:
right = self.right
if isinstance(right, TypedDictType):
for name, typ in left.items.items():
if name in right.items and not is_same_type(typ, right.items[name]):
return False
for name, typ in right.items.items():
if name not in left.items:
return False
return True
return is_proper_subtype(left.fallback, right)
开发者ID:greatmazinger,项目名称:mypy,代码行数:11,代码来源:subtypes.py
示例9: assert_simple_is_same
def assert_simple_is_same(self, s: Type, t: Type, expected: bool, strict: bool) -> None:
actual = is_same_type(s, t)
assert_equal(actual, expected,
'is_same_type({}, {}) is {{}} ({{}} expected)'.format(s, t))
if strict:
actual2 = (s == t)
assert_equal(actual2, expected,
'({} == {}) is {{}} ({{}} expected)'.format(s, t))
assert_equal(hash(s) == hash(t), expected,
'(hash({}) == hash({}) is {{}} ({{}} expected)'.format(s, t))
开发者ID:python,项目名称:mypy,代码行数:11,代码来源:testtypes.py
示例10: check_type_var_values
def check_type_var_values(self, type: TypeInfo, actuals: List[Type],
valids: List[Type], arg_number: int, context: Context) -> None:
for actual in actuals:
if (not isinstance(actual, AnyType) and
not any(is_same_type(actual, value) for value in valids)):
if len(actuals) > 1 or not isinstance(actual, Instance):
self.fail('Invalid type argument value for "{}"'.format(
type.name()), context)
else:
self.fail('Type argument {} of "{}" has incompatible value "{}"'.format(
arg_number, type.name(), actual.type.name()), context)
开发者ID:alexandrul,项目名称:mypy,代码行数:11,代码来源:typeanal.py
示例11: generate_type_combinations
def generate_type_combinations(types: List[Type]) -> List[Type]:
"""Generate possible combinations of a list of types.
mypy essentially supports two different ways to do this: joining the types
and unioning the types. We try both.
"""
joined_type = join_type_list(types)
union_type = UnionType.make_simplified_union(types)
if is_same_type(joined_type, union_type):
return [joined_type]
else:
return [joined_type, union_type]
开发者ID:Michael0x2a,项目名称:mypy,代码行数:12,代码来源:suggestions.py
示例12: update_from_options
def update_from_options(self, frames: List[Frame]) -> bool:
"""Update the frame to reflect that each key will be updated
as in one of the frames. Return whether any item changes.
If a key is declared as AnyType, only update it if all the
options are the same.
"""
frames = [f for f in frames if not f.unreachable]
changed = False
keys = set(key for f in frames for key in f)
for key in keys:
current_value = self._get(key)
resulting_values = [f.get(key, current_value) for f in frames]
if any(x is None for x in resulting_values):
# We didn't know anything about key before
# (current_value must be None), and we still don't
# know anything about key in at least one possible frame.
continue
type = resulting_values[0]
assert type is not None
declaration_type = self.declarations.get(key)
if isinstance(declaration_type, AnyType):
# At this point resulting values can't contain None, see continue above
if not all(is_same_type(type, cast(Type, t)) for t in resulting_values[1:]):
type = AnyType(TypeOfAny.from_another_any, source_any=declaration_type)
else:
for other in resulting_values[1:]:
assert other is not None
type = join_simple(self.declarations[key], type, other)
if current_value is None or not is_same_type(type, current_value):
self._put(key, type)
changed = True
self.frames[-1].unreachable = not frames
return changed
开发者ID:greatmazinger,项目名称:mypy,代码行数:39,代码来源:binder.py
示例13: is_more_precise
def is_more_precise(t: Type, s: Type) -> bool:
"""Check if t is a more precise type than s.
A t is a proper subtype of s, t is also more precise than s. Also, if
s is Any, t is more precise than s for any t. Finally, if t is the same
type as s, t is more precise than s.
"""
# TODO Should List[int] be more precise than List[Any]?
if isinstance(s, AnyType):
return True
if isinstance(s, Instance):
return is_proper_subtype(t, s)
return sametypes.is_same_type(t, s)
开发者ID:bussiere,项目名称:mypy,代码行数:13,代码来源:subtypes.py
示例14: check_type_var_values
def check_type_var_values(self, type: TypeInfo, actuals: List[Type], arg_name: str,
valids: List[Type], arg_number: int, context: Context) -> None:
for actual in actuals:
if (not isinstance(actual, AnyType) and
not any(is_same_type(actual, value)
for value in valids)):
if len(actuals) > 1 or not isinstance(actual, Instance):
self.fail('Invalid type argument value for "{}"'.format(
type.name()), context)
else:
class_name = '"{}"'.format(type.name())
actual_type_name = '"{}"'.format(actual.type.name())
self.fail(messages.INCOMPATIBLE_TYPEVAR_VALUE.format(
arg_name, class_name, actual_type_name), context)
开发者ID:sixolet,项目名称:mypy,代码行数:14,代码来源:semanal_pass3.py
示例15: is_more_precise
def is_more_precise(t: Type, s: Type) -> bool:
"""Check if t is a more precise type than s.
A t is a proper subtype of s, t is also more precise than s. Also, if
s is Any, t is more precise than s for any t. Finally, if t is the same
type as s, t is more precise than s.
"""
# TODO Should List[int] be more precise than List[Any]?
if isinstance(s, AnyType):
return True
if isinstance(s, Instance):
if isinstance(t, CallableType):
# Fall back to subclass check and ignore other properties of the callable.
return is_proper_subtype(t.fallback, s)
return is_proper_subtype(t, s)
return sametypes.is_same_type(t, s)
开发者ID:alexandrul,项目名称:mypy,代码行数:16,代码来源:subtypes.py
示例16: is_valid_inferred_type
def is_valid_inferred_type(self, typ):
"""Is an inferred type invalid?
Examples include the None type or a type with a None component.
"""
if is_same_type(typ, NoneTyp()):
return False
elif isinstance(typ, Instance):
for arg in (typ).args:
if not self.is_valid_inferred_type(arg):
return False
elif isinstance(typ, TupleType):
for item in (typ).items:
if not self.is_valid_inferred_type(item):
return False
return True
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:16,代码来源:checker.py
示例17: is_trivial_coercion
def is_trivial_coercion(target_type, source_type, is_java):
"""Is an implicit coercion from source_type to target_type a no-op?
Note that we omit coercions of form any <= C, unless C is a primitive that
may have a special representation.
"""
# FIX: Replace type vars in source type with any?
if isinstance(source_type, Void) or is_same_type(target_type, source_type):
return True
# Coercions from a primitive type to any other type are non-trivial, since
# we may have to change the representation.
if not is_java and is_special_primitive(source_type):
return False
return (is_proper_subtype(source_type, target_type)
or isinstance(source_type, NoneTyp)
or isinstance(target_type, Any))
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:18,代码来源:coerce.py
示例18: is_simple_override
def is_simple_override(fdef, info):
"""Is function an override with the same type precision as the original?
Compare to the original method in the superclass of info.
"""
# If this is not an override, this can't be a simple override either.
# Generic inheritance is not currently supported, since we need to map
# type variables between types; in the future this restriction can be
# lifted.
if info.base is None or info.base.type_vars != []:
return False
orig = info.base.get_method(fdef.name())
# Ignore the first argument (self) when determining type sameness.
# TODO overloads
newtype = function_type(fdef)
newtype = replace_self_type(newtype, Any())
origtype = function_type(orig)
origtype = replace_self_type(origtype, Any())
return is_same_type(newtype, origtype)
开发者ID:SRiikonen,项目名称:mypy-py,代码行数:19,代码来源:transutil.py
示例19: is_simple_override
def is_simple_override(fdef: FuncDef, info: TypeInfo) -> bool:
"""Is function an override with the same type precision as the original?
Compare to the original method in the superclass of info.
"""
# If this is not an override, this can't be a simple override either.
# Generic inheritance is not currently supported, since we need to map
# type variables between types; in the future this restriction can be
# lifted.
if len(info.mro) <= 1:
return False
base = info.mro[1]
if base.type_vars != []:
return False
orig = base.get_method(fdef.name())
# Ignore the first argument (self) when determining type sameness.
# TODO overloads
newtype = cast(Callable, function_type(fdef))
newtype = replace_leading_arg_type(newtype, AnyType())
origtype = cast(Callable, function_type(orig))
origtype = replace_leading_arg_type(origtype, AnyType())
return is_same_type(newtype, origtype)
开发者ID:akaihola,项目名称:mypy,代码行数:22,代码来源:transutil.py
示例20: apply_generic_arguments
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,
)
开发者ID:chadrik,项目名称:mypy,代码行数:76,代码来源:applytype.py
注:本文中的mypy.sametypes.is_same_type函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论