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

Function reduce_conditional_maps

mypy/checker.py:8884–8920  ·  view source on GitHub ↗

Reduces a list containing pairs of if/else TypeMaps into a single pair. We "and" together all of the if TypeMaps and "or" together the else TypeMaps. So for example, if we had the input: [ ({x: TypeIfX, shared: TypeIfShared1}, {x: TypeElseX, shared: TypeElseShared1}),

(
    type_maps: list[tuple[TypeMap, TypeMap]], use_meet: bool = False
)

Source from the content-addressed store, hash-verified

8882
8883
8884def reduce_conditional_maps(
8885 type_maps: list[tuple[TypeMap, TypeMap]], use_meet: bool = False
8886) -> tuple[TypeMap, TypeMap]:
8887 """Reduces a list containing pairs of if/else TypeMaps into a single pair.
8888
8889 We "and" together all of the if TypeMaps and "or" together the else TypeMaps. So
8890 for example, if we had the input:
8891
8892 [
8893 ({x: TypeIfX, shared: TypeIfShared1}, {x: TypeElseX, shared: TypeElseShared1}),
8894 ({y: TypeIfY, shared: TypeIfShared2}, {y: TypeElseY, shared: TypeElseShared2}),
8895 ]
8896
8897 ...we'd return the output:
8898
8899 (
8900 {x: TypeIfX, y: TypeIfY, shared: PseudoIntersection[TypeIfShared1, TypeIfShared2]},
8901 {shared: Union[TypeElseShared1, TypeElseShared2]},
8902 )
8903
8904 ...where "PseudoIntersection[X, Y] == Y" because mypy actually doesn't understand intersections
8905 yet, so we settle for just arbitrarily picking the right expr's type.
8906
8907 We only retain the shared expression in the 'else' case because we don't actually know
8908 whether x was refined or y was refined -- only just that one of the two was refined.
8909 """
8910 if len(type_maps) == 0:
8911 return {}, {}
8912 elif len(type_maps) == 1:
8913 return type_maps[0]
8914 else:
8915 final_if_map, final_else_map = type_maps[0]
8916 for if_map, else_map in type_maps[1:]:
8917 final_if_map = and_conditional_maps(final_if_map, if_map, use_meet=use_meet)
8918 final_else_map = or_conditional_maps(final_else_map, else_map)
8919
8920 return final_if_map, final_else_map
8921
8922
8923def reduce_or_conditional_type_maps(ms: list[TypeMap]) -> TypeMap:

Callers 1

Calls 3

lenFunction · 0.85
and_conditional_mapsFunction · 0.85
or_conditional_mapsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…