Problem
We use "before" validators to read data from a file input and transform it. According to pydantic (pydantic/pydantic#468) and a user on Stack (https://stackoverflow.com/a/72362147/8557834), that is what you should do, although I'm not sure this is still the case today or if there are better options.
Yet, this plugin does not pickup such transformations in the "before" validator:
from pydantic import BaseModel, field_validator
class BeforeValidationTransformModel(BaseModel):
real_value: float
@field_validator('real_value', mode='before')
def _convert_version(cls, input_value: Any) -> float:
if isinstance(input_value, str):
return 2.54
return input_value
model = BeforeValidationTransformModel(real_value='a string')
print(model)
Potential solution
Incorporate the typing from the "before" validation method and expect a union.
Alternative solution
Suggest to replace "before" validators with better alternatives.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too