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

multiple_of failing on some negative floats #9871

Open
1 task done
Tracked by #10189
plutaniano opened this issue Jul 8, 2024 · 5 comments · May be fixed by pydantic/pydantic-core#1373
Open
1 task done
Tracked by #10189

multiple_of failing on some negative floats #9871

plutaniano opened this issue Jul 8, 2024 · 5 comments · May be fixed by pydantic/pydantic-core#1373
Assignees
Labels
bug V2 Bug related to Pydantic V2 good first issue help wanted Pull Request welcome

Comments

@plutaniano
Copy link

plutaniano commented Jul 8, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Why does it work on x but fails on -x? Is this intended behavior?

Example Code

from typing import Annotated
from pydantic import BaseModel, TypeAdapter, Field

for i in range(-10, 10):
	i = i / 100
	try:
		TypeAdapter(Annotated[float, Field(multiple_of=0.01)]).validate_python(i)
		print(f"  OK: {i}")
	except:
		print(f"FAIL: {i}")

prints:

  OK: -0.1
FAIL: -0.09
  OK: -0.08
  OK: -0.07
FAIL: -0.06
  OK: -0.05
  OK: -0.04
FAIL: -0.03
  OK: -0.02
  OK: -0.01
  OK: 0.0
  OK: 0.01
  OK: 0.02
  OK: 0.03
  OK: 0.04
  OK: 0.05
  OK: 0.06
  OK: 0.07
  OK: 0.08
  OK: 0.09

Python, Pydantic & OS Version

pydantic version: 2.7.2
        pydantic-core version: 2.18.3
          pydantic-core build: profile=release pgo=true
                 install path: /Users/lucas/{redacted}/venv/lib/python3.12/site-packages/pydantic
               python version: 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
                     platform: macOS-14.5-arm64-arm-64bit
             related packages: typing_extensions-4.12.0 fastapi-0.111.0 pydantic-settings-2.2.1
                       commit: unknown
@plutaniano plutaniano added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Jul 8, 2024
@sydney-runkle sydney-runkle added good first issue help wanted Pull Request welcome and removed pending Awaiting a response / confirmation labels Jul 17, 2024
@sydney-runkle
Copy link
Member

@plutaniano,

Huh, this is definitely a bug. Thanks for reporting this! Likely an issue due to undesired rounding. A PR with a fix is more than welcome!

@rinzio
Copy link

rinzio commented Jul 21, 2024

Hi, I've been investigating this today, seems that using decimal.Decimal instead of float makes the trick.

from typing import Annotated
from pydantic import TypeAdapter, Field
from decimal import Decimal

for i in range(-10, 10):
    x = i / 100
    try:
        TypeAdapter(Annotated[Decimal, Field(multiple_of=0.01)]).validate_python(x)
        print(f"  OK: {x}")
    except:
        print(f"FAIL: {x}")

Results in:

  OK: -0.1
  OK: -0.09
  OK: -0.08
  OK: -0.07
  OK: -0.06
  OK: -0.05
  OK: -0.04
  OK: -0.03
  OK: -0.02
  OK: -0.01
  OK: 0.0
  OK: 0.01
  OK: 0.02
  OK: 0.03
  OK: 0.04
  OK: 0.05
  OK: 0.06
  OK: 0.07
  OK: 0.08
  OK: 0.09

This should be fixed at pydantic-core though.

@K-dash
Copy link
Contributor

K-dash commented Jul 22, 2024

@sydney-runkle
Hi!
It looks like the validation in pydantic-core needs some modification. If this issue is still unresolved, could you please assign it to me? I have a good idea of where the changes are needed, so I believe I can submit a PR relatively quickly, including additional test cases.

@AlessandroMiola
Copy link
Contributor

Hi @K-dash,
I was looking at the same issue, but you made it quicker:).

Just wanted to point out that IMO - to maintain coherence with the existing setting - you might want to add the same test parametrizations of pydantic-core/tests/validators/test_float.py::test_float_multiple_of to pydantic-core/tests/validators/test_decimal.py::test_decimal_multiple_of and make sure tests pass.

@K-dash
Copy link
Contributor

K-dash commented Jul 22, 2024

Hi @AlessandroMiola
Thank you for your suggestion:).
I agree with your point, so I have added the same test cases to test_decimal_multiple_of as well.

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 good first issue help wanted Pull Request welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants