Skip to content

Commit

Permalink
Fix f-string parsing with format specification (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Oct 24, 2024
1 parent 4940090 commit 8b4398e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion rewrite/rewrite/python/_parser_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,7 @@ def __map_fstring(self, node: ast.JoinedStr, prefix: Space, tok: TokenInfo, toke
try:
while (tokens.peek()).type not in (token.FSTRING_END, token.FSTRING_MIDDLE):
tok = next(tokens)
if tok.type == token.OP and tok.string in ('!', ':'):
if tok.type == token.OP and tok.string in ('!'):
break
la_tok = tokens.peek()
if tok.type == token.OP and tok.string == '}' and (
Expand All @@ -2163,6 +2163,8 @@ def __map_fstring(self, node: ast.JoinedStr, prefix: Space, tok: TokenInfo, toke
break
if tok.type == token.OP and tok.string == '=' and la_tok.string in ('!', ':', '}'):
break
if tok.type == token.OP and tok.string == ':' and la_tok.string in ('{'):
break
except StopIteration:
pass

Expand Down
7 changes: 6 additions & 1 deletion rewrite/tests/python/all/fstring_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,16 @@ def test_concat_fstring_7():
)


def test_concat_fstring_9():
def test_concat_fstring_8():
# language=python
rewrite_run(python('_ = f"n{\' \':{1}}Groups"'))


def test_concat_fstring_9():
# language=python
rewrite_run(python('print(f"Progress: {output.escaped_title[:30]:<30} {default_output:>161}")'))


def test_empty():
# language=python
rewrite_run(python("a = f''"))
Expand Down

0 comments on commit 8b4398e

Please sign in to comment.