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

Method construct_sequence_child

mypy/checkpattern.py:811–845  ·  view source on GitHub ↗

If outer_type is a child class of typing.Sequence returns a new instance of outer_type, that is a Sequence of inner_type. If outer_type is not a child class of typing.Sequence just returns a Sequence of inner_type For example: construct_sequence_child(List[i

(self, outer_type: Type, inner_type: Type)

Source from the content-addressed store, hash-verified

809 original_type_map[expr] = typ
810
811 def construct_sequence_child(self, outer_type: Type, inner_type: Type) -> Type:
812 """
813 If outer_type is a child class of typing.Sequence returns a new instance of
814 outer_type, that is a Sequence of inner_type. If outer_type is not a child class of
815 typing.Sequence just returns a Sequence of inner_type
816
817 For example:
818 construct_sequence_child(List[int], str) = List[str]
819
820 TODO: this doesn't make sense. For example if one has class S(Sequence[int], Generic[T])
821 or class T(Sequence[Tuple[T, T]]), there is no way any of those can map to Sequence[str].
822 """
823 proper_type = get_proper_type(outer_type)
824 if isinstance(proper_type, TypeVarType):
825 new_bound = self.construct_sequence_child(proper_type.upper_bound, inner_type)
826 return proper_type.copy_modified(upper_bound=new_bound)
827 if isinstance(proper_type, AnyType):
828 return outer_type
829 if isinstance(proper_type, UnionType):
830 types = [
831 self.construct_sequence_child(item, inner_type)
832 for item in proper_type.items
833 if self.can_match_sequence(get_proper_type(item))
834 ]
835 return make_simplified_union(types)
836 sequence = self.chk.named_generic_type("typing.Sequence", [inner_type])
837 if is_subtype(outer_type, self.chk.named_type("typing.Sequence")):
838 if isinstance(proper_type, TupleType):
839 proper_type = tuple_fallback(proper_type)
840 assert isinstance(proper_type, Instance)
841 empty_type = fill_typevars(proper_type.type)
842 partial_type = expand_type_by_instance(empty_type, sequence)
843 return expand_type_by_instance(partial_type, proper_type)
844 else:
845 return sequence
846
847 def early_non_match(self) -> PatternType:
848 return PatternType(UninhabitedType(), self.type_context[-1], {})

Callers 1

Calls 11

can_match_sequenceMethod · 0.95
get_proper_typeFunction · 0.90
make_simplified_unionFunction · 0.90
is_subtypeFunction · 0.90
tuple_fallbackFunction · 0.90
fill_typevarsFunction · 0.90
expand_type_by_instanceFunction · 0.90
isinstanceFunction · 0.85
copy_modifiedMethod · 0.45
named_generic_typeMethod · 0.45
named_typeMethod · 0.45

Tested by

no test coverage detected