Skip to content

bagof.hints.collections

Protocols that are compatible with collections.abc.

Classes

Container

Hashable

Iterable

Iterator

Reversible

Bases: Iterable[T], Protocol[T]

See collections.abc.Reversible.

This is a purely structural check for a __reversed__ method. Builtins that are reversible only through the __len__/__getitem__ fallback used by reversed (tuple, str, bytes, range, ...) do not expose __reversed__, so they are not instances of this protocol -- unlike collections.abc.Reversible, which reports them as virtual subclasses via registration rather than structure.

Example

list defines __reversed__; tuple does not:

>>> from bagof.hints.collections import Reversible
>>> isinstance([1, 2, 3], Reversible)
True
>>> isinstance((1, 2, 3), Reversible)
False

Generator

Bases: Iterator[YieldType], Protocol[YieldType, SendType, ReturnType]

See collections.abc.Generator.

Sized

Sequence

Bases: Collection[T], Protocol[T]

A read-only sequence, structurally close to collections.abc.Sequence.

Note

Unlike the standard library's ABC, this protocol does not derive from Reversible and so does not require a __reversed__ method. This is deliberate: tuple, str, bytes and range are reversible only through the __len__/__getitem__ fallback used by reversed and do not expose __reversed__ themselves. Requiring it would make those builtins fail a structural isinstance check, even though collections.abc.Sequence -- which relies on explicit registration rather than structure -- treats them as sequences. Use Reversible explicitly when a __reversed__ method is actually required.

MutableSequence

MutableSet

Mapping

Bases: Collection[K], Protocol[K, T]

See collections.abc.Mapping.

The key type is invariant and the value type is covariant, matching the standard library's Mapping: keys are both accepted (__getitem__) and produced (keys), whereas values are only ever produced.

MutableMapping

Awaitable

Buffer