Skip to content

Commit

Permalink
Merge pull request #980 from asottile/named-escapes-in-fstrings
Browse files Browse the repository at this point in the history
fix named escapes in FSTRING_MIDDLE
  • Loading branch information
asottile authored Oct 22, 2024
2 parents c29b8d6 + aa12347 commit 466dfb1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
14 changes: 2 additions & 12 deletions pyupgrade/_string_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

import codecs
import re
import string
from typing import Optional

NAMED_UNICODE_RE = re.compile(r'(?<!\\)(?:\\\\)*(\\N\{[^}]+\})')
from tokenize_rt import curly_escape
from tokenize_rt import NAMED_UNICODE_RE

DotFormatPart = tuple[str, Optional[str], Optional[str], Optional[str]]

Expand Down Expand Up @@ -53,16 +53,6 @@ def _convert_tup(tup: DotFormatPart) -> str:
return ''.join(_convert_tup(tup) for tup in parsed)


def curly_escape(s: str) -> str:
parts = NAMED_UNICODE_RE.split(s)
return ''.join(
part.replace('{', '{{').replace('}', '}}')
if not NAMED_UNICODE_RE.fullmatch(part)
else part
for part in parts
)


def is_codec(encoding: str, name: str) -> bool:
try:
return codecs.lookup(encoding).name == name
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ classifiers =
[options]
packages = find:
install_requires =
tokenize-rt>=5.2.0
tokenize-rt>=6.1.0
python_requires = >=3.9

[options.packages.find]
Expand Down
2 changes: 2 additions & 0 deletions tests/main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def test_main_noop(tmpdir, capsys):
x=version_info
def f():
global x, y
f'hello snowman: \\N{SNOWMAN}'
'''
f = tmpdir.join('f.py')
f.write(s)
Expand Down

0 comments on commit 466dfb1

Please sign in to comment.