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
# The default and requiredness of a field is not a property of a field
# In the case of DisplayOnlyFieldTypes, we do kind of want that.
# Using this method we set the right properties after the form is created
forfieldincls.model_fields.values():
iffield.frozen:
field.validate_default=False
This is not working as you would expect, because Pydantic constructs a core schema used during validation based on model fields; and this happens before this __pydantic_init_subclass__ hook is called.
This core schema is used to understand how validation should be applied, and it contains the logic related to validate_default:
Note that Pydantic does not really explicitly support this pattern of rebuilding a model after altering the model fields.
On a related note, relying on model_fields just after class creation might lead to incorrect results. The reason for this is that subclasses of FormPage might use unresolvable forward references. This PR gives more context: pydantic/pydantic#11388.
In 2.11, we might raise a warning when incomplete model fields are being accessed (cross ref this issue), but be assured that if we do so, we will provide another kind of hook similar to __pydantic_init_subclass__, that is only called when fields are guaranteed to be complete.
The text was updated successfully, but these errors were encountered:
I came across this implementation while trying to evaluate potential regressions in the next Pydantic release (2.11):
pydantic-forms/pydantic_forms/core/shared.py
Lines 45 to 53 in 0dbbd50
This is not working as you would expect, because Pydantic constructs a core schema used during validation based on model fields; and this happens before this
__pydantic_init_subclass__
hook is called.This core schema is used to understand how validation should be applied, and it contains the logic related to
validate_default
:To make sure the core schema is reconstructed, you can call
model_rebuild(force=True)
:Note that Pydantic does not really explicitly support this pattern of rebuilding a model after altering the model fields.
On a related note, relying on
model_fields
just after class creation might lead to incorrect results. The reason for this is that subclasses ofFormPage
might use unresolvable forward references. This PR gives more context: pydantic/pydantic#11388.In 2.11, we might raise a warning when incomplete model fields are being accessed (cross ref this issue), but be assured that if we do so, we will provide another kind of hook similar to
__pydantic_init_subclass__
, that is only called when fields are guaranteed to be complete.The text was updated successfully, but these errors were encountered: