question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support dataclass default None as allowed type

See original GitHub issue

Hi, 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:open
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
wanderrfulcommented, Jul 28, 2020

For what it’s worth, my random two cents is that when we instantiate the desert.Schema in the first place, just as we can add meta={"unknown": marshmallow.EXCLUDE} we should also be able to add a "partial": True", which should implicitly make all fields in the Dataclass schema have from typing import Optional wrapped 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=True kwarg 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! 🚀

1reaction
remidebettecommented, May 2, 2020

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 = None that makes it a python keyword (hence optional):

from dataclasses import dataclass
from typing import Optional

import desert

@dataclass
class WebhooksInfoItem:
    url: str = None

WebhooksInfoItemSchema = desert.schema(WebhooksInfoItem)

WebhooksInfoItemSchema.load({})
# Ok: WebhooksInfoItem(url=None)   as there is a default value
WebhooksInfoItem()
# Ok: WebhooksInfoItem(url=None)  as there is a default value

@dataclass
class WebhooksInfoItem:
    url: Optional[str]

WebhooksInfoItemSchema = desert.schema(WebhooksInfoItem)

WebhooksInfoItemSchema.load({})
# Ok: WebhooksInfoItem(url=None)   since it is Optional
# also WebhooksInfoItemSchema.dump(WebhooksInfoItemSchema.load({}))
# {"url":None}

WebhooksInfoItem()
# Error: TypeError: __init__() missing 1 required positional argument: 'url'

This all might confuse a python beginner starting to use desert.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found