Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

The `...` (`Ellipsis`) literal in protocol methods

In Python 3.12, `...` is a literal for the `Ellipsis` object. In a protocol method body it signals that no implementation is supplied, but it does not return that object.

Version: Python 3.12

Syntax

...

from typing import Protocol

class Reader(Protocol):
    def read(self) -> int:
        ...

The syntax token ... (Ellipsis literal) is three consecutive dots in Python source, not an editorial marker for omitted code. It evaluates to the sole instance of types.EllipsisType, also available through the built-in name Ellipsis. Use is to test that the literal and the built-in name refer to the same object.

When ... is the only statement in an ordinary function body, it is an expression statement: its value is not returned. Reaching the end of the body returns None, just as it would with pass alone. But pass is a statement, not an expression. In contrast, return ... explicitly returns Ellipsis.

In a typing.Protocol class, a ... body signals that the method specifies an interface requirement rather than providing a usable default implementation. A static type checker can accept a class with a compatible method signature even if it does not inherit from the protocol. The literal itself neither applies @abstractmethod nor triggers runtime signature checks.

Examples

from typing import Protocol


class Reader(Protocol):
    def read(self) -> int:
        ...


class FixedReader:
    def read(self) -> int:
        return 7


def read_value(reader: Reader) -> int:
    return reader.read()


def placeholder() -> None:
    ...


print((...) is Ellipsis)
print(placeholder() is None)
print(read_value(FixedReader()))

Result

True
True
7

The first `True` confirms identity with `Ellipsis`; the second shows that `placeholder()` returns `None`. The `7` comes from `FixedReader.read()`, not from the protocol method body.

Related lessons

  • Python · Lesson 22 · Advanced · Version: 5

Sources