Skip to content

dict.keys() is empty in subclassed dict #8850

@miguelgrinberg

Description

@miguelgrinberg

CircuitPython version

MicroPython 9.0.0-alpha.6-49-gb704452f70 on 2024-01-27; linux [GCC 11.4.0] version

Code/REPL

class MyDict(dict):
    def __init__(self, initial_dict=None):
        super().__init__(initial_dict)

d = dict({'foo': 'bar', 'a': 123})
print(d.keys())  # keys is correct
d = MyDict({'foo': 'bar', 'a': 123})
print(d.keys())  # keys is empty

Behavior

When running the above example, the base dictionary is not initialized correctly.

Output using CircuitPython:

{'a': 123, 'foo': 'bar'} dict_keys(['a', 'foo'])
{'a': 123, 'foo': 'bar'} dict_keys([])

Output using MicroPython:

{'a': 123, 'foo': 'bar'} dict_keys(['a', 'foo'])
{'a': 123, 'foo': 'bar'} dict_keys(['a', 'foo'])

Output using CPython:

{'foo': 'bar', 'a': 123} dict_keys(['foo', 'a'])
{'foo': 'bar', 'a': 123} dict_keys(['foo', 'a'])

Description

I think this might be in part related to #8758, but unfortunately that fix does not help.

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions