1
1
from mypy .myunit import Suite , assert_true , run_test
2
- from mypy .nodes import CONTRAVARIANT
2
+ from mypy .nodes import CONTRAVARIANT , INVARIANT , COVARIANT
3
3
from mypy .subtypes import is_subtype
4
4
from mypy .typefixture import TypeFixture , InterfaceTypeFixture
5
5
6
6
7
7
class SubtypingSuite (Suite ):
8
8
def set_up (self ):
9
- self .fx = TypeFixture ()
9
+ self .fx = TypeFixture (INVARIANT )
10
10
self .fx_contra = TypeFixture (CONTRAVARIANT )
11
+ self .fx_co = TypeFixture (COVARIANT )
11
12
12
13
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 :
14
15
self .assert_subtype (simple , simple )
15
16
16
17
def test_instance_subtyping (self ):
@@ -21,27 +22,41 @@ def test_instance_subtyping(self):
21
22
self .assert_not_subtype (self .fx .a , self .fx .d )
22
23
self .assert_not_subtype (self .fx .b , self .fx .c )
23
24
24
- def test_simple_generic_instance_subtyping (self ):
25
+ def test_simple_generic_instance_subtyping_invariant (self ):
25
26
self .assert_subtype (self .fx .ga , self .fx .ga )
26
27
self .assert_subtype (self .fx .hab , self .fx .hab )
27
28
28
29
self .assert_not_subtype (self .fx .ga , self .fx .g2a )
29
30
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 )
31
32
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 ):
33
42
self .assert_subtype (self .fx_contra .ga , self .fx_contra .ga )
34
43
self .assert_subtype (self .fx_contra .hab , self .fx_contra .hab )
35
44
45
+ self .assert_not_subtype (self .fx_contra .ga , self .fx_contra .g2a )
36
46
self .assert_subtype (self .fx_contra .ga , self .fx_contra .gb )
37
47
self .assert_not_subtype (self .fx_contra .gb , self .fx_contra .ga )
38
48
39
- def test_generic_subtyping_with_inheritance (self ):
49
+ def test_generic_subtyping_with_inheritance_invariant (self ):
40
50
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 )
42
52
self .assert_not_subtype (self .fx .gsaa , self .fx .gb )
43
53
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 ):
45
60
self .assert_subtype (self .fx_contra .gsab , self .fx_contra .gb )
46
61
self .assert_not_subtype (self .fx_contra .gsab , self .fx_contra .ga )
47
62
self .assert_subtype (self .fx_contra .gsaa , self .fx_contra .gb )
0 commit comments