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

Unnecessary linebreak on 3.10-style Union types. #3301

Closed
kthy opened this issue Sep 30, 2022 · 2 comments
Closed

Unnecessary linebreak on 3.10-style Union types. #3301

kthy opened this issue Sep 30, 2022 · 2 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@kthy
Copy link

kthy commented Sep 30, 2022

Describe the style change

Black 22.8.0 makes typing hints in Python ≥ 3.10 harder to read by line breaking on the pipe (type union) operator without any apparent reason.

Examples in the current Black style

I'm writing a FastAPI application:

from fastapi import APIRouter, Query

router = APIRouter(prefix="/black", tags=["black"])


@router.get("/foo")
async def foo(
    param1: str
    | None = Query(
        default=None, title="A parameter", description="A longer description of the parameter."
    ),
    param2: str
    | None = Query(
        default=None,
        title="Another parameter",
        description="A longer description of the other parameter.",
    ),
) -> str:
    """Docstring goes here."""
    return f"{param1=} {param2=}"

Desired style

from fastapi import APIRouter, Query

router = APIRouter(prefix="/pfx", tags=["pfx"])


@router.get("/foo")
async def foo(
    param1: str | None = Query(
        default=None, title="A parameter", description="A longer description of the parameter."
    ),
    param2: str | None = Query(
        default=None,
        title="Another parameter",
        description="A longer description of the other parameter.",
    ),
) -> str:
    """Docstring goes here."""
    return f"{param1=} {param2=}"

Additional context

If you remove the | None part of the typing hint, a linebreak is not inserted by Black.

str | None for types is syntax introduced in Python 3.10. Python 3.9 and earlier had Union[str, None] which is not susceptible to the problem.

@kthy kthy added the T: style What do we want Blackened code to look like? label Sep 30, 2022
@calumy
Copy link

calumy commented Sep 30, 2022

This seems similar to #3296 which is a duplicate of #2316.

@kthy
Copy link
Author

kthy commented Sep 30, 2022

Thanks, I'd tried to search for existing issues but came up blank. Closing as dupe.

@kthy kthy closed this as not planned Won't fix, can't repro, duplicate, stale Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

2 participants