lovasoa / marshmallow_dataclass

Automatic generation of marshmallow schemas from dataclasses.

Home Page:https://lovasoa.github.io/marshmallow_dataclass/html/marshmallow_dataclass.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parameters of EnumField are ignored when loading a schema

Grennith opened this issue · comments

return marshmallow_enum.EnumField(typ, **metadata)

If a field is declared as follows, the line referenced above drops the parameters default and by_value (and any other parameters specified).

some_field: SomeEnum = EnumField(enum=SomeEnum, default=SomeEnum.FOO, by_value=True)

One quick workaround would be to check for the type of default as it already is an EnumField. There's no reason to instantiate one again dropping all parameters imho:

    if isinstance(typ, EnumMeta):
        import marshmallow_enum
        return default if isinstance(default, marshmallow_enum.EnumField) \
            else marshmallow_enum.EnumField(typ, **metadata)

Could be related to #166 and #117

I think this is a dup of #228 (or vice versa).

Note that:

@dataclass
class SomeData:
    some_field: SomeEnum = EnumField(...)

is not correct.

Correct usage would be, e.g.:

@dataclass
class SomeData:
    some_field: SomeEnum = dataclasses.field(default=SomeEnum.FOO, metadata={"by_value": True})

I don't think this is a bug, so I'm closing this. If conditions warrant, please comment on and reopen #228.