← index #18198Issue #16969
Off-topic · high · value 1.352
QUERY · ISSUE

JSON not converting namedtuple to list

openby billbreitopened 2025-10-03updated 2025-10-03
bug

Port, board and/or hardware

Pico 2

MicroPython version

JSON correctly converts namedtuples to lists when they appear in list form, but not when used as value in dictionary. Seems to work the same on mpy verion 1.20 - 1.26, running on Pico, Pico2 , Arduino ESP32.

Work around by manually converting.

Reproduction

MicroPython 1.20 - 1.26

dict[str,list[tuple] {'a': [(1, 2, 3)], 'b': [(4, 5, 6)]}
dict[str,list[namedtuple] {'a': [TT(x=1, y=2, z=3)], 'b': [TT(x=4, y=5, z=6)]}

json.dumps tuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}
json.dumps namedtuples {"a": [TT(x=1, y=2, z=3)], "b": [TT(x=4, y=5, z=6)]}

Python 3.9

dict[str,list[tuple] {'a': [(1, 2, 3)], 'b': [(4, 5, 6)]}
dict[str,list[namedtuple] {'a': [TT(x=1, y=2, z=3)], 'b': [TT(x=4, y=5, z=6)]}

json.dumps tuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}
json.dumps namedtuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}

Expected behaviour

json.dumps namedtuples {"a": [[1, 2, 3]], "b": [[4, 5, 6]]}

Observed behaviour

json.dumps namedtuples {"a": [TT(x=1, y=2, z=3)], "b": [TT(x=4, y=5, z=6)]}

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

modulo operator on strings does not support namedtuple

closedby yoctopuceopened 2025-03-20updated 2025-04-21
bug

Port, board and/or hardware

any build with MICROPY_PY_BUILTINS_STR_OP_MODULO, including unix port

MicroPython version

MicroPython v1.25.0-preview.393.gf1018ee5c.dirty on 2025-03-20; linux [GCC 13.3.0] version

Reproduction

from collections import namedtuple
T = namedtuple("Tup", ["foo", "bar"])
t = T(1, 2)
"(%d, %d)" % t

Expected behaviour

Last expression is expected to return '(1, 2)' as in CPython.
NamedTuples should be handled the same as tuples.

Observed behaviour

An exception is raised:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't convert Tup to int

Additional Information

There is already a fixme in the code to deal with this issue.
I will file a PR with the fix shortly.

Code of Conduct

Yes, I agree

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