-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Coerce decimal constraints to Decimal instances
#11350
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
Conversation
Since core schema validation was disabled, constraints such as `gt` are not coerced to a `Decimal` instance if provided as e.g. an `int`.
Deploying pydantic-docs with
|
| Latest commit: |
54f8b7d
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://f5b318da.pydantic-docs.pages.dev |
| Branch Preview URL: | https://decimal-constraints.pydantic-docs.pages.dev |
CodSpeed Performance ReportMerging #11350 will not alter performanceComparing Summary
|
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
| if schema_type == 'decimal' and constraint in {'multiple_of', 'le', 'ge', 'lt', 'gt'}: | ||
| schema[constraint] = Decimal(value) | ||
| else: | ||
| schema[constraint] = value |
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.
Is it better to put this in pydantic-core rather than coupling this to what pydantic-core expects?
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.
This is a good idea
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.
I thought about this, only concern is that as is, the core schema will not comply to the corresponding typed dict definition (e.g. with f: Decimal = Field(gt=1), the core_schema is {'type': 'decimal', 'gt': 1}).
Unless we change the constraints type on the typed dict to be Decimal | float, and then do the coercion during the validator build in pydantic-core?
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.
It seems ok to me to make the core schema definition more lax in this way? 🤔
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.
Hum but then how easy will it be to do so e.g. for date constraints? As ideally we'd want to do Pydantic coercion on it. Is it possible to perform such validation/coercion during pydantic-core schema build?
I know the current change looks ugly, but this is inherent to the current implementation of the known metadata application that has several flaws.
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.
How about proceeding as is, can you open an issue in core for these cases and I'll propose it there another time?
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.
|
I'm fine with merging as is 👍 |
Since core schema validation was disabled, constraints such as
gtare not coerced to aDecimalinstance if provided as e.g. anint.Partially addresses #11341.
Change Summary
Related issue number
Checklist