← index #18198Issue #16266
Off-topic · high · value 0.915
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

f-string too greedy ?

closedby WebReflectionopened 2024-11-19updated 2026-03-17
bugpy-core

Port, board and/or hardware

Arduino Nano ESP32 or MicroPython WASM

MicroPython version

MicroPython v1.24.0

Reproduction

import json

b = "b"

# throws
f'f{json.dumps(f"a{b}c")}'

# works
value = json.dumps(f"a{b}c")
f'f{value}'

Expected behaviour

I wasn't expecting the need to create a reference for a value passed as interpolation and I've thought f was weird in that regard, then I've tested on my Linux Python and everything works as expected where it throws in MicroPython instead.

Observed behaviour

It looks like the f in MicroPython is a bit too greedy ... I believed that scanning for nested { and } would solve the issue as it needs to ignore those curly braces but then again, even trying to make some curly explicit:

f'f{json.dumps("a{{b}}c")}'

there is a Syntax error, but also that would produce an invalid f string in C-Python: 'f"a{{b}}c"' so that such string can't be evaluated anymore.

Additional Information

I've no idea if this is a duplicate but the example is fairly simple and somehow weird to reason about ... the goal of the code is to create strings that can be evaluated without needing to enforce f"..." around content

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