Skip to content

Commit

Permalink
Line breaks before logical operators (#36)
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
autophagy authored and ambv committed Mar 17, 2018
1 parent b0b568d commit 47ae92c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Keep in sync with setup.cfg which is used for source packages.

[flake8]
ignore = E266, E501
ignore = E266, E501, W503
max-line-length = 80
max-complexity = 15
select = B,C,E,F,W,T4,B9
125 changes: 64 additions & 61 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,19 +357,25 @@ def mark(self, leaf: Leaf) -> None:
if leaf.type == token.STRING and self.previous.type == token.STRING:
self.delimiters[id(self.previous)] = STRING_PRIORITY
elif (
leaf.type == token.NAME and
leaf.value == 'for' and
leaf.parent and
leaf.parent.type in {syms.comp_for, syms.old_comp_for}
leaf.type == token.NAME
and leaf.value == 'for'
and leaf.parent
and leaf.parent.type in {syms.comp_for, syms.old_comp_for}
):
self.delimiters[id(self.previous)] = COMPREHENSION_PRIORITY
elif (
leaf.type == token.NAME and
leaf.value == 'if' and
leaf.parent and
leaf.parent.type in {syms.comp_if, syms.old_comp_if}
leaf.type == token.NAME
and leaf.value == 'if'
and leaf.parent
and leaf.parent.type in {syms.comp_if, syms.old_comp_if}
):
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:
self.bracket_match[self.depth, BRACKET[leaf.type]] = leaf
self.depth += 1
Expand Down Expand Up @@ -432,9 +438,9 @@ def is_import(self) -> bool:
@property
def is_class(self) -> bool:
return (
bool(self) and
self.leaves[0].type == token.NAME and
self.leaves[0].value == 'class'
bool(self)
and self.leaves[0].type == token.NAME
and self.leaves[0].value == 'class'
)

@property
Expand All @@ -450,37 +456,37 @@ def is_def(self) -> bool:
except IndexError:
second_leaf = None
return (
(first_leaf.type == token.NAME and first_leaf.value == 'def') or
(
first_leaf.type == token.NAME and
first_leaf.value == 'async' and
second_leaf is not None and
second_leaf.type == token.NAME and
second_leaf.value == 'def'
(first_leaf.type == token.NAME and first_leaf.value == 'def')
or (
first_leaf.type == token.NAME
and first_leaf.value == 'async'
and second_leaf is not None
and second_leaf.type == token.NAME
and second_leaf.value == 'def'
)
)

@property
def is_flow_control(self) -> bool:
return (
bool(self) and
self.leaves[0].type == token.NAME and
self.leaves[0].value in FLOW_CONTROL
bool(self)
and self.leaves[0].type == token.NAME
and self.leaves[0].value in FLOW_CONTROL
)

@property
def is_yield(self) -> bool:
return (
bool(self) and
self.leaves[0].type == token.NAME and
self.leaves[0].value == 'yield'
bool(self)
and self.leaves[0].type == token.NAME
and self.leaves[0].value == 'yield'
)

def maybe_remove_trailing_comma(self, closing: Leaf) -> bool:
if not (
self.leaves and
self.leaves[-1].type == token.COMMA and
closing.type in CLOSING_BRACKETS
self.leaves
and self.leaves[-1].type == token.COMMA
and closing.type in CLOSING_BRACKETS
):
return False

Expand Down Expand Up @@ -551,8 +557,8 @@ def maybe_adapt_standalone_comment(self, comment: Leaf) -> bool:
appended will appear "too long" when splitting.
"""
if not (
comment.type == STANDALONE_COMMENT and
self.bracket_tracker.any_open_brackets()
comment.type == STANDALONE_COMMENT
and self.bracket_tracker.any_open_brackets()
):
return False

Expand Down Expand Up @@ -655,17 +661,17 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return before, 1

if (
self.previous_line and
self.previous_line.is_import and
not current_line.is_import and
depth == self.previous_line.depth
self.previous_line
and self.previous_line.is_import
and not current_line.is_import
and depth == self.previous_line.depth
):
return (before or 1), 0

if (
self.previous_line and
self.previous_line.is_yield and
(not current_line.is_yield or depth != self.previous_line.depth)
self.previous_line
and self.previous_line.is_yield
and (not current_line.is_yield or depth != self.previous_line.depth)
):
return (before or 1), 0

Expand Down Expand Up @@ -969,9 +975,9 @@ def whitespace(leaf: Leaf) -> str: # noqa C901
return NO

elif (
p.type == syms.listmaker or
p.type == syms.testlist_gexp or
p.type == syms.subscriptlist
p.type == syms.listmaker
or p.type == syms.testlist_gexp
or p.type == syms.subscriptlist
):
# list interior, including unpacking
if not prev:
Expand Down Expand Up @@ -1049,16 +1055,13 @@ def is_delimiter(leaf: Leaf) -> int:
if leaf.type == token.COMMA:
return COMMA_PRIORITY

if leaf.type == token.NAME and leaf.value in LOGIC_OPERATORS:
return LOGIC_PRIORITY

if leaf.type in COMPARATORS:
return COMPARATOR_PRIORITY

if (
leaf.type in MATH_OPERATORS and
leaf.parent and
leaf.parent.type not in {syms.factor, syms.star_expr}
leaf.type in MATH_OPERATORS
and leaf.parent
and leaf.parent.type not in {syms.factor, syms.star_expr}
):
return MATH_PRIORITY

Expand Down Expand Up @@ -1178,9 +1181,9 @@ def left_hand_split(line: Line, py36: bool = False) -> Iterator[Line]:
matching_bracket = None
for leaf in line.leaves:
if (
current_leaves is body_leaves and
leaf.type in CLOSING_BRACKETS and
leaf.opening_bracket is matching_bracket
current_leaves is body_leaves
and leaf.type in CLOSING_BRACKETS
and leaf.opening_bracket is matching_bracket
):
current_leaves = tail_leaves if body_leaves else head_leaves
current_leaves.append(leaf)
Expand Down Expand Up @@ -1287,9 +1290,9 @@ def delimiter_split(line: Line, py36: bool = False) -> Iterator[Line]:
current_line.append(comment_after, preformatted=True)
lowest_depth = min(lowest_depth, leaf.bracket_depth)
if (
leaf.bracket_depth == lowest_depth and
leaf.type == token.STAR or
leaf.type == token.DOUBLESTAR
leaf.bracket_depth == lowest_depth
and leaf.type == token.STAR
or leaf.type == token.DOUBLESTAR
):
trailing_comma_safe = trailing_comma_safe and py36
leaf_priority = delimiters.get(id(leaf))
Expand All @@ -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)
if current_line:
if (
delimiter_priority == COMMA_PRIORITY and
current_line.leaves[-1].type != token.COMMA and
trailing_comma_safe
delimiter_priority == COMMA_PRIORITY
and current_line.leaves[-1].type != token.COMMA
and trailing_comma_safe
):
current_line.append(Leaf(token.COMMA, ','))
normalize_prefix(current_line.leaves[0])
Expand All @@ -1315,10 +1318,10 @@ def is_import(leaf: Leaf) -> bool:
t = leaf.type
v = leaf.value
return bool(
t == token.NAME and
(
(v == 'import' and p and p.type == syms.import_name) or
(v == 'from' and p and p.type == syms.import_from)
t == token.NAME
and (
(v == 'import' and p and p.type == syms.import_name)
or (v == 'from' and p and p.type == syms.import_from)
)
)

Expand Down Expand Up @@ -1351,9 +1354,9 @@ def is_python36(node: Node) -> bool:
return True

elif (
n.type == syms.typedargslist and
n.children and
n.children[-1].type == token.COMMA
n.type == syms.typedargslist
and n.children
and n.children[-1].type == token.COMMA
):
for ch in n.children:
if ch.type == token.STAR or ch.type == token.DOUBLESTAR:
Expand Down
12 changes: 6 additions & 6 deletions tests/comments2.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def inline_comments_in_brackets_ruin_everything():
body,
parameters.children[-1], # )2
]
if (self._proc is not None and
if (self._proc is not None
# has the child process finished?
self._returncode is None and
and self._returncode is None
# the child process has finished, but the
# transport hasn't been notified yet?
self._proc.poll() is None):
and self._proc.poll() is None):
pass
short = [
# one
Expand Down Expand Up @@ -137,12 +137,12 @@ def inline_comments_in_brackets_ruin_everything():
parameters.children[-1], # )2
]
if (
self._proc is not None and
self._proc is not None
# has the child process finished?
self._returncode is None and
and self._returncode is None
# the child process has finished, but the
# transport hasn't been notified yet?
self._proc.poll() is None
and self._proc.poll() is None
):
pass
short = [
Expand Down
14 changes: 14 additions & 0 deletions tests/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ def gen():
async def f():
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

Expand Down Expand Up @@ -261,3 +267,11 @@ def gen():

async def f():
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

0 comments on commit 47ae92c

Please sign in to comment.