New submission from Alex Waygood <alex.wayg...@gmail.com>:

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.

----------
components: Library (Lib)
files: collections popitem weirdness.py
messages: 398310
nosy: AlexWaygood
priority: normal
severity: normal
status: open
title: .popitem() is inconsistent in collections and collections.abc
type: behavior
versions: Python 3.9
Added file: https://bugs.python.org/file50187/collections popitem weirdness.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44750>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to