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
{{ message }}
Repository navigation
doc: issubclass without registration only works for "one-trick pony" collections ABCs. #68052
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifedocsDocumentation in the Doc direasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/rhettinger'closed_at=<Date2021-09-10.03:59:35.028>created_at=<Date2015-04-04.10:57:47.433>labels= ['easy', 'type-bug', '3.9', '3.10', '3.11', 'library', 'docs']
title='doc: issubclass without registration only works for "one-trick pony" collections ABCs.'updated_at=<Date2021-10-04.19:18:44.019>user='https://github.com/mjpieters'
The collections.abc documentation implies that *any* of the container ABCs can be used in an issubclass test against a class that implements all abstract methods:
These ABCs allow us to ask classes or instances if they provide particular functionality [...]
In reality this only applies to the "One Trick Ponies" (term from PEP-3119, things like Container and Iterable, those classes with one or two methods). It fails for the compound container ABCs:
That's because the One Trick Ponies implement a __subclasshook__ method that is locked to the specific class and returns NotImplemented for subclasses; for instance, the Iterable.__subclasshook__ implementation is:
The compound container classes build on top of the One Trick Ponies, so the class test will fail, NotImplemented is returned and the normal ABC tests for base classes that have been explicitly registered continues, but this won't include unregistered complete implementations.
Either the compound classes need their own __subclasshook__ implementations, or the documentation needs to be updated to make it clear that without explicit registrations the issubclass() (and isinstance()) tests only apply to the One Trick Ponies.
This does apply to all versions of Python from 2.6 up. Registering does work of course.
I believe the reason for not having the __subclasshook__ is the following sentence in PEP-3119: "ABCs are intended to solve problems that don't have a good solution at all in Python 2, such as distinguishing between mappings and sequences."
This used to be worse in <3.3 because there if you ever inherit from Sequence you will always end up having __dict__, even if you just want __slots__.
(By the way, if Py2 documentation is fixed, it should also say that these ABCs are new as of 2.6, not since 2.4 like the rest of the collections module).
Probably I'm overlooking something, but why isn't this hook defined cooperatively, with a terminating base class method that returns True? If the call chain progresses to the base, then all of the interfaces have been satisfied. Otherwise one of the bases returns NotImplemented. If it's implemented cooperatively, then the cls is Iterable check can be removed, because it returns super().subclasshook(C) instead of True.
saulshanabrook commented on Apr 13, 2015
saulshanabrookmannequin
Mannequin
More actions
I have added a failing test to based on the first example, of a class that provides the necessary methods, but fails to be an instance of Sequence.
maggyero commented on Dec 10, 2018
maggyeromannequin
Mannequin
More actions
Guido, could we add those missing __subclasshook__ for consistency?
No, I consider this is a documentation problem. I don't recall why the docs say that (I don't even know if they still say that or whether Martijn misread them), but IMO this should not be changed.
This isn't meant as a comment from any previous posts. It's simply meant to correct a statement (based on new information in the past 2 years) from the original post.
Since this original report, some ABCs that are not "One Trick Ponies" have been added which implement subclasshook. Collection is one of those, so using the original example:
changed the title [-]issubclass without registration only works for "one-trick pony" collections ABCs.[/-][+]doc: issubclass without registration only works for "one-trick pony" collections ABCs.[/+]on Sep 2, 2021
New changeset 62fa613 by Raymond Hettinger in branch 'main': bpo-45024 and bpo-23864: Document how interface testing works with the collections ABCs (GH-28218) 62fa613
New changeset 9d8a64a by Pablo Galindo (Miss Islington (bot)) in branch '3.10': bpo-45024 and bpo-23864: Document how interface testing works with the collections ABCs (GH-28218) (GH-28266) 9d8a64a
3.10 (EOL)end of life3.11only security fixes3.9 (EOL)end of lifedocsDocumentation in the Doc direasystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
Activity
mjpieters commentedon Apr 4, 2015
The collections.abc documentation implies that *any* of the container ABCs can be used in an issubclass test against a class that implements all abstract methods:
In reality this only applies to the "One Trick Ponies" (term from PEP-3119, things like Container and Iterable, those classes with one or two methods). It fails for the compound container ABCs:
That's because the One Trick Ponies implement a __subclasshook__ method that is locked to the specific class and returns NotImplemented for subclasses; for instance, the Iterable.__subclasshook__ implementation is:
The compound container classes build on top of the One Trick Ponies, so the class test will fail, NotImplemented is returned and the normal ABC tests for base classes that have been explicitly registered continues, but this won't include unregistered complete implementations.
Either the compound classes need their own __subclasshook__ implementations, or the documentation needs to be updated to make it clear that without explicit registrations the issubclass() (and isinstance()) tests only apply to the One Trick Ponies.
mjpieters commentedon Apr 4, 2015
I should have added the mixin methods for the Sequence implementation; the more complete demonstration is:
ztane commentedon Apr 4, 2015
This does apply to all versions of Python from 2.6 up. Registering does work of course.
I believe the reason for not having the __subclasshook__ is the following sentence in PEP-3119: "ABCs are intended to solve problems that don't have a good solution at all in Python 2, such as distinguishing between mappings and sequences."
This used to be worse in <3.3 because there if you ever inherit from
Sequenceyou will always end up having__dict__, even if you just want__slots__.(By the way, if Py2 documentation is fixed, it should also say that these ABCs are new as of 2.6, not since 2.4 like the rest of the collections module).
eryksun commentedon Apr 4, 2015
Probably I'm overlooking something, but why isn't this hook defined cooperatively, with a terminating base class method that returns True? If the call chain progresses to the base, then all of the interfaces have been satisfied. Otherwise one of the bases returns NotImplemented. If it's implemented cooperatively, then the
cls is Iterablecheck can be removed, because it returns super().subclasshook(C) instead of True.saulshanabrook commentedon Apr 13, 2015
I have added a failing test to based on the first example, of a class that provides the necessary methods, but fails to be an instance of Sequence.
maggyero commentedon Dec 10, 2018
Guido, could we add those missing __subclasshook__ for consistency?
gvanrossum commentedon Dec 10, 2018
No, I consider this is a documentation problem. I don't recall why the docs say that (I don't even know if they still say that or whether Martijn misread them), but IMO this should not be changed.
csabella commentedon Dec 24, 2018
This isn't meant as a comment from any previous posts. It's simply meant to correct a statement (based on new information in the past 2 years) from the original post.
Since this original report, some ABCs that are not "One Trick Ponies" have been added which implement subclasshook.
Collectionis one of those, so using the original example:Collection is not a "One Trick Pony" because it is used for Sized, Iterable Containers.
Generator, Coroutine, and ASyncGenerator are also not "One Trick Ponies" (although they are defined under that section in _collections_abc.py).
Again, for reference, the definition of One Trick Pony from PEP-3119 is:
These abstract classes represent single methods like __iter__ or __len__.
If only One Trick Ponies implemented __subclasshook__, then the original documentation issue:
maybe could have been changed to:
But, that's not really correct anymore.
iritkatriel commentedon Sep 2, 2021
See also bpo-35190.
[-]issubclass without registration only works for "one-trick pony" collections ABCs.[/-][+]doc: issubclass without registration only works for "one-trick pony" collections ABCs.[/+]gvanrossum commentedon Sep 2, 2021
Maybe bpo-45024 is also connected? (It also seems to relate to hasattr() logic.)
rhettinger commentedon Sep 10, 2021
New changeset 62fa613 by Raymond Hettinger in branch 'main':
bpo-45024 and bpo-23864: Document how interface testing works with the collections ABCs (GH-28218)
62fa613
rhettinger commentedon Sep 10, 2021
New changeset 89edd18 by Miss Islington (bot) in branch '3.10':
bpo-45024 and bpo-23864: Document how interface testing works with the collections ABCs (GH-28218) (GH-28266)
89edd18
pablogsal commentedon Oct 4, 2021
New changeset 9d8a64a by Pablo Galindo (Miss Islington (bot)) in branch '3.10':
bpo-45024 and bpo-23864: Document how interface testing works with the collections ABCs (GH-28218) (GH-28266)
9d8a64a