Skip to content

Commit e3ccabb

Browse files
authored
Fix unstable formatting on string split + % formatting (#1680)
Fixes #1595
1 parent 6b5753a commit e3ccabb

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/black/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2668,9 +2668,9 @@ def rhs(line: Line, features: Collection[Feature]) -> Iterator[Line]:
26682668
transformers = [
26692669
string_merge,
26702670
string_paren_strip,
2671+
string_split,
26712672
delimiter_split,
26722673
standalone_comment_split,
2673-
string_split,
26742674
string_paren_wrap,
26752675
rhs,
26762676
]

src/blib2to3/pgen2/pgen.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ def calcfirst(self, name: Text) -> None:
168168
if symbol in inverse:
169169
raise ValueError(
170170
"rule %s is ambiguous; %s is in the first sets of %s as well"
171-
" as %s"
172-
% (name, symbol, label, inverse[symbol])
171+
" as %s" % (name, symbol, label, inverse[symbol])
173172
)
174173
inverse[symbol] = label
175174
self.first[name] = totalset

tests/data/long_strings.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ def foo():
380380

381381
old_fmt_string1 = (
382382
"While we are on the topic of %s, we should also note that old-style formatting"
383-
" must also be preserved, since some %s still uses it."
384-
% ("formatting", "code")
383+
" must also be preserved, since some %s still uses it." % ("formatting", "code")
385384
)
386385

387386
old_fmt_string2 = "This is a %s %s %s %s" % (
@@ -448,8 +447,7 @@ def foo():
448447

449448
assert some_type_of_boolean_expression, (
450449
"Followed by a really really really long string that is used to provide context to"
451-
" the AssertionError exception, which uses dynamic string %s."
452-
% "formatting"
450+
" the AssertionError exception, which uses dynamic string %s." % "formatting"
453451
)
454452

455453
assert some_type_of_boolean_expression, (

tests/data/long_strings__regression.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ def who(self):
310310
passenger_association=passenger_association,
311311
)
312312

313+
if __name__ == "__main__":
314+
for i in range(4, 8):
315+
cmd = (
316+
r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk '{print $2}'); do kill $pid; done"
317+
% (i)
318+
)
319+
313320
# output
314321

315322

@@ -435,14 +442,12 @@ def foo():
435442

436443
func_call_where_string_arg_has_old_fmt_and_bad_parens(
437444
"A long string with {}. This string is so long that it is ridiculous. It can't fit"
438-
" on one line at alllll."
439-
% "formatting",
445+
" on one line at alllll." % "formatting",
440446
)
441447

442448
func_call_where_string_arg_has_old_fmt_and_bad_parens(
443449
"A long string with {}. This {} is so long that it is ridiculous. It can't fit on"
444-
" one line at alllll."
445-
% ("formatting", "string"),
450+
" one line at alllll." % ("formatting", "string"),
446451
)
447452

448453

@@ -702,3 +707,11 @@ def who(self):
702707
passenger_association=passenger_association,
703708
)
704709
)
710+
711+
712+
if __name__ == "__main__":
713+
for i in range(4, 8):
714+
cmd = (
715+
r"for pid in $(ps aux | grep paster | grep -v grep | grep '\-%d' | awk"
716+
r" '{print $2}'); do kill $pid; done" % (i)
717+
)

0 commit comments

Comments
 (0)