Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify test conversions to fix failing tests #194

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -852,16 +852,17 @@ lambda_param[arg_ty]: a=NAME { _PyAST_arg(a->v.Name.id, NULL, NULL, EXTRA) }
# LITERALS
# ========

gstring_conversion[expr_ty]:
| "!" conv=NAME { conv }
| "!" !NAME { RAISE_SYNTAX_ERROR("f-string: invalid conversion character: expected 's', 'r', or 'a'") }
gstring_middle[expr_ty]:
| gstring_replacement_field
| t=FSTRING_MIDDLE { _PyPegen_constant_from_token2(p, t) }
# There are some shenanigans with the gstring_format_spec: Don't try to put it in its own rule
# or otherwise it will try to parse the first token with the regular tokenizer mode (due to the EXTRA).
# TODO: (Ideally we need a way similar to 'memo' so the parser can set the tokenize mode on fstring/normal)
gstring_replacement_field[expr_ty]:
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[
"!" conv=NAME {conv}
] format=[
| expr_start='{' a=(yield_expr | star_expressions) debug_expr="="? conversion=[gstring_conversion] format=[
':' spec=gstring_format_spec* { spec ? _PyAST_JoinedStr((asdl_expr_seq*)spec, EXTRA) : NULL }
] &&'}' {
_PyPegen_formatted_value(p, a, debug_expr, conversion, format, EXTRA)
Expand Down
8 changes: 3 additions & 5 deletions Lib/test/test_fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,16 +993,14 @@ def test_conversions(self):
"f'{3!G}'",
"f'{3!!}'",
"f'{3!:}'",
"f'{3! s}'", # no space before conversion char
])

self.assertAllRaise(SyntaxError, "f-string: expecting '}'",
["f'{x!s{y}}'",
"f'{3!ss}'",
"f'{3!ss:}'",
"f'{3!ss:s}'",
])

with self.assertRaisesRegex(SyntaxError, "expected '}'"):
eval("f'{x!s{y}}'")

def test_assignment(self):
self.assertAllRaise(SyntaxError, r'invalid syntax',
["f'' = 3",
Expand Down
Loading