|
| 1 | +import dataclasses |
| 2 | +import uuid |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from ipaddress import IPv4Address |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from pydantic import BaseModel |
| 8 | + |
| 9 | +from temporalio import activity, workflow |
| 10 | +from temporalio.client import Client |
| 11 | +from temporalio.contrib.pydantic.converter import pydantic_data_converter |
| 12 | +from temporalio.worker import Worker |
| 13 | +from temporalio.worker.workflow_sandbox import ( |
| 14 | + SandboxedWorkflowRunner, |
| 15 | + SandboxRestrictions, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class MyPydanticModel(BaseModel): |
| 20 | + some_ip: IPv4Address |
| 21 | + some_date: datetime |
| 22 | + |
| 23 | + |
| 24 | +@activity.defn |
| 25 | +async def my_activity(models: List[MyPydanticModel]) -> List[MyPydanticModel]: |
| 26 | + activity.logger.info("Got models in activity: %s" % models) |
| 27 | + return models |
| 28 | + |
| 29 | + |
| 30 | +@workflow.defn |
| 31 | +class MyWorkflow: |
| 32 | + @workflow.run |
| 33 | + async def run(self, models: List[MyPydanticModel]) -> List[MyPydanticModel]: |
| 34 | + workflow.logger.info("Got models in workflow: %s" % models) |
| 35 | + return await workflow.execute_activity( |
| 36 | + my_activity, models, start_to_close_timeout=timedelta(minutes=1) |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +# Due to known issues with Pydantic's use of issubclass and our inability to |
| 41 | +# override the check in sandbox, Pydantic will think datetime is actually date |
| 42 | +# in the sandbox. At the expense of protecting against datetime.now() use in |
| 43 | +# workflows, we're going to remove datetime module restrictions. See sdk-python |
| 44 | +# README's discussion of known sandbox issues for more details. |
| 45 | +def new_sandbox_runner() -> SandboxedWorkflowRunner: |
| 46 | + # TODO(cretz): Use with_child_unrestricted when https://github.com/temporalio/sdk-python/issues/254 |
| 47 | + # is fixed and released |
| 48 | + invalid_module_member_children = dict( |
| 49 | + SandboxRestrictions.invalid_module_members_default.children |
| 50 | + ) |
| 51 | + del invalid_module_member_children["datetime"] |
| 52 | + return SandboxedWorkflowRunner( |
| 53 | + restrictions=dataclasses.replace( |
| 54 | + SandboxRestrictions.default, |
| 55 | + invalid_module_members=dataclasses.replace( |
| 56 | + SandboxRestrictions.invalid_module_members_default, |
| 57 | + children=invalid_module_member_children, |
| 58 | + ), |
| 59 | + ) |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +async def test_workflow_with_pydantic_model(client: Client): |
| 64 | + # Replace data converter in client |
| 65 | + new_config = client.config() |
| 66 | + new_config["data_converter"] = pydantic_data_converter |
| 67 | + client = Client(**new_config) |
| 68 | + task_queue_name = str(uuid.uuid4()) |
| 69 | + |
| 70 | + orig_models = [ |
| 71 | + MyPydanticModel( |
| 72 | + some_ip=IPv4Address("127.0.0.1"), |
| 73 | + some_date=datetime(2000, 1, 2, 3, 4, 5), |
| 74 | + ), |
| 75 | + MyPydanticModel( |
| 76 | + some_ip=IPv4Address("127.0.0.2"), |
| 77 | + some_date=datetime(2001, 2, 3, 4, 5, 6), |
| 78 | + ), |
| 79 | + ] |
| 80 | + |
| 81 | + async with Worker( |
| 82 | + client, |
| 83 | + task_queue=task_queue_name, |
| 84 | + workflows=[MyWorkflow], |
| 85 | + activities=[my_activity], |
| 86 | + workflow_runner=new_sandbox_runner(), |
| 87 | + ): |
| 88 | + result = await client.execute_workflow( |
| 89 | + MyWorkflow.run, |
| 90 | + orig_models, |
| 91 | + id=str(uuid.uuid4()), |
| 92 | + task_queue=task_queue_name, |
| 93 | + ) |
| 94 | + assert orig_models == result |
0 commit comments