From 5dd175d859217c93050030cf2504584b979f91c9 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Tue, 19 Sep 2023 13:56:26 -0400 Subject: [PATCH] fix string formatting with named escape adjacent to placeholder --- pyupgrade/_string_helpers.py | 2 +- tests/features/format_literals_test.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyupgrade/_string_helpers.py b/pyupgrade/_string_helpers.py index 42718399..1c15655d 100644 --- a/pyupgrade/_string_helpers.py +++ b/pyupgrade/_string_helpers.py @@ -19,7 +19,7 @@ def parse_format(s: str) -> list[DotFormatPart]: for part in NAMED_UNICODE_RE.split(s): if NAMED_UNICODE_RE.fullmatch(part): - if not ret: + if not ret or ret[-1][1:] != (None, None, None): ret.append((part, None, None, None)) else: ret[-1] = (ret[-1][0] + part, None, None, None) diff --git a/tests/features/format_literals_test.py b/tests/features/format_literals_test.py index aaa6ef05..837e593d 100644 --- a/tests/features/format_literals_test.py +++ b/tests/features/format_literals_test.py @@ -25,6 +25,8 @@ '("{0}" # {1}\n"{2}").format(1, 2, 3)', # don't touch f-strings (these are wrong but don't make it worse) 'f"{0}".format(a)', + # shouldn't touch the format spec + r'"{}\N{SNOWMAN}".format("")', ), ) def test_format_literals_noop(s):