From 9daa1aff38b0ad1148804d63ca8dabeba9a1c05e Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 16 May 2023 19:09:25 -0700 Subject: [PATCH 1/2] gh-104572: Improve error messages for invalid constructs in PEP 695 contexts --- Lib/test/test_syntax.py | 62 +++++++++++++++++++++++++++++++++++++++++ Python/symtable.c | 8 +++--- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index f959bbb4400702..477879db2fd493 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -1877,6 +1877,68 @@ def f(x: *b) ^^^^^^^^^^^ 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/Python/symtable.c b/Python/symtable.c index 3451f6c7bffb6d..f896f7cbe3382b 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -45,16 +45,16 @@ "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'" From 6a3e37cf587938979973e8e8e8f1266709498171 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Tue, 16 May 2023 19:17:52 -0700 Subject: [PATCH 2/2] blurb --- .../2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst 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 index 00000000000000..25bad8aa6e3e6f --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-16-19-17-48.gh-issue-104572.eBZQYS.rst @@ -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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy