From: Ɓukasz Langa Date: Wed, 26 Sep 2018 15:26:35 +0000 (-0700) Subject: Add trailing comma for single `as` imports, too X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/1610fd6bc5d594c4f27698825913d2f791d3ea02?hp=0c5c5374313566dd9047c3e992a3c23a7ea4b8f2 Add trailing comma for single `as` imports, too --- diff --git a/black.py b/black.py index 84ae9f1..2eefe11 100644 --- a/black.py +++ b/black.py @@ -2329,8 +2329,9 @@ def bracket_split_build_line( # Since body is a new indent level, remove spurious leading whitespace. normalize_prefix(leaves[0], inside_brackets=True) # Ensure a trailing comma when expected. - if original.is_import and len(leaves) == 1: - leaves.append(Leaf(token.COMMA, ",")) + if original.is_import: + if leaves[-1].type != token.COMMA: + leaves.append(Leaf(token.COMMA, ",")) # Populate the line for leaf in leaves: result.append(leaf, preformatted=True) diff --git a/docs/reference/reference_functions.rst b/docs/reference/reference_functions.rst index ccea313..dffde25 100644 --- a/docs/reference/reference_functions.rst +++ b/docs/reference/reference_functions.rst @@ -79,6 +79,8 @@ Parsing Split functions --------------- +.. autofunction:: black.bracket_split_build_line + .. autofunction:: black.bracket_split_succeeded_or_raise .. autofunction:: black.delimiter_split diff --git a/tests/data/comments2.py b/tests/data/comments2.py index 32a2ab3..53d4881 100644 --- a/tests/data/comments2.py +++ b/tests/data/comments2.py @@ -1,3 +1,10 @@ +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent # NOT DRY +) +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent as component # DRY +) + # Please keep __all__ alphabetized within each category. __all__ = [ @@ -148,6 +155,13 @@ instruction() # output +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent, # NOT DRY +) +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent as component, # DRY +) + # Please keep __all__ alphabetized within each category. __all__ = [ diff --git a/tests/data/comments4.py b/tests/data/comments4.py index 013684c..c0db5b1 100644 --- a/tests/data/comments4.py +++ b/tests/data/comments4.py @@ -1,3 +1,11 @@ +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent, # NOT DRY +) +from com.my_lovely_company.my_lovely_team.my_lovely_project.my_lovely_component import ( + MyLovelyCompanyTeamProjectComponent as component, # DRY +) + + class C: @pytest.mark.parametrize( ("post_data", "message"),