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

Avoid needless linebreaks on | in PEP-604 type annotations #3319

Closed
Zac-HD opened this issue Oct 9, 2022 · 2 comments
Closed

Avoid needless linebreaks on | in PEP-604 type annotations #3319

Zac-HD opened this issue Oct 9, 2022 · 2 comments
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?

Comments

@Zac-HD
Copy link
Contributor

Zac-HD commented Oct 9, 2022

Describe the style change

Given the increasing popularity of Python 3.10 and PEP604 type annotations (arg: X | Y), it's unfortunate that Black is so eager to put linebreaks in the middle. This happens when it's not necessary to meet line-length limits; and also when splitting doesn't achieve line-length limits!

I propose that the | operator be treated as binding more tightly in type annotations, to make Python 3.10+ code more elegant.

Examples in the current Black style

def func(
    options: tuple[str]
    | None = (
        "one_option",
        "another_option",
    ),
    long_arg: str
    | None = (
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    ),
    another_long_arg: str 
    | None = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",  # exceeds length limit!
):
    pass

Lines beginning | None = are all too common, and I'll admit that I don't like them at all. Instead, I think that we should keep annotations on a single line where possible (or parenthesized where not, instead of split out to a term per line).

Desired style

def func(
    options: tuple[str] | None = (
        "one_option",
        "another_option",
    ),
    long_arg: str | None = (
        "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    ),
    # treat another_long_arg the same way as long_arg
):
    pass

Notably I think it's important to ship this before freezing the 2023 code style, preferably with enough time for a round of feedback from --preview users.

@Zac-HD Zac-HD added the T: style What do we want Blackened code to look like? label Oct 9, 2022
@ichard26
Copy link
Collaborator

ichard26 commented Oct 9, 2022

Yup, see #2316.

@ichard26 ichard26 added R: duplicate This issue or pull request already exists F: linebreak How should we split up lines? labels Oct 9, 2022
@Zac-HD
Copy link
Contributor Author

Zac-HD commented Oct 9, 2022

🤦‍♂️ should've searched for "PEP 604", right. Closing as duplicate.

@Zac-HD Zac-HD closed this as completed Oct 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? R: duplicate This issue or pull request already exists T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

2 participants