From c9b73b99ac567ce30c36596371bf0a1fad8191c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C5=81ukasz=20Langa?= Date: Tue, 8 May 2018 20:59:18 -0700 Subject: [PATCH] Support nested lambdas in BracketTracker --- black.py | 12 ++++++------ tests/expression.diff | 9 +++++---- tests/expression.py | 2 ++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/black.py b/black.py index 00d5758..c2363e4 100644 --- a/black.py +++ b/black.py @@ -647,8 +647,8 @@ class BracketTracker: bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict) delimiters: Dict[LeafID, Priority] = Factory(dict) previous: Optional[Leaf] = None - _for_loop_variable: bool = False - _lambda_arguments: bool = False + _for_loop_variable: int = 0 + _lambda_arguments: int = 0 def mark(self, leaf: Leaf) -> None: """Mark `leaf` with bracket-related metadata. Keep track of delimiters. @@ -710,7 +710,7 @@ class BracketTracker: """ if leaf.type == token.NAME and leaf.value == "for": self.depth += 1 - self._for_loop_variable = True + self._for_loop_variable += 1 return True return False @@ -719,7 +719,7 @@ class BracketTracker: """See `maybe_increment_for_loop_variable` above for explanation.""" if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in": self.depth -= 1 - self._for_loop_variable = False + self._for_loop_variable -= 1 return True return False @@ -732,7 +732,7 @@ class BracketTracker: """ if leaf.type == token.NAME and leaf.value == "lambda": self.depth += 1 - self._lambda_arguments = True + self._lambda_arguments += 1 return True return False @@ -741,7 +741,7 @@ class BracketTracker: """See `maybe_increment_lambda_arguments` above for explanation.""" if self._lambda_arguments and leaf.type == token.COLON: self.depth -= 1 - self._lambda_arguments = False + self._lambda_arguments -= 1 return True return False diff --git a/tests/expression.diff b/tests/expression.diff index 2af955c..3d170dd 100644 --- a/tests/expression.diff +++ b/tests/expression.diff @@ -11,7 +11,7 @@ True False 1 -@@ -29,61 +29,83 @@ +@@ -29,62 +29,84 @@ ~great +value -1 @@ -24,9 +24,10 @@ lambda a, b, c=True: a -lambda a, b, c=True, *, d=(1 << v2), e='str': a -lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b --foo = (lambda port_id, ignore_missing: {"port1": port1_resource, "port2": port2_resource}[port_id]) +lambda a, b, c=True, *, d=(1 << v2), e="str": a +lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b + manylambdas = lambda x=lambda y=lambda z=1: z: y(): x() +-foo = (lambda port_id, ignore_missing: {"port1": port1_resource, "port2": port2_resource}[port_id]) +foo = lambda port_id, ignore_missing: { + "port1": port1_resource, "port2": port2_resource +}[ @@ -116,7 +117,7 @@ call(**self.screen_kwargs) call(b, **self.screen_kwargs) lukasz.langa.pl -@@ -92,11 +114,11 @@ +@@ -93,11 +115,11 @@ 1.0 .real ....__class__ list[str] @@ -129,7 +130,7 @@ ] slice[0] slice[0:1] -@@ -123,103 +145,140 @@ +@@ -124,103 +146,140 @@ numpy[-(c + 1) :, d] numpy[:, l[-2]] numpy[:, ::-1] diff --git a/tests/expression.py b/tests/expression.py index 82d9dfa..63f6a4f 100644 --- a/tests/expression.py +++ b/tests/expression.py @@ -37,6 +37,7 @@ lambda a=True: a lambda a, b, c=True: a lambda a, b, c=True, *, d=(1 << v2), e='str': a lambda a, b, c=True, *vararg, d=(v1 << 2), e='str', **kwargs: a + b +manylambdas = lambda x=lambda y=lambda z=1: z: y(): x() foo = (lambda port_id, ignore_missing: {"port1": port1_resource, "port2": port2_resource}[port_id]) 1 if True else 2 str or None if True else str or bytes or None @@ -266,6 +267,7 @@ lambda a=True: a lambda a, b, c=True: a lambda a, b, c=True, *, d=(1 << v2), e="str": a lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b +manylambdas = lambda x=lambda y=lambda z=1: z: y(): x() foo = lambda port_id, ignore_missing: { "port1": port1_resource, "port2": port2_resource }[ -- 2.39.5