|
11 | 11 | from mypy.indirection import TypeIndirectionVisitor
|
12 | 12 | from mypy.types import (
|
13 | 13 | UnboundType, AnyType, CallableType, TupleType, TypeVarDef, Type, Instance, NoneType,
|
14 |
| - Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, |
15 |
| - LiteralType, get_proper_type |
| 14 | + Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, get_proper_type |
16 | 15 | )
|
17 | 16 | from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, CONTRAVARIANT, INVARIANT, COVARIANT
|
18 | 17 | from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
|
19 | 18 | from mypy.test.typefixture import TypeFixture, InterfaceTypeFixture
|
20 | 19 | from mypy.state import strict_optional_set
|
21 |
| -from mypy.typeops import true_only, false_only |
| 20 | +from mypy.typeops import true_only, false_only, make_simplified_union |
22 | 21 |
|
23 | 22 |
|
24 | 23 | class TypesSuite(Suite):
|
@@ -299,9 +298,9 @@ def test_is_proper_subtype_invariance(self) -> None:
|
299 | 298 | def test_is_proper_subtype_and_subtype_literal_types(self) -> None:
|
300 | 299 | fx = self.fx
|
301 | 300 |
|
302 |
| - lit1 = LiteralType(1, fx.a) |
303 |
| - lit2 = LiteralType("foo", fx.d) |
304 |
| - lit3 = LiteralType("bar", fx.d) |
| 301 | + lit1 = fx.lit1 |
| 302 | + lit2 = fx.lit2 |
| 303 | + lit3 = fx.lit3 |
305 | 304 |
|
306 | 305 | assert_true(is_proper_subtype(lit1, fx.a))
|
307 | 306 | assert_false(is_proper_subtype(lit1, fx.d))
|
@@ -451,6 +450,40 @@ def test_false_only_of_union(self) -> None:
|
451 | 450 | assert_true(fo.items[0].can_be_false)
|
452 | 451 | assert_true(fo.items[1] is tup_type)
|
453 | 452 |
|
| 453 | + def test_simplified_union(self) -> None: |
| 454 | + fx = self.fx |
| 455 | + |
| 456 | + self.assert_simplified_union([fx.a, fx.a], fx.a) |
| 457 | + self.assert_simplified_union([fx.a, fx.b], fx.a) |
| 458 | + self.assert_simplified_union([fx.a, fx.d], UnionType([fx.a, fx.d])) |
| 459 | + self.assert_simplified_union([fx.a, fx.uninhabited], fx.a) |
| 460 | + self.assert_simplified_union([fx.ga, fx.gs2a], fx.ga) |
| 461 | + self.assert_simplified_union([fx.ga, fx.gsab], UnionType([fx.ga, fx.gsab])) |
| 462 | + self.assert_simplified_union([fx.ga, fx.gsba], fx.ga) |
| 463 | + self.assert_simplified_union([fx.a, UnionType([fx.d])], UnionType([fx.a, fx.d])) |
| 464 | + self.assert_simplified_union([fx.a, UnionType([fx.a])], fx.a) |
| 465 | + self.assert_simplified_union([fx.b, UnionType([fx.c, UnionType([fx.d])])], |
| 466 | + UnionType([fx.b, fx.c, fx.d])) |
| 467 | + self.assert_simplified_union([fx.lit1, fx.a], fx.a) |
| 468 | + self.assert_simplified_union([fx.lit1, fx.lit1], fx.lit1) |
| 469 | + self.assert_simplified_union([fx.lit1, fx.lit2], UnionType([fx.lit1, fx.lit2])) |
| 470 | + self.assert_simplified_union([fx.lit1, fx.lit3], UnionType([fx.lit1, fx.lit3])) |
| 471 | + self.assert_simplified_union([fx.lit1, fx.uninhabited], fx.lit1) |
| 472 | + self.assert_simplified_union([fx.lit1_inst, fx.a], fx.a) |
| 473 | + self.assert_simplified_union([fx.lit1_inst, fx.lit1_inst], fx.lit1_inst) |
| 474 | + self.assert_simplified_union([fx.lit1_inst, fx.lit2_inst], |
| 475 | + UnionType([fx.lit1_inst, fx.lit2_inst])) |
| 476 | + self.assert_simplified_union([fx.lit1_inst, fx.lit3_inst], |
| 477 | + UnionType([fx.lit1_inst, fx.lit3_inst])) |
| 478 | + self.assert_simplified_union([fx.lit1_inst, fx.uninhabited], fx.lit1_inst) |
| 479 | + self.assert_simplified_union([fx.lit1, fx.lit1_inst], UnionType([fx.lit1, fx.lit1_inst])) |
| 480 | + self.assert_simplified_union([fx.lit1, fx.lit2_inst], UnionType([fx.lit1, fx.lit2_inst])) |
| 481 | + self.assert_simplified_union([fx.lit1, fx.lit3_inst], UnionType([fx.lit1, fx.lit3_inst])) |
| 482 | + |
| 483 | + def assert_simplified_union(self, original: List[Type], union: Type) -> None: |
| 484 | + assert_equal(make_simplified_union(original), union) |
| 485 | + assert_equal(make_simplified_union(list(reversed(original))), union) |
| 486 | + |
454 | 487 | # Helpers
|
455 | 488 |
|
456 | 489 | def tuple(self, *a: Type) -> TupleType:
|
@@ -723,9 +756,9 @@ def test_type_type(self) -> None:
|
723 | 756 | def test_literal_type(self) -> None:
|
724 | 757 | a = self.fx.a
|
725 | 758 | d = self.fx.d
|
726 |
| - lit1 = LiteralType(1, a) |
727 |
| - lit2 = LiteralType(2, a) |
728 |
| - lit3 = LiteralType("foo", d) |
| 759 | + lit1 = self.fx.lit1 |
| 760 | + lit2 = self.fx.lit2 |
| 761 | + lit3 = self.fx.lit3 |
729 | 762 |
|
730 | 763 | self.assert_join(lit1, lit1, lit1)
|
731 | 764 | self.assert_join(lit1, a, a)
|
@@ -951,10 +984,9 @@ def test_type_type(self) -> None:
|
951 | 984 |
|
952 | 985 | def test_literal_type(self) -> None:
|
953 | 986 | a = self.fx.a
|
954 |
| - d = self.fx.d |
955 |
| - lit1 = LiteralType(1, a) |
956 |
| - lit2 = LiteralType(2, a) |
957 |
| - lit3 = LiteralType("foo", d) |
| 987 | + lit1 = self.fx.lit1 |
| 988 | + lit2 = self.fx.lit2 |
| 989 | + lit3 = self.fx.lit3 |
958 | 990 |
|
959 | 991 | self.assert_meet(lit1, lit1, lit1)
|
960 | 992 | self.assert_meet(lit1, a, lit1)
|
@@ -1011,11 +1043,10 @@ def setUp(self) -> None:
|
1011 | 1043 |
|
1012 | 1044 | def test_literal_type(self) -> None:
|
1013 | 1045 | b = self.fx.b # Reminder: b is a subclass of a
|
1014 |
| - d = self.fx.d |
1015 | 1046 |
|
1016 |
| - lit1 = LiteralType(1, b) |
1017 |
| - lit2 = LiteralType(2, b) |
1018 |
| - lit3 = LiteralType("foo", d) |
| 1047 | + lit1 = self.fx.lit1 |
| 1048 | + lit2 = self.fx.lit2 |
| 1049 | + lit3 = self.fx.lit3 |
1019 | 1050 |
|
1020 | 1051 | self.assert_same(lit1, lit1)
|
1021 | 1052 | self.assert_same(UnionType([lit1, lit2]), UnionType([lit1, lit2]))
|
|
0 commit comments