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

Conversation

mishamsk
Copy link
Contributor

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
Copy link
Owner

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.

@Fatal1ty Fatal1ty merged commit 4eefc4f into Fatal1ty:master Mar 31, 2024
27 checks passed
@mishamsk mishamsk deleted the fix-omit-default-enum branch July 3, 2024 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants