|
3 | 3 | from typing import List, Tuple
|
4 | 4 |
|
5 | 5 | from mypy.test.helpers import Suite, assert_equal, assert_type, skip
|
6 |
| -from mypy.erasetype import erase_type |
| 6 | +from mypy.erasetype import erase_type, remove_instance_last_known_values |
7 | 7 | from mypy.expandtype import expand_type
|
8 | 8 | from mypy.join import join_types, join_simple
|
9 | 9 | from mypy.meet import meet_types, narrow_declared_type
|
10 | 10 | from mypy.sametypes import is_same_type
|
11 | 11 | from mypy.indirection import TypeIndirectionVisitor
|
12 | 12 | from mypy.types import (
|
13 | 13 | UnboundType, AnyType, CallableType, TupleType, TypeVarType, Type, Instance, NoneType,
|
14 |
| - Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, get_proper_type |
| 14 | + Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, ProperType, |
| 15 | + get_proper_type |
15 | 16 | )
|
16 | 17 | from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, CONTRAVARIANT, INVARIANT, COVARIANT
|
17 | 18 | from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
|
@@ -1092,3 +1093,46 @@ def assert_simple_is_same(self, s: Type, t: Type, expected: bool, strict: bool)
|
1092 | 1093 | '({} == {}) is {{}} ({{}} expected)'.format(s, t))
|
1093 | 1094 | assert_equal(hash(s) == hash(t), expected,
|
1094 | 1095 | '(hash({}) == hash({}) is {{}} ({{}} expected)'.format(s, t))
|
| 1096 | + |
| 1097 | + |
| 1098 | +class RemoveLastKnownValueSuite(Suite): |
| 1099 | + def setUp(self) -> None: |
| 1100 | + self.fx = TypeFixture() |
| 1101 | + |
| 1102 | + def test_optional(self) -> None: |
| 1103 | + t = UnionType.make_union([self.fx.a, self.fx.nonet]) |
| 1104 | + self.assert_union_result(t, [self.fx.a, self.fx.nonet]) |
| 1105 | + |
| 1106 | + def test_two_instances(self) -> None: |
| 1107 | + t = UnionType.make_union([self.fx.a, self.fx.b]) |
| 1108 | + self.assert_union_result(t, [self.fx.a, self.fx.b]) |
| 1109 | + |
| 1110 | + def test_multiple_same_instances(self) -> None: |
| 1111 | + t = UnionType.make_union([self.fx.a, self.fx.a]) |
| 1112 | + assert remove_instance_last_known_values(t) == self.fx.a |
| 1113 | + t = UnionType.make_union([self.fx.a, self.fx.a, self.fx.b]) |
| 1114 | + self.assert_union_result(t, [self.fx.a, self.fx.b]) |
| 1115 | + t = UnionType.make_union([self.fx.a, self.fx.nonet, self.fx.a, self.fx.b]) |
| 1116 | + self.assert_union_result(t, [self.fx.a, self.fx.nonet, self.fx.b]) |
| 1117 | + |
| 1118 | + def test_single_last_known_value(self) -> None: |
| 1119 | + t = UnionType.make_union([self.fx.lit1_inst, self.fx.nonet]) |
| 1120 | + self.assert_union_result(t, [self.fx.a, self.fx.nonet]) |
| 1121 | + |
| 1122 | + def test_last_known_values_with_merge(self) -> None: |
| 1123 | + t = UnionType.make_union([self.fx.lit1_inst, self.fx.lit2_inst, self.fx.lit4_inst]) |
| 1124 | + assert remove_instance_last_known_values(t) == self.fx.a |
| 1125 | + t = UnionType.make_union([self.fx.lit1_inst, |
| 1126 | + self.fx.b, |
| 1127 | + self.fx.lit2_inst, |
| 1128 | + self.fx.lit4_inst]) |
| 1129 | + self.assert_union_result(t, [self.fx.a, self.fx.b]) |
| 1130 | + |
| 1131 | + def test_generics(self) -> None: |
| 1132 | + t = UnionType.make_union([self.fx.ga, self.fx.gb]) |
| 1133 | + self.assert_union_result(t, [self.fx.ga, self.fx.gb]) |
| 1134 | + |
| 1135 | + def assert_union_result(self, t: ProperType, expected: List[Type]) -> None: |
| 1136 | + t2 = remove_instance_last_known_values(t) |
| 1137 | + assert type(t2) is UnionType |
| 1138 | + assert t2.items == expected |
0 commit comments