From 9d9b9f5fa4dd9406ff811198f446b5f77a3068bc Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 8 Nov 2023 20:14:23 -0800 Subject: [PATCH 1/5] Disable the stability check with --line-ranges for now. --- CHANGES.md | 3 ++ docs/usage_and_configuration/the_basics.md | 2 ++ src/black/__init__.py | 9 ++++-- .../data/cases/line_ranges_diff_edge_case.py | 28 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/data/cases/line_ranges_diff_edge_case.py diff --git a/CHANGES.md b/CHANGES.md index 9446927b8d1..03d4437123c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -18,6 +18,9 @@ +- `--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 diff --git a/docs/usage_and_configuration/the_basics.md b/docs/usage_and_configuration/the_basics.md index 6e7ee584cf9..9cbbc4e9496 100644 --- a/docs/usage_and_configuration/the_basics.md +++ b/docs/usage_and_configuration/the_basics.md @@ -192,6 +192,8 @@ 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". +Currently it also disables _Black_'s formatting stability check in `--safe` mode. + #### `--color` / `--no-color` Show (or do not show) colored diff. Only applies when `--diff` is given. diff --git a/src/black/__init__.py b/src/black/__init__.py index 2455e8648fc..b33beeeeb23 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -1465,11 +1465,16 @@ def assert_stable( src: str, dst: str, mode: Mode, *, lines: Collection[Tuple[int, int]] = () ) -> None: """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 # twice and may hide a bug where we bounce back and forth between two # versions. - if lines: - lines = adjusted_lines(lines, src, dst) newdst = _format_str_once(dst, mode=mode, lines=lines) if dst != newdst: log = dump_to_file( diff --git a/tests/data/cases/line_ranges_diff_edge_case.py b/tests/data/cases/line_ranges_diff_edge_case.py new file mode 100644 index 00000000000..113fac9447e --- /dev/null +++ b/tests/data/cases/line_ranges_diff_edge_case.py @@ -0,0 +1,28 @@ +# flags: --line-ranges=8-9 +# 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=8-9 +# 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") From 1a6206d769191852b5800269622e4a21b1a32a45 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Wed, 8 Nov 2023 20:53:13 -0800 Subject: [PATCH 2/5] Oops, fix the test case after last minute new comment. --- tests/data/cases/line_ranges_diff_edge_case.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/data/cases/line_ranges_diff_edge_case.py b/tests/data/cases/line_ranges_diff_edge_case.py index 113fac9447e..f5cb2d0bb5f 100644 --- a/tests/data/cases/line_ranges_diff_edge_case.py +++ b/tests/data/cases/line_ranges_diff_edge_case.py @@ -1,4 +1,4 @@ -# flags: --line-ranges=8-9 +# 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. @@ -13,11 +13,11 @@ print ( "format me" ) # output -# flags: --line-ranges=8-9 +# 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 +# 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. From c59268889ee512d90c8d5595dde9921bfdbcf4d3 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Sat, 18 Nov 2023 13:09:47 -0800 Subject: [PATCH 3/5] Update the documentation of `--line-ranges`. --- docs/usage_and_configuration/the_basics.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/usage_and_configuration/the_basics.md b/docs/usage_and_configuration/the_basics.md index 294bd93a9f0..57bc1be14b5 100644 --- a/docs/usage_and_configuration/the_basics.md +++ b/docs/usage_and_configuration/the_basics.md @@ -196,7 +196,11 @@ 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". -Currently it also disables _Black_'s formatting stability check in `--safe` mode. +```{note} +Due to #4052, `--line-ranges` might format extra lines outside of the ranges when there +are unformatted lines with the exact content. It also disables _Black_'s formatting +stability check in `--safe` mode. +``` #### `--color` / `--no-color` From 7366fc6fb0e73d63d0db98979b46e8be0b79d054 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Sat, 18 Nov 2023 13:14:59 -0800 Subject: [PATCH 4/5] Update docs/usage_and_configuration/the_basics.md Co-authored-by: Jelle Zijlstra --- docs/usage_and_configuration/the_basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage_and_configuration/the_basics.md b/docs/usage_and_configuration/the_basics.md index 57bc1be14b5..864c81e039a 100644 --- a/docs/usage_and_configuration/the_basics.md +++ b/docs/usage_and_configuration/the_basics.md @@ -197,7 +197,7 @@ 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". ```{note} -Due to #4052, `--line-ranges` might format extra lines outside of the ranges when there +Due to [#4052](https://github.com/psf/black/issues/4052), `--line-ranges` might format extra lines outside of the ranges when there are unformatted lines with the exact content. It also disables _Black_'s formatting stability check in `--safe` mode. ``` From 54b650a4ae0cd0ab7bd7841c503dc0da2c4aad02 Mon Sep 17 00:00:00 2001 From: Yilei Yang Date: Sat, 18 Nov 2023 13:15:24 -0800 Subject: [PATCH 5/5] Reformat. --- docs/usage_and_configuration/the_basics.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/usage_and_configuration/the_basics.md b/docs/usage_and_configuration/the_basics.md index 864c81e039a..0c1a4d3b5a1 100644 --- a/docs/usage_and_configuration/the_basics.md +++ b/docs/usage_and_configuration/the_basics.md @@ -197,9 +197,9 @@ 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". ```{note} -Due to [#4052](https://github.com/psf/black/issues/4052), `--line-ranges` might format extra lines outside of the ranges when there -are unformatted lines with the exact content. It also disables _Black_'s formatting -stability check in `--safe` mode. +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`