Coerce decimal constraints to Decimal instances#11350
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.
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.
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.
It seems ok to me to make the core schema definition more lax in this way? 🤔
There was a problem hiding this comment.
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.
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.
|
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