Disable the stability check with --line-ranges for now. (#4034)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
89e28ea66f
commit
a8062983cd
@ -21,6 +21,9 @@
|
|||||||
|
|
||||||
<!-- Changes to how Black can be configured -->
|
<!-- Changes to how Black can be configured -->
|
||||||
|
|
||||||
|
- `--line-ranges` now skips _Black_'s internal stability check in `--safe` mode. This
|
||||||
|
avoids a crash on rare inputs that have many unformatted same-content lines. (#4034)
|
||||||
|
|
||||||
### Packaging
|
### Packaging
|
||||||
|
|
||||||
- Upgrade to mypy 1.6.1 (#4049)
|
- Upgrade to mypy 1.6.1 (#4049)
|
||||||
|
@ -196,6 +196,12 @@ Example: `black --line-ranges=1-10 --line-ranges=21-30 test.py` will format line
|
|||||||
|
|
||||||
This option is mainly for editor integrations, such as "Format Selection".
|
This option is mainly for editor integrations, such as "Format Selection".
|
||||||
|
|
||||||
|
```{note}
|
||||||
|
Due to [#4052](https://github.com/psf/black/issues/4052), `--line-ranges` might format
|
||||||
|
extra lines outside of the ranges when ther are unformatted lines with the exact
|
||||||
|
content. It also disables _Black_'s formatting stability check in `--safe` mode.
|
||||||
|
```
|
||||||
|
|
||||||
#### `--color` / `--no-color`
|
#### `--color` / `--no-color`
|
||||||
|
|
||||||
Show (or do not show) colored diff. Only applies when `--diff` is given.
|
Show (or do not show) colored diff. Only applies when `--diff` is given.
|
||||||
|
@ -1465,11 +1465,16 @@ def assert_stable(
|
|||||||
src: str, dst: str, mode: Mode, *, lines: Collection[Tuple[int, int]] = ()
|
src: str, dst: str, mode: Mode, *, lines: Collection[Tuple[int, int]] = ()
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Raise AssertionError if `dst` reformats differently the second time."""
|
"""Raise AssertionError if `dst` reformats differently the second time."""
|
||||||
|
if lines:
|
||||||
|
# Formatting specified lines requires `adjusted_lines` to map original lines
|
||||||
|
# to the formatted lines before re-formatting the previously formatted result.
|
||||||
|
# Due to less-ideal diff algorithm, some edge cases produce incorrect new line
|
||||||
|
# ranges. Hence for now, we skip the stable check.
|
||||||
|
# See https://github.com/psf/black/issues/4033 for context.
|
||||||
|
return
|
||||||
# We shouldn't call format_str() here, because that formats the string
|
# We shouldn't call format_str() here, because that formats the string
|
||||||
# twice and may hide a bug where we bounce back and forth between two
|
# twice and may hide a bug where we bounce back and forth between two
|
||||||
# versions.
|
# versions.
|
||||||
if lines:
|
|
||||||
lines = adjusted_lines(lines, src, dst)
|
|
||||||
newdst = _format_str_once(dst, mode=mode, lines=lines)
|
newdst = _format_str_once(dst, mode=mode, lines=lines)
|
||||||
if dst != newdst:
|
if dst != newdst:
|
||||||
log = dump_to_file(
|
log = dump_to_file(
|
||||||
|
28
tests/data/cases/line_ranges_diff_edge_case.py
Normal file
28
tests/data/cases/line_ranges_diff_edge_case.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
# flags: --line-ranges=10-11
|
||||||
|
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
|
||||||
|
# flag above as it's formatting specifically these lines.
|
||||||
|
|
||||||
|
# Reproducible example for https://github.com/psf/black/issues/4033.
|
||||||
|
# This can be fixed in the future if we use a better diffing algorithm, or make Black
|
||||||
|
# perform formatting in a single pass.
|
||||||
|
|
||||||
|
print ( "format me" )
|
||||||
|
print ( "format me" )
|
||||||
|
print ( "format me" )
|
||||||
|
print ( "format me" )
|
||||||
|
print ( "format me" )
|
||||||
|
|
||||||
|
# output
|
||||||
|
# flags: --line-ranges=10-11
|
||||||
|
# NOTE: If you need to modify this file, pay special attention to the --line-ranges=
|
||||||
|
# flag above as it's formatting specifically these lines.
|
||||||
|
|
||||||
|
# Reproducible example for https://github.com/psf/black/issues/4033.
|
||||||
|
# This can be fixed in the future if we use a better diffing algorithm, or make Black
|
||||||
|
# perform formatting in a single pass.
|
||||||
|
|
||||||
|
print ( "format me" )
|
||||||
|
print("format me")
|
||||||
|
print("format me")
|
||||||
|
print("format me")
|
||||||
|
print("format me")
|
Loading…
Reference in New Issue
Block a user