Skip to content

Commit 7376f90

Browse files
committed
Added subtype tests for invariant types.
1 parent e439d3a commit 7376f90

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

mypy/test/testsubtypes.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from mypy.myunit import Suite, assert_true, run_test
2-
from mypy.nodes import CONTRAVARIANT
2+
from mypy.nodes import CONTRAVARIANT, INVARIANT, COVARIANT
33
from mypy.subtypes import is_subtype
44
from mypy.typefixture import TypeFixture, InterfaceTypeFixture
55

66

77
class SubtypingSuite(Suite):
88
def set_up(self):
9-
self.fx = TypeFixture()
9+
self.fx = TypeFixture(INVARIANT)
1010
self.fx_contra = TypeFixture(CONTRAVARIANT)
11+
self.fx_co = TypeFixture(COVARIANT)
1112

1213
def test_trivial_cases(self):
13-
for simple in self.fx.void, self.fx.a, self.fx.o, self.fx.b:
14+
for simple in self.fx_co.void, self.fx_co.a, self.fx_co.o, self.fx_co.b:
1415
self.assert_subtype(simple, simple)
1516

1617
def test_instance_subtyping(self):
@@ -21,27 +22,41 @@ def test_instance_subtyping(self):
2122
self.assert_not_subtype(self.fx.a, self.fx.d)
2223
self.assert_not_subtype(self.fx.b, self.fx.c)
2324

24-
def test_simple_generic_instance_subtyping(self):
25+
def test_simple_generic_instance_subtyping_invariant(self):
2526
self.assert_subtype(self.fx.ga, self.fx.ga)
2627
self.assert_subtype(self.fx.hab, self.fx.hab)
2728

2829
self.assert_not_subtype(self.fx.ga, self.fx.g2a)
2930
self.assert_not_subtype(self.fx.ga, self.fx.gb)
30-
self.assert_subtype(self.fx.gb, self.fx.ga)
31+
self.assert_not_subtype(self.fx.gb, self.fx.ga)
3132

32-
def test_simple_generic_instance_subtyping_contra_variant(self):
33+
def test_simple_generic_instance_subtyping_covariant(self):
34+
self.assert_subtype(self.fx_co.ga, self.fx_co.ga)
35+
self.assert_subtype(self.fx_co.hab, self.fx_co.hab)
36+
37+
self.assert_not_subtype(self.fx_co.ga, self.fx_co.g2a)
38+
self.assert_not_subtype(self.fx_co.ga, self.fx_co.gb)
39+
self.assert_subtype(self.fx_co.gb, self.fx_co.ga)
40+
41+
def test_simple_generic_instance_subtyping_contravariant(self):
3342
self.assert_subtype(self.fx_contra.ga, self.fx_contra.ga)
3443
self.assert_subtype(self.fx_contra.hab, self.fx_contra.hab)
3544

45+
self.assert_not_subtype(self.fx_contra.ga, self.fx_contra.g2a)
3646
self.assert_subtype(self.fx_contra.ga, self.fx_contra.gb)
3747
self.assert_not_subtype(self.fx_contra.gb, self.fx_contra.ga)
3848

39-
def test_generic_subtyping_with_inheritance(self):
49+
def test_generic_subtyping_with_inheritance_invariant(self):
4050
self.assert_subtype(self.fx.gsab, self.fx.gb)
41-
self.assert_subtype(self.fx.gsab, self.fx.ga)
51+
self.assert_not_subtype(self.fx.gsab, self.fx.ga)
4252
self.assert_not_subtype(self.fx.gsaa, self.fx.gb)
4353

44-
def test_generic_subtyping_with_inheritance_contra_variant(self):
54+
def test_generic_subtyping_with_inheritance_covariant(self):
55+
self.assert_subtype(self.fx_co.gsab, self.fx_co.gb)
56+
self.assert_subtype(self.fx_co.gsab, self.fx_co.ga)
57+
self.assert_not_subtype(self.fx_co.gsaa, self.fx_co.gb)
58+
59+
def test_generic_subtyping_with_inheritance_contravariant(self):
4560
self.assert_subtype(self.fx_contra.gsab, self.fx_contra.gb)
4661
self.assert_not_subtype(self.fx_contra.gsab, self.fx_contra.ga)
4762
self.assert_subtype(self.fx_contra.gsaa, self.fx_contra.gb)

0 commit comments

Comments
 (0)