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

Feature - Allow serialize Enum object #2457

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

trgiangdo
Copy link
Member

What type of PR is this? (check all applicable)

  • Refactor
  • Feature

Description

This PR allows the _BaseSerializer to serialize and deserialize an Enum object.

@trgiangdo trgiangdo self-assigned this Feb 22, 2025
@trgiangdo trgiangdo changed the title feature: allow serialize Enum object Feature - Allow serialize Enum object Feb 22, 2025
Copy link
Member Author

@trgiangdo trgiangdo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for reviewer

Comment on lines +170 to +171
if len(val) == 1 and list(val.keys())[0] in cls._registered_types.keys():
return cls._registered_types[list(val.keys())[0]]._pythonify(list(val.values())[0])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if command handles the case where a dataclass instance is serialized into a dictionary.

For example, UserAttribute("email", "credential_mail", default="[email protected]") will be serialized to

{ "USER_ATTRIBUTE": {"name": "email", "credentials_name": "credentials_mail", "default": "[email protected]"}

The key is used to identify the type of the serialized object.

@toan-quach
Copy link
Member

LGTM 😃

@jrobinAV jrobinAV added the ⚙️Configuration Related to Taipy Configuration label Feb 24, 2025
Comment on lines +162 to +163
elif dynamic_type == "enum":
return _TemplateHandler._to_enum(actual_val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure I understand: This case does not cover internal enums (SCOPE and FREQUENCY), as they are caught up by:

if dynamic_type in cls._registered_types.keys():
    return cls._registered_types[dynamic_type]._pythonify(actual_val)

which transforms the string SCENARIO:SCOPE into a Scope instance with the SCENARIO value.

Your case transforms the string Gender.male:enum into the Gender enum with the male value.

Am I correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, all registered types are handled first, then the enum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⚙️Configuration Related to Taipy Configuration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants