Giter Site home page Giter Site logo

Comments (3)

otonnesen avatar otonnesen commented on June 1, 2024 2

I think the issue is in this line:

    version : Version = field_for_schema(Version, metadata={'by_value' : True})

Fields are intended to be passed as annotations for the dataclass's fields, not as defaults for them. marshmallow-dataclass sees version : Version and makes a field called "version" and binds to it a marshmallow field constructed from the Version Enum (i.e. no custom metadata), then sets the default for that marshmallow field to field_for_schema(Version, metadata={'by_value' : True}) . We can see this behaviour by not passing any value for version when loading:

>>> @dataclass
... class VersionContainer:
...     version : Version = field_for_schema(Version, metadata={'by_value' : True})
...
>>> class_schema(VersionContainer)().load({})
VersionContainer(version=<fields.Enum(dump_default=<marshmallow.missing>, attribute=None, validate=None, required=True, load_only=False, dump_only=False, load_default=<marshmallow.missing>, allow_none=False, error_messages={'required': 'Missing data for required field.', 'null': 'Field may not be null.', 'validator_failed': 'Invalid value.', 'unknown': 'Must be one of: {choices}.'})>)

The workaround you posted is just the standard/intended way to pass in extra field metadata when using this library (see https://github.com/lovasoa/marshmallow_dataclass#customizing-generated-fields). Another option if you wish to avoid this field syntax could be to use marshmallow_dataclass.NewType instead, eg:

>>> VersionField = marshmallow_dataclass.NewType("Version", Version, field=marshmallow.fields.Enum, enum=Version, by_value=True)
>>> @dataclass
... class VersionContainer:
...     version: VersionField
...
>>> class_schema(VersionContainer)().load({"version": "foo/v1"})
VersionContainer(version=<Version.v1: 'foo/v1'>)

Hope that helps! Let me know if I misunderstood the problem.

from marshmallow_dataclass.

dairiki avatar dairiki commented on June 1, 2024 1

Test case which demonstrates the problem

...
@dataclass
class VersionContainer:
    version : Version = field_for_schema(Version, metadata={'by_value' : True})

This is incorrect usage of field_for_schema. (In fact direct use of field_for_schema is not documented.)

Workaround appears to declare the field using dataclasses directly instead of marshmallow_dataclass

from dataclasses import dataclass, field
...
@dataclass
class VersionContainer:
    version : Version = field(default=None, metadata={'by_value' : True})

This is not so much a workaround, as closer to correct usage.

Even more correct, assuming that you do not want to specify a default value for VersionContainer.version would be:

@dataclass
class VersionContainer:
    version : Version = field(metadata={'by_value' : True})

I don't think this is a bug. (The immediately preceding example appears to work fine for me.)
Closing. Feel free to reopen if the situation warrants.

from marshmallow_dataclass.

therice avatar therice commented on June 1, 2024

Workaround appears to declare the field using dataclasses directly instead of marshmallow_dataclass

...
from dataclasses import dataclass, field
...
@dataclass
class VersionContainer:
    version : Version = field(default=None, metadata={'by_value' : True})

from marshmallow_dataclass.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.