JSON not converting namedtuple to list
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
f-string too greedy ?
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