Support dataclass default None as allowed type
See original GitHub issueHi, thanks for the nice library.
The following use case triggers an error in desert:
from dataclasses import dataclass
import desert
@dataclass
class WebhooksInfoItem:
url: str = None
WebhooksInfoItemSchema = desert.schema(WebhooksInfoItem)
WebhooksInfoItemSchema.loads('{}')
# Ok: WebhooksInfoItem(url=None)
WebhooksInfoItemSchema.loads('{"url": null}')
# marshmallow.exceptions.ValidationError: {'url': ['Field may not be null.']}
marshmallow_dataclass seems to support optional declaration through default None in both cases. Is it forbidden in desert by design? It does not look like it according to the first usecase.
I feel that = None to be an implicit equivalent of Optional[] to be a good shortcut.
Maybe I do not see a potential drawback?
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (5 by maintainers)
Top Results From Across the Web
How to apply default value to Python dataclass field when ...
In data classes you can access a default value of class attribute: Specs.b You can check for None and pass default value if...
Read more >dataclasses — Data Classes — Python 3.11.1 documentation
A field is defined as a class variable that has a type annotation. With two exceptions described below, nothing in dataclass() examines the...
Read more >6.8. Dataclass Field — Python: From None to Machine Learning
If you want to create a list with default values, you have to create a field with default_factory=lambda: ['Ares3', 'Apollo18'] . Lambda expression...
Read more >Data Classes in Python 3.7+ (Guide)
With data classes you do not have to write boilerplate code to get proper ... How to add default values to data class...
Read more >Everything you need to know about dataclasses - rmcomplexity
There are two ways of defining a field in a data class. Using type hints and an optional default value. from dataclasses import...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

For what it’s worth, my random two cents is that when we instantiate the
desert.Schemain the first place, just as we can addmeta={"unknown": marshmallow.EXCLUDE}we should also be able to add a"partial": True", which should implicitly make all fields in the Dataclass schema havefrom typing import Optionalwrapped around each member field of the Dataclass if it doesn’t already have it when it gets processed internally.My personal inuitition when I encountered this problem was that the
partial=Truekwarg in the schema’s instantiation or load methods should have done that, but it didn’t, which led me to this thread. Thanks for reading! 🚀I am still discovering the way to use the lib.
I do think Optional is actually more suited to my purpose. As it works as expected I already reworked my code to use Optional instead.
(It also has the advantage that I do not need to bubble down all optional parameters to the bottom of my dataclass.)
But I still think the error does not protect against much.
Very interestingly, when using the python dataclass constructor itself the behavior is exactly reversed since it is
= Nonethat makes it a python keyword (hence optional):This all might confuse a python beginner starting to use desert.