circuitpython/tests/basics/dict_views.py
David Lechner 2fe6d4eb86 py/objdict: Fix __hash__ for dict_view types.
This adds a unary_op implementation for the dict_view type that makes
the implementation of `hash()` for these types compatible with CPython.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:06:17 +10:00

40 lines
678 B
Python

d = {1: 2}
for m in d.items, d.values, d.keys:
print(m())
print(list(m()))
# print a view with more than one item
print({1:1, 2:1}.values())
# unsupported binary op on a dict values view
try:
{1:1}.values() + 1
except TypeError:
print('TypeError')
# unsupported binary op on a dict keys view
try:
{1:1}.keys() + 1
except TypeError:
print('TypeError')
# keys dict_view is not hashable
try:
hash({}.keys())
except TypeError:
print('TypeError')
# values dict_view is hashable
print(type(hash({}.values())))
# items dict_view is not hashable
try:
hash({}.items())
except TypeError:
print('TypeError')
# set operations still to come