From 014ee6faf8fe6a6b8c72da9485488f1e26bf9cc6 Mon Sep 17 00:00:00 2001 From: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:26:39 -0600 Subject: [PATCH 1/6] Make trailing comma logic more consise Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> --- src/black/linegen.py | 24 ++++++------------------ src/black/nodes.py | 28 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index c74ff9c0b4b..4d9cc4612c7 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1037,11 +1037,12 @@ def bracket_split_build_line( result.inside_brackets = True result.depth += 1 if leaves: - # Ensure a trailing comma for imports and standalone function arguments, but - # be careful not to add one after any comments or within type annotations. no_commas = ( + # Ensure a trailing comma for imports and standalone function arguments original.is_def + # Don't add one after any comments or within type annotations and opening_bracket.value == "(" + # Don't add one if there's already one there and not any( leaf.type == token.COMMA and ( @@ -1050,22 +1051,9 @@ def bracket_split_build_line( ) for leaf in leaves ) - # In particular, don't add one within a parenthesized return annotation. - # Unfortunately the indicator we're in a return annotation (RARROW) may - # be defined directly in the parent node, the parent of the parent ... - # and so on depending on how complex the return annotation is. - # This isn't perfect and there's some false negatives but they are in - # contexts were a comma is actually fine. - and not any( - node.prev_sibling.type == RARROW - for node in ( - leaves[0].parent, - getattr(leaves[0].parent, "parent", None), - ) - if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf) - ) - # Except the false negatives above for PEP 604 unions where we - # can't add the comma. + # Don't add one inside parenthesized return annotations + and not get_annotation_type(leaves[0]) == "return" + # Don't add one inside PEP 604 unions. and not ( leaves[0].parent and leaves[0].parent.next_sibling diff --git a/src/black/nodes.py b/src/black/nodes.py index a8869cba234..a062a6f6731 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -3,7 +3,18 @@ """ import sys -from typing import Final, Generic, Iterator, List, Optional, Set, Tuple, TypeVar, Union +from typing import ( + Final, + Generic, + Iterator, + List, + Optional, + Set, + Tuple, + TypeVar, + Union, + Literal, +) if sys.version_info >= (3, 10): from typing import TypeGuard @@ -951,16 +962,21 @@ def is_number_token(nl: NL) -> TypeGuard[Leaf]: return nl.type == token.NUMBER -def is_part_of_annotation(leaf: Leaf) -> bool: - """Returns whether this leaf is part of type annotations.""" +def get_annotation_type(leaf: Leaf) -> Optional[Literal["return", "param"]]: + """Returns the type of annotation this leaf is part of, if any.""" ancestor = leaf.parent while ancestor is not None: if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW: - return True + return "return" if ancestor.parent and ancestor.parent.type == syms.tname: - return True + return "param" ancestor = ancestor.parent - return False + return None + + +def is_part_of_annotation(leaf: Leaf) -> bool: + """Returns whether this leaf is part of a type annotation.""" + return get_annotation_type(leaf) is not None def first_leaf(node: LN) -> Optional[Leaf]: From 40eda29dd30d29a4bde63a9acff56a68c58592b0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 1 Feb 2024 16:29:35 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/black/nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/black/nodes.py b/src/black/nodes.py index a062a6f6731..27af7d5ce9e 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -8,12 +8,12 @@ Generic, Iterator, List, + Literal, Optional, Set, Tuple, TypeVar, Union, - Literal, ) if sys.version_info >= (3, 10): From 26f912fe6f620f5d6c7a3d4912b7a5728830ceba Mon Sep 17 00:00:00 2001 From: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:31:49 -0600 Subject: [PATCH 3/6] fix: forgotten import Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> --- src/black/linegen.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 4d9cc4612c7..dcb1feb0d74 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -37,6 +37,7 @@ WHITESPACE, Visitor, ensure_visible, + get_annotation_type, is_arith_like, is_async_stmt_or_funcdef, is_atom_with_invisible_parens, @@ -1053,7 +1054,7 @@ def bracket_split_build_line( ) # Don't add one inside parenthesized return annotations and not get_annotation_type(leaves[0]) == "return" - # Don't add one inside PEP 604 unions. + # Don't add one inside PEP 604 unions and not ( leaves[0].parent and leaves[0].parent.next_sibling From 00593d2e2d6752fa74b2d36bc7cc53f0039b55a7 Mon Sep 17 00:00:00 2001 From: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> Date: Thu, 1 Feb 2024 10:58:02 -0600 Subject: [PATCH 4/6] fix: other forgotten import Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> --- src/black/linegen.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index dcb1feb0d74..4ef27d7aa98 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -31,7 +31,6 @@ BRACKETS, CLOSING_BRACKETS, OPENING_BRACKETS, - RARROW, STANDALONE_COMMENT, STATEMENT, WHITESPACE, From a78ee8b29a85928f81bd2e6eae176f7341a7b343 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 1 Feb 2024 21:58:15 -0800 Subject: [PATCH 5/6] Apply suggestions from code review --- src/black/linegen.py | 2 +- src/black/nodes.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/black/linegen.py b/src/black/linegen.py index 4ef27d7aa98..168b5e4930c 100644 --- a/src/black/linegen.py +++ b/src/black/linegen.py @@ -1052,7 +1052,7 @@ def bracket_split_build_line( for leaf in leaves ) # Don't add one inside parenthesized return annotations - and not get_annotation_type(leaves[0]) == "return" + and get_annotation_type(leaves[0]) != "return" # Don't add one inside PEP 604 unions and not ( leaves[0].parent diff --git a/src/black/nodes.py b/src/black/nodes.py index 27af7d5ce9e..c0dca6e5783 100644 --- a/src/black/nodes.py +++ b/src/black/nodes.py @@ -962,7 +962,7 @@ def is_number_token(nl: NL) -> TypeGuard[Leaf]: return nl.type == token.NUMBER -def get_annotation_type(leaf: Leaf) -> Optional[Literal["return", "param"]]: +def get_annotation_type(leaf: Leaf) -> Literal["return", "param", None]: """Returns the type of annotation this leaf is part of, if any.""" ancestor = leaf.parent while ancestor is not None: From 266c8e5963d3b62d4a13102a3777a8e09de9a69d Mon Sep 17 00:00:00 2001 From: RedGuy12 <61329810+RedGuy12@users.noreply.github.com> Date: Mon, 5 Feb 2024 10:44:27 -0600 Subject: [PATCH 6/6] empty commit for ci Signed-off-by: RedGuy12 <61329810+RedGuy12@users.noreply.github.com>