From bbf38400ceef34119e63c18c71e3316040a90a59 Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Tue, 25 Sep 2018 16:01:56 +0100 Subject: [PATCH] Uppercase digits in hex literals (#530) --- README.md | 3 ++- black.py | 8 ++++++-- tests/data/numeric_literals.py | 2 +- tests/data/numeric_literals_py2.py | 2 ++ 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 244f60d..a23cb61 100644 --- a/README.md +++ b/README.md @@ -374,7 +374,8 @@ an adoption helper, avoid using this for new projects. ### Numeric literals -*Black* standardizes most numeric literals to use lowercase letters: `0xab` +*Black* standardizes most numeric literals to use lowercase letters for the +syntactic parts and uppercase letters for the digits themselves: `0xAB` instead of `0XAB` and `1e10` instead of `1E10`. Python 2 long literals are styled as `2L` instead of `2l` to avoid confusion between `l` and `1`. In Python 3.6+, *Black* adds underscores to long numeric literals to aid diff --git a/black.py b/black.py index f2c450c..5224741 100644 --- a/black.py +++ b/black.py @@ -2542,9 +2542,13 @@ def normalize_numeric_literal(leaf: Leaf, allow_underscores: bool) -> None: in Python 2 long literals), and long number literals are split using underscores. """ text = leaf.value.lower() - if text.startswith(("0o", "0x", "0b")): - # Leave octal, hex, and binary literals alone. + if text.startswith(("0o", "0b")): + # Leave octal and binary literals alone. pass + elif text.startswith("0x"): + # Change hex literals to upper case. + before, after = text[:2], text[2:] + text = f"{before}{after.upper()}" elif "e" in text: before, after = text.split("e") sign = "" diff --git a/tests/data/numeric_literals.py b/tests/data/numeric_literals.py index b812ebf..9a49dfa 100644 --- a/tests/data/numeric_literals.py +++ b/tests/data/numeric_literals.py @@ -34,7 +34,7 @@ x = 123_456_789.123_456_789e123_456_789 x = 123_456_789e123_456_789 x = 123_456_789j x = 123_456_789.123_456_789j -x = 0xb1acc +x = 0xB1ACC x = 0b1011 x = 0o777 x = 0.000_000_006 diff --git a/tests/data/numeric_literals_py2.py b/tests/data/numeric_literals_py2.py index d2db7b0..8f85c43 100644 --- a/tests/data/numeric_literals_py2.py +++ b/tests/data/numeric_literals_py2.py @@ -3,6 +3,7 @@ x = 123456789L x = 123456789l x = 123456789 +x = 0xb1acc # output @@ -12,3 +13,4 @@ x = 123456789 x = 123456789L x = 123456789L x = 123456789 +x = 0xB1ACC -- 2.39.5