Skip to content

desert crash on optional types when using custom fields #198

@sveinse

Description

@sveinse

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.']}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions