1
1
import sys
2
2
import pickle
3
3
import typing
4
+ from contextlib import contextmanager
5
+ from textwrap import dedent
4
6
from unittest import TestCase , main , skipUnless
5
7
from mypy_extensions import TypedDict , i64 , i32 , i16 , u8
6
8
@@ -25,27 +27,39 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None):
25
27
PY36 = sys .version_info [:2 ] >= (3 , 6 )
26
28
27
29
PY36_TESTS = """
28
- Label = TypedDict('Label', [('label', str)])
30
+ import warnings
29
31
30
- class Point2D(TypedDict):
31
- x: int
32
- y: int
32
+ with warnings.catch_warnings():
33
+ warnings.simplefilter("ignore", category=DeprecationWarning)
33
34
34
- class LabelPoint2D(Point2D, Label): ...
35
+ Label = TypedDict('Label', [('label', str)])
35
36
36
- class Options(TypedDict, total=False):
37
- log_level: int
38
- log_path: str
37
+ class Point2D(TypedDict):
38
+ x: int
39
+ y: int
40
+
41
+ class LabelPoint2D(Point2D, Label): ...
42
+
43
+ class Options(TypedDict, total=False):
44
+ log_level: int
45
+ log_path: str
39
46
"""
40
47
41
48
if PY36 :
42
49
exec (PY36_TESTS )
43
50
44
51
45
52
class TypedDictTests (BaseTestCase ):
53
+ @contextmanager
54
+ def assert_typeddict_deprecated (self ):
55
+ with self .assertWarnsRegex (
56
+ DeprecationWarning , "mypy_extensions.TypedDict is deprecated"
57
+ ):
58
+ yield
46
59
47
60
def test_basics_iterable_syntax (self ):
48
- Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
61
+ with self .assert_typeddict_deprecated ():
62
+ Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
49
63
self .assertIsSubclass (Emp , dict )
50
64
self .assertIsSubclass (Emp , typing .MutableMapping )
51
65
if sys .version_info [0 ] >= 3 :
@@ -62,7 +76,8 @@ def test_basics_iterable_syntax(self):
62
76
self .assertEqual (Emp .__total__ , True )
63
77
64
78
def test_basics_keywords_syntax (self ):
65
- Emp = TypedDict ('Emp' , name = str , id = int )
79
+ with self .assert_typeddict_deprecated ():
80
+ Emp = TypedDict ('Emp' , name = str , id = int )
66
81
self .assertIsSubclass (Emp , dict )
67
82
self .assertIsSubclass (Emp , typing .MutableMapping )
68
83
if sys .version_info [0 ] >= 3 :
@@ -79,7 +94,8 @@ def test_basics_keywords_syntax(self):
79
94
self .assertEqual (Emp .__total__ , True )
80
95
81
96
def test_typeddict_errors (self ):
82
- Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
97
+ with self .assert_typeddict_deprecated ():
98
+ Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
83
99
self .assertEqual (TypedDict .__module__ , 'mypy_extensions' )
84
100
jim = Emp (name = 'Jim' , id = 1 )
85
101
with self .assertRaises (TypeError ):
@@ -88,9 +104,9 @@ def test_typeddict_errors(self):
88
104
isinstance (jim , Emp ) # type: ignore
89
105
with self .assertRaises (TypeError ):
90
106
issubclass (dict , Emp ) # type: ignore
91
- with self .assertRaises (TypeError ):
107
+ with self .assertRaises (TypeError ), self . assert_typeddict_deprecated () :
92
108
TypedDict ('Hi' , x = ())
93
- with self .assertRaises (TypeError ):
109
+ with self .assertRaises (TypeError ), self . assert_typeddict_deprecated () :
94
110
TypedDict ('Hi' , [('x' , int ), ('y' , ())])
95
111
with self .assertRaises (TypeError ):
96
112
TypedDict ('Hi' , [('x' , int )], y = int )
@@ -109,9 +125,20 @@ def test_py36_class_syntax_usage(self):
109
125
other = LabelPoint2D (x = 0 , y = 1 , label = 'hi' ) # noqa
110
126
self .assertEqual (other ['label' ], 'hi' )
111
127
128
+ if PY36 :
129
+ exec (dedent (
130
+ """
131
+ def test_py36_class_usage_emits_deprecations(self):
132
+ with self.assert_typeddict_deprecated():
133
+ class Foo(TypedDict):
134
+ bar: int
135
+ """
136
+ ))
137
+
112
138
def test_pickle (self ):
113
139
global EmpD # pickle wants to reference the class by name
114
- EmpD = TypedDict ('EmpD' , name = str , id = int )
140
+ with self .assert_typeddict_deprecated ():
141
+ EmpD = TypedDict ('EmpD' , name = str , id = int )
115
142
jane = EmpD ({'name' : 'jane' , 'id' : 37 })
116
143
for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
117
144
z = pickle .dumps (jane , proto )
@@ -123,13 +150,15 @@ def test_pickle(self):
123
150
self .assertEqual (EmpDnew ({'name' : 'jane' , 'id' : 37 }), jane )
124
151
125
152
def test_optional (self ):
126
- EmpD = TypedDict ('EmpD' , name = str , id = int )
153
+ with self .assert_typeddict_deprecated ():
154
+ EmpD = TypedDict ('EmpD' , name = str , id = int )
127
155
128
156
self .assertEqual (typing .Optional [EmpD ], typing .Union [None , EmpD ])
129
157
self .assertNotEqual (typing .List [EmpD ], typing .Tuple [EmpD ])
130
158
131
159
def test_total (self ):
132
- D = TypedDict ('D' , {'x' : int }, total = False )
160
+ with self .assert_typeddict_deprecated ():
161
+ D = TypedDict ('D' , {'x' : int }, total = False )
133
162
self .assertEqual (D (), {})
134
163
self .assertEqual (D (x = 1 ), {'x' : 1 })
135
164
self .assertEqual (D .__total__ , False )
0 commit comments