You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
gracepetryk
changed the title
Explicitly mark sublcasses of protocols as abstract base classes.
Explicitly mark sublcasses of collections.abc protocols as abstract base classes
Jan 10, 2024
gracepetryk
changed the title
Explicitly mark sublcasses of collections.abc protocols as abstract base classes
Explicitly mark subclasses of collections.abc protocols as abstract base classes
Jan 10, 2024
This sounds like something we would have tried before and didn't do for one reason or another. Possibly because we also mark some of the classes as protocols, and that might not interact well with ABCMeta. That said, as we strive to follow the implementation if possible, I agree that this something we should at least try do to – or at least document in the stubs why we don't do it.
Exploratory PR welcome, unless someone else knows why didn't do it before.
There's a note in PEP-544 about this that to me sounds like marking all the non-protocol collection.abc classes as ABCs in the stubs would be a good solution. I'll submit a PR doing that a little later today.
Subclassing a protocol class would not turn the subclass into a protocol unless it also has typing.Protocol as an explicit base class. Without this base, the class is “downgraded” to a regular ABC that cannot be used with structural subtyping.
There are a few types in
collection.abc
that subclass protocols but are not protocols themselves. For exampleSequence
is a subclass ofCollection
andReversible
. It expects subclasses to implement__getitem__
and__len__
, the latter being a requirement of theCollection
protocol. I think that the stubs for these classes should explicitly subclassabc.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:At runtime the types in
collections.abc
are already abstract base classes:The text was updated successfully, but these errors were encountered: