Refine a union type based on another type. This is done by refining every component of the union against the right hand side type (or every component of its union if it is one). If an element of the union is successfully refined, we drop it from the union in favor of the refined ver
(t: UnionType, s: ProperType)
| 1005 | |
| 1006 | |
| 1007 | def refine_union(t: UnionType, s: ProperType) -> Type: |
| 1008 | """Refine a union type based on another type. |
| 1009 | |
| 1010 | This is done by refining every component of the union against the |
| 1011 | right hand side type (or every component of its union if it is |
| 1012 | one). If an element of the union is successfully refined, we drop it |
| 1013 | from the union in favor of the refined versions. |
| 1014 | """ |
| 1015 | # Don't try to do any union refining if the types are already the |
| 1016 | # same. This prevents things like refining Optional[Any] against |
| 1017 | # itself and producing None. |
| 1018 | if t == s: |
| 1019 | return t |
| 1020 | |
| 1021 | rhs_items = s.items if isinstance(s, UnionType) else [s] |
| 1022 | |
| 1023 | new_items = [] |
| 1024 | for lhs in t.items: |
| 1025 | refined = False |
| 1026 | for rhs in rhs_items: |
| 1027 | new = refine_type(lhs, rhs) |
| 1028 | if new != lhs: |
| 1029 | new_items.append(new) |
| 1030 | refined = True |
| 1031 | if not refined: |
| 1032 | new_items.append(lhs) |
| 1033 | |
| 1034 | # Turn strict optional on when simplifying the union since we |
| 1035 | # don't want to drop Nones. |
| 1036 | with state.strict_optional_set(True): |
| 1037 | return make_simplified_union(new_items) |
| 1038 | |
| 1039 | |
| 1040 | def refine_callable(t: CallableType, s: CallableType) -> CallableType: |
no test coverage detected
searching dependent graphs…