Skip to content

Commit

Permalink
Merge pull request #204 from mishamsk/fix-omit-default-enum
Browse files Browse the repository at this point in the history
fix str subtypes not working with omit_defaults
  • Loading branch information
Fatal1ty authored Mar 31, 2024
2 parents 78a1bd3 + c0b0708 commit 4eefc4f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mashumaro/core/meta/code/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ def get_dialect_or_config_option(
def get_field_default_literal(self, value: typing.Any) -> str:
if isinstance(value, enum.IntFlag):
return str(value.value)
elif isinstance(value, (str, int, bool, NoneType)): # type: ignore
elif type(value) in (str, int, bool, NoneType): # type: ignore
return repr(value)
elif (
isinstance(value, float)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_dialect.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections
import enum
import typing
from dataclasses import dataclass, field
from datetime import date, datetime
Expand Down Expand Up @@ -279,6 +280,11 @@ class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]


class MyStrEnum(str, enum.Enum):
VAL1 = "val1"
VAL2 = "val2"


@dataclass
class DataClassWithDefaultAndOmitDefaultDialect(DataClassDictMixin):
a: int = 42
Expand All @@ -295,6 +301,7 @@ class DataClassWithDefaultAndOmitDefaultDialect(DataClassDictMixin):
l: Set[int] = field(default_factory=lambda: {1, 2, 3})
m: FrozenSet[int] = field(default_factory=frozenset)
n: FrozenSet[int] = field(default_factory=lambda: frozenset({1, 2, 3}))
o: str = MyStrEnum.VAL1

class Config(BaseConfig):
dialect = OmitDefaultDialect
Expand All @@ -318,6 +325,7 @@ class DataClassWithDefaultAndOmitDefaultDialectAndOmitDefaultFalse(
l: Set[int] = field(default_factory=lambda: {1, 2, 3})
m: FrozenSet[int] = field(default_factory=frozenset)
n: FrozenSet[int] = field(default_factory=lambda: frozenset({1, 2, 3}))
o: str = MyStrEnum.VAL1

class Config(BaseConfig):
dialect = OmitDefaultDialect
Expand All @@ -342,6 +350,7 @@ class DataClassWithDefaultAndNotOmitDefaultDialectAndOmitDefaultTrue(
l: Set[int] = field(default_factory=lambda: {1, 2, 3})
m: FrozenSet[int] = field(default_factory=frozenset)
n: FrozenSet[int] = field(default_factory=lambda: frozenset({1, 2, 3}))
o: str = MyStrEnum.VAL1

class Config(BaseConfig):
dialect = NotOmitDefaultDialect
Expand All @@ -364,6 +373,7 @@ class DataClassWithDefaultAndEmptyDialect(DataClassDictMixin):
l: Set[int] = field(default_factory=lambda: {1, 2, 3})
m: FrozenSet[int] = field(default_factory=frozenset)
n: FrozenSet[int] = field(default_factory=lambda: frozenset({1, 2, 3}))
o: str = MyStrEnum.VAL1

class Config(BaseConfig):
dialect = EmptyDialect
Expand All @@ -386,6 +396,7 @@ class DataClassWithDefaultAndDialectSupport(DataClassDictMixin):
l: Set[int] = field(default_factory=lambda: {1, 2, 3})
m: FrozenSet[int] = field(default_factory=frozenset)
n: FrozenSet[int] = field(default_factory=lambda: frozenset({1, 2, 3}))
o: str = MyStrEnum.VAL1

class Config(BaseConfig):
code_generation_options = [ADD_DIALECT_SUPPORT]
Expand Down Expand Up @@ -1135,6 +1146,7 @@ def test_dataclass_omit_default_dialects():
"l": [1, 2, 3],
"m": [],
"n": [1, 2, 3],
"o": MyStrEnum.VAL1,
}
assert DataClassWithDefaultAndOmitDefaultDialect().to_dict() == {}
assert (
Expand Down

0 comments on commit 4eefc4f

Please sign in to comment.