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
0 commit comments