You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm at the final stages of migrating a v1 code base to v2. Functionally it's complete, the code runs as expected without any issues (the v1 to v2 migration guide was really useful). But I'm getting mypy errors when using my attempt to migrate "reused" v1 root_validator instances to v2 model_validator instances.
When I use a decorated model_validator inline in the model definition all is well. The model validates as expected and mypy is content and happy.
When I use my attempt at a reusable model_validator the model still validates as expected but mypy is not happy at all.
The "inline" test case I've put together, plus the output from mypy is:
#!/usr/bin/env python
# pylint: disable=all
from typing import Any, Dict, Optional
from pydantic import BaseModel, ConfigDict, model_validator
def normalise_known_invalids(values: Dict[str, Any]) -> Dict[str, Any]:
if values:
for key, value in values.items():
value = value.strip()
if value:
if value.lower() in ['', 'n/a', '-']:
values[key] = None
else:
values[key] = value
else:
values[key] = None
return values
class InlineValidatorModel(BaseModel):
fld: Optional[str] = None
@model_validator(mode='before')
@classmethod
def normaliser(cls, values: Dict[str, Any]) -> Dict[str, Any]:
return normalise_known_invalids(values)
model_config = ConfigDict(str_strip_whitespace=True, extra='forbid')
def main():
model = InlineValidatorModel()
print(f'empty inline model: {model}')
model = InlineValidatorModel(fld='OK')
print(f'valid input inline model: {model}')
model = InlineValidatorModel(fld='n/a ')
print(f'invalid input inline model: {model}')
if __name__ == '__main__':
main()
$ mypy model_test.py
Success: no issues found in 1 source file
But the mypy output from the "reused" variant, code in the example below, is:
$ mypy model_test2.py
model_test2.py:29: error: Argument 1 has incompatible type "Callable[[dict[str, Any]], dict[str, Any]]"; expected "ModelBeforeValidator | ModelBeforeValidatorWithoutInfo" [arg-type]
Found 1 error in 1 file (checked 1 source file)
Initial Checks
Description
I'm at the final stages of migrating a v1 code base to v2. Functionally it's complete, the code runs as expected without any issues (the v1 to v2 migration guide was really useful). But I'm getting
mypy
errors when using my attempt to migrate "reused" v1root_validator
instances to v2model_validator
instances.When I use a decorated
model_validator
inline in the model definition all is well. The model validates as expected andmypy
is content and happy.When I use my attempt at a reusable
model_validator
the model still validates as expected butmypy
is not happy at all.The "inline" test case I've put together, plus the output from
mypy
is:But the
mypy
output from the "reused" variant, code in the example below, is:Also, this is possibly related to #6693.
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: