Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix named escapes in FSTRING_MIDDLE #980

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading