Skip to content

PEP 604 and line breaks #2316

Closed
Closed
@srittau

Description

Describe the style change

Long PEP 604 annotations (X | Y) in function definitions (and probably other contexts) are currently broken into multiple lines without extra indentation. This makes it hard to recognize the arguments in the signature. My suggestion would be to treat those annotations similar to regular bitwise or operators inside type annotations: Indent followup lines and possibly use parentheses.

Examples in the current Black style

def foo(
    i: int,
    x: Loooooooooooooooooooooooong
    | Looooooooooooooooong
    | Looooooooooooooooooooong
    | Looooooong,
    *,
    s: str
) -> None:
    pass

A practical example (with shorter line width):

def create_medical_record_entries(
    context: RequestContext,
    db_entries: Iterable[CharlyMedicalRecordEntry]
    | Iterable[CharlyLabRecordEntry],
    *,
    ignore_signs: bool = True
) -> list[MedicalRecordEntry]:
    ...

Desired style (without extra arguments)

def foo(
    x: (
        Loooooooooooooooooooooooong
        | Looooooooooooooooong
        | Looooooooooooooooooooong
        | Looooooong
    ),
) -> None:
    pass

Or maybe:

def foo(
    x:
        Loooooooooooooooooooooooong
        | Looooooooooooooooong
        | Looooooooooooooooooooong
        | Looooooong,
) -> None:
    pass

Or:

def foo(
    x: Loooooooooooooooooooooooong
        | Looooooooooooooooong
        | Looooooooooooooooooooong
        | Looooooong,
) -> None:
    pass

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    F: linebreakHow should we split up lines?F: parenthesesToo many parentheses, not enough parentheses, and so on.T: styleWhat do we want Blackened code to look like?

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions