Skip to content

Formatter undocumented deviation: extra unnecessary wrapping of arguments and extra parenthesis for long return types #14143

@Avasam

Description

@Avasam
  • List of keywords you searched for before creating this issue. Write them down here so that others can find this issue more easily and help provide feedback.
    undocumented deviation black return

This is similar to #11791, but for return type annotations rather than variable annotations.

  • A minimal code snippet that reproduces the bug.

With preview = True:

Black:

class Polygon:
    def cut_section(self, line) -> tuple[
        RegularPolygon | Polygon | Triangle | Point | Point2D | Point3D | Segment2D | Segment3D | Segment | None,
        RegularPolygon | Polygon | Triangle | Point | Point2D | Point3D | Segment2D | Segment3D | Segment | None,
    ]: ...

Ruff:

class Polygon:
    def cut_section(
        self, line
    ) -> tuple[
        RegularPolygon | Polygon | Triangle | Point | Point2D | Point3D | Segment2D | Segment3D | Segment | None,
        RegularPolygon | Polygon | Triangle | Point | Point2D | Point3D | Segment2D | Segment3D | Segment | None,
    ]: ...

See microsoft/python-type-stubs@9eb6bb5 for a lot more examples

As an added note, with preview = False, I also get the following deviation:

Black:

def get_basis_vectors() -> tuple[
    tuple[Literal[1], Literal[0], Literal[0]],
    tuple[Literal[0], Literal[1], Literal[0]],
    tuple[Literal[0], Literal[0], Literal[1]],
]: ...

Ruff:

def get_basis_vectors() -> (
    tuple[
        tuple[Literal[1], Literal[0], Literal[0]],
        tuple[Literal[0], Literal[1], Literal[0]],
        tuple[Literal[0], Literal[0], Literal[1]],
    ]
): ...
  • The command you invoked (e.g., ruff /path/to/file.py --fix), ideally including the --isolated flag.
    ruff format

  • The current Ruff settings (any relevant sections from your pyproject.toml).

[tool.ruff]
line-length = 130
# Target oldest supported Python version
target-version = "py39"
preview = true
  • The current Ruff version (ruff --version).
    ruff 0.7.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    formatterRelated to the formatter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions