Title: lazyproperty re-evaluates its getter on every access when the cached value is None · Issue #1600 · python-openxml/python-docx · GitHub
Open Graph Title: lazyproperty re-evaluates its getter on every access when the cached value is None · Issue #1600 · python-openxml/python-docx
X Title: lazyproperty re-evaluates its getter on every access when the cached value is None · Issue #1600 · python-openxml/python-docx
Description: Summary docx.shared.lazyproperty (src/docx/shared.py) does not correctly cache a wrapped getter whose correct return value is None. Instead of evaluating the getter once and reusing the cached value, it re-invokes the getter on every acc...
Open Graph Description: Summary docx.shared.lazyproperty (src/docx/shared.py) does not correctly cache a wrapped getter whose correct return value is None. Instead of evaluating the getter once and reusing the cached valu...
X Description: Summary docx.shared.lazyproperty (src/docx/shared.py) does not correctly cache a wrapped getter whose correct return value is None. Instead of evaluating the getter once and reusing the cached valu...
Opengraph URL: https://github.com/python-openxml/python-docx/issues/1600
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"lazyproperty re-evaluates its getter on every access when the cached value is None","articleBody":"## Summary\n\n`docx.shared.lazyproperty` (`src/docx/shared.py`) does not correctly cache a wrapped getter whose correct return value is `None`. Instead of evaluating the getter once and reusing the cached value, it re-invokes the getter on **every** access whenever the cached value happens to be `None`.\n\n## Mechanism\n\n`lazyproperty.__get__` uses presence-in-`__dict__`-via-`None`-sentinel to decide whether the value has already been computed:\n\n```python\ndef __get__(self, obj, type=None):\n ...\n value = obj.__dict__.get(self._name)\n if value is None:\n value = self._fget(obj)\n obj.__dict__[self._name] = value\n return cast(T, value)\n```\n\n`dict.get(key)` returns `None` both when the key is absent *and* when the key is present with value `None`. Since `None` is a completely valid thing for a getter to legitimately compute (e.g. \"look up this optional related object; there isn't one yet\"), the two cases are indistinguishable here. Every subsequent access re-runs `fget` and re-stores `None`, forever.\n\nThis directly contradicts the class's own docstring, which states:\n\n> \"...evaluated only on first access; the resulting value is cached and that same value returned on second and later access **without re-evaluation** of the method.\"\n\nand\n\n> \"One common use is to construct collaborator objects, removing that 'real work' from the constructor, while still only executing once.\"\n\nIf such a getter has any side effect (e.g. constructs and registers an object, appends to a log, does I/O), that side effect fires on every access instead of once, breaking the documented idempotence guarantee.\n\n## Reproduction (verified against PyPI `python-docx` 1.2.0 and current `master`)\n\n```python\nfrom docx.shared import lazyproperty\n\nclass Widget:\n def __init__(self):\n self.call_count = 0\n\n @lazyproperty\n def maybe_missing_child(self):\n self.call_count += 1\n return None\n\nw = Widget()\n_ = w.maybe_missing_child\n_ = w.maybe_missing_child\n_ = w.maybe_missing_child\nprint(w.call_count) # prints 3\n```\n\nOutput:\n```\nresults: None, None, None\nfget call_count: 3 (lazyproperty's own docstring promises this must be 1)\n```\n\n## Expected vs actual\n\n- Expected: `fget` runs exactly once; the cached `None` is returned on subsequent accesses (matching the documented \"evaluated only on first access\" / \"without re-evaluation\" contract).\n- Actual: `fget` runs again on every access for as long as it keeps returning `None`.\n\n## Suggested fix\n\nUse a proper \"not yet computed\" sentinel instead of relying on `None`, e.g.:\n\n```python\n_NOT_COMPUTED = object()\n\ndef __get__(self, obj, type=None):\n if obj is None:\n return self\n value = obj.__dict__.get(self._name, _NOT_COMPUTED)\n if value is _NOT_COMPUTED:\n value = self._fget(obj)\n obj.__dict__[self._name] = value\n return cast(T, value)\n```\n\nor check `self._name in obj.__dict__` instead of comparing the fetched value to `None`.\n\n## Notes\n\n- I did not find any current `@lazyproperty`-decorated method inside python-docx's own element classes that happens to return `None` today, so this doesn't currently misbehave through the document-editing API itself. However, `lazyproperty` is a general-purpose, documented utility in `docx.shared` that the project's own docstring explicitly recommends for exactly this \"may return an absent/optional collaborator\" use case, and it's the standard tool used throughout `python-docx`'s own oxml element classes (see e.g. `docx/oxml/xmlchemy.py`, `docx/oxml/text/pagebreak.py`) as well as by third-party code extending python-docx with custom oxml element classes (a common, documented customization pattern) - so a future or third-party getter that legitimately returns `None` will silently lose its caching and idempotence guarantees.\n- Verified this reproduces identically against the current `master` branch (fetched fresh via the GitHub raw URL) - the `__get__` implementation is unchanged there.\n","author":{"url":"https://github.com/VANDRANKI","@type":"Person","name":"VANDRANKI"},"datePublished":"2026-08-25T19:36:47Z","url":"https://github.com/python-openxml/python-docx/issues/1600"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:df856bb1-292e-9df3-db7b-3f87fd06f1c8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C3F2:30DBED:7B7562:8CDD8B:6AFF6389 |
| html-safe-nonce | a0f3c379ff52f183f7f47c00837cd0d02a9d3a9119d19f5dd62e5a98bc2cfaec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDM0YyOjMwREJFRDo3Qjc1NjI6OENERDhCOjZBRkY2Mzg5IiwidmlzaXRvcl9pZCI6IjY3NzMzNDMwMTMwNTQ2Njk3MDUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e3821445b6b1ae8eeaae985721fd4b4b160ee09e6381d98ec7607971ceb08160 |
| hovercard-subject-tag | issue:5249835671 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-openxml/python-docx/1600/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c569198acfd76b3e5d9f52271868fc17c0ed5d315089104255081d90207c7c69/python-openxml/python-docx/issues/1600 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c569198acfd76b3e5d9f52271868fc17c0ed5d315089104255081d90207c7c69/python-openxml/python-docx/issues/1600 |
| og:image:alt | Summary docx.shared.lazyproperty (src/docx/shared.py) does not correctly cache a wrapped getter whose correct return value is None. Instead of evaluating the getter once and reusing the cached valu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | VANDRANKI |
| hostname | github.com |
| expected-hostname | github.com |
| None | 41b58589a1c4eda1e3c8904144a412b88ca4455f63ca2ad3629f2e9f3c3a1d12 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-openxml/python-docx git https://github.com/python-openxml/python-docx.git |
| octolytics-dimension-user_id | 3403760 |
| octolytics-dimension-user_login | python-openxml |
| octolytics-dimension-repository_id | 13592924 |
| octolytics-dimension-repository_nwo | python-openxml/python-docx |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 13592924 |
| octolytics-dimension-repository_network_root_nwo | python-openxml/python-docx |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8968d6d6acc65d74e4df332d23932dd3fd0502a4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width