-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Allow io manager keys to be str enums not just strs #18778
Allow io manager keys to be str enums not just strs #18778
Conversation
from typing import AbstractSet, Any, Dict, List, Mapping, NamedTuple, Optional, Sequence | ||
|
||
from strenum import LowercaseStrEnum |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of taking this extra dep, I think we can test the same thing by just doing [1]
strenum
doesn't add too much past that inheritance structure
https://github.com/irgeek/StrEnum/blob/master/strenum/__init__.py#L21C15-L21C29
""" | ||
Checks that (de)serializing an StrEnum from the strenum lib does not produces any error and returns the string value of the enum | ||
""" | ||
class MyStrEnum(LowercaseStrEnum): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[1]
class MyStrEnum(str, Enum):
if isinstance(val, str): # serialize StrEnums in their string value | ||
return val.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smackesey thoughts on this fallback behavior? The trade-off here is some risk in having in our code a str Enum accidentally does not get whitelisted deserializing as a normal str
for the upside of allowing users to use str Enums in apis that accept str.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry this went stale
back to you for comments and the failures in the buildkite run here https://buildkite.com/dagster/unit-tests/builds/277
Hi @ShootingStarD, thanks for writing this up with such a thorough explanation. Unfortunately I'm concerned about risk here in modifying our serialization machinery. We have a current invariant where enums have to be whitelisted to be serialized, and we don't accept user enums. It's hard to assess the likely consequences of breaking that invariant, especially given the breaking change/confusion around StrEnum in Python 3.11: python/cpython#100458 Also, IIUC, you can achieve your stated goal by calling
In any case, thanks for the submission and I understand what you were hoping for but I'm going to close this because I don't think risk/reward here exceeds the threshold. |
Summary & Motivation
More details in this issue : #18623
Previously, Dagster didn't accept to serialize enums from the StrEnum library.
I modified the _pack_value() method so that if it receives an Enum that is also of type str, we return the value of this enum.
With this change, users will be able to define keys for IOManager, Jobs, Assets etc into a single file separate in scope through the name of the enum, just like this example :
How I Tested These Changes
I tested that it was possible to serialize and de-serialize an StrEnum and that the resulting de-serialized object was the str value of the enum (which is an ok behaviour for me because I will never read back the StrEnum in my use cases).
I also rerun all test from the
test_serdes.py
to check if it was okayI also tested the test provided in the issue but I didn't added to the code because it was maybe a too big test
To make the test I had to add strenum as extra-dependency for the tests, is it ok?