Skip to content

Commit b6e8758

Browse files
committed
Improve tests and docs, also resolve linting
1 parent 93ddad3 commit b6e8758

File tree

4 files changed

+66
-23
lines changed

4 files changed

+66
-23
lines changed

docs/the_black_code_style/current_style.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,24 @@ multiple lines. This is so that _Black_ is compliant with the recent changes in
285285
style guide, which emphasizes that this approach improves readability.
286286

287287
Almost all operators will be surrounded by single spaces, the only exceptions are unary
288-
operators (`+`, `-`, and `~`), and power operators when both operands are simple.
289-
For power ops, an operand is considered simple if it's only a NAME, numeric
290-
CONSTANT, or attribute access (chained attribute access is allowed), with or without a
291-
preceding unary operator.
288+
operators (`+`, `-`, and `~`), and power operators when both operands are simple. For
289+
powers, an operand is considered simple if it's only a NAME, numeric CONSTANT, or
290+
attribute access (chained attribute access is allowed), with or without a preceding
291+
unary operator.
292+
293+
```python
294+
# For example, these won't be surrounded by whitespace
295+
a = x**y
296+
b = config.base**5.2
297+
c = config.base**runtime.config.exponent
298+
d = 2**5
299+
e = 2**~5
300+
301+
# ... but these will be surrounded by whitespace
302+
f = 2 ** get_exponent()
303+
g = get_x() ** get_y()
304+
h = config['base'] ** 2
305+
```
292306

293307
### Slices
294308

src/black/linegen.py

+9-18
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121
from black.numerics import normalize_numeric_literal
2222
from black.strings import get_string_prefix, fix_docstring
2323
from black.strings import normalize_string_prefix, normalize_string_quotes
24-
from black.trans import Transformer, CannotTransform, StringMerger
25-
from black.trans import (
26-
StringSplitter,
27-
StringParenWrapper,
28-
StringParenStripper,
29-
hug_power_op,
30-
)
24+
from black.trans import Transformer, CannotTransform, StringMerger, StringSplitter
25+
from black.trans import StringParenWrapper, StringParenStripper, hug_power_op
3126
from black.mode import Mode, Feature, Preview
3227

3328
from blib2to3.pytree import Node, Leaf
@@ -343,9 +338,9 @@ def transform_line(
343338
):
344339
# Only apply basic string preprocessing, since lines shouldn't be split here.
345340
if Preview.string_processing in mode:
346-
transformers = [string_merge, string_paren_strip, hug_power_op]
341+
transformers = [string_merge, string_paren_strip]
347342
else:
348-
transformers = [hug_power_op]
343+
transformers = []
349344
elif line.is_def:
350345
transformers = [left_hand_split]
351346
else:
@@ -395,7 +390,6 @@ def _rhs(
395390
standalone_comment_split,
396391
string_paren_wrap,
397392
rhs,
398-
hug_power_op,
399393
]
400394
else:
401395
transformers = [
@@ -404,18 +398,15 @@ def _rhs(
404398
string_split,
405399
string_paren_wrap,
406400
rhs,
407-
hug_power_op,
408401
]
409402
else:
410403
if line.inside_brackets:
411-
transformers = [
412-
delimiter_split,
413-
standalone_comment_split,
414-
rhs,
415-
hug_power_op,
416-
]
404+
transformers = [delimiter_split, standalone_comment_split, rhs]
417405
else:
418-
transformers = [rhs, hug_power_op]
406+
transformers = [rhs]
407+
# It's always safe to attempt hugging of power operations and pretty much every line
408+
# could match.
409+
transformers.append(hug_power_op)
419410

420411
for transform in transformers:
421412
# We are accumulating lines in `result` because we might want to abort

src/black/trans.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def hug_power_op(line: Line, features: Collection[Feature]) -> Iterator[Line]:
8282
raise CannotTransform("No doublestar token was found in the line.")
8383

8484
def is_simple_lookup(index: int, step: Literal[1, -1]) -> bool:
85-
# Brackets and parenthesises indicate calls, subscripts, etc. ...
85+
# Brackets and parentheses indicate calls, subscripts, etc. ...
8686
# basically stuff that doesn't count as "simple". Only a NAME lookup
8787
# or dotted lookup (eg. NAME.NAME) is OK.
8888
if step == -1:

tests/data/power_op_spacing.py

+38
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ def function_dont_replace_spaces():
2828
o = settings(max_examples=10**6)
2929
p = {(k, k**2): v**2 for k, v in pairs}
3030
q = [10**i for i in range(6)]
31+
r = x**y
32+
33+
a = 5.0**~4.0
34+
b = 5.0 ** f()
35+
c = -(5.0**2.0)
36+
d = 5.0 ** f["hi"]
37+
e = lazy(lambda **kwargs: 5)
38+
f = f() ** 5.0
39+
g = a.b**c.d
40+
h = 5.0 ** funcs.f()
41+
i = funcs.f() ** 5.0
42+
j = super().name ** 5.0
43+
k = [(2.0**idx, value) for idx, value in pairs]
44+
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
45+
m = [([2.0**63.0], [1.0, 2**63.0])]
46+
n = count <= 10**5.0
47+
o = settings(max_examples=10**6.0)
48+
p = {(k, k**2): v**2.0 for k, v in pairs}
49+
q = [10.5**i for i in range(6)]
3150

3251

3352
# output
@@ -63,3 +82,22 @@ def function_dont_replace_spaces():
6382
o = settings(max_examples=10**6)
6483
p = {(k, k**2): v**2 for k, v in pairs}
6584
q = [10**i for i in range(6)]
85+
r = x**y
86+
87+
a = 5.0**~4.0
88+
b = 5.0 ** f()
89+
c = -(5.0**2.0)
90+
d = 5.0 ** f["hi"]
91+
e = lazy(lambda **kwargs: 5)
92+
f = f() ** 5.0
93+
g = a.b**c.d
94+
h = 5.0 ** funcs.f()
95+
i = funcs.f() ** 5.0
96+
j = super().name ** 5.0
97+
k = [(2.0**idx, value) for idx, value in pairs]
98+
l = mod.weights_[0] == pytest.approx(0.95**100, abs=0.001)
99+
m = [([2.0**63.0], [1.0, 2**63.0])]
100+
n = count <= 10**5.0
101+
o = settings(max_examples=10**6.0)
102+
p = {(k, k**2): v**2.0 for k, v in pairs}
103+
q = [10.5**i for i in range(6)]

0 commit comments

Comments
 (0)