Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teach mypy plugin that validator methods are classmethods (#4102)
* Teach mypy plugin that validator methods are classmethods For your consideration: a patch which implements the suggestion I made here: #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! * changelog * Add failure test
- Loading branch information
1 parent
6afc0c6
commit 11d8589
Showing
6 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Teach the mypy plugin that methods decorated by `@validator` are classmethods. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters