Files
micropython/py/objdict.c
Damien George 128420359e py/objdict: Implement bool and len unary ops for dict views.
Currently, dict views (eg `dict.keys()`, `dict.values()`) do not implement
the `bool` or `len` unary operations.  That may seem like a reasonable
omission for MicroPython to keep code size down, but it actually leads to
silently incorrect bool operations, because by default things are true.

Eg we currently have:

    >>> bool(dict().keys())
    True

which is wrong, it should be `False` because the dict is empty.

This commit implements `bool` and `len` unary operations on dict views by
simply delegating to the existing dict unary op function.

Fixes issue #12385.

Signed-off-by: Damien George <damien@micropython.org>
2025-11-21 13:28:28 +11:00

24 KiB