Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional refactors based on the PR #2206 #2379

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/black/brackets.py
Expand Up @@ -136,7 +136,7 @@ def delimiter_count_with_priority(self, priority: Priority = 0) -> int:
return 0

priority = priority or self.max_delimiter_priority()
return sum(1 for p in self.delimiters.values() if p == priority)
return self.delimiters.values().count(priority)

def maybe_increment_for_loop_variable(self, leaf: Leaf) -> bool:
"""In a for loop, or comprehension, the variables are often unpacks.
Expand Down Expand Up @@ -317,7 +317,7 @@ def max_delimiter_priority_in_atom(node: LN) -> Priority:

first = node.children[0]
last = node.children[-1]
if not (first.type == token.LPAR and last.type == token.RPAR):
if first.type != token.LPAR or last.type != token.RPAR:
return 0

bt = BracketTracker()
Expand Down
35 changes: 17 additions & 18 deletions src/black/comments.py
Expand Up @@ -210,8 +210,7 @@ def generate_ignored_nodes(leaf: Leaf, comment: ProtoComment) -> Iterator[LN]:
):
prev_sibling = prev_sibling.prev_sibling
siblings.insert(0, prev_sibling)
for sibling in siblings:
yield sibling
yield from siblings
elif leaf.parent is not None:
yield leaf.parent
return
Expand Down Expand Up @@ -245,17 +244,18 @@ def is_fmt_on(container: LN) -> bool:

def contains_fmt_on_at_column(container: LN, column: int) -> bool:
"""Determine if children at a given column have formatting switched on."""
for child in container.children:
if (
isinstance(child, Node)
and first_leaf_column(child) == column
or isinstance(child, Leaf)
and child.column == column
):
if is_fmt_on(child):
return True

return False
return any(
(
(
isinstance(child, Node)
and first_leaf_column(child) == column
or isinstance(child, Leaf)
and child.column == column
)
)
and is_fmt_on(child)
for child in container.children
)


def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
Expand All @@ -265,8 +265,7 @@ def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
of the more common static analysis tools for python (e.g. mypy, flake8,
pylint).
"""
for comment in comment_list:
if comment.value.startswith(("# type:", "# noqa", "# pylint:")):
return True

return False
return any(
comment.value.startswith(("# type:", "# noqa", "# pylint:"))
for comment in comment_list
)
76 changes: 41 additions & 35 deletions src/black/linegen.py
Expand Up @@ -423,10 +423,9 @@ def left_hand_split(line: Line, _features: Collection[Feature] = ()) -> Iterator
):
current_leaves = tail_leaves if body_leaves else head_leaves
current_leaves.append(leaf)
if current_leaves is head_leaves:
if leaf.type in OPENING_BRACKETS:
matching_bracket = leaf
current_leaves = body_leaves
if current_leaves is head_leaves and leaf.type in OPENING_BRACKETS:
matching_bracket = leaf
current_leaves = body_leaves
if not matching_bracket:
raise CannotSplit("No brackets found")

