fix: Model.from_storage should handle missing optional fields#419
Merged
ngjunsiang merged 1 commit intoweeklyfrom Apr 7, 2026
Merged
fix: Model.from_storage should handle missing optional fields#419ngjunsiang merged 1 commit intoweeklyfrom
ngjunsiang merged 1 commit intoweeklyfrom
Conversation
Fixes #418 The Model.from_storage() method was raising KeyError when loading storage records that were missing optional fields (fields with default values), even though those fields were correctly defined with defaults. This was causing OAuth token loading to fail for campus tokens, which don't have a refresh_token field. When stored in MongoDB, the sparse document doesn't include the refresh_token key, and from_storage() would raise KeyError when trying to access it. Changes: - Modified Model.from_storage() to use record.get() pattern with fallback to field defaults for missing keys - Added get_value() helper function that: 1. Returns record value if key exists 2. Falls back to field.default if available 3. Falls back to field.default_factory() if available 4. Raises KeyError with helpful message for truly required fields Tests: - Added test_base.py with comprehensive tests for Model serialization: - from_storage with all fields present - from_storage with missing optional field (uses None) - from_storage with missing default field (uses default value) - from_storage with missing default_factory field (calls factory) - from_storage with missing required field (raises KeyError) - Roundtrip serialization tests - Sparse record handling (simulates MongoDB documents) Co-Authored-By: Claude Opus 4.6 <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
Fixes #418 - The
Model.from_storage()method was raisingKeyErrorwhen loading storage records that were missing optional fields (fields with default values).Problem
When campus OAuth tokens (which don't have
refresh_token) were stored in MongoDB and then retrieved, theOAuthToken.from_storage()method would raiseKeyError: 'refresh_token'because:Model.from_storage()used direct dictionary accessrecord[field.name]refresh_token: str | None = Nonehas a default, the code didn't use itSolution
Modified
Model.from_storage()to handle missing fields gracefully:field.defaultfor fields with default valuesfield.default_factory()for fields with factory defaultsKeyErroronly for truly required fields (no defaults)Tests
Added
tests/unit/model/test_base.pywith comprehensive coverage:from_storagewith all fields presentfrom_storagewith missing optional field (uses None)from_storagewith missing default field (uses default value)from_storagewith missing default_factory field (calls factory)from_storagewith missing required field (raises KeyError with helpful message)Test Results
All tests pass:
🤖 Generated with Claude Code