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
Unexpected behavior with pydantic.functional_validators.model_validator
(union of Protocols for Callables)
#6875
Comments
This is related to the way pydantic defines some of its callback protocols. In particular, look at class ModelBeforeValidatorWithoutInfo(Protocol):
def __call__(self, cls: Any, __value: Any) -> Any:
... I think the intent of this signature is to specify that the
Pyright is using interpretation 2, which I think is well justified. However, the typing spec is ambiguous on this point, so it could be interpreted either way. I'll post a question to the typing forums to solicit opinions from the broader community about how this should be interpreted. The best solution here is for the pydantic code to be unambiguous in its intent. If the intent is for class ModelBeforeValidatorWithoutInfo(Protocol):
def __call__(self, __cls: Any, __value: Any) -> Any:
... or class ModelBeforeValidatorWithoutInfo(Protocol):
def __call__(self, cls: Any, __value: Any, /) -> Any:
... Both of these eliminate the error you're seeing. I'll keep this issue open until we have some feedback from the typing community on how to interpret the spec. |
For reference, here's the thread in the typing forum: https://discuss.python.org/t/ambiguities-about-positional-only-parameters/42328. |
Interesting. I've been using I wonder how many other places this may pop up since it just worked in the past. Depending on the amount of effort it would take, might be worthwhile having a setting for this due to the ambiguity? Something along the lines of |
So, the only answer to getting this to pass is for the definition to be changed upstream, or to ignore all If there's no syntax that I can use to make my function match what pydantic expected, then it would be helpful to separate this from the More generally, it would be really nice to have more granularity in ignores. With so much packed into |
I'll try to chime in on intent as the original author of the code, although I wrote it a bit ago so may not recall of the details. The intent was to force users to have the first parameter be called I think it'd be fine to change it on the pydantic side. |
@adriangb, thanks for the additional context. There's a discussion about how a type checker should treat this case (where a parameter whose name starts with a double underscore appears after a parameter whose name does not). We seem to be converging on the consensus that it should be considered an error by a type checker and the resulting behavior should be considered undefined or indeterminate. If that's the final conclusion to the discussion, you'll need to make the change on the pydantic side. |
Makes sense. My only feedback would be that it’d be nice if libraries could express that a decorator acts as if it were |
The thread in the typing forum has resulted in a clarification to the typing spec that indicates type checkers should consider the following code an error: class ModelBeforeValidatorWithoutInfo(Protocol):
def __call__(self, cls: Any, __value: Any) -> Any:
... The pydantic library will need to be updated accordingly. I've filed a new issue #7290 to track the addition of this new error. |
Looking through the recent release, there were a few changes regarding the handling of protocols, callables, and overloads which would have impacted this.
Not sure if this is intentional behavior but it does not seem like it as I have not been able to find a combo that works.
Describe the bug
See code example below which includes the error messages in the docstrings.
Included is only a few things that I tried to satisfy pyright after my original signature (
_original_before_model_validator
), which passed for <1.1.344, started to fail type checking.One attempt (
_second_attempt
) which i included fails on both (1.1.343 & 1.1.344), which is fine, just showing what i tried based on the error message provided.Links to relevant pydantic source code:
module_validator
_AnyModeBeforeValidator
- this is a union of a few protocolsCode or Screenshots
VS Code extension or command-line
CLI - 1.1.344
The text was updated successfully, but these errors were encountered: