Skip to content

Commit

Permalink
Cover more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Jul 30, 2021
1 parent 1495f48 commit 99bceb1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/black/linegen.py
Expand Up @@ -575,10 +575,19 @@ def bracket_split_build_line(
and opening_bracket.value == "("
and not any(leaf.type == token.COMMA for leaf in leaves)
# In particular, don't add one within a parenthesized return annotation.
and not (
leaves[0].parent
and leaves[0].parent.prev_sibling
and leaves[0].parent.prev_sibling.type == RARROW
# Unfortunately the indicator we're in a return annotation (RARROW) may
# be defined directly in the parent node OR the *parent of the parent*
# depending on how complex the return annotation is. Oh yeah and we
# have to be careful to not trigger AttributeError making this unpretty.
# 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)
)
)

Expand Down
44 changes: 44 additions & 0 deletions tests/data/function_trailing_comma.py
Expand Up @@ -37,6 +37,24 @@ def some_method_with_a_really_long_name(very_long_parameter_so_yeah: str, anothe
pass


def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(this_shouldn_t_get_a_trailing_comma_too)
):
pass


def func() -> ((also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
))
):
pass


def func() -> ( a:= also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)):
pass

# output

def f(
Expand Down Expand Up @@ -118,3 +136,29 @@ def some_method_with_a_really_long_name(
another_case_of_returning_a_deeply_nested_import_of_a_type_i_suppose_cause_why_not
):
pass


def func() -> (
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass


def func() -> (
(
also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
)
):
pass


def func() -> (
a := also_super_long_type_annotation_that_may_cause_an_AST_related_crash_in_black(
this_shouldn_t_get_a_trailing_comma_too
)
):
pass

0 comments on commit 99bceb1

Please sign in to comment.