Skip to content

Commit e439d3a

Browse files
committed
Added subtype tests for contravariant types.
1 parent 623a2ac commit e439d3a

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

mypy/test/testsubtypes.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import typing
2-
31
from mypy.myunit import Suite, assert_true, run_test
2+
from mypy.nodes import CONTRAVARIANT
43
from mypy.subtypes import is_subtype
54
from mypy.typefixture import TypeFixture, InterfaceTypeFixture
65

76

87
class SubtypingSuite(Suite):
98
def set_up(self):
109
self.fx = TypeFixture()
10+
self.fx_contra = TypeFixture(CONTRAVARIANT)
1111

1212
def test_trivial_cases(self):
1313
for simple in self.fx.void, self.fx.a, self.fx.o, self.fx.b:
@@ -29,11 +29,23 @@ def test_simple_generic_instance_subtyping(self):
2929
self.assert_not_subtype(self.fx.ga, self.fx.gb)
3030
self.assert_subtype(self.fx.gb, self.fx.ga)
3131

32+
def test_simple_generic_instance_subtyping_contra_variant(self):
33+
self.assert_subtype(self.fx_contra.ga, self.fx_contra.ga)
34+
self.assert_subtype(self.fx_contra.hab, self.fx_contra.hab)
35+
36+
self.assert_subtype(self.fx_contra.ga, self.fx_contra.gb)
37+
self.assert_not_subtype(self.fx_contra.gb, self.fx_contra.ga)
38+
3239
def test_generic_subtyping_with_inheritance(self):
3340
self.assert_subtype(self.fx.gsab, self.fx.gb)
3441
self.assert_subtype(self.fx.gsab, self.fx.ga)
3542
self.assert_not_subtype(self.fx.gsaa, self.fx.gb)
3643

44+
def test_generic_subtyping_with_inheritance_contra_variant(self):
45+
self.assert_subtype(self.fx_contra.gsab, self.fx_contra.gb)
46+
self.assert_not_subtype(self.fx_contra.gsab, self.fx_contra.ga)
47+
self.assert_subtype(self.fx_contra.gsaa, self.fx_contra.gb)
48+
3749
def test_interface_subtyping(self):
3850
self.assert_subtype(self.fx.e, self.fx.f)
3951
self.assert_equivalent(self.fx.f, self.fx.f)

0 commit comments

Comments
 (0)