Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

<!-- Changes that affect Black's stable style -->

Fix `ASTSafetyError` crash when return type contains one element and comments spanning
multiple lines. (#4444)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
8 changes: 8 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,14 @@ def bracket_split_build_line(
)
# Don't add one inside parenthesized return annotations
and get_annotation_type(leaves[0]) != "return"
# Don't add comment for single element return type with comments
and not (
sum(leaf.type is not STANDALONE_COMMENT for leaf in leaves) < 2
and any(
leaf.parent is not None and leaf.parent.type != syms.parameters
for leaf in leaves
)
)
# Don't add one inside PEP 604 unions
and not (
leaves[0].parent
Expand Down
50 changes: 50 additions & 0 deletions tests/data/cases/single_element_parametized_return_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
def MyClass():
pass


def create_my_nested_class() -> (
# test asnkdasldnsalkdnaskldnaklsdnsakln
int
):
class MyNestedClass(MyClass):
pass
return MyNestedClass


def create_my_nested_class() -> (
# test asnkdasldnsalkdnaskldnaklsdnsakln
# test asnkdasldnsalkdnaskldnaklsdnsakln
int
# test asnkdasldnsalkdnaskldnaklsdnsakln
):
class MyNestedClass(MyClass):
pass
return MyNestedClass

# output


def MyClass():
pass


def create_my_nested_class() -> (
# test asnkdasldnsalkdnaskldnaklsdnsakln
int
):
class MyNestedClass(MyClass):
pass

return MyNestedClass


def create_my_nested_class() -> (
# test asnkdasldnsalkdnaskldnaklsdnsakln
# test asnkdasldnsalkdnaskldnaklsdnsakln
int
# test asnkdasldnsalkdnaskldnaklsdnsakln
):
class MyNestedClass(MyClass):
pass

return MyNestedClass