What happened?
The current implementation is as follows.
import pydantic
class MyModel(pydantic.BaseModel):
model_config = pydantic.ConfigDict(validate_assignment=True)
value:str = pydantic.Field(max_length=3)
fi = MyModel.model_fields["value"]
pydantic.TypeAdapter(fi.annotation).validate_python("1234")
With this approach, validating "1234" will not raise an error.
The following approach can be used to validate with complete constraints.
getattr(MyModel.__pydantic_validator__.validate_assignment(MyModel.model_construct(), "value", "1234"),"value")
The above approach is based on a Pydantic issue, where you can find more detailed discussions.
pydantic/pydantic#7367
Steps to reproduce
import pydantic
class MyModel(pydantic.BaseModel):
model_config = pydantic.ConfigDict(validate_assignment=True)
value:str = pydantic.Field(max_length=3)
fi = MyModel.model_fields["value"]
pydantic.TypeAdapter(fi.annotation).validate_python("1234")
DSPy version
2.6.10