Skip to content

Commit

Permalink
add support for MyEnum(str, Enum) (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ydylla committed Jan 27, 2021
1 parent 8f92e21 commit 3d32ef1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions mashumaro/serializer/base/metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,9 @@ def _pack_value(
raise UnserializableDataError(
f"{ftype} as a field type is not supported by mashumaro"
)
elif issubclass(origin_type, typing.Collection):
elif issubclass(origin_type, typing.Collection) and not issubclass(
origin_type, enum.Enum
):
args = getattr(ftype, "__args__", ())

def inner_expr(arg_num=0, v_name="value"):
Expand Down Expand Up @@ -567,7 +569,9 @@ def _unpack_field_value(
raise UnserializableDataError(
f"{ftype} as a field type is not supported by mashumaro"
)
elif issubclass(origin_type, typing.Collection):
elif issubclass(origin_type, typing.Collection) and not issubclass(
origin_type, enum.Enum
):
args = getattr(ftype, "__args__", ())

def inner_expr(arg_num=0, v_name="value"):
Expand Down
5 changes: 5 additions & 0 deletions tests/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ class MyEnum(Enum):
b = "letter b"


class MyStrEnum(str, Enum):
a = "letter a"
b = "letter b"


class MyIntEnum(IntEnum):
a = 1
b = 2
Expand Down
3 changes: 3 additions & 0 deletions tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
MyFlag,
MyIntEnum,
MyIntFlag,
MyStrEnum,
)
from .utils import same_types

Expand All @@ -84,6 +85,7 @@ class Fixture:
STR = "123"
ENUM = MyEnum.a
INT_ENUM = MyIntEnum.a
STR_ENUM = MyStrEnum.a
FLAG = MyFlag.a
INT_FLAG = MyIntFlag.a
DATA_CLASS = MyDataClass(a=1, b=2)
Expand Down Expand Up @@ -136,6 +138,7 @@ class Fixture:
(bytearray, Fixture.BYTE_ARRAY, Fixture.BYTE_ARRAY),
(str, Fixture.STR, Fixture.STR),
(MyEnum, Fixture.ENUM, Fixture.ENUM),
(MyStrEnum, Fixture.STR_ENUM, Fixture.STR_ENUM),
(MyIntEnum, Fixture.INT_ENUM, Fixture.INT_ENUM),
(MyFlag, Fixture.FLAG, Fixture.FLAG),
(MyIntFlag, Fixture.INT_FLAG, Fixture.INT_FLAG),
Expand Down

0 comments on commit 3d32ef1

Please sign in to comment.