MCPcopy Index your code
hub / github.com/python/mypy / visit_instance

Method visit_instance

mypy/meet.py:873–956  ·  view source on GitHub ↗
(self, t: Instance)

Source from the content-addressed store, hash-verified

871 return self.default(self.s)
872
873 def visit_instance(self, t: Instance) -> ProperType:
874 if isinstance(self.s, Instance):
875 if t.type == self.s.type:
876 if is_subtype(t, self.s) or is_subtype(self.s, t):
877 # Combine type arguments. We could have used join below
878 # equivalently.
879 args: list[Type] = []
880 # N.B: We use zip instead of indexing because the lengths might have
881 # mismatches during daemon reprocessing.
882 if t.type.has_type_var_tuple_type:
883 # We handle meet of variadic instances by simply creating correct mapping
884 # for type arguments and compute the individual meets same as for regular
885 # instances. All the heavy lifting is done in the meet of tuple types.
886 s = self.s
887 assert s.type.type_var_tuple_prefix is not None
888 assert s.type.type_var_tuple_suffix is not None
889 prefix = s.type.type_var_tuple_prefix
890 suffix = s.type.type_var_tuple_suffix
891 tvt = s.type.defn.type_vars[prefix]
892 assert isinstance(tvt, TypeVarTupleType)
893 fallback = tvt.tuple_fallback
894 s_prefix, s_middle, s_suffix = split_with_prefix_and_suffix(
895 s.args, prefix, suffix
896 )
897 t_prefix, t_middle, t_suffix = split_with_prefix_and_suffix(
898 t.args, prefix, suffix
899 )
900 s_args = s_prefix + (TupleType(list(s_middle), fallback),) + s_suffix
901 t_args = t_prefix + (TupleType(list(t_middle), fallback),) + t_suffix
902 else:
903 t_args = t.args
904 s_args = self.s.args
905 for ta, sa, tv in zip(t_args, s_args, t.type.defn.type_vars):
906 meet = self.meet(ta, sa)
907 if isinstance(tv, TypeVarTupleType):
908 # Correctly unpack possible outcomes of meets of tuples: it can be
909 # either another tuple type or Never (normalized as *tuple[Never, ...])
910 if isinstance(meet, TupleType):
911 args.extend(meet.items)
912 continue
913 else:
914 assert isinstance(meet, UninhabitedType)
915 meet = UnpackType(tv.tuple_fallback.copy_modified(args=[meet]))
916 args.append(meet)
917 return Instance(t.type, args)
918 else:
919 if state.strict_optional:
920 return UninhabitedType()
921 else:
922 return NoneType()
923 else:
924 alt_promote = t.type.alt_promote
925 if alt_promote and alt_promote.type is self.s.type:
926 return t
927 alt_promote = self.s.type.alt_promote
928 if alt_promote and alt_promote.type is t.type:
929 return self.s
930 if is_subtype(t, self.s):

Callers

nothing calls this directly

Calls 15

meetMethod · 0.95
defaultMethod · 0.95
is_subtypeFunction · 0.90
TupleTypeClass · 0.90
UnpackTypeClass · 0.90
InstanceClass · 0.90
UninhabitedTypeClass · 0.90
NoneTypeClass · 0.90
isinstanceFunction · 0.85
listClass · 0.85
zipFunction · 0.85

Tested by

no test coverage detected