Skip to content

Commit 1648ac5

Browse files
authoredOct 16, 2023
Fix long lines with power operator(s) getting splitted before line length (#3942)
Fixes #3889
1 parent 6f84f65 commit 1648ac5

File tree

5 files changed

+137
-1
lines changed

5 files changed

+137
-1
lines changed
 

‎CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<!-- Changes that affect Black's preview style -->
1818

19+
- Fix long lines with power operators getting splitted before the line length (#3942)
1920
- Long type hints are now wrapped in parentheses and properly indented when split across
2021
multiple lines (#3899)
2122
- Magic trailing commas are now respected in return types. (#3916)

‎src/black/linegen.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,17 @@ def __post_init__(self) -> None:
536536
self.visit_case_block = self.visit_match_case
537537

538538

539+
def _hugging_power_ops_line_to_string(
540+
line: Line,
541+
features: Collection[Feature],
542+
mode: Mode,
543+
) -> Optional[str]:
544+
try:
545+
return line_to_string(next(hug_power_op(line, features, mode)))
546+
except CannotTransform:
547+
return None
548+
549+
539550
def transform_line(
540551
line: Line, mode: Mode, features: Collection[Feature] = ()
541552
) -> Iterator[Line]:
@@ -551,6 +562,14 @@ def transform_line(
551562

552563
line_str = line_to_string(line)
553564

565+
# We need the line string when power operators are hugging to determine if we should
566+
# split the line. Default to line_str, if no power operator are present on the line.
567+
line_str_hugging_power_ops = (
568+
(_hugging_power_ops_line_to_string(line, features, mode) or line_str)
569+
if Preview.fix_power_op_line_length in mode
570+
else line_str
571+
)
572+
554573
ll = mode.line_length
555574
sn = mode.string_normalization
556575
string_merge = StringMerger(ll, sn)
@@ -564,7 +583,7 @@ def transform_line(
564583
and not line.should_split_rhs
565584
and not line.magic_trailing_comma
566585
and (
567-
is_line_short_enough(line, mode=mode, line_str=line_str)
586+
is_line_short_enough(line, mode=mode, line_str=line_str_hugging_power_ops)
568587
or line.contains_unsplittable_type_ignore()
569588
)
570589
and not (line.inside_brackets and line.contains_standalone_comments())

‎src/black/mode.py

+1
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class Preview(Enum):
188188
dummy_implementations = auto()
189189
walrus_subscript = auto()
190190
module_docstring_newlines = auto()
191+
fix_power_op_line_length = auto()
191192

192193

193194
class Deprecated(UserWarning):

‎tests/data/cases/power_op_spacing.py

+18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def function_dont_replace_spaces():
2929
p = {(k, k**2): v**2 for k, v in pairs}
3030
q = [10**i for i in range(6)]
3131
r = x**y
32+
s = 1 ** 1
33+
t = (
34+
1
35+
** 1
36+
**1
37+
** 1
38+
)
3239

3340
a = 5.0**~4.0
3441
b = 5.0 ** f()
@@ -47,6 +54,13 @@ def function_dont_replace_spaces():
4754
o = settings(max_examples=10**6.0)
4855
p = {(k, k**2): v**2.0 for k, v in pairs}
4956
q = [10.5**i for i in range(6)]
57+
s = 1.0 ** 1.0
58+
t = (
59+
1.0
60+
** 1.0
61+
**1.0
62+
** 1.0
63+
)
5064

5165

5266
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
@@ -97,6 +111,8 @@ def function_dont_replace_spaces():
97111
p = {(k, k**2): v**2 for k, v in pairs}
98112
q = [10**i for i in range(6)]
99113
r = x**y
114+
s = 1**1
115+
t = 1**1**1**1
100116

101117
a = 5.0**~4.0
102118
b = 5.0 ** f()
@@ -115,6 +131,8 @@ def function_dont_replace_spaces():
115131
o = settings(max_examples=10**6.0)
116132
p = {(k, k**2): v**2.0 for k, v in pairs}
117133
q = [10.5**i for i in range(6)]
134+
s = 1.0**1.0
135+
t = 1.0**1.0**1.0**1.0
118136

119137

120138
# WE SHOULD DEFINITELY NOT EAT THESE COMMENTS (https://github.com/psf/black/issues/2873)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# flags: --preview
2+
a = 1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1
3+
b = 1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1
4+
c = 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1 ** 1
5+
d = 1**1 ** 1**1 ** 1**1 ** 1**1 ** 1**1**1 ** 1 ** 1**1 ** 1**1**1**1**1 ** 1 ** 1**1**1 **1**1** 1 ** 1 ** 1
6+
e = 𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟
7+
f = 𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟
8+
9+
a = 1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0
10+
b = 1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0
11+
c = 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0 ** 1.0
12+
d = 1.0**1.0 ** 1.0**1.0 ** 1.0**1.0 ** 1.0**1.0 ** 1.0**1.0**1.0 ** 1.0 ** 1.0**1.0 ** 1.0**1.0**1.0
13+
14+
# output
15+
a = 1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1
16+
b = (
17+
1
18+
** 1
19+
** 1
20+
** 1
21+
** 1
22+
** 1
23+
** 1
24+
** 1
25+
** 1
26+
** 1
27+
** 1
28+
** 1
29+
** 1
30+
** 1
31+
** 1
32+
** 1
33+
** 1
34+
** 1
35+
** 1
36+
** 1
37+
** 1
38+
** 1
39+
** 1
40+
** 1
41+
** 1
42+
** 1
43+
** 1
44+
** 1
45+
** 1
46+
)
47+
c = 1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1
48+
d = 1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1**1
49+
e = 𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟**𨉟
50+
f = (
51+
𨉟
52+
** 𨉟
53+
** 𨉟
54+
** 𨉟
55+
** 𨉟
56+
** 𨉟
57+
** 𨉟
58+
** 𨉟
59+
** 𨉟
60+
** 𨉟
61+
** 𨉟
62+
** 𨉟
63+
** 𨉟
64+
** 𨉟
65+
** 𨉟
66+
** 𨉟
67+
** 𨉟
68+
** 𨉟
69+
** 𨉟
70+
** 𨉟
71+
** 𨉟
72+
** 𨉟
73+
)
74+
75+
a = 1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0
76+
b = (
77+
1.0
78+
** 1.0
79+
** 1.0
80+
** 1.0
81+
** 1.0
82+
** 1.0
83+
** 1.0
84+
** 1.0
85+
** 1.0
86+
** 1.0
87+
** 1.0
88+
** 1.0
89+
** 1.0
90+
** 1.0
91+
** 1.0
92+
** 1.0
93+
** 1.0
94+
** 1.0
95+
)
96+
c = 1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0
97+
d = 1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0**1.0

0 commit comments

Comments
 (0)
Please sign in to comment.