pydantic / bump-pydantic

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

uncovered case in validator->field_validator #138

Open anilbey opened 9 months ago

anilbey commented 9 months ago

Hello, moving from V1 to V2, this output gets created by bump-pydantic.

    # TODO[pydantic]: We couldn't refactor the `validator`, please replace it by `field_validator` manually.
    # Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-validators for more information.
    @validator("decay_time")
    def decay_time_gt_rise_time(cls, v, values):
        if v <= values["rise_time"]:
            raise ValueError("decay_time must be greater than rise_time")
        return v

When this occurs, the values need to become values.data then all works fine. Maybe this refactoring can be added to the tool.

Before (V1)

    @validator("decay_time")
    def decay_time_gt_rise_time(cls, v, values):
        if v <= values["rise_time"]:
            raise ValueError("decay_time must be greater than rise_time")
        return v

After (V2)

    @field_validator("decay_time")
    @classmethod
    def decay_time_gt_rise_time(cls, v, values):
        if v <= values.data["rise_time"]:
            raise ValueError("decay_time must be greater than rise_time")
        return v