]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104572: Improve error messages for invalid constructs in PEP 695 contexts (#104573)
authorJelle Zijlstra <jelle.zijlstra@gmail.com>
Wed, 17 May 2023 13:05:42 +0000 (06:05 -0700)
committerGitHub <noreply@github.com>
Wed, 17 May 2023 13:05:42 +0000 (06:05 -0700)
Lib/test/test_syntax.py
Misc/NEWS.d/next/Core and Builtins/2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst [new file with mode: 0644]
Python/symtable.c

index f959bbb4400702627f81105f22660fb9e73344fa..477879db2fd493af821166078a1775fa0abdb90e 100644 (file)
@@ -1877,6 +1877,68 @@ Invalid bytes literals:
         ^^^^^^^^^^^
    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
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst
new file mode 100644 (file)
index 0000000..25bad8a
--- /dev/null
@@ -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.
index 3451f6c7bffb6d086d24c5560e4b233567e86ec6..f896f7cbe3382b8f990e5f74cc8c9b2b85e84507 100644 (file)
 "assignment expression cannot be used in a comprehension iterable expression"
 
 #define ANNOTATION_NOT_ALLOWED \
-"'%s' can not be used within an annotation"
+"%s cannot be used within an annotation"
 
 #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 \
-"'%s' can not be used within a type alias"
+"%s cannot be used within a type alias"
 
 #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 \
 "duplicate type parameter '%U'"