[3.13] gh-119933: Improve `SyntaxError` message for invalid type parameters expressions (GH-119976) (#120641)

(cherry picked from commit 4bf17c381f)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Jelle Zijlstra 2024-06-17 08:01:49 -07:00 committed by GitHub
parent 03b89e3a3d
commit 7c47f93dff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 277 additions and 55 deletions

View file

@ -2046,16 +2046,91 @@ Invalid expressions in type scopes:
...
SyntaxError: Type parameter list cannot be empty
>>> def f[T: (x:=3)](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar bound
>>> def f[T: ((x:= 3), int)](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar constraint
>>> def f[T = ((x:=3))](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar default
>>> async def f[T: (x:=3)](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar bound
>>> async def f[T: ((x:= 3), int)](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar constraint
>>> async def f[T = ((x:=3))](): pass
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar default
>>> type A[T: (x:=3)] = int
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar bound
>>> type A[T: ((x:= 3), int)] = int
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar constraint
>>> type A[T = ((x:=3))] = int
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar default
>>> def f[T: (yield)](): pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> def f[T: (int, (yield))](): pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar constraint
>>> def f[T = (yield)](): pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar default
>>> def f[*Ts = (yield)](): pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVarTuple default
>>> def f[**P = [(yield), int]](): pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a ParamSpec default
>>> type A[T: (yield 3)] = int
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> type A[T: (int, (yield 3))] = int
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar constraint
>>> type A[T = (yield 3)] = int
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar default
>>> type A[T: (await 3)] = int
Traceback (most recent call last):
...
@ -2066,6 +2141,31 @@ Invalid expressions in type scopes:
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> class A[T: (yield 3)]: pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> class A[T: (int, (yield 3))]: pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar constraint
>>> class A[T = (yield)]: pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar default
>>> class A[*Ts = (yield)]: pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVarTuple default
>>> class A[**P = [(yield), int]]: pass
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a ParamSpec default
>>> type A = (x := 3)
Traceback (most recent call last):
...