8000 Improve some tests · python/cpython@521bd81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 521bd81

Browse files
committed
Improve some tests
1 parent 04bc911 commit 521bd81

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

Lib/test/test_type_params.py

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class TypeParamsInvalidTest(unittest.TestCase):
88
def test_name_collision_01(self):
99
10000 code = """def func[**A, A](): ..."""
1010
with self.assertRaisesRegex(SyntaxError, "duplicate type parameter 'A'"):
11-
exec(code, {}, {})
11+
exec(code, {})
1212

1313
def test_name_non_collision_02(self):
1414
code = """def func[A](A): ..."""
15-
exec(code, {}, {})
15+
exec(code, {})
1616

1717
def test_name_non_collision_03(self):
1818
code = """def func[A](*A): ..."""
19-
exec(code, {}, {})
19+
exec(code, {})
2020

2121
def test_name_non_collision_04(self):
2222
# Mangled names should not cause a conflict.
@@ -25,23 +25,23 @@ class ClassA:
2525
def func[__A](self, __A): ...
2626
"""
2727
)
28-
exec(code, {}, {})
28+
exec(code, {})
2929

3030
def test_name_non_collision_05(self):
3131
code = textwrap.dedent("""\
3232
class ClassA:
3333
def func[_ClassA__A](self, __A): ...
3434
"""
3535
)
36-
exec(code, {}, {})
36+
exec(code, {})
3737

3838
def test_name_non_collision_06(self):
3939
code = textwrap.dedent("""\
4040
class ClassA[X]:
4141
def func(self, X): ...
4242
"""
4343
)
44-
exec(code, {}, {})
44+
exec(code, {})
4545

4646
def test_name_non_collision_07(self):
4747
code = textwrap.dedent("""\
@@ -50,7 +50,7 @@ def func(self):
5050
X = 1
5151
"""
5252
)
53-
exec(code, {}, {})
53+
exec(code, {})
5454

5555
def test_name_non_collision_08(self):
5656
code = textwrap.dedent("""\
@@ -59,7 +59,7 @@ def func(self):
5959
a = [X for X in []]
6060
"""
6161
)
62-
exec(code, {}, {})
62+
exec(code, {})
6363

6464
def test_name_non_collision_9(self):
6565
code = textwrap.dedent("""\
@@ -68,15 +68,15 @@ def func[X](self):
6868
...
6969
"""
7070
)
71-
exec(code, {}, {})
71+
exec(code, {})
7272

7373
def test_name_non_collision_10(self):
7474
code = textwrap.dedent("""\
7575
class ClassA[X]:
7676
X: int
7777
"""
7878
)
79-
exec(code, {}, {})
79+
exec(code, {})
8080

8181
def test_name_non_collision_11(self):
8282
code = textwrap.dedent("""\
@@ -86,7 +86,7 @@ def inner[X]():
8686
nonlocal X
8787
"""
8888
)
89-
exec(code, {}, {})
89+
exec(code, {})
9090

9191
def test_name_non_collision_13(self):
9292
code = textwrap.dedent("""\
@@ -96,7 +96,7 @@ def inner[X]():
9696
global X
9797
"""
9898
)
99-
exec(code, {}, {})
99+
exec(code, {})
100100

101101

102102
class TypeParamsAccessTest(unittest.TestCase):
@@ -106,7 +106,7 @@ class ClassA[A, B](dict[A, B]):
106106
...
107107
"""
108108
)
109-
exec(code, {}, {})
109+
exec(code, {})
110110

111111
def test_class_access_02(self):
112112
code = textwrap.dedent("""\
@@ -115,7 +115,7 @@ class ClassA[A, B](metaclass=MyMeta[A, B]):
115115
...
116116
"""
117117
)
118-
exec(code, {}, {})
118+
exec(code, {})
119119

120120
def test_class_access_03(self):
121121
code = textwrap.dedent("""\
@@ -128,15 +128,15 @@ class ClassA[A, B]():
128128
)
129129

130130
with self.assertRaisesRegex(NameError, "name 'A' is not defined"):
131-
exec(code, {}, {})
131+
exec(code, {})
132132

133133
def test_function_access_01(self):
134134
code = textwrap.dedent("""\
135135
def func[A, B](a: dict[A, B]):
136136
...
137137
"""
138138
)
139-
exec(code, {}, {})
139+
exec(code, {})
140140

141141
def test_function_access_02(self):
142142
code = textwrap.dedent("""\
@@ -146,7 +146,7 @@ def func[A](a = list[A]()):
146146
)
147147

