comments: Dict[LeafID, Leaf] = attrib(default=Factory(dict))
bracket_tracker: BracketTracker = attrib(default=Factory(BracketTracker))
inside_brackets: bool = attrib(default=False)
+ has_for: bool = attrib(default=False)
+ _for_loop_variable: bool = attrib(default=False, init=False)
def append(self, leaf: Leaf, preformatted: bool = False) -> None:
has_value = leaf.value.strip()
# imports, for which we only preserve newlines.
leaf.prefix += whitespace(leaf)
if self.inside_brackets or not preformatted:
+ self.maybe_decrement_after_for_loop_variable(leaf)
self.bracket_tracker.mark(leaf)
self.maybe_remove_trailing_comma(leaf)
+ self.maybe_increment_for_loop_variable(leaf)
if self.maybe_adapt_standalone_comment(leaf):
return
return False
+ def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
+ """In a for loop, or comprehension, the variables are often unpacks.
+
+ To avoid splitting on the comma in this situation, we will increase
+ the depth of tokens between `for` and `in`.
+ """
+ if leaf.type == token.NAME and leaf.value == 'for':
+ self.has_for = True
+ self.bracket_tracker.depth += 1
+ self._for_loop_variable = True
+ return True
+
+ return False
+
+ def maybe_decrement_after_for_loop_variable(self, leaf: Leaf) -> bool:
+ # See `maybe_increment_for_loop_variable` above for explanation.
+ if self._for_loop_variable and leaf.type == token.NAME and leaf.value == 'in':
+ self.bracket_tracker.depth -= 1
+ self._for_loop_variable = False
+ return True
+
+ return False
+
def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
"""Hack a standalone comment to act as a trailing comment for line splitting.
[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
{i: 0 for i in (1, 2, 3)}
{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
+{k: v for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension}
Python3 > Python2 > COBOL
Life is Life
call()
[((i ** 2) + j) for i in (1, 2, 3) for j in (1, 2, 3)]
{i: 0 for i in (1, 2, 3)}
{i: j for i, j in ((1, 'a'), (2, 'b'), (3, 'c'))}
+{
+ k: v
+ for k, v in this_is_a_very_long_variable_which_will_cause_a_trailing_comma_which_breaks_the_comprehension
+}
Python3 > Python2 > COBOL
Life is Life
call()