Skip to content

Fix flattened child unknown fields - #752

Merged
yukinarit merged 1 commit into
yukinarit:mainfrom
AllanChain:fix/flatten-unknown
Jun 7, 2026
Merged

Fix flattened child unknown fields#752
yukinarit merged 1 commit into
yukinarit:mainfrom
AllanChain:fix/flatten-unknown

Conversation

@AllanChain

Copy link
Copy Markdown
Contributor

Previously, when we have a flattened child class with deny_unknown_fields=True, we will have a problem deserializing.

import serde

@serde.serde(deny_unknown_fields=True)
class A:
    a: int

@serde.serde
class B:
    b: int
    a: A = serde.field(flatten=True, default_factory=A)

serde.from_dict(B, {"a": 1, "b": 2})

Actual behavior:

serde.compat.SerdeError: unknown fields: {'b'}, expected one of {'a'}

Expected:

B(b=2, a=A(a=1))

The fix strategy is passing only the relevant flattened input to child deserializers, achieved by knowing the exact keys expected by the flattened child. Special cases are flattened dict fields. If the parent has flattened dict fields whereas the flattened child does not, we will keep the unknown fields only visible by the parent. Otherwise, we will pass those to the child.


The changes are done by a coding agent, and I have reviewed the code by trying to understand the changes line by line. The changes are reasonable and readable.

@codecov

codecov Bot commented May 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.94%. Comparing base (f09d503) to head (fb6def1).
⚠️ Report is 72 commits behind head on main.

Files with missing lines Patch % Lines
serde/de.py 93.93% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #752      +/-   ##
==========================================
+ Coverage   90.83%   90.94%   +0.10%     
==========================================
  Files          13       13              
  Lines        2040     2340     +300     
  Branches      370      457      +87     
==========================================
+ Hits         1853     2128     +275     
- Misses        124      140      +16     
- Partials       63       72       +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

When a flattened child has deny_unknown_fields=True, it should not
see the parent's own fields as unknown input.

Pass only the relevant flattened input to child deserializers, while
still allowing flatten dict fields to collect extra keys in parent,
child, and nested child classes.
@AllanChain
AllanChain force-pushed the fix/flatten-unknown branch from 1b30396 to fb6def1 Compare May 31, 2026 08:36
@yukinarit yukinarit added the bug Bug report or fix label Jun 7, 2026

@yukinarit yukinarit left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@yukinarit
yukinarit merged commit 5b786f5 into yukinarit:main Jun 7, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Bug report or fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants