11import dataclasses
2+ import datetime
3+ import os
4+ import pathlib
25import uuid
3- from datetime import datetime
46
57import pydantic
68import pytest
911from temporalio .client import Client
1012from temporalio .contrib .pydantic import pydantic_data_converter
1113from temporalio .worker import Worker
14+ from temporalio .worker .workflow_sandbox ._restrictions import (
15+ RestrictionContext ,
16+ SandboxMatcher ,
17+ _RestrictedProxy ,
18+ )
1219from tests .contrib .pydantic .models import (
1320 PydanticModels ,
1421 PydanticModelWithStrictField ,
@@ -103,7 +110,7 @@ async def test_round_trip_misc_objects(client: Client):
103110 {"7" : 7.0 },
104111 [{"7" : 7.0 }],
105112 ({"7" : 7.0 },),
106- datetime (2025 , 1 , 2 , 3 , 4 , 5 ),
113+ datetime . datetime (2025 , 1 , 2 , 3 , 4 , 5 ),
107114 uuid .uuid4 (),
108115 )
109116
@@ -262,7 +269,9 @@ async def test_datetime_usage_in_workflow(client: Client):
262269
263270def test_pydantic_model_with_strict_field_outside_sandbox ():
264271 _test_pydantic_model_with_strict_field (
265- PydanticModelWithStrictField (strict_field = datetime (2025 , 1 , 2 , 3 , 4 , 5 ))
272+ PydanticModelWithStrictField (
273+ strict_field = datetime .datetime (2025 , 1 , 2 , 3 , 4 , 5 )
274+ )
266275 )
267276
268277
@@ -276,7 +285,9 @@ async def test_pydantic_model_with_strict_field_inside_sandbox(client: Client):
276285 workflows = [PydanticModelWithStrictFieldWorkflow ],
277286 task_queue = tq ,
278287 ):
279- orig = PydanticModelWithStrictField (strict_field = datetime (2025 , 1 , 2 , 3 , 4 , 5 ))
288+ orig = PydanticModelWithStrictField (
289+ strict_field = datetime .datetime (2025 , 1 , 2 , 3 , 4 , 5 )
290+ )
280291 result = await client .execute_workflow (
281292 PydanticModelWithStrictFieldWorkflow .run ,
282293 orig ,
@@ -324,3 +335,46 @@ async def test_validation_error(client: Client):
324335 task_queue = task_queue_name ,
325336 result_type = tuple [int ],
326337 )
338+
339+
340+ class RestrictedProxyFieldsModel (BaseModel ):
341+ path_field : pathlib .Path
342+ uuid_field : uuid .UUID
343+ datetime_field : datetime .datetime
344+
345+
346+ def test_model_instantiation_from_restricted_proxy_values ():
347+ restricted_path_cls = _RestrictedProxy (
348+ "Path" ,
349+ pathlib .Path ,
350+ RestrictionContext (),
351+ SandboxMatcher (),
352+ )
353+ restricted_uuid_cls = _RestrictedProxy (
354+ "uuid" ,
355+ uuid .UUID ,
356+ RestrictionContext (),
357+ SandboxMatcher (),
358+ )
359+ restricted_datetime_cls = _RestrictedProxy (
360+ "datetime" ,
361+ datetime .datetime ,
362+ RestrictionContext (),
363+ SandboxMatcher (),
364+ )
365+
366+ restricted_path = restricted_path_cls ("test/path" )
367+ restricted_uuid = restricted_uuid_cls (bytes = os .urandom (16 ), version = 4 )
368+ restricted_datetime = restricted_datetime_cls (2025 , 1 , 2 , 3 , 4 , 5 )
369+
370+ assert type (restricted_path ) is _RestrictedProxy
371+ assert type (restricted_uuid ) is _RestrictedProxy
372+ assert type (restricted_datetime ) is not _RestrictedProxy
373+ p = RestrictedProxyFieldsModel (
374+ path_field = restricted_path , # type: ignore
375+ uuid_field = restricted_uuid , # type: ignore
376+ datetime_field = restricted_datetime , # type: ignore
377+ )
378+ assert p .path_field == restricted_path
379+ assert p .uuid_field == restricted_uuid
380+ assert p .datetime_field == restricted_datetime
0 commit comments