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
Protocol for an instance method taking a self
argument
#15620
Comments
Here's a self contained example: from typing import Any, Callable, Protocol, TypeVar
T = TypeVar('T')
class ModelAfterValidator(Protocol):
@staticmethod
def __call__(
self: T,
) -> T:
...
def model_validator() -> Callable[[ModelAfterValidator], Any]:
raise NotImplementedError
class UserModel:
@model_validator()
def check_passwords_match(self) -> 'UserModel':
raise NotImplementedError I guess the whole |
@Kludex can you update the title to something along the lines of "Protocol for an instance method taking a |
self
argument
I think we can just use |
I'm not able to repro the original bug in this bug report using mypy 1.5. Perhaps it was fixed in a recent release, or perhaps the pydantic library has changed since the original bug report. |
Pydantic has changed but the self contained example in #15620 (comment) should still be valid |
I think mypy is correct to complain about #15620 (comment) ; pyright does as well. |
I guess then let me broaden the question: how can I type this decorator to enforce that it get applied to an instance method? In the real world |
I'm not sure exactly I understand, but in pydantic case I think you can try a |
Bug Report
We have several protocols we use to define
@model_validator
in Pydantic V2, see here.The problem is that the simplest use case fails. See below:
To Reproduce
Expected Behavior
I expect no issues with
mypy
. As I don't have any withpyright
.Actual Behavior
Your Environment
mypy.ini
(and other config files): no config file provided.The text was updated successfully, but these errors were encountered: