Skip to content

Reject None for non-optional field in coerce mode#746

Open
AllanChain wants to merge 1 commit intoyukinarit:mainfrom
AllanChain:fix/coerce-none
Open

Reject None for non-optional field in coerce mode#746
AllanChain wants to merge 1 commit intoyukinarit:mainfrom
AllanChain:fix/coerce-none

Conversation

@AllanChain
Copy link
Copy Markdown
Contributor

Currently, type_check=coerce lets the deserializer silently accept None for a non-optional field, which is inconsistent with the documentation. For example:

import serde

@serde.serde(type_check=serde.coerce)
class A:
    a: int = 0

print(serde.from_dict(A, {"a": None}))

The above code outputs the following before the fix:

A(a=None)

But a SerdeError is expected.

Here, we raise SerdeError when coerce_object() receives None for a non-optional field.

Currently, `type_check=coerce` lets the deserializer silently accept
`None` for a non-optional field, which is inconsistent with the
documentations.

Here, we raise `SerdeError` when `coerce_object()` receives `None` for a
non-optional field.
@codecov
Copy link
Copy Markdown

codecov Bot commented May 5, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.90%. Comparing base (f09d503) to head (387961c).
⚠️ Report is 69 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #746      +/-   ##
==========================================
+ Coverage   90.83%   90.90%   +0.06%     
==========================================
  Files          13       13              
  Lines        2040     2308     +268     
  Branches      370      451      +81     
==========================================
+ Hits         1853     2098     +245     
- Misses        124      140      +16     
- Partials       63       70       +7     

☔ View full report in Codecov by Sentry.
📢 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant