A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/psf/black/issues/4052 below:

Improve `adjusted_lines`'s diff algorithm used in `--line-ranges`. · Issue #4052 · psf/black · GitHub

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:

def restrict_to_this_line(arg1,
  arg2,
  arg3):
    print  ( "This should not be formatted." )
    print  ( "This should not be formatted." )

If we let it format lines 1-3, after the first pass it becomes:

def restrict_to_this_line(arg1, arg2, arg3):
    print  ( "This should not be formatted." )
    print  ( "This should not be formatted." )

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 from difflib.SequenceMatcher. Unfortunately it could produce undesired results for certain edge cases like a list of unformatted lines with the same content.

For example:

print ( "format me" )
print ( "format me" )
print ( "format me" )
print ( "format me" )
print ( "format me" )

Using --line-ranges=2-3, the result of the first pass is

print ( "format me" )
print("format me")
print("format me")
print ( "format me" )
print ( "format me" )

adjusted_lines will return 5-5 with the input of 2-3 and the original & formatted code. After the second pass the code becomes:

print ( "format me" )
print("format me")
print("format me")
print ( "format me" )
print("format me")

The end result is

  1. --line-range might format extra lines when there are unformatted lines with the exact same content.
  2. The stability check fails in --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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4