31
31
BRACKETS ,
32
32
CLOSING_BRACKETS ,
33
33
OPENING_BRACKETS ,
34
- RARROW ,
35
34
STANDALONE_COMMENT ,
36
35
STATEMENT ,
37
36
WHITESPACE ,
38
37
Visitor ,
39
38
ensure_visible ,
39
+ get_annotation_type ,
40
40
is_arith_like ,
41
41
is_async_stmt_or_funcdef ,
42
42
is_atom_with_invisible_parens ,
@@ -1046,11 +1046,12 @@ def bracket_split_build_line(
1046
1046
result .inside_brackets = True
1047
1047
result .depth += 1
1048
1048
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.
1051
1049
no_commas = (
1050
+ # Ensure a trailing comma for imports and standalone function arguments
1052
1051
original .is_def
1052
+ # Don't add one after any comments or within type annotations
1053
1053
and opening_bracket .value == "("
1054
+ # Don't add one if there's already one there
1054
1055
and not any (
1055
1056
leaf .type == token .COMMA
1056
1057
and (
@@ -1059,22 +1060,9 @@ def bracket_split_build_line(
1059
1060
)
1060
1061
for leaf in leaves
1061
1062
)
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
1078
1066
and not (
1079
1067
leaves [0 ].parent
1080
1068
and leaves [0 ].parent .next_sibling
0 commit comments