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

multipleOf and number translations yield unstable Pydantic models. #2285

Open
tpanum opened this issue Jan 28, 2025 · 0 comments
Open

multipleOf and number translations yield unstable Pydantic models. #2285

tpanum opened this issue Jan 28, 2025 · 0 comments

Comments

@tpanum
Copy link

tpanum commented Jan 28, 2025

Describe the bug
Pydantic has an underlying bug when using multiple_of with float, and is covered here. To my knowledge, Decimal is more stable in this regard.

To Reproduce
Here is a small program to showcase the instability of float vs. Decimal.

from decimal import Decimal

import pydantic
from pydantic import BaseModel, Field


class A(BaseModel):
    a: float = Field(multiple_of=0.1)


class B(BaseModel):
    a: Decimal = Field(multiple_of=0.1)


def test_class(cls):
    error_value = None
    for i in range(100):
        v = (i - 50) / 10
        try:
            cls(a=v)
        except pydantic.ValidationError:
            error_value = v
            break

    if error_value:
        print(f"Got validation error with value: {error_value}")
    else:
        print("No validation errors")


def main():
    print("Testing float with `multiple_of``")
    test_class(A)

    print("Testing Decimal with `multiple_of`")
    test_class(B)


if __name__ == "__main__":
    main()

Example schema:

...
    Energy: {type: number, format: realenergy, multipleOf: 0.1, minimum: -9.99999999E7,
      maximum: 9.99999999E7, example: 1200.7}
...

Will map to:

class Energy(RootModel[confloat(ge=-99999999.9, le=99999999.9, multiple_of=0.1)]):
    root: confloat(ge=-99999999.9, le=99999999.9, multiple_of=0.1) = Field(
        ...,
        examples=[1200.7],
    )

I think it would be more sane to map to Decimal as soon as multipleOf is a requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant