Skip to content

Protocols and type aliases

Structural protocols and PEP 695 type aliases. Because the protocols are structural, your own transforms satisfy them without inheriting from anything.

_typing

Shared type aliases and structural protocols.

The aliases are written with PEP 695 type statements (Python 3.12+), and the protocols are structural: any object with the right methods satisfies them, no inheritance or registration required. That is what lets Pipeline accept a user-defined transform it has never heard of.

Series

Series = Sequence[float]

Any read-only sequence of numbers accepted as input by this package.

A list, tuple, range, or anything else implementing collections.abc.Sequence works. Inputs are coerced to list[float] on the way in and never mutated.

Transform

Bases: Protocol

Structural protocol for anything that maps a series to a new series.

Implementations must not mutate their input and must return a fresh list[float].

Examples:

>>> from mypackage import Clip, Transform
>>> isinstance(Clip(lower=0.0), Transform)
True
>>> isinstance("not a transform", Transform)
False
apply
apply(series: Series) -> list[float]

Return the transformed series.

Source code in src/mypackage/_typing.py
def apply(self, series: Series) -> list[float]:
    """Return the transformed series."""
    ...

Fittable

Bases: Protocol

Structural protocol for transforms that learn state from data.

Examples:

>>> from mypackage import Fittable, Standardize
>>> isinstance(Standardize(), Fittable)
True
fit
fit(series: Series) -> Fittable

Learn any required state from series and return self.

Source code in src/mypackage/_typing.py
def fit(self, series: Series) -> Fittable:
    """Learn any required state from ``series`` and return ``self``."""
    ...
apply
apply(series: Series) -> list[float]

Return the transformed series.

Source code in src/mypackage/_typing.py
def apply(self, series: Series) -> list[float]:
    """Return the transformed series."""
    ...