This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author AlexWaygood
Recipients AlexWaygood
Date 2021-07-27.17:35:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1627407328.07.0.937784840037.issue44750@roundup.psfhosted.org>
In-reply-to
Content
In a Python dictionary, `.popitem()` returns (key, value) pairs from the dictionary in a LIFO order. `collections.OrderedDict` and `collections.Counter` both do the same. However, a class inheriting from `collections.abc.MutableMapping` (which includes, in the standard library, `collections.UserDict`), will inherit a `.popitem()` method that returns (key, value) pairs in a FIFO order.

In my opinion, this seems like unexpected behaviour, given that these classes are designed to emulate the API of a standard Python dict.

The documentation for ordinary dictionaries states that `.popitem()` uses LIFO (https://docs.python.org/3/library/stdtypes.html#typesmapping), as does `collections.OrderedDict` (https://docs.python.org/3/library/collections.html#collections.OrderedDict). However, I can't find anything in the documentation for the collections.abc module (https://docs.python.org/3/library/collections.abc.html) or the docstring for `collections.abc.MutableMapping.popitem()` (https://github.com/python/cpython/blob/ae0a2b756255629140efcbe57fc2e714f0267aa3/Lib/_collections_abc.py#L964) that states that collections.abc.MutableMapping will use FIFO. Ditto for the collections.UserDict documentation/docstring: https://docs.python.org/3/library/collections.html#collections.UserDict, https://github.com/python/cpython/blob/6948964ecf94e858448dd28eea634317226d2913/Lib/collections/__init__.py#L1084.

Is this expected/intended behaviour? I found it highly confusing when attempting to implement a custom data structure just now. I think a note in the docstring/documentation, noting that this is the behaviour, would certainly be useful.

I have attached a minimal demonstration of this behaviour. Tests only done on Python 3.9.
History
Date User Action Args
2021-07-27 17:35:28AlexWaygoodsetrecipients: + AlexWaygood
2021-07-27 17:35:28AlexWaygoodsetmessageid: <1627407328.07.0.937784840037.issue44750@roundup.psfhosted.org>
2021-07-27 17:35:28AlexWaygoodlinkissue44750 messages
2021-07-27 17:35:27AlexWaygoodcreate