1
1
"""Test cases for mypy types and type operations."""
2
2
3
- from typing import List , Tuple
3
+ from typing import List , Tuple , cast
4
4
5
5
from mypy .test .helpers import Suite , assert_equal , assert_true , assert_false , assert_type , skip
6
6
from mypy .erasetype import erase_type
7
7
from mypy .expandtype import expand_type
8
8
from mypy .join import join_types , join_simple
9
9
from mypy .meet import meet_types
10
+ from mypy .sametypes import is_same_type
10
11
from mypy .types import (
11
12
UnboundType , AnyType , CallableType , TupleType , TypeVarDef , Type , Instance , NoneTyp , Overloaded ,
12
13
TypeType , UnionType , UninhabitedType , true_only , false_only , TypeVarId , TypeOfAny , LiteralType
13
14
)
14
15
from mypy .nodes import ARG_POS , ARG_OPT , ARG_STAR , ARG_STAR2 , CONTRAVARIANT , INVARIANT , COVARIANT
15
16
from mypy .subtypes import is_subtype , is_more_precise , is_proper_subtype
16
17
from mypy .test .typefixture import TypeFixture , InterfaceTypeFixture
18
+ from mypy .experiments import strict_optional_set
17
19
18
20
19
21
class TypesSuite (Suite ):
@@ -846,8 +848,31 @@ def test_type_type(self) -> None:
846
848
self .assert_meet (self .fx .type_type , self .fx .type_any , self .fx .type_any )
847
849
self .assert_meet (self .fx .type_b , self .fx .anyt , self .fx .type_b )
848
850
851
+ def test_literal_type (self ) -> None :
852
+ a = self .fx .a
853
+ d = self .fx .d
854
+ lit1 = LiteralType (1 , a )
855
+ lit2 = LiteralType (2 , a )
856
+ lit3 = LiteralType ("foo" , d )
857
+
858
+ self .assert_meet (lit1 , lit1 , lit1 )
859
+ self .assert_meet (lit1 , a , lit1 )
860
+ self .assert_meet_uninhabited (lit1 , lit3 )
861
+ self .assert_meet_uninhabited (lit1 , lit2 )
862
+ self .assert_meet (UnionType ([lit1 , lit2 ]), lit1 , lit1 )
863
+ self .assert_meet (UnionType ([lit1 , lit2 ]), UnionType ([lit2 , lit3 ]), lit2 )
864
+ self .assert_meet (UnionType ([lit1 , lit2 ]), UnionType ([lit1 , lit2 ]), UnionType ([lit1 , lit2 ]))
865
+ self .assert_meet (lit1 , self .fx .anyt , lit1 )
866
+ self .assert_meet (lit1 , self .fx .o , lit1 )
867
+
849
868
# FIX generic interfaces + ranges
850
869
870
+ def assert_meet_uninhabited (self , s : Type , t : Type ) -> None :
871
+ with strict_optional_set (False ):
872
+ self .assert_meet (s , t , self .fx .nonet )
873
+ with strict_optional_set (True ):
874
+ self .assert_meet (s , t , self .fx .uninhabited )
875
+
851
876
def assert_meet (self , s : Type , t : Type , meet : Type ) -> None :
852
877
self .assert_simple_meet (s , t , meet )
853
878
self .assert_simple_meet (t , s , meet )
@@ -874,3 +899,52 @@ def callable(self, *a: Type) -> CallableType:
874
899
return CallableType (list (a [:- 1 ]),
875
900
[ARG_POS ] * n , [None ] * n ,
876
901
a [- 1 ], self .fx .function )
902
+
903
+
904
+ class SameTypeSuite (Suite ):
905
+ def setUp (self ) -> None :
906
+ self .fx = TypeFixture ()
907
+
908
+ def test_literal_type (self ) -> None :
909
+ a = self .fx .a
910
+ b = self .fx .b # Reminder: b is a subclass of a
911
+ d = self .fx .d
912
+
913
+ # Literals are not allowed to contain floats, but we're going to
914
+ # test them anyways, just to make sure the semantics are robust
915
+ # against these kinds of things.
916
+ lit0 = LiteralType (cast (int , 1.0 ), a )
917
+ lit1 = LiteralType (1 , b )
918
+ lit2 = LiteralType (2 , b )
919
+ lit3 = LiteralType ("foo" , d )
920
+
921
+ self .assert_same (lit1 , lit1 )
922
+ self .assert_same (UnionType ([lit1 , lit2 ]), UnionType ([lit1 , lit2 ]))
923
+ self .assert_same (UnionType ([lit1 , lit2 ]), UnionType ([lit2 , lit1 ]))
924
+ self .assert_not_same (lit1 , b )
925
+ self .assert_not_same (lit0 , lit1 )
926
+ self .assert_not_same (lit1 , lit2 )
927
+ self .assert_not_same (lit1 , lit3 )
928
+
929
+ self .assert_not_same (lit1 , self .fx .anyt )
930
+ self .assert_not_same (lit1 , self .fx .nonet )
931
+
932
+ def assert_same (self , s : Type , t : Type , strict : bool = True ) -> None :
933
+ self .assert_simple_is_same (s , t , expected = True , strict = strict )
934
+ self .assert_simple_is_same (t , s , expected = True , strict = strict )
935
+
936
+ def assert_not_same (self , s : Type , t : Type , strict : bool = True ) -> None :
937
+ self .assert_simple_is_same (s , t , False , strict = strict )
938
+ self .assert_simple_is_same (t , s , False , strict = strict )
939
+
940
+ def assert_simple_is_same (self , s : Type , t : Type , expected : bool , strict : bool ) -> None :
941
+ actual = is_same_type (s , t )
942
+ assert_equal (actual , expected ,
943
+ 'is_same_type({}, {}) is {{}} ({{}} expected)' .format (s , t ))
944
+
945
+ if strict :
946
+ actual2 = (s == t )
947
+ assert_equal (actual2 , expected ,
948
+ '({} == {}) is {{}} ({{}} expected)' .format (s , t ))
949
+ assert_equal (hash (s ) == hash (t ), expected ,
950
+ '(hash({}) == hash({}) is {{}} ({{}} expected)' .format (s , t ))
0 commit comments