Repository navigation
fix: prevent AttributeError when schema_dump receives None values - #530
Merged
Merged
Conversation
has_dict_attribute() was incorrectly returning True for None due to protocol checking behavior, causing schema_dump(None) to fail with AttributeError when trying to access None.__dict__. This affected nullable database fields using StoredObject types during JSON serialization. The fix adds an explicit None check before the protocol isinstance check. - Add explicit None check to has_dict_attribute() - Add test case verifying None values are handled correctly
|
Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/530 |
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.
Description
Fixes an AttributeError that occurs when
schema_dump()receivesNonevalues, which commonly happens with nullable database fields usingStoredObjecttypes during JSON serialization.Problem
When nullable
StoredObjectfields containNonevalues, the JSON serialization process fails with:This error occurs in the serialization chain: SQLAlchemy →
serializer()→schema_dump()→has_dict_attribute().Root Cause
The issue was introduced in commit 7fd8161 where
hasattr(data, "__dict__")was replaced withhas_dict_attribute(data). The new function usesisinstance(obj, DictProtocol)which incorrectly returnsTrueforNonedue to Python's structural protocol checking behavior, even thoughNonedoesn't actually have a__dict__attribute.Solution
Added an explicit
Nonecheck to thehas_dict_attribute()function:Testing
test_schema_dump_none_value()that verifiesNonevalues are handled correctlyChanges
advanced_alchemy/service/typing.py: Fixedhas_dict_attribute()functiontests/unit/test_attrs_integration.py: Added test case forNonevalue handlingThis is a minimal, targeted fix that resolves the immediate issue without affecting the broader functionality of the protocol-based type checking system.