Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f580583

Browse files
committed
use check_syntax_error
1 parent c3d6464 commit f580583

2 files changed

Lines changed: 25 additions & 49 deletions

File tree

Lib/test/test_type_aliases.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import types
22
import unittest
3+
from test.support import check_syntax_error
34

4-
from typing import Callable, TypeAliasType
5+
from typing import Callable, TypeAliasType, get_args, get_origin
56

67
from .test_type_params import run_code
78

89

910
class TypeParamsInvalidTest(unittest.TestCase):
1011
def test_name_collision_01(self):
11-
with self.assertRaisesRegex(SyntaxError, "duplicate type parameter 'A'"):
12-
run_code("""type TA1[A, **A] = None""")
12+
check_syntax_error(self, """type TA1[A, **A] = None""", "duplicate type parameter 'A'")
1313

1414
def test_name_non_collision_02(self):
1515
run_code("""type TA1[A] = lambda A: None""")
@@ -24,10 +24,10 @@ class Outer[A]:
2424

2525
class TypeParamsAccessTest(unittest.TestCase):
2626
def test_alias_access_01(self):
27-
run_code("""\
28-
type TA1[A, B] = dict[A, B]
29-
"""
30-
)
27+
ns = run_code("type TA1[A, B] = dict[A, B]")
28+
alias = ns["TA1"]
29+
self.assertIsInstance(alias, TypeAliasType)
30+
self.assertEqual(alias.__type_params__, get_args(alias.__value__))
3131

3232
def test_alias_access_02(self):
3333
run_code("""\

Lib/test/test_type_params.py

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import textwrap
33
import unittest
4-
from test.support import requires_working_socket
4+
from test.support import requires_working_socket, check_syntax_error
55

66
from typing import Any, Sequence, TypeVar, TypeVarTuple, ParamSpec
77

@@ -14,8 +14,7 @@ def run_code(code: str) -> dict[str, Any]:
1414

1515
class 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

122110
class 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

467453
class 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

485466
class 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

Comments
 (0)