Skip to content

Commit 4d65ada

Browse files
committed
Hack: unbreak instantiation of pydantic models
1 parent 2c311df commit 4d65ada

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

temporalio/worker/workflow_sandbox/_restrictions.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -951,21 +951,32 @@ def _is_restrictable(v: Any) -> bool:
951951

952952

953953
class _RestrictedProxy:
954+
# When we instantiate this class, we have the signature of:
955+
# __init__(
956+
# self,
957+
# name: str,
958+
# obj: Any,
959+
# context: RestrictionContext,
960+
# matcher: SandboxMatcher
961+
# )
962+
# However there are some edge cases; see comments below.
963+
964+
def __new__(cls, *args, **kwargs) -> Any:
965+
# When pydantic instantiates a model containing a field whose type
966+
# annotation is proxied, it attempts to call the field type constructor
967+
# with the field value but instead calls the _RestrictedProxy
968+
# constructor. Return the field value.
969+
if len(args) == 1:
970+
return args[0]
971+
return super().__new__(cls)
972+
954973
def __init__(self, *args, **kwargs) -> None:
955-
# When we instantiate this class, we have the signature of:
956-
# __init__(
957-
# self,
958-
# name: str,
959-
# obj: Any,
960-
# context: RestrictionContext,
961-
# matcher: SandboxMatcher
962-
# )
963-
# However when Python subclasses a class, it calls metaclass() on the
974+
# When Python subclasses a class, it calls metaclass() on the
964975
# class object which doesn't match these args. For now, we'll just
965976
# ignore inits on these metadata classes.
966977
# TODO(cretz): Properly support subclassing restricted classes in
967978
# sandbox
968-
if isinstance(args[2], RestrictionContext):
979+
if len(args) == 4 and isinstance(args[2], RestrictionContext):
969980
_trace("__init__ on %s", args[0])
970981
_RestrictionState(
971982
name=args[0], obj=args[1], context=args[2], matcher=args[3]

0 commit comments

Comments
 (0)