← index #7639Issue #3124
Off-topic · high · value 0.681
QUERY · ISSUE

micropython dict has not __contains__ method

openby bhcuong2008opened 2021-08-11updated 2021-08-12
py-core

Hi Team,

Today I check contains method of dict, but not found. MicroPython v1.16. I open file objdict.c, and also not found. Is it missed?

Thanks,
Cuong.

CANDIDATE · ISSUE

Unable to create builtin object from a subclass of a builtin

closedby MrSurlyopened 2017-05-31updated 2017-06-01

Using the baseline https://github.com/micropython/micropython-esp32/tree/esp32

Best explained with code:

class Y(dict): 
    pass
y = Y()
y[1] = 'one'
print(y)
dict(y)

Expected output (per CPython):

{1: 'one'}
{1: 'one'}

Actual output:

{1: 'one'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

I dug into this a bit:

In py/objdict.c, dict_make_new calls dict_update, which incorrectly identifies the type of the parameter as being something other than a dict.

How bound dict methods work for object y (e.g. y.items()) is above my paygrade, but they do work. Nonetheless, I tried modifying MP_OBJ_IS_DICT_TYPE to call mp_obj_is_subclass_fast. This doesn't work -- it actually gets a different mp_obj_t pointer than what is passed to y.items(). While the error goes away, the dict returned from the dict(y) line above is empty, since it's looking for dict items in a class Y object.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied