dataclasses astuple and asdict crash on recursive dataclass structures / dont support deepcopy memo
@ericvsmith がすでに取り組んでいます。
2022年6月27日 から。
評価
この issue はまだ評価されていません。
説明
I don't see this mentioned anywhere and it seems a bit unusual, we can't use asdict() or astuple() 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
from __future__ import annotations
import dataclasses
# dataclasses support recursive structures
@dataclasses.dataclass
class A:
b: list[B] = dataclasses.field(default_factory=list)
@dataclasses.dataclass
class B:
a: A
# we can make a recursive structure
a1 = A()
b1 = B(a1)
a1.b.append(b1)
# stringify supports recursion
print(a1)
print(b1)
# asdict and astuple don't however, with no way to even work around
# it (like pass in a set to track already seen objects)
# recursion overflow
dataclasses.asdict(a1)
# same
dataclasses.astuple(a1)
output:
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?
Linked PRs
- gh-151904
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 36k
- 平均マージ
- 1日 9時間
- マージ済み PR(30日)
- 558
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/cpython のほかの issue
-
docs pending
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
stdlib type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
build type-bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
stdlib topic-email type-feature
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
似ている issue
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100