Skip to content

Commit c7fc77c

Browse files
committed
Convert groups to string to prevent mypy for shouting
1 parent 2ada012 commit c7fc77c

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/black/linegen.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ def visit_factor(self, node: Node) -> Iterator[Line]:
261261

262262
def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
263263
if Preview.hex_codes_in_unicode_sequences in self.mode:
264-
# Preview style only
265264
normalize_unicode_escape_sequences(leaf)
266265

267266
if is_docstring(leaf) and "\\\n" not in leaf.value:

src/black/strings.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
FIRST_NON_WHITESPACE_RE: Final = re.compile(r"\s*\t+\s*(\S)")
2323
UNICODE_RE = re.compile(
2424
r"(\\+)("
25-
r"(u([a-zA-Z0-9]{4}))"
26-
r"|(U([a-zA-Z0-9]{0,8}))"
27-
r"|(x([a-zA-Z0-9]{2}))"
28-
r"|(N\{([a-zA-Z0-9]{2})\})"
25+
r"(u([a-zA-Z0-9]{4}))" # Formatting 16-bit unicodes i.e. \uxxxx
26+
r"|(U([a-zA-Z0-9]{0,8}))" # Formatting 32-bit unicodes i.e. \Uxxxxxxxx
27+
r"|(x([a-zA-Z0-9]{2}))" # Formatting unicodes in format of \xhh
28+
r"|(N\{([a-zA-Z0-9]{2})\})" # Formatting named unicodes in format of \N{name}
2929
r")"
3030
)
3131

@@ -253,23 +253,24 @@ def normalize_unicode_escape_sequences(leaf: Leaf) -> None:
253253
text = leaf.value
254254
prefix = get_string_prefix(text)
255255

256-
def replace(m: Match[AnyStr]) -> AnyStr:
256+
def replace(m: Match[AnyStr]) -> str:
257257
groups = m.groups()
258+
back_slashes = str(groups[0])
258259

259-
if len(groups[0]) % 2 == 0 or prefix == "r":
260-
return groups[0] + groups[1]
260+
if len(back_slashes) % 2 == 0 or prefix == "r":
261+
return back_slashes + str(groups[1])
261262

262263
if groups[2]:
263264
# \u
264-
return groups[0] + "u" + groups[3].lower()
265+
return back_slashes + "u" + str(groups[3].lower())
265266
elif groups[4]:
266267
# \U
267-
return groups[0] + "U" + groups[5].lower()
268+
return back_slashes + "U" + str(groups[5].lower())
268269
elif groups[6]:
269270
# \x
270-
return groups[0] + "x" + groups[7].lower()
271+
return back_slashes + "x" + str(groups[7].lower())
271272
else:
272273
# \N{}
273-
return groups[0] + "N{" + groups[9].upper() + "}"
274+
return back_slashes + "N{" + str(groups[9].upper()) + "}"
274275

275276
leaf.value = re.sub(UNICODE_RE, replace, text)

test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
x = "\x1f"
2+
x = "\\x1B"
3+
x = "\\\x1b"
4+
x = "\U0001f60e"
5+
x = "\u0001F60E"
6+
x = r"\u0001F60E"
7+
x = "don't format me"
8+
x = "\xa3"
9+
x = "\u2717"
10+
x = "\N{OX}\N{OX}"

0 commit comments

Comments
 (0)