Skip to content

Commit fa2c8b5

Browse files
committed
Only use pydantic in sandbox if it can be imported
1 parent d3d9ed3 commit fa2c8b5

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

temporalio/worker/workflow_sandbox/_restrictions.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,13 @@
3232
cast,
3333
)
3434

35-
from pydantic import GetCoreSchemaHandler
36-
from pydantic_core import CoreSchema, core_schema
35+
try:
36+
import pydantic
37+
import pydantic_core
38+
39+
HAVE_PYDANTIC = True
40+
except ImportError:
41+
HAVE_PYDANTIC = False
3742

3843
import temporalio.workflow
3944

@@ -986,7 +991,7 @@ def __init__(self, *args, **kwargs) -> None:
986991
_trace("__init__ unrecognized with args %s", args)
987992

988993
def __getattribute__(self, __name: str) -> Any:
989-
if __name == "__get_pydantic_core_schema__":
994+
if HAVE_PYDANTIC and __name == "__get_pydantic_core_schema__":
990995
return object.__getattribute__(self, "__get_pydantic_core_schema__")
991996
state = _RestrictionState.from_proxy(self)
992997
_trace("__getattribute__ %s on %s", __name, state.name)
@@ -1037,14 +1042,18 @@ def __getitem__(self, key: Any) -> Any:
10371042
)
10381043
return ret
10391044

1040-
# Instruct pydantic to use the proxied type when determining the schema
1041-
@classmethod
1042-
def __get_pydantic_core_schema__(
1043-
cls, source_type: Any, handler: GetCoreSchemaHandler
1044-
) -> CoreSchema:
1045-
return core_schema.no_info_after_validator_function(
1046-
cls, handler(RestrictionContext.unwrap_if_proxied(source_type))
1047-
)
1045+
if HAVE_PYDANTIC:
1046+
# Instruct pydantic to use the proxied type when determining the schema
1047+
# https://docs.pydantic.dev/latest/concepts/types/#customizing-validation-with-__get_pydantic_core_schema__
1048+
@classmethod
1049+
def __get_pydantic_core_schema__(
1050+
cls,
1051+
source_type: Any,
1052+
handler: pydantic.GetCoreSchemaHandler,
1053+
) -> pydantic_core.CoreSchema:
1054+
return pydantic_core.core_schema.no_info_after_validator_function(
1055+
cls, handler(RestrictionContext.unwrap_if_proxied(source_type))
1056+
)
10481057

10491058
__doc__ = _RestrictedProxyLookup( # type: ignore
10501059
class_value=__doc__, fallback_func=lambda self: type(self).__doc__, is_attr=True

0 commit comments

Comments
 (0)