Skip to content
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

An Optional type expects 1 subtype #122

Open
ruaridhw opened this issue Apr 22, 2021 · 3 comments
Open

An Optional type expects 1 subtype #122

ruaridhw opened this issue Apr 22, 2021 · 3 comments

Comments

@ruaridhw
Copy link

desert throws an error when a dataclass has more than one subtype other than None

import desert
from dataclasses import dataclass
from typing import Union

from marshmallow import Schema

@dataclass
class MyOptionalKlass:
    a: Union[float, str, None] = 3

schema = desert.schema(MyOptionalKlass)
#> ValueError: too many values to unpack (expected 1)

I think this is to do with the check of is_optional_type() before is_union_type(). I think within the is_optional_type() branch we should create subfields for all remaining subtypes other than NoneType.

Even without the None in the type, the default value of the field is still <marshmallow.missing> rather than 3

@altendky
Copy link
Member

I think this is basically a lead in to #36. Perhaps a simpler 'handle the basic types only' solution could be useful. Or perhaps it is better as a trivial recipe for the general solution. For example, would a JSON true become 1 or 'True' (or 'true') or be rejected?

@JMMarchant
Copy link

Also encountering the same when using Optional as part of the type hint:

from dataclasses import dataclass
from typing import List, Optional, Union

import desert


@dataclass
class ExplicitlyOptionalClass:
    x: Optional[Union[str, List[str]]] = None


s = desert.schema(ExplicitlyOptionalClass)
# > ValueError: too many values to unpack (expected 1)

which is causing a conflict with our mypy settings of not having implicit Optionals.

@dargueta
Copy link
Contributor

I'm still running into this with the latest version (2020.11.18).

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

No branches or pull requests

4 participants