From 9de9cd2ab9ee8dd3719aff8bfa01567ef02a695b Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 4 Aug 2022 21:09:29 -0700 Subject: [PATCH 1/5] Strip trailing commas in subscripts with -C Fixes #2296, #3204 --- src/black/lines.py | 18 +++++++++- src/black/mode.py | 1 + .../data/preview/skip_magic_trailing_comma.py | 34 +++++++++++++++++++ tests/test_format.py | 5 +++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/data/preview/skip_magic_trailing_comma.py diff --git a/src/black/lines.py b/src/black/lines.py index 1ebc7808901..b7b4a26adb2 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -273,6 +273,8 @@ def has_magic_trailing_comma( - it's not a single-element subscript Additionally, if ensure_removable: - it's not from square bracket indexing + - specifically, single-element square bracket indexing with + Preview.skip_magic_trailing_comma_in_subscript """ if not ( closing.type in CLOSING_BRACKETS @@ -301,8 +303,22 @@ def has_magic_trailing_comma( if not ensure_removable: return True + comma = self.leaves[-1] - return bool(comma.parent and comma.parent.type == syms.listmaker) + if comma.parent is None: + return False + if Preview.skip_magic_trailing_comma_in_subscript in self.mode: + return bool( + comma.parent.type != syms.subscriptlist + or closing.opening_bracket is None + or not is_one_sequence_between( + closing.opening_bracket, + closing, + self.leaves, + brackets=(token.LSQB, token.RSQB), + ) + ) + return comma.parent.type == syms.listmaker if self.is_import: return True diff --git a/src/black/mode.py b/src/black/mode.py index 32b65d16ca5..6e8f9f8b625 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -151,6 +151,7 @@ class Preview(Enum): remove_block_trailing_newline = auto() remove_redundant_parens = auto() string_processing = auto() + skip_magic_trailing_comma_in_subscript = auto() class Deprecated(UserWarning): diff --git a/tests/data/preview/skip_magic_trailing_comma.py b/tests/data/preview/skip_magic_trailing_comma.py new file mode 100644 index 00000000000..e98174af427 --- /dev/null +++ b/tests/data/preview/skip_magic_trailing_comma.py @@ -0,0 +1,34 @@ +# We should not remove the trailing comma in a single-element subscript. +a: tuple[int,] +b = tuple[int,] + +# But commas in multiple element subscripts should be removed. +c: tuple[int, int,] +d = tuple[int, int,] + +# Remove commas for non-subscripts. +small_list = [1,] +list_of_types = [tuple[int,],] +small_set = {1,} +set_of_types = {tuple[int,],} + +# Except single element tuples +small_tuple = (1,) + +# output +# We should not remove the trailing comma in a single-element subscript. +a: tuple[int,] +b = tuple[int,] + +# But commas in multiple element subscripts should be removed. +c: tuple[int, int] +d = tuple[int, int] + +# Remove commas for non-subscripts. +small_list = [1] +list_of_types = [tuple[int,]] +small_set = {1} +set_of_types = {tuple[int,]} + +# Except single element tuples +small_tuple = (1,) diff --git a/tests/test_format.py b/tests/test_format.py index 3645934721f..7b39a3b06ab 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -153,6 +153,11 @@ def test_preview_docstring_no_string_normalization() -> None: assert_format(source, expected, mode) +def test_skip_magic_trailing_comma_in_subscript() -> None: + source, expected = read_data("preview", "skip_magic_trailing_comma") + assert_format(source, expected, mode=black.Mode(magic_trailing_comma=False, preview=True)) + + def test_long_strings_flag_disabled() -> None: """Tests for turning off the string processing logic.""" source, expected = read_data("miscellaneous", "long_strings_flag_disabled") From 7167fc5bb0989f16d81ac13804be0aecdf4ced3d Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 4 Aug 2022 21:16:18 -0700 Subject: [PATCH 2/5] hush now machines --- src/black/lines.py | 4 ++-- tests/test_format.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/black/lines.py b/src/black/lines.py index b7b4a26adb2..826a80f29c7 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -273,8 +273,8 @@ def has_magic_trailing_comma( - it's not a single-element subscript Additionally, if ensure_removable: - it's not from square bracket indexing - - specifically, single-element square bracket indexing with - Preview.skip_magic_trailing_comma_in_subscript + (specifically, single-element square bracket indexing with + Preview.skip_magic_trailing_comma_in_subscript) """ if not ( closing.type in CLOSING_BRACKETS diff --git a/tests/test_format.py b/tests/test_format.py index 7b39a3b06ab..01cd61eef63 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -36,7 +36,12 @@ def test_simple_format(filename: str) -> None: @pytest.mark.parametrize("filename", all_data_cases("preview")) def test_preview_format(filename: str) -> None: - check_file("preview", filename, black.Mode(preview=True)) + magic_trailing_comma = filename != "skip_magic_trailing_comma" + check_file( + "preview", + filename, + black.Mode(preview=True, magic_trailing_comma=magic_trailing_comma), + ) @pytest.mark.parametrize("filename", all_data_cases("preview_39")) @@ -153,11 +158,6 @@ def test_preview_docstring_no_string_normalization() -> None: assert_format(source, expected, mode) -def test_skip_magic_trailing_comma_in_subscript() -> None: - source, expected = read_data("preview", "skip_magic_trailing_comma") - assert_format(source, expected, mode=black.Mode(magic_trailing_comma=False, preview=True)) - - def test_long_strings_flag_disabled() -> None: """Tests for turning off the string processing logic.""" source, expected = read_data("miscellaneous", "long_strings_flag_disabled") From e28da68f65505362be6cee3713e279d406e81dba Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 4 Aug 2022 21:23:08 -0700 Subject: [PATCH 3/5] changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 5b29f20bfff..d4b13922d6d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,6 +24,8 @@ this is invalid. This was a bug introduced in version 22.6.0. (#3166) - `--skip-string-normalization` / `-S` now prevents docstring prefixes from being normalized as expected (#3168) +- When using `--skip-magic-trailing-comma` or `-C`, trailing commas are stripped + from subscript expressions with more than 1 element (#3209) ### _Blackd_ From f2d349fae5ddab0553611e380b127240aa8eb394 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 4 Aug 2022 21:24:59 -0700 Subject: [PATCH 4/5] srsly okay okay okay okay --- CHANGES.md | 4 ++-- src/black/lines.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d4b13922d6d..5e4286cf65e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -24,8 +24,8 @@ this is invalid. This was a bug introduced in version 22.6.0. (#3166) - `--skip-string-normalization` / `-S` now prevents docstring prefixes from being normalized as expected (#3168) -- When using `--skip-magic-trailing-comma` or `-C`, trailing commas are stripped - from subscript expressions with more than 1 element (#3209) +- When using `--skip-magic-trailing-comma` or `-C`, trailing commas are stripped from + subscript expressions with more than 1 element (#3209) ### _Blackd_ diff --git a/src/black/lines.py b/src/black/lines.py index 826a80f29c7..63914c9c785 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -273,8 +273,8 @@ def has_magic_trailing_comma( - it's not a single-element subscript Additionally, if ensure_removable: - it's not from square bracket indexing - (specifically, single-element square bracket indexing with - Preview.skip_magic_trailing_comma_in_subscript) + (specifically, single-element square bracket indexing with + Preview.skip_magic_trailing_comma_in_subscript) """ if not ( closing.type in CLOSING_BRACKETS From 4cab5c77cbfa03ce32b7e666e7561f12785c0834 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 12 Aug 2022 22:07:33 -0700 Subject: [PATCH 5/5] fix jelle nit --- src/black/lines.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/lines.py b/src/black/lines.py index 63914c9c785..30622650d53 100644 --- a/src/black/lines.py +++ b/src/black/lines.py @@ -308,7 +308,7 @@ def has_magic_trailing_comma( if comma.parent is None: return False if Preview.skip_magic_trailing_comma_in_subscript in self.mode: - return bool( + return ( comma.parent.type != syms.subscriptlist or closing.opening_bracket is None or not is_one_sequence_between(