Skip to content

Commit 43293bb

Browse files
committed
fix rewrite of u strings to f strings
1 parent a389e99 commit 43293bb

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

pyupgrade/_plugins/fstrings.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Iterable
55

66
from tokenize_rt import Offset
7+
from tokenize_rt import parse_string_literal
78
from tokenize_rt import Token
89
from tokenize_rt import tokens_to_src
910

@@ -43,7 +44,12 @@ def _to_fstring(
4344

4445
parts = []
4546
i = 0
46-
for s, name, spec, conv in parse_format('f' + src):
47+
48+
# need to remove `u` prefix so it isn't invalid syntax
49+
prefix, rest = parse_string_literal(src)
50+
new_src = 'f' + prefix.translate({ord('u'): None, ord('U'): None}) + rest
51+
52+
for s, name, spec, conv in parse_format(new_src):
4753
if name is not None:
4854
k, dot, rest = name.partition('.')
4955
name = ''.join((params[k or str(i)], dot, rest))

tests/features/fstrings_test.py

+5
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ def test_fix_fstrings_noop(s):
6464
r'f"\N{snowman} {a}"',
6565
id='named escape sequences',
6666
),
67+
pytest.param(
68+
'u"foo{}".format(1)',
69+
'f"foo{1}"',
70+
id='u-prefixed format',
71+
),
6772
),
6873
)
6974
def test_fix_fstrings(s, expected):

0 commit comments

Comments
 (0)