11import asyncio
22import textwrap
33import unittest
4- from test .support import requires_working_socket
4+ from test .support import requires_working_socket , check_syntax_error
55
66from typing import Any , Sequence , TypeVar , TypeVarTuple , ParamSpec
77
@@ -14,8 +14,7 @@ def run_code(code: str) -> dict[str, Any]:
1414
1515class TypeParamsInvalidTest (unittest .TestCase ):
1616 def test_name_collision_01 (self ):
17- with self .assertRaisesRegex (SyntaxError , "duplicate type parameter 'A'" ):
18- run_code ("""def func[**A, A](): ...""" )
17+ check_syntax_error (self , """def func[**A, A](): ...""" )
1918
2019 def test_name_non_collision_02 (self ):
2120 run_code ("""def func[A](A): ...""" )
@@ -95,28 +94,17 @@ def inner[X]():
9594 )
9695
9796 def test_disallowed_expressions (self ):
98- with self .assertRaises (SyntaxError ):
99- run_code ("type X = (yield)" )
100- with self .assertRaises (SyntaxError ):
101- run_code ("type X = (yield from x)" )
102- with self .assertRaises (SyntaxError ):
103- run_code ("type X = (await 42)" )
104- with self .assertRaises (SyntaxError ):
105- run_code ("async def f(): type X = (yield)" )
106- with self .assertRaises (SyntaxError ):
107- run_code ("type X = (y := 3)" )
108- with self .assertRaises (SyntaxError ):
109- run_code ("class X[T: (yield)]: pass" )
110- with self .assertRaises (SyntaxError ):
111- run_code ("class X[T: (yield from x)]: pass" )
112- with self .assertRaises (SyntaxError ):
113- run_code ("class X[T: (await 42)]: pass" )
114- with self .assertRaises (SyntaxError ):
115- run_code ("class X[T: (y := 3)]: pass" )
116- with self .assertRaises (SyntaxError ):
117- run_code ("class X[T](y := Sequence[T]): pass" )
118- with self .assertRaises (SyntaxError ):
119- run_code ("def f[T](y: (x := Sequence[T])): pass" )
97+ check_syntax_error (self , "type X = (yield)" )
98+ check_syntax_error (self , "type X = (yield from x)" )
99+ check_syntax_error (self , "type X = (await 42)" )
100+ check_syntax_error (self , "async def f(): type X = (yield)" )
101+ check_syntax_error (self , "type X = (y := 3)" )
102+ check_syntax_error (self , "class X[T: (yield)]: pass" )
103+ check_syntax_error (self , "class X[T: (yield from x)]: pass" )
104+ check_syntax_error (self , "class X[T: (await 42)]: pass" )
105+ check_syntax_error (self , "class X[T: (y := 3)]: pass" )
106+ check_syntax_error (self , "class X[T](y := Sequence[T]): pass" )
107+ check_syntax_error (self , "def f[T](y: (x := Sequence[T])): pass" )
120108
121109
122110class TypeParamsAccessTest (unittest .TestCase ):
@@ -247,12 +235,10 @@ class C[T]:
247235 def test_nonlocal (self ):
248236 code = """\
249237 def outer2[T]():
250-
251238 def inner1():
252239 nonlocal T
253240 """
254- with self .assertRaisesRegex (SyntaxError , "nonlocal binding not allowed for type parameter 'T'" ):
255- run_code (code )
241+ check_syntax_error (self , textwrap .dedent (code ))
256242
257243 def test_reference_previous_typevar (self ):
258244 def func [S , T : Sequence [S ]]():
@@ -466,13 +452,8 @@ async def coroutine[B]():
466452
467453class TypeParamsTypeVarTupleTest (unittest .TestCase ):
468454 def test_typevartuple_01 (self ):
469- code = """\
470- def func1[*A: str]():
471- return (A, B, C)
472- """
473-
474- with self .assertRaisesRegex (SyntaxError , r"expected '\('" ):
475- run_code (code )
455+ code = """def func1[*A: str](): return (A, B, C)"""
456+ check_syntax_error (self , code , r"expected '\('" )
476457
477458 def test_typevartuple_02 (self ):
478459 def func1 [* A ]():
@@ -484,13 +465,8 @@ def func1[*A]():
484465
485466class TypeParamsTypeVarParamSpec (unittest .TestCase ):
486467 def test_paramspec_01 (self ):
487- code = """\
488- def func1[**A: str]():
489- return (A, B, C)
490- """
491-
492- with self .assertRaisesRegex (SyntaxError , r"expected '\('" ):
493- run_code (code )
468+ code = """def func1[**A: str](): return (A, B, C)"""
469+ check_syntax_error (self , code , r"expected '\('" )
494470
495471 def test_paramspec_02 (self ):
496472 def func1 [** A ]():
0 commit comments