Skip to content

Valid f-string conversion throws KeyError #278

@gvangool

Description

@gvangool

While working on a conversion project, I noticed that certain files would throw KeyError's for \N{...} strings.

A minimal test case looks like:

world = "world"
print("hello {} \N{snowman}".format(world))

Which I would expect to be left alone or rewritten as:

world = "world"
print(f"hello {world} \N{snowman}")

I had trouble reproducing it in pytests. This seemed to trigger the same error:

diff --git tests/fstrings_test.py tests/fstrings_test.py
index d850293..a772a0f 100644
--- tests/fstrings_test.py
+++ tests/fstrings_test.py
@@ -48,6 +48,7 @@ def test_fix_fstrings_noop(s):
         ('"hello {}!".format(name)', 'f"hello {name}!"'),
         ('"{}{{}}{}".format(escaped, y)', 'f"{escaped}{{}}{y}"'),
         ('"{}{b}{}".format(a, c, b=b)', 'f"{a}{b}{c}"'),
+        (r'"{}\N{snowman}".format(a)', r'f"{a}\N{snowman}"'),
         # TODO: poor man's f-strings?
         # '"{foo}".format(**locals())'
     ),

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions