Skip to content

Conversation

@atyshka
Copy link

@atyshka atyshka commented Aug 20, 2025

This ensures primitive-inheriting types are cast to primitives, resolving issue #48

@dlwh
Copy link
Member

dlwh commented Aug 20, 2025

any chance i could get you add a unit test actually?

@atyshka
Copy link
Author

atyshka commented Aug 21, 2025

I'm glad you asked for that, I caught a bug with enums. The encoded string gets the class prefix, which didn't work for the existing decoding logic. I implemented a fix for this in the decoding. I think it would be cleaner if we could encode it without the prefix, that is, using the enum encoder rather than the string encoder, but I'm unsure how that should be implemented.

Although it's worth noting the messiness of this could be solved by StrEnum in python 3.11+, but draccus needs some solution for 3.9/3.10

@atyshka
Copy link
Author

atyshka commented Aug 21, 2025

I guess we could just do something like this:

def encode_string(x, _=None):
    if isinstance(x, Enum):
        return encode_enum(x)
    else:
        return str(x)
encode.register(t, encode_string, include_subclasses=True)

Let me know which solution you prefer or if you have alternative suggestions

@dlwh
Copy link
Member

dlwh commented Aug 21, 2025

enums are special enough to deserve a special case, but we could lift it up to where e.g. is_dataclass is.

it's also probably time to drop 3.9 (which is a few weeks away from EOL). 3.10 i'm not committed to supporting really but probably not quite time to drop it.

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