Skip to content

Commit 97db2f3

Browse files
gh-104572: Improve error messages for invalid constructs in PEP 695 contexts (#104573)
1 parent 0cb2fdc commit 97db2f3

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

Lib/test/test_syntax.py

+62
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,68 @@ def f(x: *b)
18771877
^^^^^^^^^^^
18781878
SyntaxError: bytes can only contain ASCII literal characters
18791879
1880+
Invalid expressions in type scopes:
1881+
1882+
>>> type A[T: (x:=3)] = int
1883+
Traceback (most recent call last):
1884+
...
1885+
SyntaxError: named expression cannot be used within a TypeVar bound
1886+
1887+
>>> type A[T: (yield 3)] = int
1888+
Traceback (most recent call last):
1889+
...
1890+
SyntaxError: yield expression cannot be used within a TypeVar bound
1891+
1892+
>>> type A[T: (await 3)] = int
1893+
Traceback (most recent call last):
1894+
...
1895+
SyntaxError: await expression cannot be used within a TypeVar bound
1896+
1897+
>>> type A[T: (yield from [])] = int
1898+
Traceback (most recent call last):
1899+
...
1900+
SyntaxError: yield expression cannot be used within a TypeVar bound
1901+
1902+
>>> type A = (x := 3)
1903+
Traceback (most recent call last):
1904+
...
1905+
SyntaxError: named expression cannot be used within a type alias
1906+
1907+
>>> type A = (yield 3)
1908+
Traceback (most recent call last):
1909+
...
1910+
SyntaxError: yield expression cannot be used within a type alias
1911+
1912+
>>> type A = (await 3)
1913+
Traceback (most recent call last):
1914+
...
1915+
SyntaxError: await expression cannot be used within a type alias
1916+
1917+
>>> type A = (yield from [])
1918+
Traceback (most recent call last):
1919+
...
1920+
SyntaxError: yield expression cannot be used within a type alias
1921+
1922+
>>> class A[T]((x := 3)): ...
1923+
Traceback (most recent call last):
1924+
...
1925+
SyntaxError: named expression cannot be used within the definition of a generic
1926+
1927+
>>> class A[T]((yield 3)): ...
1928+
Traceback (most recent call last):
1929+
...
1930+
SyntaxError: yield expression cannot be used within the definition of a generic
1931+
1932+
>>> class A[T]((await 3)): ...
1933+
Traceback (most recent call last):
1934+
...
1935+
SyntaxError: await expression cannot be used within the definition of a generic
1936+
1937+
>>> class A[T]((yield from [])): ...
1938+
Traceback (most recent call last):
1939+
...
1940+
SyntaxError: yield expression cannot be used within the definition of a generic
1941+
18801942
"""
18811943

18821944
import re
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve syntax error message for invalid constructs in :pep:`695` contexts
2+
and in annotations when ``from __future__ import annotations`` is active.

Python/symtable.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@
4545
"assignment expression cannot be used in a comprehension iterable expression"
4646

4747
#define ANNOTATION_NOT_ALLOWED \
48-
"'%s' can not be used within an annotation"
48+
"%s cannot be used within an annotation"
4949

5050
#define TYPEVAR_BOUND_NOT_ALLOWED \
51-
"'%s' can not be used within a TypeVar bound"
51+
"%s cannot be used within a TypeVar bound"
5252

5353
#define TYPEALIAS_NOT_ALLOWED \
54-
"'%s' can not be used within a type alias"
54+
"%s cannot be used within a type alias"
5555

5656
#define TYPEPARAM_NOT_ALLOWED \
57-
"'%s' can not be used within the definition of a generic"
57+
"%s cannot be used within the definition of a generic"
5858

5959
#define DUPLICATE_TYPE_PARAM \
6060
"duplicate type parameter '%U'"

0 commit comments

Comments
 (0)