gh-104572: Improve error messages for invalid constructs in PEP 695 contexts (#104573)

This commit is contained in:
Jelle Zijlstra 2023-05-17 06:05:42 -07:00 committed by GitHub
parent 0cb2fdc621
commit 97db2f3e07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 4 deletions

View file

@ -1877,6 +1877,68 @@ Invalid bytes literals:
^^^^^^^^^^^ ^^^^^^^^^^^
SyntaxError: bytes can only contain ASCII literal characters SyntaxError: bytes can only contain ASCII literal characters
Invalid expressions in type scopes:
>>> type A[T: (x:=3)] = int
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a TypeVar bound
>>> type A[T: (yield 3)] = int
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> type A[T: (await 3)] = int
Traceback (most recent call last):
...
SyntaxError: await expression cannot be used within a TypeVar bound
>>> type A[T: (yield from [])] = int
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a TypeVar bound
>>> type A = (x := 3)
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within a type alias
>>> type A = (yield 3)
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a type alias
>>> type A = (await 3)
Traceback (most recent call last):
...
SyntaxError: await expression cannot be used within a type alias
>>> type A = (yield from [])
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within a type alias
>>> class A[T]((x := 3)): ...
Traceback (most recent call last):
...
SyntaxError: named expression cannot be used within the definition of a generic
>>> class A[T]((yield 3)): ...
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within the definition of a generic
>>> class A[T]((await 3)): ...
Traceback (most recent call last):
...
SyntaxError: await expression cannot be used within the definition of a generic
>>> class A[T]((yield from [])): ...
Traceback (most recent call last):
...
SyntaxError: yield expression cannot be used within the definition of a generic
""" """
import re import re

View file

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

View file

@ -45,16 +45,16 @@
"assignment expression cannot be used in a comprehension iterable expression" "assignment expression cannot be used in a comprehension iterable expression"
#define ANNOTATION_NOT_ALLOWED \ #define ANNOTATION_NOT_ALLOWED \
"'%s' can not be used within an annotation" "%s cannot be used within an annotation"
#define TYPEVAR_BOUND_NOT_ALLOWED \ #define TYPEVAR_BOUND_NOT_ALLOWED \
"'%s' can not be used within a TypeVar bound" "%s cannot be used within a TypeVar bound"
#define TYPEALIAS_NOT_ALLOWED \ #define TYPEALIAS_NOT_ALLOWED \
"'%s' can not be used within a type alias" "%s cannot be used within a type alias"
#define TYPEPARAM_NOT_ALLOWED \ #define TYPEPARAM_NOT_ALLOWED \
"'%s' can not be used within the definition of a generic" "%s cannot be used within the definition of a generic"
#define DUPLICATE_TYPE_PARAM \ #define DUPLICATE_TYPE_PARAM \
"duplicate type parameter '%U'" "duplicate type parameter '%U'"