Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
csalerno-asml committed Oct 5, 2023
2 parents f541fb5 + 36078bc commit a476fda
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -39,7 +39,7 @@ repos:
exclude: ^src/blib2to3/

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.0
rev: v1.5.1
hooks:
- id: mypy
exclude: ^docs/conf.py
Expand All @@ -53,7 +53,7 @@ repos:
- hypothesis

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.1
rev: v3.0.3
hooks:
- id: prettier
exclude: \.github/workflows/diff_shades\.yml
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -18,6 +18,7 @@

- Long type hints are now wrapped in parentheses and properly indented when split across
multiple lines (#3899)
- Magic trailing commas are now respected in return types. (#3916)

### Configuration

Expand Down
5 changes: 3 additions & 2 deletions docs/integrations/editors.md
Expand Up @@ -399,9 +399,10 @@ close and reopen your File, _Black_ will be done with its job.
server for Black. Formatting is much more responsive using this extension, **but the
minimum supported version of Black is 22.3.0**.

## SublimeText 3
## SublimeText

Use [sublack plugin](https://github.com/jgirardet/sublack).
For SublimeText 3, use [sublack plugin](https://github.com/jgirardet/sublack). For
higher versions, it is recommended to use [LSP](#python-lsp-server) as documented below.

## Python LSP Server

Expand Down
36 changes: 35 additions & 1 deletion src/black/linegen.py
Expand Up @@ -573,7 +573,7 @@ def transform_line(
transformers = [string_merge, string_paren_strip]
else:
transformers = []
elif line.is_def:
elif line.is_def and not should_split_funcdef_with_rhs(line, mode):
transformers = [left_hand_split]
else:

Expand Down Expand Up @@ -652,6 +652,40 @@ def _rhs(
yield line


def should_split_funcdef_with_rhs(line: Line, mode: Mode) -> bool:
"""If a funcdef has a magic trailing comma in the return type, then we should first
split the line with rhs to respect the comma.
"""
if Preview.respect_magic_trailing_comma_in_return_type not in mode:
return False

return_type_leaves: List[Leaf] = []
in_return_type = False

for leaf in line.leaves:
if leaf.type == token.COLON:
in_return_type = False
if in_return_type:
return_type_leaves.append(leaf)
if leaf.type == token.RARROW:
in_return_type = True

# using `bracket_split_build_line` will mess with whitespace, so we duplicate a
# couple lines from it.
result = Line(mode=line.mode, depth=line.depth)
leaves_to_track = get_leaves_inside_matching_brackets(return_type_leaves)
for leaf in return_type_leaves:
result.append(
leaf,
preformatted=True,
track_bracket=id(leaf) in leaves_to_track,
)

# we could also return true if the line is too long, and the return type is longer
# than the param list. Or if `should_split_rhs` returns True.
return result.magic_trailing_comma is not None


class _BracketSplitComponent(Enum):
head = auto()
body = auto()
Expand Down
1 change: 1 addition & 0 deletions src/black/mode.py
Expand Up @@ -181,6 +181,7 @@ class Preview(Enum):
string_processing = auto()
parenthesize_conditional_expressions = auto()
parenthesize_long_type_hints = auto()
respect_magic_trailing_comma_in_return_type = auto()
skip_magic_trailing_comma_in_subscript = auto()
wrap_long_dict_values_in_parens = auto()
wrap_multiple_context_managers_in_parens = auto()
Expand Down
11 changes: 11 additions & 0 deletions tests/data/preview/return_annotation_brackets_string.py
Expand Up @@ -2,6 +2,10 @@
def frobnicate() -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass

# splitting the string breaks if there's any parameters
def frobnicate(a) -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass

# output

# Long string example
Expand All @@ -10,3 +14,10 @@ def frobnicate() -> (
" list[ThisIsTrulyUnreasonablyExtremelyLongClassName]"
):
pass


# splitting the string breaks if there's any parameters
def frobnicate(
a,
) -> "ThisIsTrulyUnreasonablyExtremelyLongClassName | list[ThisIsTrulyUnreasonablyExtremelyLongClassName]":
pass

0 comments on commit a476fda

Please sign in to comment.