Skip to content

Commit

Permalink
Fix parse test case on Python 3.12 (#15577)
Browse files Browse the repository at this point in the history
This fixes the test case testFStringWithFormatSpecifierExpression on
Python 3.12.

Strip redundant empty string literal from AST generated from f-string.
It's not generated on earlier Python versions and it seems fine to just
drop it.
  • Loading branch information
JukkaL committed Jul 12, 2023
1 parent edb41e0 commit 235a3bb
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,12 @@ def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
# Don't make unnecessary join call if there is only one str to join
if len(strs_to_join.items) == 1:
return self.set_line(strs_to_join.items[0], n)
elif len(strs_to_join.items) > 1:
last = strs_to_join.items[-1]
if isinstance(last, StrExpr) and last.value == "":
# 3.12 can add an empty literal at the end. Delete it for consistency
# between Python versions.
del strs_to_join.items[-1:]
join_method = MemberExpr(empty_string, "join")
join_method.set_line(empty_string)
result_expression = CallExpr(join_method, [strs_to_join], [ARG_POS], [None])
Expand Down

0 comments on commit 235a3bb

Please sign in to comment.