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

None not being assigned to Union #87

Closed
rllin opened this issue Jun 3, 2020 · 3 comments
Closed

None not being assigned to Union #87

rllin opened this issue Jun 3, 2020 · 3 comments

Comments

@rllin
Copy link

rllin commented Jun 3, 2020

packages
marshmallow==3.6.1
marshmallow-dataclass==7.6.0
marshmallow-enum==1.5.1
marshmallow-union==0.1.15

from typing import List, Union

from marshmallow_dataclass import dataclass

import dataclasses

noned = lambda: dataclasses.field(
    metadata=dict(
        required=False,
        missing=None,
        default=None,
    )
)

@dataclass
class Inner:
    inner_var: int

@dataclass
class Test:
    req: int = None
    var: Union[int, Inner, List[Inner]] = noned()

I get the following error trying to use the class

/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:1019: in dataclass
    return wrap(cls)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:1011: in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:925: in _process_class
    _init_fn(flds,
/usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/dataclasses.py:502: in _init_fn
    raise TypeError(f'non-default argument {f.name!r} '
E   TypeError: non-default argument 'var' follows default argument

It seems like default and missing None are not being assigned properly?

@lovasoa
Copy link
Owner

lovasoa commented Jun 3, 2020

Your type annotation is incorrect. A field of typeUnion[int, Inner, List[Inner]] cannot be None.

@rllin
Copy link
Author

rllin commented Jun 3, 2020

oh how come? the following is valid. am i missing something?

@dataclass
class Inner:
    inner_var: int = None

@lovasoa
Copy link
Owner

lovasoa commented Jun 3, 2020

None is not an instance of int. You can use Optional[int] or Union[int, None]

See typing — Support for type hints — Python 3.8.3 documentation
https://docs.python.org/3/library/typing.html#typing.Optional

@lovasoa lovasoa closed this as completed Sep 9, 2020
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

2 participants