From b93bf4a06a7cd8087513e52624ef543bffc0e31d Mon Sep 17 00:00:00 2001 From: wannieman98 <74849806+wannieman98@users.noreply.github.com> Date: Tue, 16 Jan 2024 01:35:43 -0800 Subject: [PATCH 01/13] Fix a bug where the doublestar operation had inconsistent formatting. --- CHANGES.md | 5 ++ src/black/trans.py | 104 ++++++++++++++++++++------- tests/data/cases/power_op_spacing.py | 10 +++ 3 files changed, 95 insertions(+), 24 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 4d646a2779e..100c391d222 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -160,6 +160,11 @@ release: - Fixed a bug that included dependencies from the `d` extra by default (#4108) +### Parser + +- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary + expression (#4149) + ## 23.12.0 ### Highlights diff --git a/src/black/trans.py b/src/black/trans.py index 7c7335a005b..77e9f651ebd 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -94,43 +94,30 @@ def hug_power_op( else: raise CannotTransform("No doublestar token was found in the line.") - def is_simple_lookup(index: int, step: Literal[1, -1]) -> bool: + def is_simple_lookup(index: int, kind: Literal[1, -1]) -> bool: # Brackets and parentheses indicate calls, subscripts, etc. ... # basically stuff that doesn't count as "simple". Only a NAME lookup # or dotted lookup (eg. NAME.NAME) is OK. - if step == -1: - disallowed = {token.RPAR, token.RSQB} + if kind == -1: + return handle_is_simple_look_up_prev(line, index, {token.RPAR, token.RSQB}) else: - disallowed = {token.LPAR, token.LSQB} - - while 0 <= index < len(line.leaves): - current = line.leaves[index] - if current.type in disallowed: - return False - if current.type not in {token.NAME, token.DOT} or current.value == "for": - # If the current token isn't disallowed, we'll assume this is simple as - # only the disallowed tokens are semantically attached to this lookup - # expression we're checking. Also, stop early if we hit the 'for' bit - # of a comprehension. - return True - - index += step - - return True + return handle_is_simple_lookup_forward( + line, index, {token.LPAR, token.LSQB} + ) - def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool: + def is_simple_operand(index: int, kind: Literal[1, -1]) -> bool: # An operand is considered "simple" if's a NAME, a numeric CONSTANT, a simple # lookup (see above), with or without a preceding unary operator. start = line.leaves[index] if start.type in {token.NAME, token.NUMBER}: - return is_simple_lookup(index, step=(1 if kind == "exponent" else -1)) + return is_simple_lookup(index, kind) if start.type in {token.PLUS, token.MINUS, token.TILDE}: if line.leaves[index + 1].type in {token.NAME, token.NUMBER}: # step is always one as bases with a preceding unary op will be checked # for simplicity starting from the next token (so it'll hit the check # above). - return is_simple_lookup(index + 1, step=1) + return is_simple_lookup(index + 1, kind=1) return False @@ -145,9 +132,9 @@ def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool: should_hug = ( (0 < idx < len(line.leaves) - 1) and leaf.type == token.DOUBLESTAR - and is_simple_operand(idx - 1, kind="base") + and is_simple_operand(idx - 1, kind=-1) and line.leaves[idx - 1].value != "lambda" - and is_simple_operand(idx + 1, kind="exponent") + and is_simple_operand(idx + 1, kind=1) ) if should_hug: new_leaf.prefix = "" @@ -162,6 +149,75 @@ def is_simple_operand(index: int, kind: Literal["base", "exponent"]) -> bool: yield new_line +def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) -> bool: + """ + Handling the determination of is_simple_lookup for the lines prior to + the doublestar token. This is required because of the need to isolate the chained + expression to determine the bracket or parenthesis belong to the single expression. + """ + contains_disallowed = False + has_changed = False + chain = [] + + while 0 <= index < len(line.leaves): + current = line.leaves[index] + chain.append(current) + if not has_changed and current.type in disallowed: + contains_disallowed = True + has_changed = True + if not is_expression_chained(chain): + return not contains_disallowed + + index += -1 + + return True + + +def handle_is_simple_lookup_forward( + line: Line, index: int, disallowed: Set[int] +) -> bool: + """ + Handling decision is_simple_lookup for the lines prior to the doublestar token. + This function is simplified to keep consistent with the prior logic and the forward + case are more straightforward and do not need to care about chained expressions. + """ + while 0 <= index < len(line.leaves): + current = line.leaves[index] + if current.type in disallowed: + return False + if current.type not in {token.NAME, token.DOT} or current.value == "for": + # If the current token isn't disallowed, we'll assume this is simple as + # only the disallowed tokens are semantically attached to this lookup + # expression we're checking. Also, stop early if we hit the 'for' bit + # of a comprehension. + return True + + index += 1 + + return True + + +def is_expression_chained(chained_leaves: List[Leaf]) -> bool: + """ + Function to determine if the variable is a chained call. + (e.g., foo.lookup, foo().lookup, (foo.lookup())) will be recognized as chained call) + """ + if len(chained_leaves) < 2: + return True + + current_leaf = chained_leaves[-1] + past_leaf = chained_leaves[-2] + + if past_leaf.type == token.NAME: + return current_leaf.type in {token.DOT} + elif past_leaf.type in {token.RPAR, token.RSQB}: + return current_leaf.type in {token.RSQB, token.RPAR} + elif past_leaf.type in {token.LPAR, token.LSQB}: + return current_leaf.type in {token.NAME, token.LPAR, token.LSQB} + else: + return False + + class StringTransformer(ABC): """ An implementation of the Transformer protocol that relies on its diff --git a/tests/data/cases/power_op_spacing.py b/tests/data/cases/power_op_spacing.py index b3ef0aae084..d48da25043d 100644 --- a/tests/data/cases/power_op_spacing.py +++ b/tests/data/cases/power_op_spacing.py @@ -1,3 +1,8 @@ +m2 = None if not isinstance(dist, Normal) else μ** 2 + σ * 2 +m3 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 +m4 = None if not isinstance(dist, Normal) else s**2 + σ * 2 +m5 = obj.method(another_obj.method()).attribute **2 +m6 = None if ... else μ**2 + σ**2 def function(**kwargs): t = a**2 + b**3 return t ** 2 @@ -78,6 +83,11 @@ def function_dont_replace_spaces(): # output +m2 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 +m3 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 +m4 = None if not isinstance(dist, Normal) else s**2 + σ * 2 +m5 = obj.method(another_obj.method()).attribute ** 2 +m6 = None if ... else μ**2 + σ**2 def function(**kwargs): From 09d1e92b572c23ad8e07ebc576146ca370c8b409 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Wed, 17 Jan 2024 03:59:04 +0900 Subject: [PATCH 02/13] Update CHANGES.md to include the correct PR number, not the issue number it is trying to fix --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 100c391d222..77b37c4a182 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -163,7 +163,7 @@ release: ### Parser - Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary - expression (#4149) + expression (#4154) ## 23.12.0 From 5da10ac08fad34a74078cf9dd77ea8b88c524003 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Tue, 16 Jan 2024 17:42:59 -0800 Subject: [PATCH 03/13] Move the change log to the unreleased section --- CHANGES.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 77b37c4a182..188895f4e6c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -34,6 +34,9 @@ +- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary + expression (#4154) + ### Performance @@ -160,11 +163,6 @@ release: - Fixed a bug that included dependencies from the `d` extra by default (#4108) -### Parser - -- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary - expression (#4154) - ## 23.12.0 ### Highlights From e9bdca6346412507fb96907e49159b7e07282f91 Mon Sep 17 00:00:00 2001 From: wannieman98 <74849806+wannieman98@users.noreply.github.com> Date: Mon, 22 Jan 2024 02:57:16 -0800 Subject: [PATCH 04/13] Move the changes comment into the preview section and remove the unnecessary boolean --- CHANGES.md | 5 ++--- src/black/trans.py | 4 +--- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 188895f4e6c..192f070f048 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,8 @@ - Move the `hug_parens_with_braces_and_square_brackets` feature to the unstable style due to an outstanding crash and proposed formatting tweaks (#4198) +- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary + expression (#4154) ### Configuration @@ -34,9 +36,6 @@ -- Fixed a bug where base expressions caused inconsistent formatting of \*\* in tenary - expression (#4154) - ### Performance diff --git a/src/black/trans.py b/src/black/trans.py index 77e9f651ebd..d6f3c954ad9 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -156,15 +156,13 @@ def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) expression to determine the bracket or parenthesis belong to the single expression. """ contains_disallowed = False - has_changed = False chain = [] while 0 <= index < len(line.leaves): current = line.leaves[index] chain.append(current) - if not has_changed and current.type in disallowed: + if not contains_disallowed and current.type in disallowed: contains_disallowed = True - has_changed = True if not is_expression_chained(chain): return not contains_disallowed From 2bbe85ddae9733515b7914ab15952c5fcb08e7a8 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Wed, 24 Jan 2024 07:00:39 +0900 Subject: [PATCH 05/13] Update CHANGES.md from `/*/*` to `**` --- docs/the_black_code_style/future_style.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index 86e5aa806b2..a0a78dbce90 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -28,6 +28,7 @@ Currently, the following features are included in the preview style: longer normalized - `typed_params_trailing_comma`: consistently add trailing commas to typed function parameters +- `is_simple_lookup_for_doublestar_expression`: fix line length computation for certain expressions that involve the power operator (labels/unstable-features)= From e2fa9f6f441a99d139e3e53fd69cb8c04ed5a3b3 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Sat, 27 Jan 2024 23:17:43 -0800 Subject: [PATCH 06/13] Apply suggestions from code review Co-authored-by: Jelle Zijlstra --- src/black/trans.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index d6f3c954ad9..60a31621a3b 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -114,7 +114,7 @@ def is_simple_operand(index: int, kind: Literal[1, -1]) -> bool: if start.type in {token.PLUS, token.MINUS, token.TILDE}: if line.leaves[index + 1].type in {token.NAME, token.NUMBER}: - # step is always one as bases with a preceding unary op will be checked + # kind is always one as bases with a preceding unary op will be checked # for simplicity starting from the next token (so it'll hit the check # above). return is_simple_lookup(index + 1, kind=1) @@ -166,7 +166,7 @@ def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) if not is_expression_chained(chain): return not contains_disallowed - index += -1 + index -= 1 return True From a6a22f36b38a3cb3cc5e9dda744c1b72bab09162 Mon Sep 17 00:00:00 2001 From: wannieman98 <74849806+wannieman98@users.noreply.github.com> Date: Sun, 28 Jan 2024 01:05:54 -0800 Subject: [PATCH 07/13] Add the newly updated is simple lookup function to the preview mode. Also update test case to use latin letters --- src/black/mode.py | 1 + src/black/trans.py | 46 ++++++++++++++++--- ...simple_lookup_for_doublestar_expression.py | 14 ++++++ tests/data/cases/power_op_spacing.py | 11 ----- 4 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 tests/data/cases/is_simple_lookup_for_doublestar_expression.py diff --git a/src/black/mode.py b/src/black/mode.py index 5738bd6b793..9101ff614d8 100644 --- a/src/black/mode.py +++ b/src/black/mode.py @@ -177,6 +177,7 @@ class Preview(Enum): wrap_long_dict_values_in_parens = auto() multiline_string_handling = auto() typed_params_trailing_comma = auto() + is_simple_lookup_for_doublestar_expression = auto() UNSTABLE_FEATURES: Set[Preview] = { diff --git a/src/black/trans.py b/src/black/trans.py index 60a31621a3b..d0fb690ff9b 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -29,7 +29,7 @@ from black.comments import contains_pragma_comment from black.lines import Line, append_leaves -from black.mode import Feature, Mode +from black.mode import Feature, Mode, Preview from black.nodes import ( CLOSING_BRACKETS, OPENING_BRACKETS, @@ -98,12 +98,18 @@ def is_simple_lookup(index: int, kind: Literal[1, -1]) -> bool: # Brackets and parentheses indicate calls, subscripts, etc. ... # basically stuff that doesn't count as "simple". Only a NAME lookup # or dotted lookup (eg. NAME.NAME) is OK. - if kind == -1: - return handle_is_simple_look_up_prev(line, index, {token.RPAR, token.RSQB}) + if Preview.is_simple_lookup_for_doublestar_expression not in mode: + return original_is_simple_lookup_func(line, index, kind) + else: - return handle_is_simple_lookup_forward( - line, index, {token.LPAR, token.LSQB} - ) + if kind == -1: + return handle_is_simple_look_up_prev( + line, index, {token.RPAR, token.RSQB} + ) + else: + return handle_is_simple_lookup_forward( + line, index, {token.LPAR, token.LSQB} + ) def is_simple_operand(index: int, kind: Literal[1, -1]) -> bool: # An operand is considered "simple" if's a NAME, a numeric CONSTANT, a simple @@ -149,6 +155,30 @@ def is_simple_operand(index: int, kind: Literal[1, -1]) -> bool: yield new_line +def original_is_simple_lookup_func( + line: Line, index: int, step: Literal[1, -1] +) -> bool: + if step == -1: + disallowed = {token.RPAR, token.RSQB} + else: + disallowed = {token.LPAR, token.LSQB} + + while 0 <= index < len(line.leaves): + current = line.leaves[index] + if current.type in disallowed: + return False + if current.type not in {token.NAME, token.DOT} or current.value == "for": + # If the current token isn't disallowed, we'll assume this is + # simple as only the disallowed tokens are semantically + # attached to this lookup expression we're checking. Also, + # stop early if we hit the 'for' bit of a comprehension. + return True + + index += step + + return True + + def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) -> bool: """ Handling the determination of is_simple_lookup for the lines prior to @@ -183,7 +213,9 @@ def handle_is_simple_lookup_forward( current = line.leaves[index] if current.type in disallowed: return False - if current.type not in {token.NAME, token.DOT} or current.value == "for": + if current.type not in {token.NAME, token.DOT} or ( + current.type == token.NAME and current.value == "for" + ): # If the current token isn't disallowed, we'll assume this is simple as # only the disallowed tokens are semantically attached to this lookup # expression we're checking. Also, stop early if we hit the 'for' bit diff --git a/tests/data/cases/is_simple_lookup_for_doublestar_expression.py b/tests/data/cases/is_simple_lookup_for_doublestar_expression.py new file mode 100644 index 00000000000..0ed49512ac4 --- /dev/null +++ b/tests/data/cases/is_simple_lookup_for_doublestar_expression.py @@ -0,0 +1,14 @@ +# flags: --preview +m2 = None if not isinstance(dist, Normal) else m** 2 + s * 2 +m3 = None if not isinstance(dist, Normal) else m**2 + s * 2 +m4 = None if not isinstance(dist, Normal) else s**2 + s * 2 +m5 = obj.method(another_obj.method()).attribute **2 +m6 = None if ... else m**2 + s**2 + + +# output +m2 = None if not isinstance(dist, Normal) else m**2 + s * 2 +m3 = None if not isinstance(dist, Normal) else m**2 + s * 2 +m4 = None if not isinstance(dist, Normal) else s**2 + s * 2 +m5 = obj.method(another_obj.method()).attribute ** 2 +m6 = None if ... else m**2 + s**2 \ No newline at end of file diff --git a/tests/data/cases/power_op_spacing.py b/tests/data/cases/power_op_spacing.py index d48da25043d..f161712e08c 100644 --- a/tests/data/cases/power_op_spacing.py +++ b/tests/data/cases/power_op_spacing.py @@ -1,8 +1,3 @@ -m2 = None if not isinstance(dist, Normal) else μ** 2 + σ * 2 -m3 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 -m4 = None if not isinstance(dist, Normal) else s**2 + σ * 2 -m5 = obj.method(another_obj.method()).attribute **2 -m6 = None if ... else μ**2 + σ**2 def function(**kwargs): t = a**2 + b**3 return t ** 2 @@ -83,12 +78,6 @@ def function_dont_replace_spaces(): # output -m2 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 -m3 = None if not isinstance(dist, Normal) else μ**2 + σ * 2 -m4 = None if not isinstance(dist, Normal) else s**2 + σ * 2 -m5 = obj.method(another_obj.method()).attribute ** 2 -m6 = None if ... else μ**2 + σ**2 - def function(**kwargs): t = a**2 + b**3 From a2419468fc0cc4c8d338c1f687217e05da403099 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Sun, 28 Jan 2024 01:07:12 -0800 Subject: [PATCH 08/13] Update power_op_spacing.py --- tests/data/cases/power_op_spacing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/data/cases/power_op_spacing.py b/tests/data/cases/power_op_spacing.py index f161712e08c..b3ef0aae084 100644 --- a/tests/data/cases/power_op_spacing.py +++ b/tests/data/cases/power_op_spacing.py @@ -79,6 +79,7 @@ def function_dont_replace_spaces(): # output + def function(**kwargs): t = a**2 + b**3 return t**2 From addc702332e29c9007a06a3ed740edcb5f5405a0 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Sun, 28 Jan 2024 01:10:22 -0800 Subject: [PATCH 09/13] Update trans.py docstring for handle_is_simple_look_up_prev --- src/black/trans.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index d0fb690ff9b..52be9392a21 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -181,9 +181,9 @@ def original_is_simple_lookup_func( def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) -> bool: """ - Handling the determination of is_simple_lookup for the lines prior to - the doublestar token. This is required because of the need to isolate the chained - expression to determine the bracket or parenthesis belong to the single expression. + Handling the determination of is_simple_lookup for the lines behind the doublestar + token. This is required because of the need to isolate the chained expression + to determine the bracket or parenthesis belong to the single expression. """ contains_disallowed = False chain = [] From f7e162171dc6ccc46c03ca1da582ca6b6178e5d9 Mon Sep 17 00:00:00 2001 From: Seung Wan Yoo <74849806+wannieman98@users.noreply.github.com> Date: Sun, 28 Jan 2024 01:14:48 -0800 Subject: [PATCH 10/13] Update trans.py --- src/black/trans.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index 52be9392a21..2668d0600f9 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -181,9 +181,9 @@ def original_is_simple_lookup_func( def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) -> bool: """ - Handling the determination of is_simple_lookup for the lines behind the doublestar + Handling the determination of is_simple_lookup for the lines prior to the doublestar token. This is required because of the need to isolate the chained expression - to determine the bracket or parenthesis belong to the single expression. + to determine the bracket or parenthesis belong to the single expression. """ contains_disallowed = False chain = [] @@ -205,9 +205,9 @@ def handle_is_simple_lookup_forward( line: Line, index: int, disallowed: Set[int] ) -> bool: """ - Handling decision is_simple_lookup for the lines prior to the doublestar token. + Handling decision is_simple_lookup for the lines behind to the doublestar token. This function is simplified to keep consistent with the prior logic and the forward - case are more straightforward and do not need to care about chained expressions. + case are more straightforward and do not need to care about chained expressions. """ while 0 <= index < len(line.leaves): current = line.leaves[index] From f514570f0f6714d61120f934cc8105077c59bf86 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 28 Jan 2024 09:15:07 +0000 Subject: [PATCH 11/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/trans.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/trans.py b/src/black/trans.py index 2668d0600f9..cd95155ccd0 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -183,7 +183,7 @@ def handle_is_simple_look_up_prev(line: Line, index: int, disallowed: Set[int]) """ Handling the determination of is_simple_lookup for the lines prior to the doublestar token. This is required because of the need to isolate the chained expression - to determine the bracket or parenthesis belong to the single expression. + to determine the bracket or parenthesis belong to the single expression. """ contains_disallowed = False chain = [] @@ -207,7 +207,7 @@ def handle_is_simple_lookup_forward( """ Handling decision is_simple_lookup for the lines behind to the doublestar token. This function is simplified to keep consistent with the prior logic and the forward - case are more straightforward and do not need to care about chained expressions. + case are more straightforward and do not need to care about chained expressions. """ while 0 <= index < len(line.leaves): current = line.leaves[index] From f2b7774c94d3ac3a1d8e48215e2887707449e47c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 5 Feb 2024 03:26:59 +0000 Subject: [PATCH 12/13] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/the_black_code_style/future_style.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/the_black_code_style/future_style.md b/docs/the_black_code_style/future_style.md index a0a78dbce90..7ef608d1a47 100644 --- a/docs/the_black_code_style/future_style.md +++ b/docs/the_black_code_style/future_style.md @@ -28,7 +28,8 @@ Currently, the following features are included in the preview style: longer normalized - `typed_params_trailing_comma`: consistently add trailing commas to typed function parameters -- `is_simple_lookup_for_doublestar_expression`: fix line length computation for certain expressions that involve the power operator +- `is_simple_lookup_for_doublestar_expression`: fix line length computation for certain + expressions that involve the power operator (labels/unstable-features)= From 75c965eb0d6accf86d180d2cc8ccdd77d4d9968d Mon Sep 17 00:00:00 2001 From: wannieman98 <74849806+wannieman98@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:00:44 -0800 Subject: [PATCH 13/13] Address comments in the revision to remove duplicate test case and fix grammer issue --- src/black/resources/black.schema.json | 3 ++- src/black/trans.py | 2 +- .../cases/is_simple_lookup_for_doublestar_expression.py | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/black/resources/black.schema.json b/src/black/resources/black.schema.json index 40bf39137f7..718411035d9 100644 --- a/src/black/resources/black.schema.json +++ b/src/black/resources/black.schema.json @@ -85,7 +85,8 @@ "no_normalize_fmt_skip_whitespace", "wrap_long_dict_values_in_parens", "multiline_string_handling", - "typed_params_trailing_comma" + "typed_params_trailing_comma", + "is_simple_lookup_for_doublestar_expression" ] }, "description": "Enable specific features included in the `--unstable` style. Requires `--preview`. No compatibility guarantees are provided on the behavior or existence of any unstable features." diff --git a/src/black/trans.py b/src/black/trans.py index cd95155ccd0..29a978c6b71 100644 --- a/src/black/trans.py +++ b/src/black/trans.py @@ -205,7 +205,7 @@ def handle_is_simple_lookup_forward( line: Line, index: int, disallowed: Set[int] ) -> bool: """ - Handling decision is_simple_lookup for the lines behind to the doublestar token. + Handling decision is_simple_lookup for the lines behind the doublestar token. This function is simplified to keep consistent with the prior logic and the forward case are more straightforward and do not need to care about chained expressions. """ diff --git a/tests/data/cases/is_simple_lookup_for_doublestar_expression.py b/tests/data/cases/is_simple_lookup_for_doublestar_expression.py index 0ed49512ac4..a0d2e2ba842 100644 --- a/tests/data/cases/is_simple_lookup_for_doublestar_expression.py +++ b/tests/data/cases/is_simple_lookup_for_doublestar_expression.py @@ -1,7 +1,7 @@ # flags: --preview m2 = None if not isinstance(dist, Normal) else m** 2 + s * 2 -m3 = None if not isinstance(dist, Normal) else m**2 + s * 2 -m4 = None if not isinstance(dist, Normal) else s**2 + s * 2 +m3 = None if not isinstance(dist, Normal) else m ** 2 + s * 2 +m4 = None if not isinstance(dist, Normal) else m**2 + s * 2 m5 = obj.method(another_obj.method()).attribute **2 m6 = None if ... else m**2 + s**2 @@ -9,6 +9,6 @@ # output m2 = None if not isinstance(dist, Normal) else m**2 + s * 2 m3 = None if not isinstance(dist, Normal) else m**2 + s * 2 -m4 = None if not isinstance(dist, Normal) else s**2 + s * 2 +m4 = None if not isinstance(dist, Normal) else m**2 + s * 2 m5 = obj.method(another_obj.method()).attribute ** 2 m6 = None if ... else m**2 + s**2 \ No newline at end of file