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
Creating a TypeAdapter for a nested TypedDict removes model_validator for other TypeAdapters #9318
Open
1 task done
Labels
bug V2
Bug related to Pydantic V2
Comments
cc-a
added
bug V2
Bug related to Pydantic V2
pending
Awaiting a response / confirmation
labels
Apr 25, 2024
Hmph, so this works as expected if you use Here's a repro with some schema information - the schema created with the inner type adapter used is missing the after validator function (which is a bug, as you've noticed behaviorally), but I'm not sure why. from pydantic import BaseModel, TypeAdapter, model_validator, ValidationError
from typing_extensions import TypedDict
class Inner(TypedDict):
a: int
@model_validator(mode="after")
def check(cls, v):
raise ValueError("expected error")
return self
InnerTypeAdapter = TypeAdapter(Inner)
# InnerTypeAdapter.validate_python(dict(a=0))
"""
pydantic_core._pydantic_core.ValidationError: 1 validation error for function-after[check(), typed-dict]
Value error, expected error [type=value_error, input_value={'a': 0}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/value_error
"""
class Outer(TypedDict):
inner: Inner
OuterTypeAdapter = TypeAdapter(Outer)
from pydantic._internal._core_utils import pretty_print_core_schema
pretty_print_core_schema(OuterTypeAdapter.core_schema)
"""
With TypedDict, with inner adapter
{
'type': 'typed-dict',
'fields': {
'inner': {
'type': 'typed-dict-field',
'schema': {
'type': 'typed-dict',
'fields': {
'a': {
'type': 'typed-dict-field',
'schema': {'type': 'int'},
'required': True,
'metadata': {
'pydantic_js_functions': [],
'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func at 0x1053f8e50>]
}
}
},
'computed_fields': [],
'ref': '__main__.Inner:4955795504',
'config': {'title': 'Inner'}
},
'required': True,
'metadata': {
'pydantic_js_functions': [],
'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func at 0x1053f8d30>]
}
}
},
'computed_fields': [],
'ref': '__main__.Outer:5369543232',
'config': {'title': 'Outer'}
}
"""
"""
With TypedDict, no inner adapter
{
'type': 'typed-dict',
'fields': {
'inner': {
'type': 'typed-dict-field',
'schema': {
'function': {'type': 'no-info', 'function': <bound method Inner.check of <class '__main__.Inner'>>},
'schema': {
'type': 'typed-dict',
'fields': {
'a': {
'type': 'typed-dict-field',
'schema': {'type': 'int'},
'required': True,
'metadata': {
'pydantic_js_functions': [],
'pydantic_js_annotation_functions': [
<function get_json_schema_update_func.<locals>.json_schema_update_func at 0x105a7ab00>
]
}
}
},
'computed_fields': [],
'config': {'title': 'Inner'}
},
'ref': '__main__.Inner:5258552160',
'type': 'function-after'
},
'required': True,
'metadata': {
'pydantic_js_functions': [],
'pydantic_js_annotation_functions': [<function get_json_schema_update_func.<locals>.json_schema_update_func at 0x105a78f70>]
}
}
},
'computed_fields': [],
'ref': '__main__.Outer:5258589040',
'config': {'title': 'Outer'}
}
""" Definitely something we need to fix, likely on the |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initial Checks
Description
Please see the example below where validation should fail but does not. Commenting out the creation of
InnerTypeAdapter
gives a validation error as expected.Creating a TypeAdapter for the Inner class seems to prevent it's model validator from being run when using a TypeAdapter for the Outer class. Performing validation with
InnerTypeAdapter
works as expected.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: