fix(query): Preserve metadata on inherited Model Query fields#58
Merged
Conversation
`QueryPatcher.for_component` used to look up the rewritten annotation in
`component.__annotations__[field_name]` and fall back to `field.annotation`
otherwise. Both `cls.__annotations__` and that fallback drop the
`PlainSerializer` / `PlainValidator` that `annotate_model` attaches for
Django Model and QuerySet types — `__annotations__` because it is the
class's *own* dict (no MRO walk), and `field.annotation` because pydantic
stores the bare type with metadata kept separately.
The result was that a public component which inherited a Query field like
editing: Annotated[Item | None, Query("editing"), Field(default=None)]
from an abstract base would build an adapter that knew how to validate but
not how to serialise: as soon as the URL needed to be updated for the
chosen instance, `dump_python` raised
`PydanticSerializationError: Unable to serialize unknown type`.
Reconstruct the annotation from `field.annotation` + `field.metadata`
instead. Pydantic carries the metadata along through inheritance, so the
adapter now picks up the model-PK serializer regardless of where the
field was declared in the MRO.
Add `test_query.py` covering both inherited and own-field declaration so
the regression cannot return.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
QueryPatcher.for_componentused to look up the rewritten annotation incomponent.__annotations__[field_name]and fall back tofield.annotationotherwise. Bothcls.__annotations__and that fallback drop thePlainSerializer/PlainValidatorthatannotate_modelattaches for Django Model and QuerySet types —__annotations__because it is the class's own dict (no MRO walk), andfield.annotationbecause pydantic stores the bare type with metadata kept separately.The result was that a public component which inherited a Query field like
from an abstract base would build an adapter that knew how to validate but not how to serialise: as soon as the URL needed to be updated for the chosen instance,
dump_pythonraisedPydanticSerializationError: Unable to serialize unknown type.Reconstruct the annotation from
field.annotation+field.metadatainstead. Pydantic carries the metadata along through inheritance, so the adapter now picks up the model-PK serializer regardless of where the field was declared in the MRO.Add
test_query.pycovering both inherited and own-field declaration so the regression cannot return.