fix(utils): walk MRO in get_class_field_annotations#302
Merged
Conversation
get_class_field_annotations previously read only cls.__dict__['__annotations__'], which misses inherited fields. This broke copy_dataloader_kls: the generated `class NewLoader(Parent): pass` has no __annotations__ in its own __dict__, so _create_loader_instance could not see (or inject) the parent's loader params, raising AttributeError at runtime. Walk cls.__mro__ and merge annotations from every class in the chain. Also fixes the requires_context check in analysis.py, which now correctly detects _context on loader subclasses (e.g. copies of a context-aware loader). Updated two existing tests that encoded the old behavior, and added a regression test for copy_dataloader_kls inheriting parent annotations. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
nshiva-prasad
pushed a commit
to nshiva-prasad/pydantic-resolve
that referenced
this pull request
Jun 30, 2026
Changelog entry includes the full history of the get_class_field_annotations fix (KLR-Pattern#302): the 3-year-old latent bug from e637541 (2023-04-06), the 2026-05-14 copy_dataloader_kls refactor (ba8762e) that activated it, why cls.__annotations__.keys() is insufficient, and the side effects on _context detection and DataLoader's own annotated fields. Co-Authored-By: Claude Opus 4.7 <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.
Summary
get_class_field_annotationspreviously read onlycls.__dict__['__annotations__'], missing fields inherited from parents.copy_dataloader_kls: the generatedclass NewLoader(Parent): passhas empty__annotations__in its own__dict__, so_create_loader_instancecould not see or inject the parent's loader params (user_id,permission, etc.), raisingAttributeErrorat runtime.cls.__mro__and merges annotations from every class in the chain. The same fix also corrects the_contextdetection inanalysis.py:385, so copies of context-aware loaders are now correctly recognized.Why explicit MRO walk (not
cls.__annotations__.keys())Python's attribute lookup returns the first
__annotations__found in the MRO. So:The explicit reversed-MRO loop merges across all levels, which is what loader param injection needs.
Side-effect check: DataLoader's own annotated fields
Walking the full MRO also surfaces
aiodataloader.DataLoader's own annotations (batch,cache,max_batch_size). These all have class-level defaults, so_create_loader_instanceskips them via its existinghas_default and field not in param_configbranch — no behavior change.Test plan
test_get_class_field_annotations_with_inheritance— derived class now includes base fieldtest_get_class_field_annotations(test_utils.py) —D(C)andE(C)now reflect inheritedhellotest_get_class_field_annotations_subclass_with_no_own_fields— coversclass Child(Parent): passtest_copy_dataloader_kls_inherits_parent_annotations— regression for the reported bug🤖 Generated with Claude Code