Skip to content

Add StrEnum conversion support #177

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

Merged
merged 4 commits into from
Nov 3, 2022
Merged

Add StrEnum conversion support #177

merged 4 commits into from
Nov 3, 2022

Conversation

KMilhan
Copy link
Contributor

@KMilhan KMilhan commented Nov 1, 2022

What was changed

Add support for string-based Enum

Why?

String-based enum is apparently widely used, along with int-based enums. Python-sdk already has a support int-based enum, and it makes sense also to support another most popular enum, string.

Checklist

  1. Closes [Feature Request] Support string-based enum #176

  2. How was this tested:
    Tested with updated unit test

  3. Any docs updates needed?
    Readme was slightly updated

@CLAassistant
Copy link

CLAassistant commented Nov 1, 2022

CLA assistant check
All committers have signed the CLA.

@KMilhan KMilhan changed the title Add StrEnum support Add StrEnum conversion support Nov 1, 2022
Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

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

Looks great! Minor exception string change and that's it.

@@ -874,6 +874,14 @@ def value_to_type(hint: Type, value: Any) -> Any:
)
return hint(value)

# StrEnum
if inspect.isclass(hint) and issubclass(hint, StrEnum):
Copy link
Contributor Author

@KMilhan KMilhan Nov 1, 2022

Choose a reason for hiding this comment

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

@cretz,
Oops, I forgot StrEnum is not in the older Python version.
We can change it to

if inspect.isclass(hint) and issubclass(hint, str) and isinstance(hint, Enum):

ref)
image
image

Copy link
Member

Choose a reason for hiding this comment

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

It may be better if we did a:

if sys.version_info >= (3, 11):
  from enum import StrEnum

And put that same conditional around this conversion code and around the test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup that would work too. Let me update my commit

Copy link
Contributor Author

@KMilhan KMilhan Nov 2, 2022

Choose a reason for hiding this comment

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

Added another commit accordingly. Please review the new commit 🙏

@@ -46,6 +46,10 @@ class SerializableEnum(IntEnum):
FOO = 1


class SerializableStrEnum(StrEnum):
Copy link
Member

@cretz cretz Nov 2, 2022

Choose a reason for hiding this comment

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

We run tests all the way back to Python 3.7, so you'll need to make this class (and its required import and its check in the test) conditional on 3.11 too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the test and confirmed both the 3.7 and 3.11 Python versions passes the test on my local machine. I should've checked it in the first place ^^; Please check the change 🙏

Copy link
Member

@cretz cretz left a comment

Choose a reason for hiding this comment

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

Looks great, will merge after CI passes

@cretz cretz merged commit cfdc548 into temporalio:main Nov 3, 2022
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.

[Feature Request] Support string-based enum
3 participants