Expand Down Expand Up @@ -460,15 +459,17 @@ def right_hand_split(
opening_bracket: Optional[Leaf] = None
closing_bracket: Optional[Leaf] = None
for leaf in reversed(line.leaves):
if current_leaves is body_leaves:
if leaf is opening_bracket:
current_leaves = head_leaves if body_leaves else tail_leaves
if current_leaves is body_leaves and leaf is opening_bracket:
current_leaves = head_leaves if body_leaves else tail_leaves
current_leaves.append(leaf)
if current_leaves is tail_leaves:
if leaf.type in CLOSING_BRACKETS and id(leaf) not in omit:
opening_bracket = leaf.opening_bracket
closing_bracket = leaf
current_leaves = body_leaves
if (
current_leaves is tail_leaves
and leaf.type in CLOSING_BRACKETS
and id(leaf) not in omit
):
opening_bracket = leaf.opening_bracket
closing_bracket = leaf
current_leaves = body_leaves
if not (opening_bracket and closing_bracket and head_leaves):
# If there is no opening or closing_bracket that means the split failed and
# all content is in the tail. Otherwise, if `head_leaves` are empty, it means
Expand Down Expand Up @@ -573,7 +574,7 @@ def bracket_split_build_line(
no_commas = (
original.is_def
and opening_bracket.value == "("
and not any(leaf.type == token.COMMA for leaf in leaves)
and all(leaf.type != token.COMMA for leaf in leaves)
)

if original.is_import or no_commas:
Expand Down Expand Up @@ -812,35 +813,40 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
):
return False

if is_walrus_assignment(node):
if parent.type in [
syms.annassign,
syms.expr_stmt,
syms.assert_stmt,
syms.return_stmt,
# these ones aren't useful to end users, but they do please fuzzers
syms.for_stmt,
syms.del_stmt,
]:
return False
if is_walrus_assignment(node) and parent.type in [
syms.annassign,
syms.expr_stmt,
syms.assert_stmt,
syms.return_stmt,
# these ones aren't useful to end users, but they do please fuzzers
syms.for_stmt,
syms.del_stmt,
]:
return False

first = node.children[0]
last = node.children[-1]
if first.type == token.LPAR and last.type == token.RPAR:
middle = node.children[1]
# make parentheses invisible
first.value = "" # type: ignore
last.value = "" # type: ignore
maybe_make_parens_invisible_in_atom(middle, parent=parent)
return _extracted_from_maybe_make_parens_invisible_in_atom_28(
node, first, last, parent
)

if is_atom_with_invisible_parens(middle):
# Strip the invisible parens from `middle` by replacing
# it with the child in-between the invisible parens
middle.replace(middle.children[1])
return True

return False

return True
def _extracted_from_maybe_make_parens_invisible_in_atom_28(node, first, last, parent):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does 28 mean here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, the number 28 is just a filler for the help-functions, but actually not needed

middle = node.children[1]
# make parentheses invisible
first.value = "" # type: ignore
last.value = "" # type: ignore
maybe_make_parens_invisible_in_atom(middle, parent=parent)

if is_atom_with_invisible_parens(middle):
# Strip the invisible parens from `middle` by replacing
# it with the child in-between the invisible parens
middle.replace(middle.children[1])

return False


def should_split_line(line: Line, opening_bracket: Leaf) -> bool:
Expand Down
34 changes: 17 additions & 17 deletions src/black/lines.py
Expand Up @@ -171,11 +171,10 @@ def is_triple_quoted_string(self) -> bool:

def contains_standalone_comments(self, depth_limit: int = sys.maxsize) -> bool:
"""If so, needs to be split before emitting."""
for leaf in self.leaves:
if leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit:
return True

return False
return any(
leaf.type == STANDALONE_COMMENT and leaf.bracket_depth <= depth_limit
for leaf in self.leaves
)

def contains_uncollapsable_type_comments(self) -> bool:
ignored_ids = set()
Expand All @@ -202,12 +201,14 @@ def contains_uncollapsable_type_comments(self) -> bool:
comment_seen = False
for leaf_id, comments in self.comments.items():
for comment in comments:
if is_type_comment(comment):
if comment_seen or (
if is_type_comment(comment) and (
comment_seen
or (
not is_type_comment(comment, " ignore")
and leaf_id not in ignored_ids
):
return True
)
):
return True

comment_seen = True

Expand Down Expand Up @@ -594,7 +595,7 @@ def can_be_split(line: Line) -> bool:
elif leaf.type == token.DOT:
dot_count += 1
elif leaf.type == token.NAME:
if not (next.type == token.DOT or next.type in OPENING_BRACKETS):
if next.type != token.DOT and next.type not in OPENING_BRACKETS:
return False

elif leaf.type not in CLOSING_BRACKETS:
Expand Down Expand Up @@ -637,13 +638,12 @@ def can_omit_invisible_parens(
# a bracket.
first = line.leaves[0]
second = line.leaves[1]
if first.type in OPENING_BRACKETS and second.type not in CLOSING_BRACKETS:
if _can_omit_opening_paren(line, first=first, line_length=line_length):
return True

# Note: we are not returning False here because a line might have *both*
# a leading opening bracket and a trailing closing bracket. If the
# opening bracket doesn't match our rule, maybe the closing will.
if (
first.type in OPENING_BRACKETS
and second.type not in CLOSING_BRACKETS
and _can_omit_opening_paren(line, first=first, line_length=line_length)
):
return True

penultimate = line.leaves[-2]
last = line.leaves[-1]
Expand Down
3 changes: 1 addition & 2 deletions src/black/parsing.py
Expand Up @@ -102,8 +102,7 @@ def lib2to3_parse(src_txt: str, target_versions: Iterable[TargetVersion] = ()) -

def lib2to3_unparse(node: Node) -> str:
"""Given a lib2to3 node, return its string representation."""
code = str(node)
return code
return str(node)
Anselmoo marked this conversation as resolved.
Show resolved Hide resolved


def parse_single_version(
Expand Down
2 changes: 1 addition & 1 deletion src/black/trans.py
Expand Up @@ -134,7 +134,7 @@ def __call__(self, line: Line, _features: Collection[Feature]) -> Iterator[Line]
"""
# Optimization to avoid calling `self.do_match(...)` when the line does
# not contain any string.
if not any(leaf.type == token.STRING for leaf in line.leaves):
if all(leaf.type != token.STRING for leaf in line.leaves):
raise CannotTransform("There are no strings in this line.")

match_result = self.do_match(line)
Expand Down
13 changes: 4 additions & 9 deletions src/blib2to3/pgen2/conv.py
Expand Up @@ -67,12 +67,10 @@ def parse_graminit_h(self, filename):
return False
self.symbol2number = {}
self.number2symbol = {}
lineno = 0
for line in f:
lineno += 1
for lineno, line in enumerate(f, start=1):
mo = re.match(r"^#define\s+(\w+)\s+(\d+)$", line)
if not mo and line.strip():
print("%s(%s): can't parse %s" % (filename, lineno, line.strip()))
print("%s(%s): can't parse %s" % (filename, lineno+1, line.strip()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need either this or the start=1 but not both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right!

else:
symbol, number = mo.groups()
number = int(number)
Expand Down Expand Up @@ -200,16 +198,13 @@ def parse_graminit_c(self, filename):
mo = re.match(r"static label labels\[(\d+)\] = {$", line)
assert mo, (lineno, line)
nlabels = int(mo.group(1))
for i in range(nlabels):
for _ in range(nlabels):
lineno, line = lineno + 1, next(f)
mo = re.match(r'\s+{(\d+), (0|"\w+")},$', line)
assert mo, (lineno, line)
x, y = mo.groups()
x = int(x)
if y == "0":
y = None
else:
y = eval(y)
y = None if y == "0" else eval(y)
labels.append((x, y))
lineno, line = lineno + 1, next(f)
assert line == "};\n", (lineno, line)
Expand Down
14 changes: 7 additions & 7 deletions src/blib2to3/pgen2/parse.py
Expand Up @@ -175,16 +175,16 @@ def addtoken(self, type: int, value: Optional[Text], context: Context) -> bool:
self.push(t, self.grammar.dfas[t], newstate, context)
break # To continue the outer while loop
else:
if (0, state) in arcs:
# An accepting state, pop it and try something else
self.pop()
if not self.stack:
# Done parsing, but another token is input
raise ParseError("too much input", type, value, context)
else:
if (0, state) not in arcs:
# No success finding a transition
raise ParseError("bad input", type, value, context)

# An accepting state, pop it and try something else
self.pop()
if not self.stack:
# Done parsing, but another token is input
raise ParseError("too much input", type, value, context)

def classify(self, type: int, value: Optional[Text], context: Context) -> int:
"""Turn a token into a label. (Internal)"""
if type == token.NAME:
Expand Down