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

Decimal Validation Error with max_digits and Specific Input #7862

Closed
1 task done
AliHosseinzadeh2000 opened this issue Oct 18, 2023 · 1 comment · Fixed by pydantic/pydantic-core#1049
Closed
1 task done
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@AliHosseinzadeh2000
Copy link

AliHosseinzadeh2000 commented Oct 18, 2023

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']
@AliHosseinzadeh2000 AliHosseinzadeh2000 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Oct 18, 2023
@sydney-runkle
Copy link
Member

Hi @AliHosseinzadeh2000,

Thanks for reporting this. I'm guessing that #7305 is related. @adriangb, what are your thoughts? I know you worked on that previous decimal parsing fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
2 participants