This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author hroncok
Recipients docs@python, hroncok, vstinner
Date 2018-11-08.14:41:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
The — Abstract Base Classes for Containers documentation says:

> This module provides abstract base classes that can be used to test whether a class provides a particular interface; for example, whether it is hashable or whether it is a mapping.

However this is not true for Sequence.

When I implement a class that provides a particular interface (defined in the Collections Abstract Base Classes table in that very page), I cannot check whether it implements a Sequence.

See an example:

    from collections import abc

    class Box:
        def __init__(self, wrapped):
            self._w = wrapped

        def __len__(self):
            return len(self._w)

        def __iter__(self):
            yield from self._w

        def __getitem__(self, i):
            return self._w[i]

        def __reversed__(self):
            yield from reversed(self._w)

        def __contains__(self, i):
            return i in self._w

        def index(self, value, start=0, stop=None):
            return self._w.index(value, start, stop)

        def count(self, value):
            return self._w.count(value)

    b = Box([1, 2, 3])

    for t in 'Sized', 'Iterable', 'Reversible', 'Container', 'Collection', 'Sequence':
        print(f'{t}: {isinstance(b, getattr(abc, t))}')

My class is Reversible.
My class is a Collection (as it is a Sized Iterable Container).
It implements __getitem__, __len__, __contains__, __iter__, __reversed__, index, and count.

Yet my class instance is not an instance of Sequence.

I suppose this behavior might be intentional, as discussed in issue16728 - or it might as well not be.

The main concern was that dict also provides these methods, but is not considered a Sequence,
however dict does not provide index() or count().

Regardless whether this is right or wrong behavior, as documented this should be a Sequence.

See also

As I see it, either: needs a __subclasshook__ so it can be used as the documentation implies.


    the documentation should not say that "abstract base classes (from abc module) can be used to test whether a class provides a particular interface" if it doesn't generally apply


    the Sequence documentation should say: "this particular abstract base class cannot be used to test whether a class provides a particular interface because reasons" (yet I don't really get those reasons)
Date User Action Args
2018-11-08 14:41:26hroncoksetrecipients: + hroncok, vstinner, docs@python
2018-11-08 14:41:26hroncoksetmessageid: <>
2018-11-08 14:41:26hroncoklinkissue35190 messages
2018-11-08 14:41:26hroncokcreate