148148
with self.assertRaisesRegex(NameError, "name 'A' is not defined"):
149-
exec(code, {}, {})
149+
exec(code, {})
150150

151151
def test_function_access_03(self):
152152
code = textwrap.dedent("""\
@@ -159,7 +159,7 @@ def func[A]():
159159
)
160160

161161
with self.assertRaisesRegex(NameError, "name 'A' is not defined"):
162-
exec(code, {}, {})
162+
exec(code, {})
163163

164164
def test_nested_access_01(self):
165165
code = textwrap.dedent("""\
@@ -170,7 +170,7 @@ def funcD[D](self):
170170
lambda : (A, B, C, D)
171171
"""
172172
)
173-
exec(code, {}, {})
173+
exec(code, {})
174174

175175
def test_out_of_scope_01(self):
176176
code = textwrap.dedent("""\
@@ -180,7 +180,7 @@ class ClassA[T]: ...
180180
)
181181

182182
with self.assertRaisesRegex(NameError, "name 'T' is not defined"):
183-
exec(code, {}, {})
183+
exec(code, {})
184184

185185
def test_out_of_scope_02(self):
186186
code = textwrap.dedent("""\
@@ -192,7 +192,7 @@ def funcB[B](self): ...
192192
)
193193

194194
with self.assertRaisesRegex(NameError, "name 'B' is not defined"):
195-
exec(code, {}, {})
195+
exec(code, {})
196196

197197

198198
class TypeParamsTraditionalTypeVars(unittest.TestCase):
@@ -204,7 +204,7 @@ class ClassA[T](Generic[T]): ...
204204
)
205205

206206
with self.assertRaisesRegex(TypeError, r"Cannot inherit from Generic\[...\] multiple types."):
207-
exec(code, {}, {})
207+
exec(code, {})
208208

209209
def test_traditional_02(self):
210210
code = textwrap.dedent("""\
@@ -215,20 +215,15 @@ class ClassA[T](dict[T, S]): ...
215215
)
216216

217217
with self.assertRaisesRegex(TypeError, r"Some type variables \(~S\) are not listed in Generic\[T\]"):
218-
exec(code, {}, {})
218+
exec(code, {})
219219

220220
def test_traditional_03(self):
221-
code = textwrap.dedent("""\
222-
from typing import TypeVar
223-
S = TypeVar("S")
224-
def func[T](a: T, b: S) -> T | S:
225-
return a
226-
"""
227-
)
228-
229221
# This does not generate a runtime error, but it should be
230222
# flagged as an error by type checkers.
231-
exec(code, {}, {})
223+
from typing import TypeVar
224+
S = TypeVar("S")
225+
def func[T](a: T, b: S) -> T | S:
226+
return a
232227

233228

234229
class TypeParamsTypeVarTest(unittest.TestCase):
@@ -304,7 +299,7 @@ def func1[*A: str]():
304299
)
305300

306301
with self.assertRaisesRegex(SyntaxError, r"expected '\('"):
307-
exec(code, {}, {})
302+
exec(code, {})
308303

309304
def test_typevartuple_02(self):
310305
def func1[*A]():
@@ -323,7 +318,7 @@ def func1[**A: str]():
323318
)
324319

325320
with self.assertRaisesRegex(SyntaxError, r"expected '\('"):
326-
exec(code, {}, {})
321+
exec(code, {})
327322

328323
def test_paramspec_02(self):
329324
def func1[**A]():
@@ -367,7 +362,7 @@ class ClassA[A]():
367362
)
368363

369364
with self.assertRaisesRegex(At 7452 tributeError, "attribute '__type_variables__' of 'type' objects is not writable"):
370-
exec(code, {}, {})
365+
exec(code, {})
371366

372367
def test_typeparams_dunder_function_01(self):
373368
def outer[A, B]():
@@ -396,4 +391,4 @@ def func[A]():
396391
)
397392

398393
with self.assertRaisesRegex(AttributeError, "attribute '__type_variables__' of 'function' objects is not writable"):
399-
exec(code, {}, {})
394+
exec(code, {})

0 commit comments

Comments
 (0)
0