Issue36455
Created on 2019-03-28 02:40 by jedwards, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (4) | |||
|---|---|---|---|
| msg339003 - (view) | Author: James Edwards (jedwards) * | Date: 2019-03-28 02:40 | |
Consider:
from collections.abc import *
class DefinitelyNotAList:
def __init__(self, backer):
self.backer = backer
def __getitem__(self, key):
return self.backer[key]
def __len__(self):
return len(self.backer)
def __contains__(self, needle):
return needle in self.backer
def __reversed__(self):
return reversed(self.backer)
def __iter__(self):
return list.__iter__(self.backer)
def index(self, *args, **kwargs): return self.backer.index(*args, **kwargs)
def count(self, *args, **kwargs): return self.backer.count(*args, **kwargs)
dnal = DefinitelyNotAList([2,4,6,8])
for abc in [Collection, Reversible, Sized, Iterable, Container, Sequence]:
print(abc.__name__.ljust(12), isinstance(dnal, abc))
Which prints:
Collection True
Reversible True
Sized True
Iterable True
Container True
Sequence False
I'm not sure whether this is a bug, a docs bug, or just a misunderstanding but my expectation, is that, somehow, I could get isinstance(obj, Sequence) to return *without* explicitly registering it, just as True is returned for the other abcs.
|
|||
| msg339004 - (view) | Author: James Edwards (jedwards) * | Date: 2019-03-28 02:44 | |
This was tagged as 3.7, but the issue still exists AFAICT in the latest github master: https://github.com/python/cpython/blob/master/Lib/_collections_abc.py |
|||
| msg339005 - (view) | Author: Josh Rosenberg (josh.r) * ![]() |
Date: 2019-03-28 02:48 | |
This is an exact duplicate of #35190 - "collections.abc.Sequence cannot be used to test whether a class provides a particular interface (doc issue)" |
|||
| msg339007 - (view) | Author: James Edwards (jedwards) * | Date: 2019-03-28 02:54 | |
Edit conflict -- indeed -- self closing.
---
I should point out that this could be fixed with something like the following:
@classmethod
def __subclasshook__(cls, C):
if cls is Sequence:
return _check_methods(C, "__reversed__", "__iter__",
"__len__", "__contains__", "__getitem__")
return NotImplemented
But seeing as it's not the only abc that is without a subclass hook, I wanted to raise an issue before I submitted a complete PR implementing subclasshooks for the rest of the abcs that need them.
|
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:13 | admin | set | github: 80636 |
| 2019-03-28 02:54:46 | jedwards | set | nosy:
- josh.r messages: + msg339007 |
| 2019-03-28 02:49:00 | josh.r | set | status: open -> closed stage: resolved |
| 2019-03-28 02:48:51 | josh.r | set | nosy:
+ josh.r messages: + msg339005 resolution: duplicate superseder: doc: collections.abc.Sequence cannot be used to test whether a class provides a particular interface |
| 2019-03-28 02:44:21 | jedwards | set | messages: + msg339004 |
| 2019-03-28 02:40:42 | jedwards | create | |
