Skip to content
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

Enforce constraints on arbitrary types via functional validators #5883

Merged
merged 8 commits into from
May 27, 2023

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented May 26, 2023

This makes constraints work on arbitrary types that don't "know" how to consume them by applying a functional validator that enforces the constraint in Python. Of course, if the type wants to it can "consume" the constraint via __prepare_pydantic_annotations__ to e.g. use a more efficient implementation, or if the schema is a schema that accepts the constraint directly (e.g. gt w/ a float schema) then it gets applied that way instead of via Python.

Selected Reviewer: @dmontagu

@cloudflare-pages
Copy link

cloudflare-pages bot commented May 26, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: e028826
Status: ✅  Deploy successful!
Preview URL: https://d22ee5ea.pydantic-docs2.pages.dev
Branch Preview URL: https://arbitrary-type-constraints.pydantic-docs2.pages.dev

View logs

@adriangb adriangb marked this pull request as ready for review May 26, 2023 15:29
@adriangb
Copy link
Member Author

please review

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise LGTM.

pydantic/_internal/_known_annotated_metadata.py Outdated Show resolved Hide resolved

def greater_than_validator(x: Any, gt: Any) -> Any:
if not (x > gt):
raise PydanticCustomError('greater_than', 'Input should be greater than {gt}', {'gt': gt})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
raise PydanticCustomError('greater_than', 'Input should be greater than {gt}', {'gt': gt})
raise PydanticKnownError('greater_than', {'gt': gt})

Copy link
Member Author

Choose a reason for hiding this comment

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

Won't work:

    def greater_than_validator(x: Any, gt: Any) -> Any:
        if not (x > gt):
>           raise PydanticKnownError('greater_than', {'gt': gt})
E           TypeError: GreaterThan: 'gt' context value must be a Number

Copy link
Member

Choose a reason for hiding this comment

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

needs to be fixed in core, bit this is fine for now.

Copy link
Member

Choose a reason for hiding this comment

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

I hadn't realised it was every single one that was using PydanticCustomError, we should adopt PydanticKnownError where possible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unless I’m missing something, you already pointed that out and as per above, it won’t work unless we make extensive changes in pydantic-core. It also makes no difference for users as far as I can tell.

pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
pydantic/_internal/_validators.py Outdated Show resolved Hide resolved
@adriangb adriangb force-pushed the arbitrary-type-constraints branch from c7252b4 to 27f476f Compare May 27, 2023 18:27
@adriangb adriangb enabled auto-merge (squash) May 27, 2023 18:27
@adriangb adriangb force-pushed the arbitrary-type-constraints branch from 27f476f to 4befd4f Compare May 27, 2023 19:26
@adriangb adriangb enabled auto-merge (squash) May 27, 2023 19:45
@adriangb adriangb merged commit 4985c17 into main May 27, 2023
53 checks passed
@adriangb adriangb deleted the arbitrary-type-constraints branch May 27, 2023 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants