From 142dc97c153177aa90e4f9120beab8a441d30533 Mon Sep 17 00:00:00 2001 From: Arnesh Banerjee Date: Sat, 26 Sep 2026 11:49:13 +0530 Subject: [PATCH] gh-154711: Add tests for comments after escaped quotes in f-strings and t-strings Main is not affected by this bug after gh-153569, but nothing tested it. These tests cover a real comment placed after a string that ends with an escaped quote, in debug f-strings and in t-string interpolations. --- Lib/test/test_fstring.py | 5 +++++ Lib/test/test_tstring.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Lib/test/test_fstring.py b/Lib/test/test_fstring.py index 63006266bcd039..fbaba0578b534e 100644 --- a/Lib/test/test_fstring.py +++ b/Lib/test/test_fstring.py @@ -1723,6 +1723,11 @@ def __repr__(self): self.assertEqual(f'{" # nooo "=}', '" # nooo "=\' # nooo \'') self.assertEqual(f'{" \" # nooo \" "=}', '" \\" # nooo \\" "=\' " # nooo " \'') + + # A real comment after a string that ends with an escaped quote must + # still be stripped (gh-154711). + self.assertEqual(f"{'\'' = # comment +}", "'\\'' = \n" + repr("'")) self.assertEqual(f'{"""a" # inside"""=}', '"""a" # inside"""=\'a" # inside\'') self.assertEqual(f"{'''a' # inside'''=}", diff --git a/Lib/test/test_tstring.py b/Lib/test/test_tstring.py index ea20688fdc9753..cb063bc6ca8fe1 100644 --- a/Lib/test/test_tstring.py +++ b/Lib/test/test_tstring.py @@ -257,6 +257,18 @@ def test_interpolation_expression_with_reconstructed_metadata(self): self.assertEqual(regular.interpolations[0].expression, expected) self.assertEqual(debug.interpolations[0].expression, expected) + def test_comment_after_escaped_quote(self): + # A comment after a string that ends with an escaped quote must be + # stripped from the interpolation expression (gh-154711). + t = t"{'a\'b' # comment +}" + self.assertEqual(t.interpolations[0].expression, "'a\\'b' \n") + + t = t"{'a\'b' = # comment +}" + self.assertEqual(t.interpolations[0].expression, "'a\\'b' ") + self.assertEqual(t.strings, ("'a\\'b' = \n", "")) + def test_raw_tstrings(self): path = r"C:\Users" t = rt"{path}\Documents"