Comments (3)
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.
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.
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)
- Add native marshmallow.fields.Enum suport HOT 1
- str | None fails HOT 5
- field_for_schema can use _RECURSION_GUARD.seen_classes before it is initialized
- TypeError when creating a marshmallow schema from dataclass inherited from generic class HOT 1
- Enum fields always cast as JSON instead of native type
- Issues with typeguard package v3 HOT 3
- Error when using marshmallow_dataclass with attr.s HOT 1
- Arch Linux package? HOT 4
- Ability to configure custom Nested field class
- 8.5.13 broken on py3.9? HOT 3
- mypy: error: "Type[X]" has no attribute "Schema" [attr-defined] HOT 1
- Validating enum type HOT 1
- Union field is broken: The order of Union arguments is indeterminate
- Union with generic types is broken HOT 3
- Type stub-only objects not allowed in dataclass fields HOT 7
- The type constraint of `errors` is broken HOT 3
- "dump" ignores validation HOT 2
- list[enum] ignores "by_value": True flag HOT 1
- Refactor 'NewType' as it no longer conforms to the typing spec HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from marshmallow_dataclass.