From 25d24a10a4f4cdf81b7335615c74f285127b1a04 Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Wed, 26 Sep 2018 13:26:50 +0100 Subject: [PATCH] Add trailing comma when a single import doesn't fit on a line. (#504) Fixes #250. --- README.md | 2 ++ black.py | 5 ++++- tests/data/import_spacing.py | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b56af4a..192f47e 100644 --- a/README.md +++ b/README.md @@ -964,6 +964,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md). to be a bad idea (#415) +* trailing comma is now added to single imports that don't fit on a line (#250) + ### 18.6b4 diff --git a/black.py b/black.py index 5676531..f76f0ff 100644 --- a/black.py +++ b/black.py @@ -2209,7 +2209,7 @@ def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]: yield result -def right_hand_split( +def right_hand_split( # noqa C901 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. @@ -2250,6 +2250,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: diff --git a/tests/data/import_spacing.py b/tests/data/import_spacing.py index 0c98650..51cfda2 100644 --- a/tests/data/import_spacing.py +++ b/tests/data/import_spacing.py @@ -86,7 +86,7 @@ from some_library import ( 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 * -- 2.39.5