Skip to content

Commit d0287e1

Browse files
authored
Make trailing comma logic more concise (#4202)
Signed-off-by: RedGuy12 <[email protected]>
1 parent 0f18001 commit d0287e1

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

src/black/linegen.py

+7-19
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
BRACKETS,
3232
CLOSING_BRACKETS,
3333
OPENING_BRACKETS,
34-
RARROW,
3534
STANDALONE_COMMENT,
3635
STATEMENT,
3736
WHITESPACE,
3837
Visitor,
3938
ensure_visible,
39+
get_annotation_type,
4040
is_arith_like,
4141
is_async_stmt_or_funcdef,
4242
is_atom_with_invisible_parens,
@@ -1046,11 +1046,12 @@ def bracket_split_build_line(
10461046
result.inside_brackets = True
10471047
result.depth += 1
10481048
if leaves:
1049-
# Ensure a trailing comma for imports and standalone function arguments, but
1050-
# be careful not to add one after any comments or within type annotations.
10511049
no_commas = (
1050+
# Ensure a trailing comma for imports and standalone function arguments
10521051
original.is_def
1052+
# Don't add one after any comments or within type annotations
10531053
and opening_bracket.value == "("
1054+
# Don't add one if there's already one there
10541055
and not any(
10551056
leaf.type == token.COMMA
10561057
and (
@@ -1059,22 +1060,9 @@ def bracket_split_build_line(
10591060
)
10601061
for leaf in leaves
10611062
)
1062-
# In particular, don't add one within a parenthesized return annotation.
1063-
# Unfortunately the indicator we're in a return annotation (RARROW) may
1064-
# be defined directly in the parent node, the parent of the parent ...
1065-
# and so on depending on how complex the return annotation is.
1066-
# This isn't perfect and there's some false negatives but they are in
1067-
# contexts were a comma is actually fine.
1068-
and not any(
1069-
node.prev_sibling.type == RARROW
1070-
for node in (
1071-
leaves[0].parent,
1072-
getattr(leaves[0].parent, "parent", None),
1073-
)
1074-
if isinstance(node, Node) and isinstance(node.prev_sibling, Leaf)
1075-
)
1076-
# Except the false negatives above for PEP 604 unions where we
1077-
# can't add the comma.
1063+
# Don't add one inside parenthesized return annotations
1064+
and get_annotation_type(leaves[0]) != "return"
1065+
# Don't add one inside PEP 604 unions
10781066
and not (
10791067
leaves[0].parent
10801068
and leaves[0].parent.next_sibling

src/black/nodes.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,18 @@
33
"""
44

55
import sys
6-
from typing import Final, Generic, Iterator, List, Optional, Set, Tuple, TypeVar, Union
6+
from typing import (
7+
Final,
8+
Generic,
9+
Iterator,
10+
List,
11+
Literal,
12+
Optional,
13+
Set,
14+
Tuple,
15+
TypeVar,
16+
Union,
17+
)
718

819
if sys.version_info >= (3, 10):
920
from typing import TypeGuard
@@ -951,16 +962,21 @@ def is_number_token(nl: NL) -> TypeGuard[Leaf]:
951962
return nl.type == token.NUMBER
952963

953964

954-
def is_part_of_annotation(leaf: Leaf) -> bool:
955-
"""Returns whether this leaf is part of type annotations."""
965+
def get_annotation_type(leaf: Leaf) -> Literal["return", "param", None]:
966+
"""Returns the type of annotation this leaf is part of, if any."""
956967
ancestor = leaf.parent
957968
while ancestor is not None:
958969
if ancestor.prev_sibling and ancestor.prev_sibling.type == token.RARROW:
959-
return True
970+
return "return"
960971
if ancestor.parent and ancestor.parent.type == syms.tname:
961-
return True
972+
return "param"
962973
ancestor = ancestor.parent
963-
return False
974+
return None
975+
976+
977+
def is_part_of_annotation(leaf: Leaf) -> bool:
978+
"""Returns whether this leaf is part of a type annotation."""
979+
return get_annotation_type(leaf) is not None
964980

965981

966982
def first_leaf(node: LN) -> Optional[Leaf]:

0 commit comments

Comments
 (0)