Skip to content

Commit fbf6fe4

Browse files
authored
Add tests for make_simplified_union (#10397)
This commit adds tests for make_simplified_union, including the new functionality added in #10373.
1 parent 6d2635d commit fbf6fe4

File tree

2 files changed

+56
-18
lines changed

2 files changed

+56
-18
lines changed

mypy/test/testtypes.py

+48-17
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
from mypy.indirection import TypeIndirectionVisitor
1212
from mypy.types import (
1313
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
1615
)
1716
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, CONTRAVARIANT, INVARIANT, COVARIANT
1817
from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
1918
from mypy.test.typefixture import TypeFixture, InterfaceTypeFixture
2019
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
2221

2322

2423
class TypesSuite(Suite):
@@ -299,9 +298,9 @@ def test_is_proper_subtype_invariance(self) -> None:
299298
def test_is_proper_subtype_and_subtype_literal_types(self) -> None:
300299
fx = self.fx
301300

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
305304

306305
assert_true(is_proper_subtype(lit1, fx.a))
307306
assert_false(is_proper_subtype(lit1, fx.d))
@@ -451,6 +450,40 @@ def test_false_only_of_union(self) -> None:
451450
assert_true(fo.items[0].can_be_false)
452451
assert_true(fo.items[1] is tup_type)
453452

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+
454487
# Helpers
455488

456489
def tuple(self, *a: Type) -> TupleType:
@@ -723,9 +756,9 @@ def test_type_type(self) -> None:
723756
def test_literal_type(self) -> None:
724757
a = self.fx.a
725758
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
729762

730763
self.assert_join(lit1, lit1, lit1)
731764
self.assert_join(lit1, a, a)
@@ -951,10 +984,9 @@ def test_type_type(self) -> None:
951984

952985
def test_literal_type(self) -> None:
953986
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
958990

959991
self.assert_meet(lit1, lit1, lit1)
960992
self.assert_meet(lit1, a, lit1)
@@ -1011,11 +1043,10 @@ def setUp(self) -> None:
10111043

10121044
def test_literal_type(self) -> None:
10131045
b = self.fx.b # Reminder: b is a subclass of a
1014-
d = self.fx.d
10151046

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
10191050

10201051
self.assert_same(lit1, lit1)
10211052
self.assert_same(UnionType([lit1, lit2]), UnionType([lit1, lit2]))

mypy/test/typefixture.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from mypy.types import (
99
Type, TypeVarType, AnyType, NoneType, Instance, CallableType, TypeVarDef, TypeType,
10-
UninhabitedType, TypeOfAny, TypeAliasType, UnionType
10+
UninhabitedType, TypeOfAny, TypeAliasType, UnionType, LiteralType
1111
)
1212
from mypy.nodes import (
1313
TypeInfo, ClassDef, Block, ARG_POS, ARG_OPT, ARG_STAR, SymbolTable,
@@ -149,6 +149,13 @@ def make_type_var(name: str, id: int, values: List[Type], upper_bound: Type,
149149
self.lsta = Instance(self.std_listi, [self.a]) # List[A]
150150
self.lstb = Instance(self.std_listi, [self.b]) # List[B]
151151

152+
self.lit1 = LiteralType(1, self.a)
153+
self.lit2 = LiteralType(2, self.a)
154+
self.lit3 = LiteralType("foo", self.d)
155+
self.lit1_inst = Instance(self.ai, [], last_known_value=self.lit1)
156+
self.lit2_inst = Instance(self.ai, [], last_known_value=self.lit2)
157+
self.lit3_inst = Instance(self.di, [], last_known_value=self.lit3)
158+
152159
self.type_a = TypeType.make_normalized(self.a)
153160
self.type_b = TypeType.make_normalized(self.b)
154161
self.type_c = TypeType.make_normalized(self.c)

0 commit comments

Comments
 (0)