Skip to content

Commit

Permalink
add entry in CHANGES.md, make the style change only in preview mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl committed Sep 22, 2023
1 parent 0f3cf1d commit 391a413
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

### Preview style

- Long type hints are now wrapped in parentheses and properly indented when split across
multiple lines (#3899)

<!-- Changes that affect Black's preview style -->

### Configuration
Expand Down
16 changes: 12 additions & 4 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,10 @@ def foo(a: int, b: float = 7): ...
def foo(a: (int), b: (float) = 7): ...
"""
assert len(node.children) == 3
if maybe_make_parens_invisible_in_atom(node.children[2], parent=node):
wrap_in_parentheses(node, node.children[2], visible=False)
if Preview.parenthesize_long_type_hints in self.mode:
assert len(node.children) == 3
if maybe_make_parens_invisible_in_atom(node.children[2], parent=node):
wrap_in_parentheses(node, node.children[2], visible=False)

yield from self.visit_default(node)

Expand Down Expand Up @@ -515,7 +516,14 @@ def __post_init__(self) -> None:
self.visit_except_clause = partial(v, keywords={"except"}, parens={"except"})
self.visit_with_stmt = partial(v, keywords={"with"}, parens={"with"})
self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)

# When this is moved out of preview, add ":" directly to ASSIGNMENTS in nodes.py
if Preview.parenthesize_long_type_hints in self.mode:
assignments = ASSIGNMENTS | {":"}
else:
assignments = ASSIGNMENTS
self.visit_expr_stmt = partial(v, keywords=Ø, parens=assignments)

self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
self.visit_import_from = partial(v, keywords=Ø, parens={"import"})
self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class Preview(Enum):
# for https://github.com/psf/black/issues/3117 to be fixed.
string_processing = auto()
parenthesize_conditional_expressions = auto()
parenthesize_long_type_hints = auto()
skip_magic_trailing_comma_in_subscript = auto()
wrap_long_dict_values_in_parens = auto()
wrap_multiple_context_managers_in_parens = auto()
Expand Down
2 changes: 0 additions & 2 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@
">>=",
"**=",
"//=",
# also handle annassign
":",
}

IMPLICIT_TUPLE: Final = {syms.testlist, syms.testlist_star_expr, syms.exprlist}
Expand Down
4 changes: 1 addition & 3 deletions tests/data/miscellaneous/long_strings_flag_disabled.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,7 @@
+ CONCATENATED
+ "using the '+' operator."
)
annotated_variable: (
Final
) = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Final = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
annotated_variable: Literal[
"fakse_literal"
] = "This is a large string that has a type annotation attached to it. A type annotation should NOT stop a long string from being wrapped."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,11 @@ def f(


# remove unnecessary paren
def foo(i: int) -> None:
...
def foo(i: int) -> None: ...


# this is a syntax error in the type annotation according to mypy, but it's not invalid *python* code, so make sure we don't mess with it and make it so.
def foo(i: (int,)) -> None:
...
def foo(i: (int,)) -> None: ...


def foo(
Expand Down Expand Up @@ -186,5 +184,4 @@ def f(
None, help="Maximum number of jobs to launch. And some additional text."
),
another_option: bool = False,
):
...
): ...

0 comments on commit 391a413

Please sign in to comment.