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

Exception parsing negative integer strings #9396

Closed
1 task done
noamkush opened this issue May 6, 2024 · 3 comments · Fixed by pydantic/pydantic-core#1291
Closed
1 task done

Exception parsing negative integer strings #9396

noamkush opened this issue May 6, 2024 · 3 comments · Fixed by pydantic/pydantic-core#1291
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@noamkush
Copy link

noamkush commented May 6, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Starting with pydantic 2.7.1 parsing an integer string like '-1.0' gives an error while '1.0' works fine. Probably related to pydantic/pydantic-core#1269 or pydantic/pydantic-core#1272.

Example Code

In [1]: import pydantic

In [2]: class A(pydantic.BaseModel):
   ...:     i: int
   ...: 

In [3]: A(i='4.0')
Out[3]: A(i=4)

In [4]: A(i='-4.0')
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[4], line 1
----> 1 A(i='-4.0')

File ~/.cache/pypoetry/virtualenvs/hl-7m4wOqun-py3.12/lib/python3.12/site-packages/pydantic/main.py:176, in BaseModel.__init__(self, **data)
    174 # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
    175 __tracebackhide__ = True
--> 176 self.__pydantic_validator__.validate_python(data, self_instance=self)

ValidationError: 1 validation error for A
i
  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='-4.0', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/int_parsing

Python, Pydantic & OS Version

             pydantic version: 2.7.1
        pydantic-core version: 2.18.2
          pydantic-core build: profile=release pgo=true
                 install path: /home/noam/.cache/pypoetry/virtualenvs/hl-7m4wOqun-py3.12/lib/python3.12/site-packages/pydantic
               python version: 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.39
             related packages: typing_extensions-4.10.0 mypy-1.7.1
                       commit: unknown
@noamkush noamkush added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels May 6, 2024
@RajatRajdeep
Copy link

I investigated this and the issue seems to be in strip_leading_zeros function where if any character occurs except (+, _, 1 ... 9) in the string then None is being returned. The - character is not handled here and due to this it's throwing validation error.

https://github.com/pydantic/pydantic-core/blob/main/src/input/shared.rs#L149-L168

@sydney-runkle
Copy link
Member

Hey folks,

Thanks for reporting this bug. Definitely something we want to fix in 2.7.2. Should be an easy fix if anyone is interested in contributing to pydantic-core. I'll pick this up soon if not!

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label May 14, 2024
@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone May 14, 2024
@RajatRajdeep
Copy link

I'll pick it up.

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
Development

Successfully merging a pull request may close this issue.

3 participants