Feature or enhancement
Proposal:
collections.UserDict.popitem currently uses the collections.abc.MutableMapping default implementation, which pops items in the order of iter(self.data) and is thus FIFO:
from collections import UserDict
d = UserDict()
d['spam'] = 0
d['ham'] = 1
assert(d.popitem() == ('spam', 0))
This does not match dict.popitem, which has been guaranteed to pop LIFO since Python 3.7:
d = dict()
d['spam'] = 0
d['ham'] = 1
assert(d.popitem() == ('ham', 1))
Given that UserDict is meant to "[simulate] a dictionary", it seems preferable for UserDict instances to pop LIFO as well. (This has already been the subject of confusion, per a recent Stack Overflow question.) This change could be a CPython implementation detail, but it could (in the long run) be a spec guarantee as well.
The spec has not historically guaranteed an order for UserDict.popitem, nor for MutableMapping.popitem (nor specified that UserDict is a subclass of MutableMapping), but this change might break programs that relied on the FIFO behavior anyways. That said, the whole point of UserDict is to emulate dict for subclassing purposes, so I think the consistency still outweighs the risk.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Feature or enhancement
Proposal:
collections.UserDict.popitemcurrently uses thecollections.abc.MutableMappingdefault implementation, which pops items in the order ofiter(self.data)and is thus FIFO:This does not match
dict.popitem, which has been guaranteed to pop LIFO since Python 3.7:Given that
UserDictis meant to "[simulate] a dictionary", it seems preferable forUserDictinstances to pop LIFO as well. (This has already been the subject of confusion, per a recent Stack Overflow question.) This change could be a CPython implementation detail, but it could (in the long run) be a spec guarantee as well.The spec has not historically guaranteed an order for
UserDict.popitem, nor forMutableMapping.popitem(nor specified thatUserDictis a subclass ofMutableMapping), but this change might break programs that relied on the FIFO behavior anyways. That said, the whole point ofUserDictis to emulatedictfor subclassing purposes, so I think the consistency still outweighs the risk.Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs