gh-94345: Fix dataclasses.asdict()/astuple() crash on circular references#151904
Open
iamsharduld wants to merge 1 commit into
Open
gh-94345: Fix dataclasses.asdict()/astuple() crash on circular references#151904iamsharduld wants to merge 1 commit into
iamsharduld wants to merge 1 commit into
Conversation
…references
asdict() and astuple() recursed into nested dataclasses and containers
without tracking which objects were already being processed, so a circular
reference recursed until a RecursionError (or crashed the interpreter on a
release build). Track the objects on the current recursion path and raise
ValueError("Circular reference detected") -- matching json.dumps() -- when
one is revisited.
Documentation build overview
|
eendebakpt
reviewed
Jun 22, 2026
| @@ -0,0 +1,4 @@ | |||
| :func:`dataclasses.asdict` and :func:`dataclasses.astuple` now raise | |||
| :exc:`ValueError` when the dataclass instance contains a circular reference, | |||
| instead of recursing until a :exc:`RecursionError` (or crashing on a release | |||
Contributor
There was a problem hiding this comment.
Is there a crash on release builds (e.g. segmentation fault) or a python exception?
| reference. | ||
|
|
||
| .. versionchanged:: next | ||
| A circular reference now raises :exc:`ValueError` instead of |
Contributor
There was a problem hiding this comment.
The PR adds overhead to the conversion to dict or tuple. Can you benchmark this?
Adding overhead just to change the type of error message might be ok, but I would like to have some quantative numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
asdict()andastuple()recurse into nested dataclasses and built-in containers without tracking which objects are already being processed, so a circular reference recurses until aRecursionError(or crashes the interpreter on a release build).This tracks the objects on the current recursion path and raises
ValueError("Circular reference detected")— matchingjson.dumps()— when one is revisited. Objects referenced more than once without forming a cycle (a DAG) are still converted normally.