]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-47129: Add more informative messages to f-string syntax errors (32127)
authorMaciej Górski <36813763+macgors@users.noreply.github.com>
Mon, 28 Mar 2022 21:08:36 +0000 (23:08 +0200)
committerGitHub <noreply@github.com>
Mon, 28 Mar 2022 21:08:36 +0000 (17:08 -0400)
* Add more informative messages to f-string syntax errors

* 📜🤖 Added by blurb_it.

* Fix whitespaces

* Change error message

* Remove the 'else' statement (as sugested in review)

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Lib/test/test_fstring.py
Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst [new file with mode: 0644]
Parser/string_parser.c

index 0c255c2af22179bc50711a72ebc987c1a013fcb8..0c3372f0335517f90ade734b4dd73ffd5f9d4e85 100644 (file)
@@ -628,16 +628,27 @@ x = (
                             ["f'{}'",
                              "f'{ }'"
                              "f' {} '",
-                             "f'{!r}'",
-                             "f'{ !r}'",
                              "f'{10:{ }}'",
                              "f' { } '",
 
                              # The Python parser ignores also the following
                              # whitespace characters in additional to a space.
                              "f'''{\t\f\r\n}'''",
+                             ])
+
+        # Different error messeges are raised when a specfier ('!', ':' or '=') is used after an empty expression
+        self.assertAllRaise(SyntaxError, "f-string: expression required before '!'",
+                            ["f'{!r}'",
+                             "f'{ !r}'",
+                             "f'{!}'",
+                             "f'''{\t\f\r\n!a}'''",
+
+                             # Catch empty expression before the
+                             #  missing closing brace.
+                             "f'{!'",
+                             "f'{!s:'",
 
-                             # Catch the empty expression before the
+                             # Catch empty expression before the
                              #  invalid conversion.
                              "f'{!x}'",
                              "f'{ !xr}'",
@@ -645,16 +656,23 @@ x = (
                              "f'{!x:a}'",
                              "f'{ !xr:}'",
                              "f'{ !xr:a}'",
+                             ])
 
-                             "f'{!}'",
-                             "f'{:}'",
-
-                             # We find the empty expression before the
-                             #  missing closing brace.
-                             "f'{!'",
-                             "f'{!s:'",
+        self.assertAllRaise(SyntaxError, "f-string: expression required before ':'",
+                            ["f'{:}'",
+                             "f'{ :!}'",
+                             "f'{:2}'",
+                             "f'''{\t\f\r\n:a}'''",
                              "f'{:'",
-                             "f'{:x'",
+                             ])
+
+        self.assertAllRaise(SyntaxError, "f-string: expression required before '='",
+                            ["f'{=}'",
+                             "f'{ =}'",
+                             "f'{ =:}'",
+                             "f'{   =!}'",
+                             "f'''{\t\f\r\n=}'''",
+                             "f'{='",
                              ])
 
         # Different error message is raised for other whitespace characters.
diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst b/Misc/NEWS.d/next/Core and Builtins/2022-03-26-16-35-57.bpo-47129.hDg2Vt.rst
new file mode 100644 (file)
index 0000000..1627aba
--- /dev/null
@@ -0,0 +1 @@
+Improve error messages in f-string syntax errors concerning empty expressions.
index fae2a3648cf9f9004c86ac65b445df1d3ba8021a..65ddd46874b20978f4d1bef13a56a192cd0eb26e 100644 (file)
@@ -357,7 +357,12 @@ fstring_compile_expr(Parser *p, const char *expr_start, const char *expr_end,
             break;
         }
     }
+    
     if (s == expr_end) {
+        if (*expr_end == '!' || *expr_end == ':' || *expr_end == '=') {
+            RAISE_SYNTAX_ERROR("f-string: expression required before '%c'", *expr_end);
+            return NULL;
+        }
         RAISE_SYNTAX_ERROR("f-string: empty expression not allowed");
         return NULL;
     }