Fix dataclass asdict - #3886
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Valid tuple or None states returned by __getstate__() are not handled correctly.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Updates dataclass_utils.asdict to respect custom object serialization state.
Changes:
- Uses
__getstate__()when available. - Adds tests for filtered custom state.
File summaries
| File | Description |
|---|---|
packages/dataclass_utils/_dataclass_asdict.py |
Adds custom-state serialization, but incorrectly assumes __getstate__() returns a mapping. |
packages/dataclass_utils/_dataclass_asdict_test.py |
Tests custom-state serialization. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| state = getattr(value, "__getstate__", None) | ||
| value = state() if state is not None else vars(cast(object, value)) |
There was a problem hiding this comment.
🟡 Changes recommended
The inherited object.__getstate__ invocation causes a critical Python 3.11+ regression.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (1)
packages/dataclass_utils/_dataclass_asdict.py:35
- Returning the original object here does not make it an
AsDictValue; every JSON-native leaf type has already been handled above, so values reaching this branch (for exampleobject()or a lock) remain non-serializable. This also defers the failure to callers such asPub.pub, which runjson.dumps(packages/helpermodules/pub.py:20), instead of producing the function's declared JSON-like result. Preserve theTypeError(or explicitly convert supported leaf types) rather than hiding it with a cast.
try:
value = vars(cast(object, value))
except TypeError:
return cast(AsDictValue, value)
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
The critical __getstate__ invocation bug must be fixed before approval.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| default_getstate = getattr(object, "__getstate__", None) | ||
| if callable(default_getstate) and getattr(type(value), "__getstate__", None) is not default_getstate: | ||
| return asdict(default_getstate()) |
There was a problem hiding this comment.
🟡 Changes recommended
The fallback can return unsupported, non-serializable objects instead of failing explicitly.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| try: | ||
| value = vars(cast(object, value)) | ||
| except TypeError: | ||
| return cast(AsDictValue, value) |
There was a problem hiding this comment.
🟡 Changes recommended
Dataclass fields must be read only once during serialization.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
* fix dataclass asdict * review * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * fix review * fix * review * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: LKuemmel <lena.kuemmel@openwb.de> Co-authored-by: LKuemmel <76958050+LKuemmel@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
No description provided.