-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
popitem from UserDict in LIFO order #147957
Copy link
Copy link
Closed
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.The issue has been accepted as valid by a triager.type-featureA feature request or enhancementA feature request or enhancement
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytriagedThe issue has been accepted as valid by a triager.The issue has been accepted as valid by a triager.type-featureA feature request or enhancementA feature request or enhancement
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