Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trailing comma when a single import doesn't fit on a line. #504

Merged
merged 2 commits into from Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -874,6 +874,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).

* *Black* no longer breaks ``async for`` statements up to separate lines (#372)

* trailing comma is now added to single imports that don't fit on a line (#250)


### 18.6b4

Expand Down
5 changes: 4 additions & 1 deletion black.py
Expand Up @@ -2179,7 +2179,7 @@ def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
yield result


def right_hand_split(
def right_hand_split( # noqa C901
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do this. There is one function in black that is essentially a giant if statement and that's whitespace. Everything else should conform to C901. I'll merge this as is and fix forward.

line: Line, line_length: int, py36: bool = False, omit: Collection[LeafID] = ()
) -> Iterator[Line]:
"""Split line into many lines, starting with the last matching bracket pair.
Expand Down Expand Up @@ -2220,6 +2220,9 @@ def right_hand_split(
# the matching `opening_bracket` wasn't available on `line` anymore.
raise CannotSplit("No brackets found")

if line.is_import and len(body_leaves) == 1:
body_leaves.append(Leaf(token.COMMA, ","))

# Build the new lines.
for result, leaves in (head, head_leaves), (body, body_leaves), (tail, tail_leaves):
for leaf in leaves:
Expand Down
2 changes: 1 addition & 1 deletion tests/data/import_spacing.py
Expand Up @@ -86,7 +86,7 @@
Use,
)
from name_of_a_company.extremely_long_project_name.component.ttypes import (
CuteLittleServiceHandlerFactoryyy
CuteLittleServiceHandlerFactoryyy,
)
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *

Expand Down