MCPcopy
hub / github.com/python/mypy / visit_instance

Method visit_instance

mypy/subtypes.py:492–658  ·  view source on GitHub ↗
(self, left: Instance)

Source from the content-addressed store, hash-verified

490 return True
491
492 def visit_instance(self, left: Instance) -> bool:
493 if left.type.fallback_to_any and not self.proper_subtype:
494 # NOTE: `None` is a *non-subclassable* singleton, therefore no class
495 # can by a subtype of it, even with an `Any` fallback.
496 # This special case is needed to treat descriptors in classes with
497 # dynamic base classes correctly, see #5456.
498 return not isinstance(self.right, NoneType)
499 right = self.right
500 if isinstance(right, TupleType) and right.partial_fallback.type.is_enum:
501 return self._is_subtype(left, mypy.typeops.tuple_fallback(right))
502 if isinstance(right, TupleType):
503 if len(right.items) == 1:
504 # Non-normalized Tuple type (may be left after semantic analysis
505 # because semanal_typearg visitor is not a type translator).
506 item = right.items[0]
507 if isinstance(item, UnpackType):
508 unpacked = get_proper_type(item.type)
509 if isinstance(unpacked, Instance):
510 return self._is_subtype(left, unpacked)
511 if left.type.has_base(right.partial_fallback.type.fullname):
512 if not self.proper_subtype:
513 # Special cases to consider:
514 # * Plain tuple[Any, ...] instance is a subtype of all tuple types.
515 # * Foo[*tuple[Any, ...]] (normalized) instance is a subtype of all
516 # tuples with fallback to Foo (e.g. for variadic NamedTuples).
517 mapped = map_instance_to_supertype(left, right.partial_fallback.type)
518 if is_erased_instance(mapped):
519 if (
520 mapped.type.fullname == "builtins.tuple"
521 or mapped.type.has_type_var_tuple_type
522 ):
523 return True
524 return False
525 if isinstance(right, TypeVarTupleType):
526 # tuple[Any, ...] is like Any in the world of tuples (see special case above).
527 if left.type.has_base("builtins.tuple"):
528 mapped = map_instance_to_supertype(left, right.tuple_fallback.type)
529 if isinstance(get_proper_type(mapped.args[0]), AnyType):
530 return not self.proper_subtype
531 if isinstance(right, Instance):
532 if type_state.is_cached_subtype_check(self._subtype_kind, left, right):
533 return True
534 if type_state.is_cached_negative_subtype_check(self._subtype_kind, left, right):
535 return False
536 if not self.subtype_context.ignore_promotions and not right.type.is_protocol:
537 for base in left.type.mro:
538 if base._promote and any(
539 self._is_subtype(p, self.right) for p in base._promote
540 ):
541 type_state.record_subtype_cache_entry(self._subtype_kind, left, right)
542 return True
543 # Special case: Low-level integer types are compatible with 'int'. We can't
544 # use promotions, since 'int' is already promoted to low-level integer types,
545 # and we can't have circular promotions.
546 if left.type.alt_promote and left.type.alt_promote.type is right.type:
547 return True
548 rname = right.type.fullname
549 # Always try a nominal check if possible,

Callers

nothing calls this directly

Calls 15

_is_subtypeMethod · 0.95
get_proper_typeFunction · 0.90
erase_typeFunction · 0.90
TupleTypeClass · 0.90
is_named_instanceFunction · 0.90
TypeTypeClass · 0.90
AnyTypeClass · 0.90
isinstanceFunction · 0.85
lenFunction · 0.85
is_erased_instanceFunction · 0.85

Tested by

no test coverage detected