Calculate what information we can learn from the truth of (e1 and e2) in terms of the information that we can learn from the truth of e1 and the truth of e2.
(m1: TypeMap, m2: TypeMap, *, use_meet: bool = False)
| 8813 | |
| 8814 | |
| 8815 | def and_conditional_maps(m1: TypeMap, m2: TypeMap, *, use_meet: bool = False) -> TypeMap: |
| 8816 | """Calculate what information we can learn from the truth of (e1 and e2) |
| 8817 | in terms of the information that we can learn from the truth of e1 and |
| 8818 | the truth of e2. |
| 8819 | """ |
| 8820 | # Both conditions can be true; combine the information. Anything |
| 8821 | # we learn from either conditions' truth is valid. |
| 8822 | result = m2.copy() |
| 8823 | m2_exprs = {literal_hash(e2): e2 for e2 in m2} |
| 8824 | for e1 in m1: |
| 8825 | e1_hash = literal_hash(e1) |
| 8826 | if e1_hash not in m2_exprs: |
| 8827 | result[e1] = m1[e1] |
| 8828 | continue |
| 8829 | |
| 8830 | if not use_meet: |
| 8831 | # If use_meet=False and the same expression's type is refined by both conditions, |
| 8832 | # we somewhat arbitrarily give precedence to m2, unless a) m1's value is Never, or |
| 8833 | # b) m2's value is Any and m1 isn't a union that could have been narrowed to Any. |
| 8834 | t1 = m1[e1] |
| 8835 | pt1 = get_proper_type(t1) |
| 8836 | if isinstance(pt1, UninhabitedType): |
| 8837 | result[e1] = t1 |
| 8838 | else: |
| 8839 | e2 = m2_exprs[e1_hash] |
| 8840 | pt2 = get_proper_type(m2[e2]) |
| 8841 | if isinstance(pt2, AnyType) and not ( |
| 8842 | isinstance(pt1, UnionType) |
| 8843 | and any(isinstance(get_proper_type(item), AnyType) for item in pt1.items) |
| 8844 | ): |
| 8845 | result[e1] = t1 |
| 8846 | |
| 8847 | if use_meet: |
| 8848 | # For now, meet common keys only if specifically requested. |
| 8849 | # This is currently used for tuple types narrowing, where having |
| 8850 | # a precise result is important. |
| 8851 | for e1 in m1: |
| 8852 | for e2 in m2: |
| 8853 | if literal_hash(e1) == literal_hash(e2): |
| 8854 | result[e1] = meet_types(m1[e1], m2[e2]) |
| 8855 | return result |
| 8856 | |
| 8857 | |
| 8858 | def or_conditional_maps(m1: TypeMap, m2: TypeMap, *, coalesce_any: bool = False) -> TypeMap: |
no test coverage detected
searching dependent graphs…