mirror of
https://github.com/python/cpython
synced 2026-09-29 12:10:30 +03:00
bpo-47129: Add more informative messages to f-string syntax errors (32127)
* 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>
This commit is contained in:
parent
15ba8167d7
commit
7b44ade018
3 changed files with 35 additions and 11 deletions
|
|
@ -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}'''",
|
||||
])
|
||||
|
||||
# Catch the empty expression before the
|
||||
# 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 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.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue