From 8d1b23dd10d679d0a989d9888ddc57831be7e34f Mon Sep 17 00:00:00 2001 From: Zack Drach Date: Sat, 21 Jan 2023 04:16:32 +0000 Subject: [PATCH 1/3] progress --- temporalio/converter.py | 8 ++++++++ tests/test_converter.py | 3 +++ 2 files changed, 11 insertions(+) diff --git a/temporalio/converter.py b/temporalio/converter.py index 45b2c8cc1..48502c8d4 100644 --- a/temporalio/converter.py +++ b/temporalio/converter.py @@ -1277,6 +1277,14 @@ def value_to_type(hint: Type, value: Any) -> Any: ) return hint(value) + # String Subtype + if inspect.isclass(hint) and issubclass(hint, str): + if not isinstance(value, str): + raise TypeError( + f"Cannot convert to {hint}, value not a string, value is {type(value)}" + ) + return hint(value) + # UUID if inspect.isclass(hint) and issubclass(hint, uuid.UUID): return hint(value) diff --git a/tests/test_converter.py b/tests/test_converter.py index 027ec5cbe..6c2c9824c 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -363,6 +363,9 @@ def fail(hint: Type, value: Any) -> None: [SerializableStrEnum.FOO, SerializableStrEnum.FOO], ) + # String Subtype + ok(StringSubtype, StringSubtype("abc")) + # 3.10+ checks if sys.version_info >= (3, 10): ok(list[int], [1, 2]) From 52b506625105162d5eb68ca1327fcb2581c565e0 Mon Sep 17 00:00:00 2001 From: Zack Drach Date: Sat, 21 Jan 2023 04:24:25 +0000 Subject: [PATCH 2/3] progress --- tests/test_converter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_converter.py b/tests/test_converter.py index 6c2c9824c..9430e3ccf 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -61,6 +61,10 @@ class SerializableStrEnum(StrEnum): FOO = "foo" +class StringSubtype(str): + pass + + @dataclass class MyDataClass: foo: str From 0aad2efe151da240d660161b71fb1da1a000d2cd Mon Sep 17 00:00:00 2001 From: Zack Drach Date: Tue, 24 Jan 2023 02:46:18 +0000 Subject: [PATCH 3/3] add str enum old style case --- tests/test_converter.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/test_converter.py b/tests/test_converter.py index 9430e3ccf..f106ed3e0 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -64,6 +64,9 @@ class SerializableStrEnum(StrEnum): class StringSubtype(str): pass +class StringEnumOldStyle(str, Enum): + FOO = "foo" + @dataclass class MyDataClass: @@ -369,6 +372,7 @@ def fail(hint: Type, value: Any) -> None: # String Subtype ok(StringSubtype, StringSubtype("abc")) + ok(StringEnumOldStyle, StringEnumOldStyle.FOO) # 3.10+ checks if sys.version_info >= (3, 10):