🔧 Refactor populate_field_type to use type-directed schema walking#1639
Merged
chrisjsewell merged 4 commits intomasterfrom Feb 3, 2026
Merged
🔧 Refactor populate_field_type to use type-directed schema walking#1639chrisjsewell merged 4 commits intomasterfrom
populate_field_type to use type-directed schema walking#1639chrisjsewell merged 4 commits intomasterfrom
Conversation
This PR simplifies the `populate_field_type` function by replacing runtime keyword detection with type-directed traversal of the `SchemasRootType` structure. ## Changes ### Refactored `populate_field_type` (~190 → ~130 lines) **Before:** Generic recursive function operating on `Any`, detecting JSON schema keywords (`properties`, `items`, `contains`, etc.) at runtime to infer structure, with special-case handling for network context. **After:** Type-directed implementation that surgically walks the known typed structure: - `SchemasRootType` → `select` / `validate` - `ValidateSchemaType` → `local` / `network` - `NeedFieldsSchemaType` → `properties` / `allOf` - `ResolvedLinkSchemaType` → recursive `items` / `contains` ### Key improvements 1. **Clearer structure** — 5 focused helper functions with single responsibilities: - `_process_validate_schema` - `_process_resolved_link` - `_process_need_fields_schema` - `_inject_field_type` - `_inject_type_and_item_type` 2. **Eliminated ambiguity** — No more `is_inside_network` path checks; the type structure dictates context 3. **Defensive handling** — All field accesses guarded with `isinstance()` checks; blanket exception handler wraps unexpected errors with context 4. **Unified field lookup** — Single pattern for extra/link/core fields with fallback to `NeedsCoreFields` ### Added tests New test file `tests/schema/test_populate_field_type.py` with 32 tests covering: - Type injection for extra fields, link fields, and core fields - Array type injection with `items`/`contains` - Recursive processing of `network` and `allOf` - Error handling for unknown fields, type mismatches, and malformed schemas - Defensive behavior for missing/invalid structure ## Breaking changes None. All existing tests pass. Error messages for malformed schemas may differ slightly in wording but still provide equivalent information.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1639 +/- ##
==========================================
+ Coverage 86.87% 88.80% +1.92%
==========================================
Files 56 70 +14
Lines 6532 9972 +3440
==========================================
+ Hits 5675 8856 +3181
- Misses 857 1116 +259
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
ubmarco
approved these changes
Feb 2, 2026
Member
ubmarco
left a comment
There was a problem hiding this comment.
2 minor comments, good for merge
populate_field_type
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.
This PR simplifies the
populate_field_typefunction by replacing runtime keyword detection with type-directed traversal of theSchemasRootTypestructure.Changes
Refactored
populate_field_type(~190 → ~130 lines)Before: Generic recursive function operating on
Any, detecting JSON schema keywords (properties,items,contains, etc.) at runtime to infer structure, with special-case handling for network context.After: Type-directed implementation that surgically walks the known typed structure:
SchemasRootType→select/validateValidateSchemaType→local/networkNeedFieldsSchemaType→properties/allOfResolvedLinkSchemaType→ recursiveitems/containsKey improvements
Clearer structure — 5 focused helper functions with single responsibilities:
_process_validate_schema_process_resolved_link_process_need_fields_schema_inject_field_type_inject_type_and_item_typeEliminated ambiguity — No more
is_inside_networkpath checks; the type structure dictates contextDefensive handling — All field accesses guarded with
isinstance()checks; blanket exception handler wraps unexpected errors with contextUnified field lookup — Single pattern for extra/link/core fields with fallback to
NeedsCoreFieldsAdded tests
New test file
tests/schema/test_populate_field_type.pywith 32 tests covering:items/containsnetworkandallOfBreaking changes
None. All existing tests pass. Error messages for malformed schemas may differ slightly in wording but still provide equivalent information.