Support nested lambdas in BracketTracker
This commit is contained in:
parent
793450aeb0
commit
c9b73b99ac
12
black.py
12
black.py
@ -647,8 +647,8 @@ class BracketTracker:
|
|||||||
bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
|
bracket_match: Dict[Tuple[Depth, NodeType], Leaf] = Factory(dict)
|
||||||
delimiters: Dict[LeafID, Priority] = Factory(dict)
|
delimiters: Dict[LeafID, Priority] = Factory(dict)
|
||||||
previous: Optional[Leaf] = None
|
previous: Optional[Leaf] = None
|
||||||
_for_loop_variable: bool = False
|
_for_loop_variable: int = 0
|
||||||
_lambda_arguments: bool = False
|
_lambda_arguments: int = 0
|
||||||
|
|
||||||
def mark(self, leaf: Leaf) -> None:
|
def mark(self, leaf: Leaf) -> None:
|
||||||
"""Mark `leaf` with bracket-related metadata. Keep track of delimiters.
|
"""Mark `leaf` with bracket-related metadata. Keep track of delimiters.
|
||||||
@ -710,7 +710,7 @@ def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
|
|||||||
"""
|
"""
|
||||||
if leaf.type == token.NAME and leaf.value == "for":
|
if leaf.type == token.NAME and leaf.value == "for":
|
||||||
self.depth += 1
|
self.depth += 1
|
||||||
self._for_loop_variable = True
|
self._for_loop_variable += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -719,7 +719,7 @@ def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
|
|||||||
"""See `maybe_increment_for_loop_variable` above for explanation."""
|
"""See `maybe_increment_for_loop_variable` above for explanation."""
|
||||||
if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
|
if self._for_loop_variable and leaf.type == token.NAME and leaf.value == "in":
|
||||||
self.depth -= 1
|
self.depth -= 1
|
||||||
self._for_loop_variable = False
|
self._for_loop_variable -= 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -732,7 +732,7 @@ def maybe_increment_lambda_arguments(self, leaf: Leaf) -> bool:
|
|||||||
"""
|
"""
|
||||||
if leaf.type == token.NAME and leaf.value == "lambda":
|
if leaf.type == token.NAME and leaf.value == "lambda":
|
||||||
self.depth += 1
|
self.depth += 1
|
||||||
self._lambda_arguments = True
|
self._lambda_arguments += 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
@ -741,7 +741,7 @@ def maybe_decrement_after_lambda_arguments(self, leaf: Leaf) -> bool:
|
|||||||
"""See `maybe_increment_lambda_arguments` above for explanation."""
|
"""See `maybe_increment_lambda_arguments` above for explanation."""
|
||||||
if self._lambda_arguments and leaf.type == token.COLON:
|
if self._lambda_arguments and leaf.type == token.COLON:
|
||||||
self.depth -= 1
|
self.depth -= 1
|
||||||
self._lambda_arguments = False
|
self._lambda_arguments -= 1
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
True
|
True
|
||||||
False
|
False
|
||||||
1
|
1
|
||||||
@@ -29,61 +29,83 @@
|
@@ -29,62 +29,84 @@
|
||||||
~great
|
~great
|
||||||
+value
|
+value
|
||||||
-1
|
-1
|
||||||
@ -24,9 +24,10 @@
|
|||||||
lambda a, b, c=True: a
|
lambda a, b, c=True: a
|
||||||
-lambda a, b, c=True, *, d=(1 << v2), e='str': 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
|
-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, *, d=(1 << v2), e="str": a
|
||||||
+lambda a, b, c=True, *vararg, d=(v1 << 2), e="str", **kwargs: a + b
|
+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: {
|
+foo = lambda port_id, ignore_missing: {
|
||||||
+ "port1": port1_resource, "port2": port2_resource
|
+ "port1": port1_resource, "port2": port2_resource
|
||||||
+}[
|
+}[
|
||||||
@ -116,7 +117,7 @@
|
|||||||
call(**self.screen_kwargs)
|
call(**self.screen_kwargs)
|
||||||
call(b, **self.screen_kwargs)
|
call(b, **self.screen_kwargs)
|
||||||
lukasz.langa.pl
|
lukasz.langa.pl
|
||||||
@@ -92,11 +114,11 @@
|
@@ -93,11 +115,11 @@
|
||||||
1.0 .real
|
1.0 .real
|
||||||
....__class__
|
....__class__
|
||||||
list[str]
|
list[str]
|
||||||
@ -129,7 +130,7 @@
|
|||||||
]
|
]
|
||||||
slice[0]
|
slice[0]
|
||||||
slice[0:1]
|
slice[0:1]
|
||||||
@@ -123,103 +145,140 @@
|
@@ -124,103 +146,140 @@
|
||||||
numpy[-(c + 1) :, d]
|
numpy[-(c + 1) :, d]
|
||||||
numpy[:, l[-2]]
|
numpy[:, l[-2]]
|
||||||
numpy[:, ::-1]
|
numpy[:, ::-1]
|
||||||
|
@ -37,6 +37,7 @@
|
|||||||
lambda a, b, c=True: a
|
lambda a, b, c=True: a
|
||||||
lambda a, b, c=True, *, d=(1 << v2), e='str': 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
|
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}[port_id])
|
||||||
1 if True else 2
|
1 if True else 2
|
||||||
str or None if True else str or bytes or None
|
str or None if True else str or bytes or None
|
||||||
@ -266,6 +267,7 @@ async def f():
|
|||||||
lambda a, b, c=True: a
|
lambda a, b, c=True: a
|
||||||
lambda a, b, c=True, *, d=(1 << v2), e="str": 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
|
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: {
|
foo = lambda port_id, ignore_missing: {
|
||||||
"port1": port1_resource, "port2": port2_resource
|
"port1": port1_resource, "port2": port2_resource
|
||||||
}[
|
}[
|
||||||
|
Loading…
Reference in New Issue
Block a user