Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Feb 15, 2025

Conversation

dandavison
Copy link
Contributor

No description provided.


p = RestrictedProxyFieldsModel(
datetime_field=restricted_datetime, # type: ignore
path_field=restricted_path, # type: ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restricted pathlib.Path instance is accepted by pydantic but the restricted datetime.datetime is not:

E       pydantic_core._pydantic_core.ValidationError: 1 validation error for RestrictedProxyFieldsModel
E       datetime_field
E         Input should be a valid datetime [type=datetime_type, input_value=datetime.datetime(2025, 1, 2, 3, 4, 5), input_type=_RestrictedProxy]
E           For further information visit https://errors.pydantic.dev/2.10/v/datetime_type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be due to the fact that datetime has a custom validation method in Pydantic's Rust layer: https://github.com/pydantic/pydantic-core/blob/741961c05847d9e9ee517cd783e24c2b58e5596b/src/input/input_python.rs#L548-L582
and _RestrictedProxy does not pass the self.downcast::<PyDateTime> test which leads to the validation failure.

UUID does also: https://github.com/pydantic/pydantic-core/blob/741961c05847d9e9ee517cd783e24c2b58e5596b/src/validators/uuid.rs#L95-L154 but _RestrictedProxy does not cause it to fail. And pathlib.Path does not seem to have a dedicated validator in pydantic_core.

@dandavison dandavison force-pushed the pydantic-restricted-instances branch from a2eb8a9 to a8a5018 Compare February 14, 2025 20:19
@dandavison dandavison changed the title Why is a restricted pathlib.Path accepted but not a restricted datetime? Conditionally whitelist datetime.datetime and add tests Feb 14, 2025
@dandavison dandavison marked this pull request as ready for review February 14, 2025 20:20
@dandavison dandavison requested a review from a team as a code owner February 14, 2025 20:20
@dandavison dandavison force-pushed the pydantic-restricted-instances branch from a8a5018 to 29b79d1 Compare February 14, 2025 20:20
@dandavison dandavison force-pushed the pydantic-restricted-instances branch from 29b79d1 to fe6edf2 Compare February 14, 2025 20:21
# and uuid.UUID which are likely to be used in Pydantic model fields
# currently pass Pydantic's validation when wrapped by RestrictedProxy.
do_not_restrict.append(datetime.date) # e.g. datetime.datetime
return v is not None and not isinstance(v, tuple(do_not_restrict))
Copy link
Contributor Author

@dandavison dandavison Feb 14, 2025

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 for v1 users, so they have to disable sandboxing of datetime.datetime entirely (e.g. per the sample), so _is_restrictable is irrelevant for them.

Copy link
Member

@cretz cretz Feb 14, 2025

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.

Copy link
Contributor Author

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

# and uuid.UUID which are likely to be used in Pydantic model fields
# currently pass Pydantic's validation when wrapped by RestrictedProxy.
do_not_restrict.append(datetime.date) # e.g. datetime.datetime
return v is not None and not isinstance(v, tuple(do_not_restrict))
Copy link
Member

@cretz cretz Feb 14, 2025

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.

@dandavison dandavison force-pushed the pydantic-restricted-instances branch from d915d5b to be66306 Compare February 14, 2025 22:09
@dandavison dandavison merged commit 8333900 into main Feb 15, 2025
14 checks passed
@dandavison dandavison deleted the pydantic-restricted-instances branch February 15, 2025 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants