Improve adjusted_lines
's diff algorithm used in --line-ranges
.
#4052
Labels
T: bug
Something isn't working
adjusted_lines
's diff algorithm used in --line-ranges
.
#4052
Currently, Black performs two formatting passes, and also performs an extra formatting pass for stability check in
--safe
mode.When performing a second formatting pass with
--line-ranges
, we can't simply use the original lines specified by the user. For example, unformatted:If we let it format lines
1-3
, after the first pass it becomes:If we use the original
1-3
lines in a second pass, it would format all the lines now.To solve this, we have an
adjusted_lines
function to calculate the new line ranges for the second pass. It uses the diffing algorithm fromdifflib.SequenceMatcher
. Unfortunately it could produce undesired results for certain edge cases like a list of unformatted lines with the same content.For example:
Using
--line-ranges=2-3
, the result of the first pass isadjusted_lines
will return5-5
with the input of2-3
and the original & formatted code. After the second pass the code becomes:The end result is
--line-range
might format extra lines when there are unformatted lines with the exact same content.--safe
mode.In #4034, we are disabling the stability check for now. This issue tracks the improvement of
adjusted_lines
so that we can eventually fix the underlying issue and re-enable the stability check.The text was updated successfully, but these errors were encountered: