Line breaks before logical operators (#36)

Fixes #21
This commit is contained in:
Mika⠙ 2018-03-17 05:45:51 +01:00 committed by Łukasz Langa
parent 28d1442d57
commit 497de7fa08
4 changed files with 85 additions and 68 deletions

View File

@ -2,7 +2,7 @@
# Keep in sync with setup.cfg which is used for source packages. # Keep in sync with setup.cfg which is used for source packages.
[flake8] [flake8]
ignore = E266, E501 ignore = E266, E501, W503
max-line-length = 80 max-line-length = 80
max-complexity = 15 max-complexity = 15
select = B,C,E,F,W,T4,B9 select = B,C,E,F,W,T4,B9

125
black.py
View File

@ -357,19 +357,25 @@ def mark(self, leaf: Leaf) -> None:
if leaf.type == token.STRING and self.previous.type == token.STRING: if leaf.type == token.STRING and self.previous.type == token.STRING:
self.delimiters[id(self.previous)] = STRING_PRIORITY self.delimiters[id(self.previous)] = STRING_PRIORITY
elif ( elif (
leaf.type == token.NAME and leaf.type == token.NAME
leaf.value == 'for' and and leaf.value == 'for'
leaf.parent and and leaf.parent
leaf.parent.type in {syms.comp_for, syms.old_comp_for} and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
): ):
self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
elif ( elif (
leaf.type == token.NAME and leaf.type == token.NAME
leaf.value == 'if' and and leaf.value == 'if'
leaf.parent and and leaf.parent
leaf.parent.type in {syms.comp_if, syms.old_comp_if} and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
): ):
self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
elif (
leaf.type == token.NAME
and leaf.value in LOGIC_OPERATORS
and leaf.parent
):
self.delimiters[id(self.previous)] = LOGIC_PRIORITY
if leaf.type in OPENING_BRACKETS: if leaf.type in OPENING_BRACKETS:
self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
self.depth += 1 self.depth += 1
@ -432,9 +438,9 @@ def is_import(self) -> bool:
@property @property
def is_class(self) -> bool: def is_class(self) -> bool:
return ( return (
bool(self) and bool(self)
self.leaves[0].type == token.NAME and and self.leaves[0].type == token.NAME
self.leaves[0].value == 'class' and self.leaves[0].value == 'class'
) )
@property @property
@ -450,37 +456,37 @@ def is_def(self) -> bool:
except IndexError: except IndexError:
second_leaf = None second_leaf = None
return ( return (
(first_leaf.type == token.NAME and first_leaf.value == 'def') or (first_leaf.type == token.NAME and first_leaf.value == 'def')
( or (
first_leaf.type == token.NAME and first_leaf.type == token.NAME
first_leaf.value == 'async' and and first_leaf.value == 'async'
second_leaf is not None and and second_leaf is not None
second_leaf.type == token.NAME and and second_leaf.type == token.NAME
second_leaf.value == 'def' and second_leaf.value == 'def'
) )
) )
@property @property
def is_flow_control(self) -> bool: def is_flow_control(self) -> bool:
return ( return (
bool(self) and bool(self)
self.leaves[0].type == token.NAME and and self.leaves[0].type == token.NAME
self.leaves[0].value in FLOW_CONTROL and self.leaves[0].value in FLOW_CONTROL
) )
@property @property
def is_yield(self) -> bool: def is_yield(self) -> bool:
return ( return (
bool(self) and bool(self)
self.leaves[0].type == token.NAME and and self.leaves[0].type == token.NAME
self.leaves[0].value == 'yield' and self.leaves[0].value == 'yield'
) )
def maybe_remove_trailing_comma(self, closing: Leaf) -> bool: def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
if not ( if not (
self.leaves and self.leaves
self.leaves[-1].type == token.COMMA and and self.leaves[-1].type == token.COMMA
closing.type in CLOSING_BRACKETS and closing.type in CLOSING_BRACKETS
): ):
return False return False
@ -551,8 +557,8 @@ def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
appended will appear "too long" when splitting. appended will appear "too long" when splitting.
""" """
if not ( if not (
comment.type == STANDALONE_COMMENT and comment.type == STANDALONE_COMMENT
self.bracket_tracker.any_open_brackets() and self.bracket_tracker.any_open_brackets()
): ):
return False return False
@ -655,17 +661,17 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return before, 1 return before, 1
if ( if (
self.previous_line and self.previous_line
self.previous_line.is_import and and self.previous_line.is_import
not current_line.is_import and and not current_line.is_import
depth == self.previous_line.depth and depth == self.previous_line.depth
): ):
return (before or 1), 0 return (before or 1), 0
if ( if (
self.previous_line and self.previous_line
self.previous_line.is_yield and and self.previous_line.is_yield
(not current_line.is_yield or depth != self.previous_line.depth) and (not current_line.is_yield or depth != self.previous_line.depth)
): ):
return (before or 1), 0 return (before or 1), 0
@ -969,9 +975,9 @@ def whitespace(leaf: Leaf) -> str: # noqa C901
return NO return NO
elif ( elif (
p.type == syms.listmaker or p.type == syms.listmaker
p.type == syms.testlist_gexp or or p.type == syms.testlist_gexp
p.type == syms.subscriptlist or p.type == syms.subscriptlist
): ):
# list interior, including unpacking # list interior, including unpacking
if not prev: if not prev:
@ -1049,16 +1055,13 @@ def is_delimiter(leaf: Leaf) -> int:
if leaf.type == token.COMMA: if leaf.type == token.COMMA:
return COMMA_PRIORITY return COMMA_PRIORITY
if leaf.type == token.NAME and leaf.value in LOGIC_OPERATORS:
return LOGIC_PRIORITY
if leaf.type in COMPARATORS: if leaf.type in COMPARATORS:
return COMPARATOR_PRIORITY return COMPARATOR_PRIORITY
if ( if (
leaf.type in MATH_OPERATORS and leaf.type in MATH_OPERATORS
leaf.parent and and leaf.parent
leaf.parent.type not in {syms.factor, syms.star_expr} and leaf.parent.type not in {syms.factor, syms.star_expr}
): ):
return MATH_PRIORITY return MATH_PRIORITY
@ -1178,9 +1181,9 @@ def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
matching_bracket = None matching_bracket = None
for leaf in line.leaves: for leaf in line.leaves:
if ( if (
current_leaves is body_leaves and current_leaves is body_leaves
leaf.type in CLOSING_BRACKETS and and leaf.type in CLOSING_BRACKETS
leaf.opening_bracket is matching_bracket and leaf.opening_bracket is matching_bracket
): ):
current_leaves = tail_leaves if body_leaves else head_leaves current_leaves = tail_leaves if body_leaves else head_leaves
current_leaves.append(leaf) current_leaves.append(leaf)
@ -1287,9 +1290,9 @@ def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
current_line.append(comment_after, preformatted=True) current_line.append(comment_after, preformatted=True)
lowest_depth = min(lowest_depth, leaf.bracket_depth) lowest_depth = min(lowest_depth, leaf.bracket_depth)
if ( if (
leaf.bracket_depth == lowest_depth and leaf.bracket_depth == lowest_depth
leaf.type == token.STAR or and leaf.type == token.STAR
leaf.type == token.DOUBLESTAR or leaf.type == token.DOUBLESTAR
): ):
trailing_comma_safe = trailing_comma_safe and py36 trailing_comma_safe = trailing_comma_safe and py36
leaf_priority = delimiters.get(id(leaf)) leaf_priority = delimiters.get(id(leaf))
@ -1300,9 +1303,9 @@ def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets) current_line = Line(depth=line.depth, inside_brackets=line.inside_brackets)
if current_line: if current_line:
if ( if (
delimiter_priority == COMMA_PRIORITY and delimiter_priority == COMMA_PRIORITY
current_line.leaves[-1].type != token.COMMA and and current_line.leaves[-1].type != token.COMMA
trailing_comma_safe and trailing_comma_safe
): ):
current_line.append(Leaf(token.COMMA, ',')) current_line.append(Leaf(token.COMMA, ','))
normalize_prefix(current_line.leaves[0]) normalize_prefix(current_line.leaves[0])
@ -1315,10 +1318,10 @@ def is_import(leaf: Leaf) -> bool:
t = leaf.type t = leaf.type
v = leaf.value v = leaf.value
return bool( return bool(
t == token.NAME and t == token.NAME
( and (
(v == 'import' and p and p.type == syms.import_name) or (v == 'import' and p and p.type == syms.import_name)
(v == 'from' and p and p.type == syms.import_from) or (v == 'from' and p and p.type == syms.import_from)
) )
) )
@ -1351,9 +1354,9 @@ def is_python36(node: Node) -> bool:
return True return True
elif ( elif (
n.type == syms.typedargslist and n.type == syms.typedargslist
n.children and and n.children
n.children[-1].type == token.COMMA and n.children[-1].type == token.COMMA
): ):
for ch in n.children: for ch in n.children:
if ch.type == token.STAR or ch.type == token.DOUBLESTAR: if ch.type == token.STAR or ch.type == token.DOUBLESTAR:

