1
- import sys
1
+ import collections . abc
2
2
import pickle
3
3
import typing
4
+ import warnings
4
5
from contextlib import contextmanager
5
- from textwrap import dedent
6
- from unittest import TestCase , main , skipUnless
6
+ from unittest import TestCase , main
7
7
from mypy_extensions import TypedDict , i64 , i32 , i16 , u8
8
8
9
9
@@ -24,11 +24,6 @@ def assertNotIsSubclass(self, cls, class_or_tuple, msg=None):
24
24
raise self .failureException (message )
25
25
26
26
27
- PY36 = sys .version_info [:2 ] >= (3 , 6 )
28
-
29
- PY36_TESTS = """
30
- import warnings
31
-
32
27
with warnings .catch_warnings ():
33
28
warnings .simplefilter ("ignore" , category = DeprecationWarning )
34
29
@@ -43,10 +38,6 @@ class LabelPoint2D(Point2D, Label): ...
43
38
class Options (TypedDict , total = False ):
44
39
log_level : int
45
40
log_path : str
46
- """
47
-
48
- if PY36 :
49
- exec (PY36_TESTS )
50
41
51
42
52
43
class TypedDictTests (BaseTestCase ):
@@ -62,9 +53,7 @@ def test_basics_iterable_syntax(self):
62
53
Emp = TypedDict ('Emp' , {'name' : str , 'id' : int })
63
54
self .assertIsSubclass (Emp , dict )
64
55
self .assertIsSubclass (Emp , typing .MutableMapping )
65
- if sys .version_info [0 ] >= 3 :
66
- import collections .abc
67
- self .assertNotIsSubclass (Emp , collections .abc .Sequence )
56
+ self .assertNotIsSubclass (Emp , collections .abc .Sequence )
68
57
jim = Emp (name = 'Jim' , id = 1 )
69
58
self .assertIs (type (jim ), dict )
70
59
self .assertEqual (jim ['name' ], 'Jim' )
@@ -80,9 +69,7 @@ def test_basics_keywords_syntax(self):
80
69
Emp = TypedDict ('Emp' , name = str , id = int )
81
70
self .assertIsSubclass (Emp , dict )
82
71
self .assertIsSubclass (Emp , typing .MutableMapping )
83
- if sys .version_info [0 ] >= 3 :
84
- import collections .abc
85
- self .assertNotIsSubclass (Emp , collections .abc .Sequence )
72
+ self .assertNotIsSubclass (Emp , collections .abc .Sequence )
86
73
jim = Emp (name = 'Jim' , id = 1 ) # type: ignore # mypy doesn't support keyword syntax yet
87
74
self .assertIs (type (jim ), dict )
88
75
self .assertEqual (jim ['name' ], 'Jim' )
@@ -111,7 +98,6 @@ def test_typeddict_errors(self):
111
98
with self .assertRaises (TypeError ):
112
99
TypedDict ('Hi' , [('x' , int )], y = int )
113
100
114
- @skipUnless (PY36 , 'Python 3.6 required' )
115
101
def test_py36_class_syntax_usage (self ):
116
102
self .assertEqual (LabelPoint2D .__name__ , 'LabelPoint2D' ) # noqa
117
103
self .assertEqual (LabelPoint2D .__module__ , __name__ ) # noqa
@@ -125,15 +111,10 @@ def test_py36_class_syntax_usage(self):
125
111
other = LabelPoint2D (x = 0 , y = 1 , label = 'hi' ) # noqa
126
112
self .assertEqual (other ['label' ], 'hi' )
127
113
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
- ))
114
+ def test_py36_class_usage_emits_deprecations (self ):
115
+ with self .assert_typeddict_deprecated ():
116
+ class Foo (TypedDict ):
117
+ bar : int
137
118
138
119
def test_pickle (self ):
139
120
global EmpD # pickle wants to reference the class by name
@@ -163,10 +144,9 @@ def test_total(self):
163
144
self .assertEqual (D (x = 1 ), {'x' : 1 })
164
145
self .assertEqual (D .__total__ , False )
165
146
166
- if PY36 :
167
- self .assertEqual (Options (), {}) # noqa
168
- self .assertEqual (Options (log_level = 2 ), {'log_level' : 2 }) # noqa
169
- self .assertEqual (Options .__total__ , False ) # noqa
147
+ self .assertEqual (Options (), {}) # noqa
148
+ self .assertEqual (Options (log_level = 2 ), {'log_level' : 2 }) # noqa
149
+ self .assertEqual (Options .__total__ , False ) # noqa
170
150
171
151
172
152
native_int_types = [i64 , i32 , i16 , u8 ]
0 commit comments