Skip to content

Explicitly mark subclasses of collections.abc protocols as abstract base classes #11266

Closed as not planned
@gracepetryk

Description

@gracepetryk

There are a few types in collection.abc that subclass protocols but are not protocols themselves. For example Sequence is a subclass of Collection and Reversible. It expects subclasses to implement __getitem__ and __len__, the latter being a requirement of the Collection protocol. I think that the stubs for these classes should explicitly subclass abc.ABC so that typecheckers require implementation of all abstract methods when the @final annotation is used.

Currently typecheckers disagree on how to handle these stubs. For this example mypy asks for __getitem__ and __len__ implementations and pyright throws no error:

from collections.abc import Sequence
from typing import final

@final
class Foo(Sequence[int]):
    pass
bash-3.2$ mypy test.py
test.py:5: error: Final class test.Foo has abstract attributes "__getitem__", "__len__"  [misc]
Found 1 error in 1 file (checked 1 source file)
bash-3.2$ pyright test.py
0 errors, 0 warnings, 0 informations

At runtime the types in collections.abc are already abstract base classes:

>>> from collections.abc import Sequence
>>> Sequence.__class__
<class 'abc.ABCMeta'>

Metadata

Metadata

Assignees

No one assigned

    Labels

    stubs: improvementImprove/refactor existing annotations, other stubs issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions