Implement template strings (#17851)

This PR implements template strings (t-strings) in the parser and
formatter for Ruff.

Minimal changes necessary to compile were made in other parts of the code (e.g. ty, the linter, etc.). These will be covered properly in follow-up PRs.
This commit is contained in:
Dylan 2025-05-30 15:00:56 -05:00 committed by GitHub
parent ad024f9a09
commit 9bbf4987e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
261 changed files with 18023 additions and 1802 deletions

View file

@ -0,0 +1,74 @@
# Empty t-strings
t""
t""
t''
t""""""
t''''''
t"{" t"}"
t"{foo!s}"
t"{3,}"
t"{3!=4:}"
t'{3:{"}"}>10}'
t'{3:{"{"}>10}'
t"{ foo = }"
t"{ foo = :.3f }"
t"{ foo = !s }"
t"{ 1, 2 = }"
t'{t"{3.1415=:.1f}":*^20}'
{"foo " t"bar {x + y} " "baz": 10}
match foo:
case "one":
pass
case "implicitly " "concatenated":
pass
t"\{foo}\{bar:\}"
t"\\{{foo\\}}"
t"""{
foo:x
y
z
}"""
t"{ ( foo ) = }"
t"normal {foo} {{another}} {bar} {{{three}}}"
t"normal {foo!a} {bar!s} {baz!r} {foobar}"
t"normal {x:y + 2}"
t"{x:{{1}.pop()}}"
t"{(lambda x:{x})}"
t"{x =}"
t"{ x = }"
t"{x=!a}"
t"{x:.3f!r =}"
t"{x = !r :.3f}"
t"{x:.3f=!r}"
"hello" t"{x}"
t"{x}" t"{y}"
t"{x}" "world"
t"Invalid args in command: {command, *args}"
"foo" t"{x}" "bar"
(
t"a"
t"b"
"c"
rt"d"
fr"e"
)
# With unicode strings
u"foo" t"{bar}" "baz" " some"
"foo" t"{bar}" u"baz" " some"
"foo" t"{bar}" "baz" u" some"
u"foo" t"bar {baz} really" u"bar" "no"
# With f-strings
f"{this}" t"{that}"
t"{this}"f"{that}"
t"{this}" "that" f"{other}"
f"one {this} two" "that" t"three {other} four"
# Nesting
t"{f"{t"{this}"}"}"