mypy plugin: teach mypy that validator methods are classmethods #4101
Closed
DMRobertson
started this conversation in
Feature Request
Replies: 1 comment
-
This was done in #4102. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The docs explain that validator functions take a type object as their first argument---
cls
rather thanself
.(This seems to be a small lie---Pydantic internally calls
classmethod
on the decorated function---but let's ignore that.)With this in mind, I tried to annotate a validator as follows:
This works great---but mypy isn't happy, even using Pydantic's mypy plugin.
After removing the annotation for
cls
,reveal_type(cls)
printstemp.Model
; I think mypy is analysing the pre-decorationvalidate_x
as if it were a regular method rather than a classmethod. If we instead writeThen mypy is happy.
It'd be great if the mypy plugin could do something similar under the covers.
Beta Was this translation helpful? Give feedback.
All reactions