Repository navigation
Reject non-mapping input when deserializing a dataclass from dict - #815
Merged
yukinarit merged 1 commit intoOct 22, 2026
Merged
Conversation
from_dict() looked up fields with `"name" in data`, which also works on
lists and strings, so inputs like [] or "" silently produced a dataclass
built from its defaults. The generated dict deserializer now raises
SerdeError unless data is a Mapping. {} still uses the defaults.
Fixes #811
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.
Fixes #811
from_dict()on a dataclass checked fields with"name" in data. That also works on lists and strings, sofrom_dict(Policy, [])orfrom_dict(Policy, "")returnedPolicy()built from the defaults instead of failing.The generated dict deserializer now raises
SerdeErrorwhendatais not aMapping. A plaindictis matched first with an exact class check, so the common case skips theisinstance(..., Mapping)call.{}still gives the defaults, and other mappings such asOrderedDictandMappingProxyTypestill work. The same check covers nested dataclass fields. In untagged unions, a list or string no longer matches a dataclass member just because that member has defaults.The check uses the global name
_Mappingbecause the generated code already stores field type names in that namespace.Tests: added
test_from_dict_rejects_non_mappingintests/test_basics.py. It fails onmainand passes with this change. Full suite (pytest tests --doctest-modules serde -nauto): 5298 passed, 2 skipped, 1 xfailed. Examples pass, and the pre-commit hooks (black, ruff, mypy, pyright) pass on the changed files.This is related to #809, which deals with sequence fields; both PRs add
Mappingto the same import line inserde/de.py.