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

Fix crash on some power hugging cases #2806

Merged
merged 8 commits into from Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
2 changes: 2 additions & 0 deletions src/black/linegen.py
Expand Up @@ -942,6 +942,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
if (
prev
and prev.type == token.COMMA
and leaf.opening_bracket is not None
and not is_one_tuple_between(
leaf.opening_bracket, leaf, line.leaves
)
Expand Down Expand Up @@ -969,6 +970,7 @@ def generate_trailers_to_omit(line: Line, line_length: int) -> Iterator[Set[Leaf
if (
prev
and prev.type == token.COMMA
and leaf.opening_bracket is not None
and not is_one_tuple_between(leaf.opening_bracket, leaf, line.leaves)
):
# Never omit bracket pairs with trailing commas.
Expand Down
4 changes: 3 additions & 1 deletion src/black/lines.py
Expand Up @@ -277,7 +277,9 @@ def has_magic_trailing_comma(
if self.is_import:
return True

if not is_one_tuple_between(closing.opening_bracket, closing, self.leaves):
if closing.opening_bracket is not None and not is_one_tuple_between(
closing.opening_bracket, closing, self.leaves
):
return True

return False
Expand Down
6 changes: 5 additions & 1 deletion src/blib2to3/pytree.py
Expand Up @@ -386,7 +386,8 @@ class Leaf(Base):
value: Text
fixers_applied: List[Any]
bracket_depth: int
opening_bracket: "Leaf"
# Changed later in brackets.py
opening_bracket: Optional["Leaf"] = None
used_names: Optional[Set[Text]]
_prefix = "" # Whitespace and comments preceding this token in the input
lineno: int = 0 # Line where this token starts in the input
Expand All @@ -399,6 +400,7 @@ def __init__(
context: Optional[Context] = None,
prefix: Optional[Text] = None,
fixers_applied: List[Any] = [],
opening_bracket: Optional["Leaf"] = None,
) -> None:
"""
Initializer.
Expand All @@ -416,6 +418,7 @@ def __init__(
self._prefix = prefix
self.fixers_applied: Optional[List[Any]] = fixers_applied[:]
self.children = []
self.opening_bracket = opening_bracket

def __repr__(self) -> str:
"""Return a canonical string representation."""
Expand Down Expand Up @@ -448,6 +451,7 @@ def clone(self) -> "Leaf":
self.value,
(self.prefix, (self.lineno, self.column)),
fixers_applied=self.fixers_applied,
opening_bracket=self.opening_bracket,
)

def leaves(self) -> Iterator["Leaf"]:
Expand Down
10 changes: 10 additions & 0 deletions tests/data/power_op_newline.py
@@ -0,0 +1,10 @@
importA;()<<0**0#

# output

importA
(
()
<< 0
** 0
) #
7 changes: 7 additions & 0 deletions tests/test_format.py
Expand Up @@ -256,3 +256,10 @@ def test_python38() -> None:
def test_python39() -> None:
source, expected = read_data("python39")
assert_format(source, expected, minimum_version=(3, 9))


def test_power_op_newline() -> None:
# requires line_length=0
source, expected = read_data("power_op_newline")
# fast=True because this exposes a different stability bug
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
assert_format(source, expected, mode=black.Mode(line_length=0))