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

enums not reflect in object response #944

Open
trondhindenes opened this issue Mar 11, 2024 · 2 comments
Open

enums not reflect in object response #944

trondhindenes opened this issue Mar 11, 2024 · 2 comments

Comments

@trondhindenes
Copy link

Given this model:

class ReindexStatus(Enum):
    ready_to_start = 'ready_to_start'
    started = 'started'

class Test(Table):
  status = Varchar(choices=ReindexStatus, null=False)

if I do a query like

thing = await Test.objects().first()

I would expect to be able to use the enum class to compare the object field value like:

if thing.status == ReindexStatus.ready_to_start:

However, the field seems to be returned just as a string.

This seems like a bug to me, is it intentional that enum-based fields aren't deserialized back to the enum?

@dantownsend
Copy link
Member

I agree that converting the values back into enums would be a nice feature. I wonder if it should be a default behaviour, or configurable in some way (maybe via the output clause).

To make the enum comparable to the raw string value you can do this:

# Note - it inherits from `str`
class ReindexStatus(str, Enum):
    ready_to_start = 'ready_to_start'
    started = 'started'

@trondhindenes
Copy link
Author

I would argue that it should be the default value, since the object() call is an explicit desire to get the returned object back in a "fully deserialized" form, and Enum casting is part of that. Not sure if it would break for existing users tho.

Thanks for the trick, I'll play with that!

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

No branches or pull requests

2 participants