Skip to content

fix: prevent AttributeError when schema_dump receives None values - #530

Merged
cofin merged 1 commit into
litestar-org:mainfrom
barapa:fix/none-dict-protocol-check
Aug 29, 2025
Merged

cofin merged 1 commit into
litestar-org:mainfrom
barapa:fix/none-dict-protocol-check

Conversation

@barapa

@barapa barapa commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

Description

Fixes an AttributeError that occurs when schema_dump() receives None values, which commonly happens with nullable database fields using StoredObject types during JSON serialization.

Problem

When nullable StoredObject fields contain None values, the JSON serialization process fails with:

AttributeError: 'NoneType' object has no attribute '__dict__'

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 with has_dict_attribute(data). The new function uses isinstance(obj, DictProtocol) which incorrectly returns True for None due to Python's structural protocol checking behavior, even though None doesn't actually have a __dict__ attribute.

Solution

Added an explicit None check to the has_dict_attribute() function:

def has_dict_attribute(obj: Any) -> "TypeGuard[DictProtocol]":
    # Protocol checking incorrectly returns True for None, so add explicit check
    return obj is not None and isinstance(obj, DictProtocol)

Testing

  • Added test case test_schema_dump_none_value() that verifies None values are handled correctly
  • All existing tests continue to pass
  • Verified end-to-end serialization scenarios work properly

Changes

  • advanced_alchemy/service/typing.py: Fixed has_dict_attribute() function
  • tests/unit/test_attrs_integration.py: Added test case for None value handling

This is a minimal, targeted fix that resolves the immediate issue without affecting the broader functionality of the protocol-based type checking system.

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
@github-actions

Copy link
Copy Markdown

Documentation preview will be available shortly at https://litestar-org.github.io/advanced-alchemy-docs-preview/530

@cofin cofin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cofin
cofin merged commit cf3ddfc into litestar-org:main Aug 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants