diff --git a/Lib/test/test_tstring.py b/Lib/test/test_tstring.py index ea20688fdc9753..4cbe0b178f1c35 100644 --- a/Lib/test/test_tstring.py +++ b/Lib/test/test_tstring.py @@ -435,5 +435,14 @@ def test_triple_quoted(self): t = t'''{f"{d["a#b"]}"}''' self.assertEqual(t.interpolations[0].expression, 'f"{d["a#b"]}"') + def test_not_equal_with_format_spec(self): + # gh-146448: != in expression with format spec should not be + # confused with the ! conversion specifier + t = t"{0!=0:}" + self.assertTStringEqual(t, ("", ""), [(False, "0!=0")]) + + t = t"{0!=0:s}" + self.assertTStringEqual(t, ("", ""), [(False, "0!=0", None, "s")]) + if __name__ == '__main__': unittest.main() diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-30-00-00-00.gh-issue-146448.5fce3b.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-30-00-00-00.gh-issue-146448.5fce3b.rst new file mode 100644 index 00000000000000..40a19df9bd0a99 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-30-00-00-00.gh-issue-146448.5fce3b.rst @@ -0,0 +1,2 @@ +Fix template string parser incorrectly truncating the ``Interpolation.str`` +attribute when the expression contains ``!=`` followed by a format spec.