Giter Site home page Giter Site logo

Comments (8)

Kludex avatar Kludex commented on July 24, 2024 2

Then...

If validator method has 2 params (parameter names doesn't matter, but numbers do):

from pydantic import BaseModel, validator


class User(BaseModel):
    first_names: List[str]
    last_names: List[str]

    @validator("first_name", "last_name", each_item=True)
    def names_must_not_be_empty_strings(cls, v):
        if v:
            return v
        raise ValueError("Empty strings are not permitted")

Then we'd get:

from pydantic import BaseModel, validator, BeforeValidator
from typing_extensions import Annotated
from typing import List


# This function goes to the outer scope.
# I think we can get the `ClassDef` parent node with `LibCST.ParentProvider`. But I'm not sure...
def names_must_not_be_empty_strings(v):
    if v:
        return v
    raise ValueError("Empty strings are not permitted")


class User(BaseModel):
    # Check if the `AnnAssign.annotation` (List in this case) is a `Subscriptable` - and if it's, apply `BeforeValidator`.  
    first_names: List[Annotated[str, BeforeValidator(names_must_not_be_empty_strings)]]
    last_names: List[Annotated[str, BeforeValidator(names_must_not_be_empty_strings)]]

Remember to import typing_extensions.Annotated, and BeforeValidator, and remove validator.

Also, you might want to check function name collision on the module level - but I'm not sure if this is needed - but an xfail would be nice.

Not finished yet... If the number of params is more than 2, then add a note on top of the method:

# TODO: We couldn't replace the validator automatically.
# Please check https://docs.pydantic.dev/latest/migration/#validator-and-root_validator-are-deprecated.

from bump-pydantic.

Kludex avatar Kludex commented on July 24, 2024 1

Well... It's feasible.

I guess what would be faster is to teach how to do it via comment.

from bump-pydantic.

Kludex avatar Kludex commented on July 24, 2024

What are you proposing here?

from bump-pydantic.

cclauss avatar cclauss commented on July 24, 2024

Rules

BP0xy: Replace each_item

  • ✅ Replace each_item by something better.

The following code will be transformed:

from pydantic import BaseModel, validator


class User(BaseModel):
    first_name: str
    last_name: str

    @validator("first_name", "last_name", each_item=True)
    def names_must_not_be_empty_strings(cls, v):
        if v:
            return v
        raise ValueError("Empty strings are not permitted")

Into:

from pydantic import BaseModel, field_validator


class User(BaseModel):
    first_name: str
    last_name: str

    @field_validator("first_name", "last_name",  # I am not sure what comes next...

from bump-pydantic.

Viicos avatar Viicos commented on July 24, 2024

I'm using a BeforeValidator to implement each_item in v2:

def validator_for_each_list_item(item: str):
   ...

class A(BaseModel):
    my_list: List[Annotated[str, BeforeValidator(validator_for_each_list_item)]]

Not sure if this is doable with a codemod

from bump-pydantic.

cclauss avatar cclauss commented on July 24, 2024

teach how to do it via comment.

Given the source above #70 (comment), what should the expected be?

from bump-pydantic.

Kludex avatar Kludex commented on July 24, 2024

teach how to do it via comment.

Given the source above #70 (comment), what should the expected be?

Were last_name and first_name supposed to be str, and not List[str]?

from bump-pydantic.

cclauss avatar cclauss commented on July 24, 2024

Let's do List[str] so last_names and first_names (plural).

from bump-pydantic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.