Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix str subtypes not working with omit_defaults #204

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mashumaro/core/meta/code/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,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
Loading