python-attrs / python-attrs/attrs
Missing recursion detection in attr.asdict
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.8k
- Forks
- 480
- Avg merge
- 2h 15m
- Merged PRs (30d)
- 2
Description
Suppose we use attrs instances to represent graphs:
>>> @attr.s
... class Node:
... val = attr.ib()
... linked_node = attr.ib(default=None)
...
>>> n = Node(1)
>>> attr.asdict(n)
{'val': 1, 'linked_node': None}
>>> n.linked_node = n
>>> n
Node(val=1, linked_node=...)
Although the repr handles it, attr.asdict does not detect and resolve cycles - should it?
>>> d = attr.asdict(n)
...
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
...
.venv/lib/python3.9/site-packages/attr/_funcs.py in asdict(inst, recurse, filter, dict_factory, retain_collection_types, value_serializer)
60 if recurse is True:
61 if has(v.__class__):
---> 62 rv[a.name] = asdict(
63 v,
64 True,
RecursionError: maximum recursion depth exceeded while calling a Python object
Maybe this the intended behavior (if so, please close this issue), I'm not sure. But I half-expected a self-referencing dict to be returned:
>>> d
{'val': 1, 'linked_node': {...}}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the asdict entry point in attr/_funcs.py shown in the traceback and reproduce the self-referencing Node example from the issue. Resolve the intended cycle behavior before implementing it, then add regression coverage; the issue is complete when recursive input has explicit, tested behavior instead of an unbounded RecursionError.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100