-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Introspection of fields during validation #6429
Comments
I couldn't tell immediately from the links what is going on, a self contained example would help. In v2 you can access some field info via Line 100 in c3fc4e6
The For from datetime import datetime
from typing import Annotated
from pydantic import BaseModel, ConfigDict, Field, field_validator
class DemoModel(BaseModel):
ts: Annotated[datetime | None, Field(validate_default=True)] = None
@field_validator('ts', mode='before')
def set_ts_now(cls, v: datetime | None) -> datetime:
return v or datetime(2032, 1, 2, 3, 4, 5, 6)
model_config = ConfigDict(validate_default=True) # either option works, you don't need both
print(DemoModel())
#> ts=datetime.datetime(2032, 1, 2, 3, 4, 5, 6)
print(DemoModel(ts=datetime(2022, 1, 2, 3, 4, 5, 6)))
#> ts=datetime.datetime(2022, 1, 2, 3, 4, 5, 6)
If you're trying to do some of this from within a validator function you can get the field from the model using the field name: from datetime import datetime
from typing import Any
from pydantic import BaseModel, FieldValidationInfo, field_validator
def validator_func(cls: type[BaseModel], v: Any, info: FieldValidationInfo) -> Any:
print(repr(cls.model_fields[info.field_name]))
return v
class DemoModel(BaseModel):
ts: datetime | None
val_ts = field_validator('ts', mode='before')(classmethod(validator_func))
DemoModel(ts=None)
#> FieldInfo(annotation=Union[datetime, NoneType], required=True) You could also use a model_validator with Let me know if this helps or if you have any more questions. |
I'll just note that, in Also, I had a chance to discuss this with @samuelcolvin this morning, and he noted that the right way to inject defaults might be to instead post-process the |
Thanks! I am at the last step I think. Is there a way to perform validation/update the raw dictionary of fields after all validation right before the model gets created? It looks like the after mode for model validators returns the final object which in our case cannot be modified because we enable frozen. I tried the wrap mode but am getting an error (this might not even provide the dictionary but I was just trying stuff): @model_validator(mode='wrap')
def _final_validation(cls, values, handler):
return values
|
That implementation would work with |
It looks like that is not true but rather model validation with before mode gets run before field validation |
My point was that |
Is there a way to modify the dictionary at the very end after all field validators have executed? |
There is no more dictionary at that point, just the model |
I do think there's two very reasonable feature requests here:
|
Initial Checks
Description
Here is an example of what we are currently doing in v1:
We define the configuration for each integration as OpenAPI and generate the model code. Basically, we:
None
Is there a way in v2 to achieve this? Specifically, it looks like we need replacements for
field.required
,field.shape
, andalways=True
.Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeSelected Assignee: @hramezani
The text was updated successfully, but these errors were encountered: