Skip to content

Don't strip an ellipsis (...) from a function's body - this has special meaning to type-checkers #10358

@rsokl

Description

@rsokl

Before ruff:

if TYPE_CHECKING:
    def compress(bytes: Sequence[int], target: int) -> list[int]:
        """Docs"
        ...   # <- this tells type-checkers to treat this function like a stub and ignore its body

After ruff:

if TYPE_CHECKING:
    def compress(bytes: Sequence[int], target: int) -> list[int]:  # type: ignore
        """Docs"""
        # Without ... the type-checker assumes the return type is None
        # thus the type-checker marks the return-type annotation as an error.

The ellipses signals to the type-checker (e.g. pyright) that function is acting as a stub and the body should be ignored. In the latter case, the type-checker thinks that the function returns None and thus marks the return-type as an error.

I.e. without the ellipses, you have to either: provide a fake function body or # type: ignore the function's signature, which would mask actual type-errors in the signature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions