fix: respect default_factory for abstract generic fields in from_dict#570
Open
sanjibani wants to merge 1 commit into
Open
fix: respect default_factory for abstract generic fields in from_dict#570sanjibani wants to merge 1 commit into
sanjibani wants to merge 1 commit into
Conversation
When a field typed as an abstract generic (e.g. MutableMapping[str, str])
has a default_factory, calling from_dict({}) previously raised
TypeError: MutableMapping() takes no arguments. Track fields populated
by default_factory and skip _decode_generic for them.
Fixes lidatong#505
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sanjibani
added a commit
to sanjibani/sanjibani
that referenced
this pull request
Jun 16, 2026
… OSS repos - merged: withastro/astro#17088 (docs: remove dead llms.txt) - open: vercel/next.js#94846 (eslint-plugin-next fix), #94850 (docs links) - open: denoland/deno#35266, vitejs/vite#22694, BerriAI/litellm#30530 - open: pixijs/pixijs#12094, mattermost/mattermost-mobile#9649 #9650 - open: lidatong/dataclasses-json#570, The-OpenROAD-Project/ORAssistant#258 #260 Star badges via shields.io. Sorted by repo stars descending.
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.
Problem
When a dataclass field is typed as an abstract generic collection (e.g.
MutableMapping[str, str]) and given adefault_factory, callingfrom_dict({})raisesTypeError: MutableMapping() takes no arguments.Fixes #505.
Root cause
In
_decode_dataclass, fields missing from the input dict are correctly populated viadefault_factory(). However, the subsequent decoding loop still calls_decode_genericon that already-correct value, which attempts to instantiate the abstractMutableMappingtype directly.Fix
Track which fields were populated by
default_factory(as opposed to arriving from the input data). In the decode loop, skip_decode_genericfor those fields and pass the factory-produced value through as-is.Test
Added a regression test in
tests/test_collections.pycovering both the empty-dict case and a full round-trip:```python
@DataClass
class MyClass(DataClassJsonMixin):
field1: MutableMapping[str, str] = field(default_factory=dict)
MyClass.from_dict({}) # previously raised TypeError
MyClass.from_dict(MyClass(field1={"key": "value"}).to_dict()) # round-trip
```
Full test suite: 323 passed, 4 skipped, 0 failures.
🤖 Generated with Claude Code