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

Allow io manager keys to be str enums not just strs #18778

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python_modules/dagster/dagster/_serdes/serdes.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,13 @@ def _pack_value(
if isinstance(val, Enum):
klass_name = val.__class__.__name__
if not whitelist_map.has_enum_entry(klass_name):
if isinstance(val, str): # serialize StrEnums in their string value
return val.value
Comment on lines +832 to +833
Copy link
Member

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.

raise SerializationError(
"Can only serialize whitelisted Enums, received"
f" {klass_name}.\nDescent path: {descent_path}",
)

enum_serializer = whitelist_map.get_enum_entry(klass_name)
return {"__enum__": enum_serializer.pack(val, whitelist_map, descent_path)}
if (
Expand Down
17 changes: 15 additions & 2 deletions python_modules/dagster/dagster_tests/general_tests/test_serdes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import re
import string
from collections import namedtuple
from enum import Enum
from enum import Enum, auto
from typing import AbstractSet, Any, Dict, List, Mapping, NamedTuple, Optional, Sequence

from strenum import LowercaseStrEnum
Copy link
Member

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

import pydantic
import pytest
from dagster._check import ParameterCheckError, inst_param, set_param
Expand Down Expand Up @@ -180,6 +180,19 @@ class Corge(Enum):
assert deserialized == Corge.FOO_FOO



def test_str_enum_serialization_deserialisation():
"""
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):
Copy link
Member

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):

ONE_STR_ENUM = auto()

enum_val = MyStrEnum.ONE_STR_ENUM
serialized_value = serialize_value(val=enum_val)
assert serialized_value == f'"{enum_val.value}"'
assert deserialize_value(serialized_value) == enum_val.value

def test_backward_compat_serdes():
test_map = WhitelistMap.create()

Expand Down
1 change: 1 addition & 0 deletions python_modules/dagster/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def get_version() -> str:
"syrupy<4", # 3.7 compatible,
"tox==3.25.0",
"morefs[asynclocal]; python_version>='3.8'",
"strenum",
],
"mypy": [
"mypy==0.991",
Expand Down