-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I make a field wrapped in Optional
without a default value to be required during deserialization?
#352
Comments
Optional
without a default value to be required during deserialization?Optional
without a default value to be required during deserialization?
Hi @Yura52! Do you mean that With this serde class, @serde.serde(type_check=serde.Strict)
@dataclass
class A:
b: Optional[int] This should raise an error because the dict has no key of "b"? 🤔 serde.from_dict(A, {}) |
Hi @yukinarit , yes, this is what I would expect, since there is no default value for the It seems to me that this kind of behavior (implicit That said, having a flag to make a required field truly required would be nice! |
Understood, I am okay to have that flag in pyserde, as long as we don't change the default behaviour! |
Hi! Thank you for the great library!
Let's consider this snippet:
Here, I require the field
b
to be explicitly passed to the constructor ofA
. However,serde
allows not passing the fieldb
during deserialization:Knowing that dacite made the same design choice, I assume this is an intentional thing, not a bug. Is there any available workaround to require being explicit whenever a field is missing a default value?
Also, I would like to share why, to me personally, the current behavior is surprising.
First, because of the mismatch between Python and serde: I cannot create the object without passing the required field in Python, but I can do it via serde. In other words, for serde,
Optional[int]
andOptional[int] = None
are equivalent, which is not intuitive to me.Second, because of the following inconsistency:
What do you think?
The text was updated successfully, but these errors were encountered: