]> git.madduck.net Git - etc/vim.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Fix invalid code on stars in long from-imports being wrapped in parentheses
authorŁukasz Langa <lukasz@langa.pl>
Mon, 21 May 2018 23:35:45 +0000 (16:35 -0700)
committerŁukasz Langa <lukasz@langa.pl>
Mon, 21 May 2018 23:37:29 +0000 (16:37 -0700)
Fixes #234

README.md
black.py
tests/fmtonoff.py
tests/import_spacing.py

index 9b42f20a10f7fe9b27348cd19afb44dd484a15f5..90731d231c2171ba130134e2a52e9f59726e211a 100644 (file)
--- a/README.md
+++ b/README.md
@@ -661,6 +661,8 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
 
 * fixed optional parentheses being removed within `# fmt: off` sections (#224)
 
 
 * fixed optional parentheses being removed within `# fmt: off` sections (#224)
 
+* fixed invalid code produced when stars in very long imports were incorrectly 
+  wrapped in optional parentheses (#234)
 
 ### 18.5b0
 
 
 ### 18.5b0
 
index 9fbacc1048cd15fd7816ccdfb8dd3f6dd7252b9c..afc37d99fe3ac547383c1fa3c59cfba763aefc51 100644 (file)
--- a/black.py
+++ b/black.py
@@ -1375,32 +1375,6 @@ class LineGenerator(Visitor[Line]):
             yield from self.line()
             yield from self.visit(child)
 
             yield from self.line()
             yield from self.visit(child)
 
-    def visit_import_from(self, node: Node) -> Iterator[Line]:
-        """Visit import_from and maybe put invisible parentheses.
-
-        This is separate from `visit_stmt` because import statements don't
-        support arbitrary atoms and thus handling of parentheses is custom.
-        """
-        check_lpar = False
-        for index, child in enumerate(node.children):
-            if check_lpar:
-                if child.type == token.LPAR:
-                    # make parentheses invisible
-                    child.value = ""  # type: ignore
-                    node.children[-1].value = ""  # type: ignore
-                else:
-                    # insert invisible parentheses
-                    node.insert_child(index, Leaf(token.LPAR, ""))
-                    node.append_child(Leaf(token.RPAR, ""))
-                break
-
-            check_lpar = (
-                child.type == token.NAME and child.value == "import"  # type: ignore
-            )
-
-        for child in node.children:
-            yield from self.visit(child)
-
     def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
         """Remove a semicolon and put the other statement on a separate line."""
         yield from self.line()
     def visit_SEMI(self, leaf: Leaf) -> Iterator[Line]:
         """Remove a semicolon and put the other statement on a separate line."""
         yield from self.line()
@@ -1447,6 +1421,7 @@ class LineGenerator(Visitor[Line]):
         self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
         self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
         self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
         self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
         self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
         self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
+        self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
         self.visit_async_funcdef = self.visit_async_stmt
         self.visit_decorated = self.visit_decorators
 
         self.visit_async_funcdef = self.visit_async_stmt
         self.visit_decorated = self.visit_decorators
 
@@ -2343,7 +2318,7 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
         return  # This `node` has a prefix with `# fmt: off`, don't mess with parens.
 
     check_lpar = False
         return  # This `node` has a prefix with `# fmt: off`, don't mess with parens.
 
     check_lpar = False
-    for child in list(node.children):
+    for index, child in enumerate(list(node.children)):
         if check_lpar:
             if child.type == syms.atom:
                 maybe_make_parens_invisible_in_atom(child)
         if check_lpar:
             if child.type == syms.atom:
                 maybe_make_parens_invisible_in_atom(child)
@@ -2351,8 +2326,21 @@ def normalize_invisible_parens(node: Node, parens_after: Set[str]) -> None:
                 # wrap child in visible parentheses
                 lpar = Leaf(token.LPAR, "(")
                 rpar = Leaf(token.RPAR, ")")
                 # wrap child in visible parentheses
                 lpar = Leaf(token.LPAR, "(")
                 rpar = Leaf(token.RPAR, ")")
-                index = child.remove() or 0
+                child.remove()
                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
                 node.insert_child(index, Node(syms.atom, [lpar, child, rpar]))
+            elif node.type == syms.import_from:
+                # "import from" nodes store parentheses directly as part of
+                # the statement
+                if child.type == token.LPAR:
+                    # make parentheses invisible
+                    child.value = ""  # type: ignore
+                    node.children[-1].value = ""  # type: ignore
+                elif child.type != token.STAR:
+                    # insert invisible parentheses
+                    node.insert_child(index, Leaf(token.LPAR, ""))
+                    node.append_child(Leaf(token.RPAR, ""))
+                break
+
             elif not (isinstance(child, Leaf) and is_multiline_string(child)):
                 # wrap child in invisible parentheses
                 lpar = Leaf(token.LPAR, "")
             elif not (isinstance(child, Leaf) and is_multiline_string(child)):
                 # wrap child in invisible parentheses
                 lpar = Leaf(token.LPAR, "")
index e76e6a7fcd671cbaf81682bc2c7d0f02e28e9b49..5666dd88c47c73229f0215de6b5355eb161b76b0 100644 (file)
@@ -6,6 +6,10 @@ from third_party import X, Y, Z
 
 from library import some_connection, \
                     some_decorator
 
 from library import some_connection, \
                     some_decorator
+# fmt: off
+from third_party import (X,
+                         Y, Z)
+# fmt: on
 f'trigger 3.6 mode'
 # fmt: off
 def func_no_args():
 f'trigger 3.6 mode'
 # fmt: off
 def func_no_args():
@@ -102,6 +106,10 @@ from third_party import X, Y, Z
 
 from library import some_connection, some_decorator
 
 
 from library import some_connection, some_decorator
 
+# fmt: off
+from third_party import (X,
+                         Y, Z)
+# fmt: on
 f"trigger 3.6 mode"
 # fmt: off
 def func_no_args():
 f"trigger 3.6 mode"
 # fmt: off
 def func_no_args():
index cefa1e96754b105857d4f397282c84b25ec41be9..0c98650f8339c56728442cdca93345d268c045c7 100644 (file)
@@ -24,6 +24,7 @@ from some_library import (
     Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
 )
 from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
     Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
 )
 from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
+from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
 
 from .a.b.c.subprocess import *
 from . import (tasks)
 
 from .a.b.c.subprocess import *
 from . import (tasks)
@@ -87,6 +88,7 @@ from some_library import (
 from name_of_a_company.extremely_long_project_name.component.ttypes import (
     CuteLittleServiceHandlerFactoryyy
 )
 from name_of_a_company.extremely_long_project_name.component.ttypes import (
     CuteLittleServiceHandlerFactoryyy
 )
+from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *
 
 from .a.b.c.subprocess import *
 from . import tasks
 
 from .a.b.c.subprocess import *
 from . import tasks