View File

@ -35,12 +35,12 @@ def inline_comments_in_brackets_ruin_everything():
body, body,
parameters.children[-1], # )2 parameters.children[-1], # )2
] ]
if (self._proc is not None and if (self._proc is not None
# has the child process finished? # has the child process finished?
self._returncode is None and and self._returncode is None
# the child process has finished, but the # the child process has finished, but the
# transport hasn't been notified yet? # transport hasn't been notified yet?
self._proc.poll() is None): and self._proc.poll() is None):
pass pass
short = [ short = [
# one # one
@ -137,12 +137,12 @@ def inline_comments_in_brackets_ruin_everything():
parameters.children[-1], # )2 parameters.children[-1], # )2
] ]
if ( if (
self._proc is not None and self._proc is not None
# has the child process finished? # has the child process finished?
self._returncode is None and and self._returncode is None
# the child process has finished, but the # the child process has finished, but the
# transport hasn't been notified yet? # transport hasn't been notified yet?
self._proc.poll() is None and self._proc.poll() is None
): ):
pass pass
short = [ short = [

View File

@ -118,6 +118,12 @@ def gen():
async def f(): async def f():
await some.complicated[0].call(with_args=(True or (1 is not 1))) await some.complicated[0].call(with_args=(True or (1 is not 1)))
if (
threading.current_thread() != threading.main_thread() and
threading.current_thread() != threading.main_thread() or
signal.getsignal(signal.SIGINT) != signal.default_int_handler
):
return True
# output # output
@ -261,3 +267,11 @@ def gen():
async def f(): async def f():
await some.complicated[0].call(with_args=(True or (1 is not 1))) await some.complicated[0].call(with_args=(True or (1 is not 1)))
if (
threading.current_thread() != threading.main_thread()
and threading.current_thread() != threading.main_thread()
or signal.getsignal(signal.SIGINT) != signal.default_int_handler
):
return True