Fix lazyproperty to cache a legitimately None return value - #1145
Open
afonsojanu wants to merge 1 commit into
Open
Fix lazyproperty to cache a legitimately None return value#1145afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
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
force-pushed
the
fix/lazyproperty-none-value-caching
branch
from
August 29, 2026 17:48
9678332 to
390decc
Compare
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
lazyproperty.__get__(src/pptx/util.py) decided whether the wrapped getter had already run by checkingobj.__dict__.get(self._name) is None. That means anylazypropertywhose getter legitimately returnsNonenever 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 againstNone:Testing
Added
DescribeLazypropertyintests/test_util.pywith two cases:Noneis 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.pychange and confirming the newit_caches_a_None_return_value_toocase 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 intests/opc/test_package.py/tests/opc/test_serialized.pyare a pytest-version incompatibility (class-scoped fixture defined as an instance method) unrelated to this change and reproduce identically on an unmodified checkout.ruff checkandblack --checkpass clean on both changed files, andpyrightreports no errors onsrc/pptx/util.py.