A(b=[B(a=...)])
B(a=A(b=[...]))
Traceback (most recent call last):
File "/home/classic/dev/sqlalchemy/test4.py", line 29, in <module>
dataclasses.asdict(a1)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1232, in asdict
return _asdict_inner(obj, dict_factory)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1239, in _asdict_inner
value = _asdict_inner(getattr(obj, f.name), dict_factory)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1267, in _asdict_inner
return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1267, in <genexpr>
return type(obj)(_asdict_inner(v, dict_factory) for v in obj)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1239, in _asdict_inner
value = _asdict_inner(getattr(obj, f.name), dict_factory)
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1239, in _asdict_inner
...
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1236, in _asdict_inner
if _is_dataclass_instance(obj):
File "/opt/python-3.10.0/lib/python3.10/dataclasses.py", line 1201, in _is_dataclass_instance
return hasattr(type(obj), _FIELDS)
RecursionError: maximum recursion depth exceeded while calling a Python object
there seems to be no workaround as these two methods are pretty simple and don't accept any arguments like "check_recursion", IIUC there is also no directive on field() that would change this either?
I don't see this mentioned anywhere and it seems a bit unusual, we can't use
asdict()orastuple()with dataclasses that have cycles to each other. This is something that is handled by most other stdlib features such as deepcopy, dataclasses stringify, etc.Example below
output:
there seems to be no workaround as these two methods are pretty simple and don't accept any arguments like "check_recursion", IIUC there is also no directive on field() that would change this either?
Linked PRs