本文整理汇总了Python中mypy.sametypes.is_same_type函数的典型用法代码示例。如果您正苦于以下问题:Python is_same_type函数的具体用法?Python is_same_type怎么用?Python is_same_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_same_type函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
示例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)
示例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)
示例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)
示例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
示例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)
示例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,
)
示例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)
示例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))
示例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)
示例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]
示例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
示例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)
示例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)
示例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)