Description
Initial Checks
- I confirm that I'm using Pydantic V2
Description
Hi. I have a model named "Product" that has a field "price" of type Decimal with max_digits and decimal_places set to 34 and 18, respectively. when I try to pass the value "9999999999999999.999999999999999999" to it, I encounter the following error:
File "/home/ali/p1/venv/lib/python3.11/site-packages/pydantic/main.py", line 165, in __init__
__pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Product
price
Decimal input should have no more than 16 digits before the decimal point [type=decimal_whole_digits, input_value=Decimal('9999999999999999.999999999999999999'), input_type=Decimal]
For further information visit https://errors.pydantic.dev/2.3/v/decimal_whole_digits
I guess it has something to do with rounding strategy cause it only occurs for this specific number. When I change any of the NINEs (before the decimal point I mean), the error goes away. For example if I pass "9999999999999998.999999999999999999" as price, it works.
Example Code
from pydantic import BaseModel, ConfigDict, Field
from typing import Annotated
from decimal import Decimal
class Product(BaseModel):
model_config = ConfigDict(extra="ignore", validate_default=True, from_attributes=True, frozen=True)
name: str
price: Annotated[Decimal, Field(max_digits=34, decimal_places=18, ge=0)]
p1 = Product(name='p1', price=Decimal('9_999_999_999_999_999.999_999_999_999_999_999')) # doesn't work
# p1 = Product(name='p1', price=Decimal('9_999_999_999_999_998.999_999_999_999_999_999')) # works!
Python, Pydantic & OS Version
pydantic version: 2.3.0
pydantic-core version: 2.6.3
pydantic-core build: profile=release pgo=true
install path: /home/ali/p1/venv/lib/python3.11/site-packages/pydantic
python version: 3.11.5 (main, Aug 25 2023, 13:19:50) [GCC 11.4.0]
platform: Linux-6.2.0-34-generic-x86_64-with-glibc2.35
optional deps. installed: ['email-validator', 'typing-extensions']