Skip to content

Commit

Permalink
Add octal support, more test cases, and fixup long ints
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
ichard26 and JelleZijlstra committed Nov 8, 2021
1 parent f2a54ee commit 78d32bf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/black/__init__.py
Expand Up @@ -1135,9 +1135,14 @@ def get_features_used(node: Node) -> Set[Feature]: # noqa: C901
assert isinstance(n, Leaf)
if "_" in n.value:
features.add(Feature.NUMERIC_UNDERSCORES)
elif n.value.endswith("L"):
elif n.value.endswith(("L", "l")):
# Python 2: 10L
features.add(Feature.LONG_INT_LITERAL)
elif len(n.value) >= 2 and n.value[0] == "0" and n.value[1].isdigit():
# Python 2: 0123; 00123; ...
if not all(char == "0" for char in n.value):
# although we don't want to match 0000 or similar
features.add(Feature.OCTAL_INT_LITERAL)

elif n.type == token.SLASH:
if n.parent and n.parent.type in {
Expand Down Expand Up @@ -1175,7 +1180,7 @@ def get_features_used(node: Node) -> Set[Feature]: # noqa: C901
if argch.type in STARS:
features.add(feature)

# Python 2 only features (for its deprecation) except for 10L, see above
# Python 2 only features (for its deprecation) except for integers, see above
elif n.type == syms.print_stmt:
features.add(Feature.PRINT_STMT)
elif n.type == syms.exec_stmt:
Expand Down
4 changes: 3 additions & 1 deletion src/black/mode.py
Expand Up @@ -48,7 +48,8 @@ class Feature(Enum):
COMMA_STYLE_EXCEPT = 203
COMMA_STYLE_RAISE = 204
LONG_INT_LITERAL = 205
BACKQUOTE_REPR = 206
OCTAL_INT_LITERAL = 206
BACKQUOTE_REPR = 207


VERSION_TO_FEATURES: Dict[TargetVersion, Set[Feature]] = {
Expand All @@ -60,6 +61,7 @@ class Feature(Enum):
Feature.COMMA_STYLE_EXCEPT,
Feature.COMMA_STYLE_RAISE,
Feature.LONG_INT_LITERAL,
Feature.OCTAL_INT_LITERAL,
Feature.BACKQUOTE_REPR,
},
TargetVersion.PY33: {Feature.UNICODE_LITERALS, Feature.ASYNC_IDENTIFIERS},
Expand Down
24 changes: 24 additions & 0 deletions tests/data/python2_detection.py
Expand Up @@ -30,6 +30,14 @@ def set_position((x, y), value):

10L

###

10l

###

0123

# output

print("hello python three!")
Expand Down Expand Up @@ -58,9 +66,25 @@ def set_position((x, y), value):

###

raise RuntimeError("boom!",)

###

def set_position(x, y, value):
pass

###

10

###

0

###

000

###

0o12
6 changes: 4 additions & 2 deletions tests/test_black.py
Expand Up @@ -2039,10 +2039,12 @@ def test_python_2_deprecation_autodetection_extended() -> None:
python2, non_python2 = read_data("python2_detection")
for python2_case in python2.split("###"):
node = black.lib2to3_parse(python2_case)
assert black.detect_target_versions(node) == {TargetVersion.PY27}
assert black.detect_target_versions(node) == {TargetVersion.PY27}, python2_case
for non_python2_case in non_python2.split("###"):
node = black.lib2to3_parse(non_python2_case)
assert black.detect_target_versions(node) != {TargetVersion.PY27}
assert black.detect_target_versions(node) != {
TargetVersion.PY27
}, non_python2_case


with open(black.__file__, "r", encoding="utf-8") as _bf:
Expand Down

0 comments on commit 78d32bf

Please sign in to comment.