-
Notifications
You must be signed in to change notification settings - Fork 113
Conditionally whitelist datetime.datetime and add tests #767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import dataclasses | ||
import datetime | ||
import os | ||
import pathlib | ||
import uuid | ||
from datetime import datetime | ||
|
||
import pydantic | ||
import pytest | ||
|
@@ -9,6 +11,11 @@ | |
from temporalio.client import Client | ||
from temporalio.contrib.pydantic import pydantic_data_converter | ||
from temporalio.worker import Worker | ||
from temporalio.worker.workflow_sandbox._restrictions import ( | ||
RestrictionContext, | ||
SandboxMatcher, | ||
_RestrictedProxy, | ||
) | ||
from tests.contrib.pydantic.models import ( | ||
PydanticModels, | ||
PydanticModelWithStrictField, | ||
|
@@ -103,7 +110,7 @@ async def test_round_trip_misc_objects(client: Client): | |
{"7": 7.0}, | ||
[{"7": 7.0}], | ||
({"7": 7.0},), | ||
datetime(2025, 1, 2, 3, 4, 5), | ||
datetime.datetime(2025, 1, 2, 3, 4, 5), | ||
uuid.uuid4(), | ||
) | ||
|
||
|
@@ -262,7 +269,9 @@ async def test_datetime_usage_in_workflow(client: Client): | |
|
||
def test_pydantic_model_with_strict_field_outside_sandbox(): | ||
_test_pydantic_model_with_strict_field( | ||
PydanticModelWithStrictField(strict_field=datetime(2025, 1, 2, 3, 4, 5)) | ||
PydanticModelWithStrictField( | ||
strict_field=datetime.datetime(2025, 1, 2, 3, 4, 5) | ||
) | ||
) | ||
|
||
|
||
|
@@ -276,7 +285,9 @@ async def test_pydantic_model_with_strict_field_inside_sandbox(client: Client): | |
workflows=[PydanticModelWithStrictFieldWorkflow], | ||
task_queue=tq, | ||
): | ||
orig = PydanticModelWithStrictField(strict_field=datetime(2025, 1, 2, 3, 4, 5)) | ||
orig = PydanticModelWithStrictField( | ||
strict_field=datetime.datetime(2025, 1, 2, 3, 4, 5) | ||
) | ||
result = await client.execute_workflow( | ||
PydanticModelWithStrictFieldWorkflow.run, | ||
orig, | ||
|
@@ -324,3 +335,46 @@ async def test_validation_error(client: Client): | |
task_queue=task_queue_name, | ||
result_type=tuple[int], | ||
) | ||
|
||
|
||
class RestrictedProxyFieldsModel(BaseModel): | ||
path_field: pathlib.Path | ||
uuid_field: uuid.UUID | ||
datetime_field: datetime.datetime | ||
|
||
|
||
def test_model_instantiation_from_restricted_proxy_values(): | ||
restricted_path_cls = _RestrictedProxy( | ||
"Path", | ||
pathlib.Path, | ||
RestrictionContext(), | ||
SandboxMatcher(), | ||
) | ||
restricted_uuid_cls = _RestrictedProxy( | ||
"uuid", | ||
uuid.UUID, | ||
RestrictionContext(), | ||
SandboxMatcher(), | ||
) | ||
restricted_datetime_cls = _RestrictedProxy( | ||
"datetime", | ||
datetime.datetime, | ||
RestrictionContext(), | ||
SandboxMatcher(), | ||
) | ||
|
||
restricted_path = restricted_path_cls("test/path") | ||
restricted_uuid = restricted_uuid_cls(bytes=os.urandom(16), version=4) | ||
restricted_datetime = restricted_datetime_cls(2025, 1, 2, 3, 4, 5) | ||
|
||
assert type(restricted_path) is _RestrictedProxy | ||
assert type(restricted_uuid) is _RestrictedProxy | ||
assert type(restricted_datetime) is not _RestrictedProxy | ||
p = RestrictedProxyFieldsModel( | ||
path_field=restricted_path, # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The restricted
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be due to the fact that UUID does also: https://github.com/pydantic/pydantic-core/blob/741961c05847d9e9ee517cd783e24c2b58e5596b/src/validators/uuid.rs#L95-L154 but |
||
uuid_field=restricted_uuid, # type: ignore | ||
datetime_field=restricted_datetime, # type: ignore | ||
) | ||
assert p.path_field == restricted_path | ||
assert p.uuid_field == restricted_uuid | ||
assert p.datetime_field == restricted_datetime |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So
datetime.datetime
will be whitelisted for Pydantic v2 users, but not for v1 users. However, the__get_pydantic_core_schema__
impl doesn't do anything forv1
users, so they have to disable sandboxing ofdatetime.datetime
entirely (e.g. per the sample), so_is_restrictable
is irrelevant for them.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. Though I know we don't prematurely optimize, I wonder if there are performance concerns for making list, appending to list, and turning to tuple every time this call is made if you also happen to have pydantic v2 in your venv. We always made the tuple before, so maybe it doesn't matter. Could make this tuple global if you wanted, but I'm ok with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, I agree. Performance or not that code looked a bit silly. d915d5b