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

desert crash on optional types when using custom fields #198

Open
sveinse opened this issue May 21, 2022 · 1 comment
Open

desert crash on optional types when using custom fields #198

sveinse opened this issue May 21, 2022 · 1 comment

Comments

@sveinse
Copy link
Collaborator

sveinse commented May 21, 2022

Using an optional field with any custom marshmallow field class cause desert to crash on de-serialization. E.g. a field with b: Optional[B] = desert.ib(BField(), default=None) does not work.

from typing import Optional
import desert
import marshmallow
import attr

class B:
    pass

class BField(marshmallow.fields.Field):
    pass

@attr.s
class A:
    a: int = attr.ib()
    b: Optional[B] = desert.ib(BField(), default=None)

a = A(42)
print(a)

s = desert.schema(A)
d = s.dump(a)
print(d)

a2 = s.load(d)
print(a2)

This results in:

A(a=42, b=None)
{'a': 42, 'b': None}
Traceback (most recent call last):
  File "C:\Svein\Prosjekt\elns\__work\desertbug.py", line 24, in <module>
    a2 = s.load(d)
  File "C:\Svein\Prosjekt\elns\venv\lib\site-packages\marshmallow\schema.py", line 717, in load
    return self._do_load(
  File "C:\Svein\Prosjekt\elns\venv\lib\site-packages\marshmallow\schema.py", line 900, in _do_load
    raise exc
marshmallow.exceptions.ValidationError: {'b': ['Field may not be null.']}
@sveinse
Copy link
Collaborator Author

sveinse commented May 22, 2022

I've found a fix or workaround (I'm not sure which it is): If I change to

    b: Optional[B] = desert.ib(BField(allow_none=True), default=None)

I.e. when using custom fields, one must manually declare if the field can be none. The typehint Optional[B] is ignored. So is the consequence of using a custom field that it voids the desert-ness?

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

1 participant