Skip to content

Add tests for make_simplified_union #10397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 48 additions & 17 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
from mypy.indirection import TypeIndirectionVisitor
from mypy.types import (
UnboundType, AnyType, CallableType, TupleType, TypeVarDef, Type, Instance, NoneType,
Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny,
LiteralType, get_proper_type
Overloaded, TypeType, UnionType, UninhabitedType, TypeVarId, TypeOfAny, get_proper_type
)
from mypy.nodes import ARG_POS, ARG_OPT, ARG_STAR, ARG_STAR2, CONTRAVARIANT, INVARIANT, COVARIANT
from mypy.subtypes import is_subtype, is_more_precise, is_proper_subtype
from mypy.test.typefixture import TypeFixture, InterfaceTypeFixture
from mypy.state import strict_optional_set
from mypy.typeops import true_only, false_only
from mypy.typeops import true_only, false_only, make_simplified_union


class TypesSuite(Suite):
Expand Down Expand Up @@ -299,9 +298,9 @@ def test_is_proper_subtype_invariance(self) -> None:
def test_is_proper_subtype_and_subtype_literal_types(self) -> None:
fx = self.fx

lit1 = LiteralType(1, fx.a)
lit2 = LiteralType("foo", fx.d)
lit3 = LiteralType("bar", fx.d)
lit1 = fx.lit1
lit2 = fx.lit2
lit3 = fx.lit3

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

def test_simplified_union(self) -> None:
fx = self.fx

self.assert_simplified_union([fx.a, fx.a], fx.a)
self.assert_simplified_union([fx.a, fx.b], fx.a)
self.assert_simplified_union([fx.a, fx.d], UnionType([fx.a, fx.d]))
self.assert_simplified_union([fx.a, fx.uninhabited], fx.a)
self.assert_simplified_union([fx.ga, fx.gs2a], fx.ga)
self.assert_simplified_union([fx.ga, fx.gsab], UnionType([fx.ga, fx.gsab]))
self.assert_simplified_union([fx.ga, fx.gsba], fx.ga)
self.assert_simplified_union([fx.a, UnionType([fx.d])], UnionType([fx.a, fx.d]))
self.assert_simplified_union([fx.a, UnionType([fx.a])], fx.a)
self.assert_simplified_union([fx.b, UnionType([fx.c, UnionType([fx.d])])],
UnionType([fx.b, fx.c, fx.d]))
self.assert_simplified_union([fx.lit1, fx.a], fx.a)
self.assert_simplified_union([fx.lit1, fx.lit1], fx.lit1)
self.assert_simplified_union([fx.lit1, fx.lit2], UnionType([fx.lit1, fx.lit2]))
self.assert_simplified_union([fx.lit1, fx.lit3], UnionType([fx.lit1, fx.lit3]))
self.assert_simplified_union([fx.lit1, fx.uninhabited], fx.lit1)
self.assert_simplified_union([fx.lit1_inst, fx.a], fx.a)
self.assert_simplified_union([fx.lit1_inst, fx.lit1_inst], fx.lit1_inst)
self.assert_simplified_union([fx.lit1_inst, fx.lit2_inst],
UnionType([fx.lit1_inst, fx.lit2_inst]))
self.assert_simplified_union([fx.lit1_inst, fx.lit3_inst],
UnionType([fx.lit1_inst, fx.lit3_inst]))
self.assert_simplified_union([fx.lit1_inst, fx.uninhabited], fx.lit1_inst)
self.assert_simplified_union([fx.lit1, fx.lit1_inst], UnionType([fx.lit1, fx.lit1_inst]))
self.assert_simplified_union([fx.lit1, fx.lit2_inst], UnionType([fx.lit1, fx.lit2_inst]))
self.assert_simplified_union([fx.lit1, fx.lit3_inst], UnionType([fx.lit1, fx.lit3_inst]))

def assert_simplified_union(self, original: List[Type], union: Type) -> None:
assert_equal(make_simplified_union(original), union)
assert_equal(make_simplified_union(list(reversed(original))), union)

# Helpers

def tuple(self, *a: Type) -> TupleType:
Expand Down Expand Up @@ -723,9 +756,9 @@ def test_type_type(self) -> None:
def test_literal_type(self) -> None:
a = self.fx.a
d = self.fx.d
lit1 = LiteralType(1, a)
lit2 = LiteralType(2, a)
lit3 = LiteralType("foo", d)
lit1 = self.fx.lit1
lit2 = self.fx.lit2
lit3 = self.fx.lit3

self.assert_join(lit1, lit1, lit1)
self.assert_join(lit1, a, a)
Expand Down Expand Up @@ -951,10 +984,9 @@ def test_type_type(self) -> None:

def test_literal_type(self) -> None:
a = self.fx.a
d = self.fx.d
lit1 = LiteralType(1, a)
lit2 = LiteralType(2, a)
lit3 = LiteralType("foo", d)
lit1 = self.fx.lit1
lit2 = self.fx.lit2
lit3 = self.fx.lit3

self.assert_meet(lit1, lit1, lit1)
self.assert_meet(lit1, a, lit1)
Expand Down Expand Up @@ -1011,11 +1043,10 @@ def setUp(self) -> None:

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

lit1 = LiteralType(1, b)
lit2 = LiteralType(2, b)
lit3 = LiteralType("foo", d)
lit1 = self.fx.lit1
lit2 = self.fx.lit2
lit3 = self.fx.lit3

self.assert_same(lit1, lit1)
self.assert_same(UnionType([lit1, lit2]), UnionType([lit1, lit2]))
Expand Down
9 changes: 8 additions & 1 deletion mypy/test/typefixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

self.lit1 = LiteralType(1, self.a)
self.lit2 = LiteralType(2, self.a)
self.lit3 = LiteralType("foo", self.d)
self.lit1_inst = Instance(self.ai, [], last_known_value=self.lit1)
self.lit2_inst = Instance(self.ai, [], last_known_value=self.lit2)
self.lit3_inst = Instance(self.di, [], last_known_value=self.lit3)

self.type_a = TypeType.make_normalized(self.a)
self.type_b = TypeType.make_normalized(self.b)
self.type_c = TypeType.make_normalized(self.c)
Expand Down