Skip to content

Commit dfa5741

Browse files
committed
uppercase hex in strings
1 parent c6755bb commit dfa5741

5 files changed

Lines changed: 24 additions & 5 deletions

File tree

src/black/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ def validate_regex(
318318
is_flag=True,
319319
help="Don't normalize string quotes or prefixes.",
320320
)
321+
@click.option(
322+
"--uppercase-unicode-escapes",
323+
is_flag=True,
324+
help="Make unicode escapes uppercase."
325+
)
321326
@click.option(
322327
"-C",
323328
"--skip-magic-trailing-comma",
@@ -549,6 +554,7 @@ def main(
549554
python_cell_magics: Sequence[str],
550555
skip_source_first_line: bool,
551556
skip_string_normalization: bool,
557+
uppercase_unicode_escapes: bool,
552558
skip_magic_trailing_comma: bool,
553559
preview: bool,
554560
unstable: bool,
@@ -657,6 +663,7 @@ def main(
657663
skip_source_first_line=skip_source_first_line,
658664
string_normalization=not skip_string_normalization,
659665
magic_trailing_comma=not skip_magic_trailing_comma,
666+
uppercase_unicode_escapes=uppercase_unicode_escapes,
660667
preview=preview,
661668
unstable=unstable,
662669
python_cell_magics=set(python_cell_magics),

src/black/linegen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,10 @@ def foo(a: (int), b: (float) = 7): ...
465465
yield from self.visit_default(node)
466466

467467
def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
468-
normalize_unicode_escape_sequences(leaf)
468+
normalize_unicode_escape_sequences(
469+
leaf,
470+
str.upper if self.mode.uppercase_unicode_escapes else str.lower,
471+
)
469472

470473
if is_docstring(leaf) and not re.search(r"\\\s*\n", leaf.value):
471474
# We're ignoring docstrings with backslash newline escapes because changing

src/black/mode.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class Mode:
248248
target_versions: set[TargetVersion] = field(default_factory=set)
249249
line_length: int = DEFAULT_LINE_LENGTH
250250
string_normalization: bool = True
251+
uppercase_unicode_escapes: bool = False
251252
is_pyi: bool = False
252253
is_ipynb: bool = False
253254
skip_source_first_line: bool = False
@@ -295,6 +296,7 @@ def get_cache_key(self) -> str:
295296
version_str,
296297
str(self.line_length),
297298
str(int(self.string_normalization)),
299+
str(int(self.uppercase_unicode_escapes)),
298300
str(int(self.is_pyi)),
299301
str(int(self.is_ipynb)),
300302
str(int(self.skip_source_first_line)),
@@ -310,6 +312,7 @@ def __hash__(self) -> int:
310312
frozenset(self.target_versions),
311313
self.line_length,
312314
self.string_normalization,
315+
self.uppercase_unicode_escapes,
313316
self.is_pyi,
314317
self.is_ipynb,
315318
self.skip_source_first_line,

src/black/resources/black.schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
"description": "How many characters per line to allow.",
1515
"default": 88
1616
},
17+
"uppercase-unicode-escapes": {
18+
"type": "boolean",
19+
"description": "Wether to make unicode escapes upper case.",
20+
"default": false
21+
},
1722
"target-version": {
1823
"type": "array",
1924
"items": {

src/black/strings.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import re
66
import sys
7+
from collections.abc import Callable
78
from functools import lru_cache
89
from re import Match, Pattern
910
from typing import Final
@@ -306,7 +307,7 @@ def normalize_fstring_quotes(
306307
return middles, new_quote
307308

308309

309-
def normalize_unicode_escape_sequences(leaf: Leaf) -> None:
310+
def normalize_unicode_escape_sequences(leaf: Leaf, _fix_hex: Callable[[str], str]) -> None:
310311
"""Replace hex codes in Unicode escape sequences with lowercase representation."""
311312
text = leaf.value
312313
prefix = get_string_prefix(text)
@@ -322,13 +323,13 @@ def replace(m: Match[str]) -> str:
322323

323324
if groups["u"]:
324325
# \u
325-
return back_slashes + "u" + groups["u"].lower()
326+
return back_slashes + "u" + _fix_hex(groups["u"])
326327
elif groups["U"]:
327328
# \U
328-
return back_slashes + "U" + groups["U"].lower()
329+
return back_slashes + "U" + _fix_hex(groups["U"])
329330
elif groups["x"]:
330331
# \x
331-
return back_slashes + "x" + groups["x"].lower()
332+
return back_slashes + "x" + _fix_hex(groups["x"])
332333
else:
333334
assert groups["N"], f"Unexpected match: {m}"
334335
# \N{}

0 commit comments

Comments
 (0)