Fatal1ty / mashumaro

Fast and well tested serialization library
Apache License 2.0
751 stars 44 forks source link

fix str subtypes not working with omit_defaults #204

Closed mishamsk closed 4 months ago

mishamsk commented 4 months ago

So, basically this would fail at code compile time:

import enum
from dataclasses import dataclass
from mashumaro import DataClassDictMixin

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

@dataclass
class MyClass(DataClassDictMixin):
    o: str = MyStrEnum.VAL1

    class Config(BaseConfig):
        omit_default = False

because repr is used to generate a string version of a default value. I've changed it to only use repr on exact type match and added a test.

PS. not sure if using repr worth it at all in this function, but this PR at least fixes it for subtypes of some of the base types.

Fatal1ty commented 4 months ago

Oh, good catch. Thanks!

PS. not sure if using repr worth it at all in this function

It's just a small optimization — when we compare a variable with a literal value, we don't need an additional LOAD_NAME.