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

Teach mypy plugin that validator methods are classmethods #4102

Merged
merged 5 commits into from Aug 9, 2022

Commits on May 22, 2022

  1. Teach mypy plugin that validator methods are classmethods

    For your consideration: a patch which implements the suggestion I made here:
    pydantic#4101
    
    Briefly: pydantic automatically wraps validator methods using
    `@classmethod`. Hence the first argument to a user-provided validator
    should be `cls`. But mypy doesn't know this: it analyses validator
    methods like any other regular method, believing the first parameter
    `cls` to be a model instance (usually denoted self).
    
    This means that if one annotates `cls` as `Type[...]` then mypy
    believes raises an error:
    
    	error: The erased type of self "Type[temp.Model]" is not a supertype of its class "temp.Model"
    
    I concede that this is an extremely niche thing. The only tangible
    end-user benefit I can think of is that it'll stop you from calling
    instance methods in a validator.
    
    ----
    
    I haven't written a mypy plugin before, so this is a bit of a
    hack-until-it-works. But it was more straightforward than I expected to
    get something working!
    DMRobertson committed May 22, 2022
    Configuration menu
    Copy the full SHA
    404ae62 View commit details
    Browse the repository at this point in the history
  2. changelog

    DMRobertson committed May 22, 2022
    Configuration menu
    Copy the full SHA
    faf9251 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    8de9108 View commit details
    Browse the repository at this point in the history

Commits on Aug 8, 2022

  1. Configuration menu
    Copy the full SHA
    ca6430e View commit details
    Browse the repository at this point in the history
  2. Add failure test

    DMRobertson committed Aug 8, 2022
    Configuration menu
    Copy the full SHA
    375ee99 View commit details
    Browse the repository at this point in the history