pydantic / bump-pydantic

Convert Pydantic from V1 to V2 ♻
MIT License
303 stars 24 forks source link

BP008: Not Handling conint(ge= , lt=) Correctly #123

Closed questionlp closed 12 months ago

questionlp commented 1 year ago

Python version: 3.10.12 bump-pydantic version: 0.6.1 Pydantic version: 2.3.0

I installed and ran bump-pydantic per the project's README.md file instructions against my FastAPI project*. The resulting diff doesn't handle conint(ge=, lt=) correctly.

Before running bump-pydantic, one of the models looks like:

 class GuestAppearance(BaseModel):
     """Appearance Information"""

    show_id: conint(ge=0, lt=2**31) = Field(title="Show ID")

The diff generated by bump-pydantic returns:

 class GuestAppearance(BaseModel):
     """Appearance Information"""

    show_id: Annotated = Field(title="Show ID")

It is losing both the int type and the ge and lt constraints. Replacing the expression 2**31 with 2147483648 doesn't change how bump-pydantic handles the conint per the following diff:

 class GuestAppearance(BaseModel):
     """Appearance Information"""

-    show_id: conint(ge=0, lt=2147483648) = Field(title="Show ID")
+    show_id: Annotated = Field(title="Show ID")
Kludex commented 12 months ago

I can't reproduce your MRE.

If I have the following code:

from pydantic import BaseModel, Field, conint

class GuestAppearance(BaseModel):
    """Appearance Information"""

    show_id: conint(ge=0, lt=2**31) = Field(title="Show ID")

And I run bump-pydantic main.py, it becomes:

from pydantic import BaseModel, Field
from typing_extensions import Annotated

class GuestAppearance(BaseModel):
    """Appearance Information"""

    show_id: Annotated[int, Field(ge=0, lt=2**31)] = Field(title="Show ID")

If you can show me an MRE that fails as you mentioned, I'll be happy to work on it. I'll be closing this for now.

Please open a new issue if you find a good example.