[formatter] allow evaluating f-string literals

by starting a format string with '\fF'.

This was technically already possible with '\fE',
but this makes it a bit more convenient.
This commit is contained in:
Mike Fährmann
2022-03-18 13:31:01 +01:00
parent 58e0b17211
commit cf44aba333
3 changed files with 23 additions and 1 deletions

View File

@@ -232,6 +232,14 @@ class TestFormatter(unittest.TestCase):
self._run_test("\fE name * 2 + ' ' + a", "{}{} {}".format(
self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
@unittest.skipIf(sys.hexversion < 0x3060000, "no fstring support")
def test_fstring(self):
self._run_test("\fF {a}", self.kwdict["a"])
self._run_test("\fF {name}{name} {a}", "{}{} {}".format(
self.kwdict["name"], self.kwdict["name"], self.kwdict["a"]))
self._run_test("\fF foo-'\"{a.upper()}\"'-bar",
"""foo-'"{}"'-bar""".format(self.kwdict["a"].upper()))
def test_module(self):
with tempfile.TemporaryDirectory() as tmpdirname:
path = os.path.join(tmpdirname, "testmod.py")