Skip to content

Fix lazyproperty to cache a legitimately None return value - #1145

Open
afonsojanu wants to merge 1 commit into
scanny:masterfrom
afonsojanu:fix/lazyproperty-none-value-caching
Open

Fix lazyproperty to cache a legitimately None return value#1145
afonsojanu wants to merge 1 commit into
scanny:masterfrom
afonsojanu:fix/lazyproperty-none-value-caching

Conversation

@afonsojanu

Copy link
Copy Markdown

Summary

lazyproperty.__get__ (src/pptx/util.py) decided whether the wrapped getter had already run by checking obj.__dict__.get(self._name) is None. That means any lazyproperty whose getter legitimately returns None never gets cached: the getter is silently re-evaluated on every single access instead of exactly once.

This contradicts the class's own docstring, which promises the getter is "only evaluated on first access" and that the result is cached for "second and later access without re-evaluation," explicitly framing this as an immutability/idempotence guarantee.

Repeated re-evaluation of a None-returning getter is at minimum a performance regression (defeats the entire purpose of the decorator for that property), and can be worse if the getter has side effects on first construction of a collaborator object, since callers reasonably assume it only runs once per instance.

Fix

Check for key presence in obj.__dict__ instead of testing the cached value against None:

if self._name not in obj.__dict__:
    obj.__dict__[self._name] = self._fget(obj)
return cast(_T, obj.__dict__[self._name])

Testing

Added DescribeLazyproperty in tests/test_util.py with two cases:

  • the getter is called exactly once across repeated access (existing correct behavior, kept as a baseline)
  • a getter returning None is also called exactly once (the new case; fails before this fix with a call count of 2)

Verified by temporarily reverting only the src/pptx/util.py change and confirming the new it_caches_a_None_return_value_too case fails (assert 2 == 1) while the baseline case still passes, then restoring the fix. Full test suite passes (2686 passed) with no new failures; the 16 pre-existing collection errors in tests/opc/test_package.py / tests/opc/test_serialized.py are a pytest-version incompatibility (class-scoped fixture defined as an instance method) unrelated to this change and reproduce identically on an unmodified checkout. ruff check and black --check pass clean on both changed files, and pyright reports no errors on src/pptx/util.py.

lazyproperty.__get__ checked obj.__dict__.get(self._name) is None to
decide whether the getter had already run, so any lazyproperty whose
getter legitimately returns None was silently recomputed on every
access instead of being cached once, contradicting the class's own
documented immutability and idempotence guarantee. Switched the check
to key presence in obj.__dict__ instead.
@afonsojanu
afonsojanu force-pushed the fix/lazyproperty-none-value-caching branch from 9678332 to 390decc Compare August 29, 2026 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant