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

Keep user inserted optional parentheses #2237

Closed
wants to merge 1 commit 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/black/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from enum import Enum

DEFAULT_LINE_LENGTH = 88
DEFAULT_EXCLUDES = r"/(\.direnv|\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn|_build|buck-out|build|dist)/" # noqa: B950
DEFAULT_INCLUDES = r"\.pyi?$"
STDIN_PLACEHOLDER = "__BLACK_STDIN_FILENAME__"


class Fixer(Enum):
HIDE_PARENTHESES = 1
8 changes: 7 additions & 1 deletion src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from black.trans import StringSplitter, StringParenWrapper, StringParenStripper
from black.mode import Mode
from black.mode import Feature
from black.const import Fixer

from blib2to3.pytree import Node, Leaf
from blib2to3.pgen2 import token
Expand Down Expand Up @@ -495,7 +496,9 @@ def right_hand_split(
# there are no standalone comments in the body
and not body.contains_standalone_comments(0)
# and we can actually remove the parens
and can_omit_invisible_parens(body, line_length, omit_on_explode=omit)
and can_omit_invisible_parens(
body, line_length, opening_bracket, closing_bracket, omit_on_explode=omit
)
):
omit = {id(closing_bracket), *omit}
try:
Expand Down Expand Up @@ -828,6 +831,9 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
if first.type == token.LPAR and last.type == token.RPAR:
middle = node.children[1]
# make parentheses invisible
if first.value == "(" and last.value == ")":
first.fixers_applied.append(Fixer.HIDE_PARENTHESES)
last.fixers_applied.append(Fixer.HIDE_PARENTHESES)
first.value = "" # type: ignore
last.value = "" # type: ignore
maybe_make_parens_invisible_in_atom(middle, parent=parent)
Expand Down
15 changes: 14 additions & 1 deletion src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from blib2to3.pytree import Node, Leaf
from blib2to3.pgen2 import token

from black.brackets import BracketTracker, DOT_PRIORITY
from black.brackets import BracketTracker, DOT_PRIORITY, LOGIC_PRIORITY
from black.const import Fixer
from black.mode import Mode
from black.nodes import STANDALONE_COMMENT, TEST_DESCENDANTS
from black.nodes import BRACKETS, OPENING_BRACKETS, CLOSING_BRACKETS
Expand Down Expand Up @@ -609,6 +610,8 @@ def can_be_split(line: Line) -> bool:
def can_omit_invisible_parens(
line: Line,
line_length: int,
opening_bracket: Leaf,
closing_bracket: Leaf,
omit_on_explode: Collection[LeafID] = (),
) -> bool:
"""Does `line` have a shape safe to reformat without optional parens around it?
Expand All @@ -627,6 +630,16 @@ def can_omit_invisible_parens(
# With more than one delimiter of a kind the optional parentheses read better.
return False

if (
max_priority >= LOGIC_PRIORITY
and bt.delimiter_count_with_priority(max_priority) > 0
and Fixer.HIDE_PARENTHESES in opening_bracket.fixers_applied
and Fixer.HIDE_PARENTHESES in closing_bracket.fixers_applied
):
# With at least one delimiter of kind LOGIC or above, optional parentheses are better.
# So don't remove them if added by user.
return False

if max_priority == DOT_PRIORITY:
# A single stranded method call doesn't require optional parentheses.
return True
Expand Down
6 changes: 3 additions & 3 deletions tests/data/trailing_comma_optional_parens2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if (e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or
(8, 5, 8) <= get_tk_patchlevel() < (8, 6)):
pass
if e123456.get_tk_patchlevel() >= (8, 6, 0, 'final') or \
(8, 5, 8) <= get_tk_patchlevel() < (8, 6):
pass