From 235a3bb456ef782ef9a6005616aeea4e3a2a432a Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 12 Jul 2023 16:50:12 +0100 Subject: [PATCH] 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. --- mypy/fastparse.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/mypy/fastparse.py b/mypy/fastparse.py index eeeda16f9d8d..f7a98e9b2b8f 100644 --- a/mypy/fastparse.py +++ b/mypy/fastparse.py @@ -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])