Skip to content

Commit 614f6a1

Browse files
authored
Replace marshmallow-enum with native marshmallow.fields.Enum (lovasoa#227)
Authored-by: Oliver Tonnesen <oliver.tonnesen@panienergy.com>
1 parent 1d0f019 commit 614f6a1

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pip3 install marshmallow-dataclass
5656

5757
You may optionally install the following extras:
5858

59-
- `enum` : for translating python enums to [marshmallow-enum](https://github.com/justanr/marshmallow_enum).
59+
- `enum` : enum support for marshmallow versions <3.18 [marshmallow-enum](https://github.com/justanr/marshmallow_enum).
6060
- `union` : for translating python [`Union` types](https://docs.python.org/3/library/typing.html#typing.Union) to union fields.
6161

6262
```shell

marshmallow_dataclass/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class User:
4040
import threading
4141
import types
4242
import warnings
43-
from enum import EnumMeta
43+
from enum import Enum
4444
from functools import lru_cache, partial
4545
from typing import (
4646
Any,
@@ -715,10 +715,14 @@ def field_for_schema(
715715
)
716716

717717
# enumerations
718-
if isinstance(typ, EnumMeta):
719-
import marshmallow_enum
718+
if issubclass(typ, Enum):
719+
try:
720+
return marshmallow.fields.Enum(typ, **metadata)
721+
except AttributeError:
722+
# Remove this once support for python 3.6 is dropped.
723+
import marshmallow_enum
720724

721-
return marshmallow_enum.EnumField(typ, **metadata)
725+
return marshmallow_enum.EnumField(typ, **metadata)
722726

723727
# Nested marshmallow dataclass
724728
# it would be just a class name instead of actual schema util the schema is not ready yet
@@ -731,7 +735,7 @@ def field_for_schema(
731735
nested_schema
732736
or forward_reference
733737
or _RECURSION_GUARD.seen_classes.get(typ)
734-
or _internal_class_schema(typ, base_schema, typ_frame)
738+
or _internal_class_schema(typ, base_schema, typ_frame) # type: ignore [arg-type]
735739
)
736740

737741
return marshmallow.fields.Nested(nested, **metadata)

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
]
1919

2020
EXTRAS_REQUIRE = {
21-
"enum": ["marshmallow-enum"],
21+
"enum": [
22+
"marshmallow-enum; python_version < '3.7'",
23+
"marshmallow>=3.18.0,<4.0; python_version >= '3.7'",
24+
],
2225
"union": ["typeguard"],
2326
"lint": ["pre-commit~=2.17"],
2427
':python_version == "3.6"': ["dataclasses", "types-dataclasses<0.6.4"],

tests/test_field_for_schema.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,23 @@ def test_optional_str(self):
114114
)
115115

116116
def test_enum(self):
117-
import marshmallow_enum
118-
119117
class Color(Enum):
120118
RED: 1
121119
GREEN: 2
122120
BLUE: 3
123121

124-
self.assertFieldsEqual(
125-
field_for_schema(Color),
126-
marshmallow_enum.EnumField(enum=Color, required=True),
127-
)
122+
if hasattr(fields, "Enum"):
123+
self.assertFieldsEqual(
124+
field_for_schema(Color),
125+
fields.Enum(enum=Color, required=True),
126+
)
127+
else:
128+
import marshmallow_enum
129+
130+
self.assertFieldsEqual(
131+
field_for_schema(Color),
132+
marshmallow_enum.EnumField(enum=Color, required=True),
133+
)
128134

129135
def test_literal(self):
130136
self.assertFieldsEqual(

0 commit comments

Comments
 (0)