Skip to content

python-stdlib/collections-defaultdict: Add items() function.#1100

Merged
dpgeorge merged 1 commit into
micropython:masterfrom
agatti:collections-defaultdict-items
Jun 19, 2026
Merged

python-stdlib/collections-defaultdict: Add items() function.#1100
dpgeorge merged 1 commit into
micropython:masterfrom
agatti:collections-defaultdict-items

Conversation

@agatti

@agatti agatti commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds the items() functions to collections.defaultdict, acting as a simple proxy to the underlying dictionary container.

This fixes #350.

The package's version number was bumped up by 0.1.0 since a new function was added to the module.

Testing

The added tests in python-stdlib/collections-defaultdict were run successfully both on CPython (with a tweaked import harness) and on MicroPython's git master.

Trade-offs and Alternatives

This adds 81 bytes to the final module. I'm a bit surprised it took that much, but unless it is documented that the backing dictionary can be accessed via self.d when code runs on MicroPython, I don't see any other safe way to enumerate the contents [1].

Generative AI

I did not use generative AI tools when creating this PR.


[1]

This trips up MicroPython, for example:

from collections import defaultdict
a = defaultdict.defaultdict(list)
for i in a:
    print(i)

On current MicroPython's git master:

$ cd python-stdlib/collections-defaultdict
$ $MICROPYTHON
MicroPython v1.28.0-preview.306.g5c00edcee2.dirty on 2026-03-23; linux [GCC 15.2.1] version
Type "help()" for more information.
>>> from collections import defaultdict
>>> a = defaultdict.defaultdict(list)
>>> for i in a:
...     print(i)
...
[]
[]
[]
[]
# `[]` being repeated a fairly large number of times here...
[]
[]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "collections/defaultdict.py", line 20, in __getitem__
MemoryError: memory allocation failed, allocating 260368 bytes
>>>

Whilst this works as expected on CPython 3.14 (and probably earlier versions too):

$ python
Python 3.14.3 (main, Feb 13 2026, 15:31:44) [GCC 15.2.1 20260209] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from collections import defaultdict
>>> a = defaultdict(list)
>>> for i in a:
...     print(i)
... 
>>>

@dpgeorge dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's a nice and simple addition, and definitely useful.

(Eventually it might be good to implement the iterator protocol on this class.)

@dpgeorge

Copy link
Copy Markdown
Member

This adds 81 bytes to the final module

That's strange. I tested it using mpy-cross defaultdict.py to just compile this source file, and see +23 byte change with this PR on the size of defaultdict.mpy. Using mpy-cross -O3 the increase is +19.

This commit adds the `items()` functions to `collections.defaultdict`,
acting as a simple proxy to the underlying dictionary container.

This fixes micropython#350.

Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
@dpgeorge dpgeorge force-pushed the collections-defaultdict-items branch from 1985bdb to 4ac5958 Compare June 19, 2026 02:59
@dpgeorge dpgeorge merged commit 4ac5958 into micropython:master Jun 19, 2026
5 checks passed
@agatti agatti deleted the collections-defaultdict-items branch June 19, 2026 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: 'defaultdict' object has no attribute 'items'

2 participants