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..f106ed3e0 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -61,6 +61,13 @@ class SerializableStrEnum(StrEnum): FOO = "foo" +class StringSubtype(str): + pass + +class StringEnumOldStyle(str, Enum): + FOO = "foo" + + @dataclass class MyDataClass: foo: str @@ -363,6 +370,10 @@ def fail(hint: Type, value: Any) -> None: [SerializableStrEnum.FOO, SerializableStrEnum.FOO], ) + # String Subtype + ok(StringSubtype, StringSubtype("abc")) + ok(StringEnumOldStyle, StringEnumOldStyle.FOO) + # 3.10+ checks if sys.version_info >= (3, 10): ok(list[int], [1, 2])