Skip to content

Commit 235a3bb

Browse files
authored
Fix parse test case on Python 3.12 (#15577)
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.
1 parent edb41e0 commit 235a3bb

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

mypy/fastparse.py

+6
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,12 @@ def visit_JoinedStr(self, n: ast3.JoinedStr) -> Expression:
15351535
# Don't make unnecessary join call if there is only one str to join
15361536
if len(strs_to_join.items) == 1:
15371537
return self.set_line(strs_to_join.items[0], n)
1538+
elif len(strs_to_join.items) > 1:
1539+
last = strs_to_join.items[-1]
1540+
if isinstance(last, StrExpr) and last.value == "":
1541+
# 3.12 can add an empty literal at the end. Delete it for consistency
1542+
# between Python versions.
1543+
del strs_to_join.items[-1:]
15381544
join_method = MemberExpr(empty_string, "join")
15391545
join_method.set_line(empty_string)
15401546
result_expression = CallExpr(join_method, [strs_to_join], [ARG_POS], [None])

0 commit comments

Comments
 (0)