1
- import typing
2
-
3
1
from mypy .myunit import Suite , assert_true , run_test
2
+ from mypy .nodes import CONTRAVARIANT
4
3
from mypy .subtypes import is_subtype
5
4
from mypy .typefixture import TypeFixture , InterfaceTypeFixture
6
5
7
6
8
7
class SubtypingSuite (Suite ):
9
8
def set_up (self ):
10
9
self .fx = TypeFixture ()
10
+ self .fx_contra = TypeFixture (CONTRAVARIANT )
11
11
12
12
def test_trivial_cases (self ):
13
13
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):
29
29
self .assert_not_subtype (self .fx .ga , self .fx .gb )
30
30
self .assert_subtype (self .fx .gb , self .fx .ga )
31
31
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
+
32
39
def test_generic_subtyping_with_inheritance (self ):
33
40
self .assert_subtype (self .fx .gsab , self .fx .gb )
34
41
self .assert_subtype (self .fx .gsab , self .fx .ga )
35
42
self .assert_not_subtype (self .fx .gsaa , self .fx .gb )
36
43
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
+
37
49
def test_interface_subtyping (self ):
38
50
self .assert_subtype (self .fx .e , self .fx .f )
39
51
self .assert_equivalent (self .fx .f , self .fx .f )
0 commit comments