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

Typing complaints for @validator decorator #3357

Closed
jannikmi opened this issue Oct 26, 2021 · 2 comments
Closed

Typing complaints for @validator decorator #3357

jannikmi opened this issue Oct 26, 2021 · 2 comments
Assignees
Labels
bug V1 Bug related to Pydantic V1.X

Comments

@jannikmi
Copy link

For the following minimal code example i get the typing complaint "Method should have "self" as first argument":

from typing import Literal

import pydantic


class BaseModelClass(pydantic.BaseModel):
    """
    Doctolib search criteria
    """

    attribute: Literal["a", "b"]

    @pydantic.validator("attribute", pre=True)
    def lower_case_str(cls, v: str) -> str:
        return v.lower()


x = BaseModelClass(attribute="a")
print(x)

y = BaseModelClass(attribute="A")
print(y)

When i add @classmethod the complaint goes away:

    @classmethod
    @pydantic.validator("attribute", pre=True)
    def lower_case_str(cls, v: str) -> str:

... but the validation won't get through:

pydantic.error_wrappers.ValidationError: 1 validation error for BaseModelClass
attribute
  unexpected value; permitted: 'a', 'b' (type=value_error.const; given=A; permitted=('a', 'b'))

When i reverse the order everything works fine:

    @pydantic.validator("attribute", pre=True)
    @classmethod
    def lower_case_str(cls, v: str) -> str:

however i get the warning "This decorator will not receive a callable it may expect; the built-in decorator returns a special object"

How to solve this?

@jannikmi jannikmi added the bug V1 Bug related to Pydantic V1.X label Oct 26, 2021
@DMRobertson
Copy link
Contributor

Is this fixed by #4102?

@dmontagu dmontagu self-assigned this Apr 28, 2023
@dmontagu
Copy link
Contributor

This is now fixed in v1 and v2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X
Projects
None yet
Development

No branches or pull requests

3 participants