Title: LOAD_ATTR specialization for ModuleType subclasses bypasses data descriptors on the subclass (3.14 regression) · Issue #156399 · python/cpython · GitHub
Open Graph Title: LOAD_ATTR specialization for ModuleType subclasses bypasses data descriptors on the subclass (3.14 regression) · Issue #156399 · python/cpython
X Title: LOAD_ATTR specialization for ModuleType subclasses bypasses data descriptors on the subclass (3.14 regression) · Issue #156399 · python/cpython
Description: Bug report Bug description: Since 3.14, attribute loads on an instance of a types.ModuleType subclass are specialized to LOAD_ATTR_MODULE, which reads the module dictionary directly. The specialization does not check whether the subclass...
Open Graph Description: Bug report Bug description: Since 3.14, attribute loads on an instance of a types.ModuleType subclass are specialized to LOAD_ATTR_MODULE, which reads the module dictionary directly. The specializa...
X Description: Bug report Bug description: Since 3.14, attribute loads on an instance of a types.ModuleType subclass are specialized to LOAD_ATTR_MODULE, which reads the module dictionary directly. The specializa...
Opengraph URL: https://github.com/python/cpython/issues/156399
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"LOAD_ATTR specialization for ModuleType subclasses bypasses data descriptors on the subclass (3.14 regression)","articleBody":"# Bug report\n\n### Bug description:\n\nSince 3.14, attribute loads on an instance of a `types.ModuleType`\nsubclass are specialized to `LOAD_ATTR_MODULE`, which reads the module\ndictionary directly. The specialization does not check whether the\nsubclass defines a data descriptor for the name, so after the first\naccess a data descriptor on the subclass is silently bypassed and the\ndictionary value is returned instead. Before 3.14 the guard was\n`PyModule_CheckExact`, so subclasses never took this path and the\ndescriptor was honoured on every access.\n\nThe following runs the reproducer on 3.13, 3.14, 3.14t, 3.15 and\n3.15t using [uv](https://docs.astral.sh/uv/):\n\n```sh\nfor v in 3.13 3.14 3.14t 3.15 3.15t; do uv run --no-project --python $v python - \u003c\u003c'EOF'\nimport sys\nimport sysconfig\nimport types\n\n\nclass Desc:\n def __get__(self, instance, owner=None):\n return \"from descriptor\"\n\n def __set__(self, instance, value):\n instance.__dict__[\"x\"] = value\n\n\nclass Module(types.ModuleType):\n pass\n\n\nModule.x = Desc()\n\nm = Module(\"m\")\nm.__dict__[\"x\"] = \"from dict\"\n\nbuild = \"free-threaded\" if sysconfig.get_config_var(\"Py_GIL_DISABLED\") else \"default\"\nprint(sys.version.split()[0], build, [m.x for _ in range(4)])\nEOF\ndone\n```\n\nOutput:\n\n```\n3.13.15 default ['from descriptor', 'from descriptor', 'from descriptor', 'from descriptor']\n3.14.7 default ['from descriptor', 'from dict', 'from dict', 'from dict']\n3.14.7 free-threaded ['from descriptor', 'from dict', 'from dict', 'from dict']\n3.15.0rc1 default ['from descriptor', 'from dict', 'from dict', 'from dict']\n3.15.0rc1 free-threaded ['from descriptor', 'from dict', 'from dict', 'from dict']\n```\n\nExpected output is four \"from descriptor\" values on every version, as\non 3.13.\n\nThe first access runs through the general path and honours the\ndescriptor; the instruction is then specialized and subsequent\nexecutions read `m.__dict__[\"x\"]`. `getattr(m, \"x\")` returns\n\"from descriptor\" every time, since it does not go through the\nspecialized instruction, so the same expression gives different\nanswers depending on how it is spelled and how many times it has run.\n\nExpected: a data descriptor on the type takes precedence over the\ninstance dictionary for a module subclass exactly as it does for any\nother class, and as it did on 3.13 and earlier.\n\nObserved on 3.14.7 and 3.15.0rc1, both default and free-threaded\nbuilds; the same code is on `main`.\n\n## Cause\n\nIn `Python/specialize.c`, `_Py_Specialize_LoadAttr` selects the module\npath with\n\n```c\nelse if (Py_TYPE(owner)->tp_getattro == PyModule_Type.tp_getattro) {\n fail = specialize_module_load_attr(owner, instr, name);\n}\n```\n\nwhich any `ModuleType` subclass that does not override\n`__getattribute__` or `__getattr__` satisfies.\n`specialize_module_load_attr_lock_held` then only inspects the module\ndictionary (unicode keys, no `__getattr__` entry, the name present,\na keys version); it never looks the name up on the type. The guard of\nthe emitted `LOAD_ATTR_MODULE` is likewise only the dict keys version,\nso a descriptor added to the type later is not noticed either.\n\nThis came in with gh-103951 (\"Fast attribute access for module\nsubclasses\", PR #126264, merged 2024-11-15), which relaxed the guard\nfrom `PyModule_CheckExact` for speed. The discussion there was about\nkeeping the guard cheap: `PyModule_Check` was rejected because it\nwalks the MRO, and the `tp_getattro` comparison was chosen as a single\npointer compare that admits only types with module attribute\nsemantics. The stated motivation was the \"Customizing module attribute\naccess\" pattern from the data model docs, that is, assigning a\n`ModuleType` subclass to a module's `__class__`. Neither the issue\ncomments nor the PR body, review threads or comments mention\ndescriptors, `property`, or type version tags, so this looks like an\nunintended consequence rather than a decision. A search of the\ntracker (LOAD_ATTR_MODULE, module subclass descriptor, ModuleType\nsubclass property, and similar) found no prior report.\n\n## Impact\n\nAny library that assigns a `ModuleType` subclass to a module's\n`__class__` in order to intercept attribute access with descriptors\n(the documented route for module-level properties and lazy\nattributes, per the \"Customizing module attribute access\" section of\nthe data model docs) sees the interception disappear after the first\naccess on 3.14+. The workaround is to define a `__getattribute__` on\nthe subclass that delegates to the base, which gives the type its own\n`tp_getattro` and so avoids the specialization, at the cost of a\nPython-level call on every attribute access to that module.\n\nEncountered in wrapture (https://github.com/GrahamDumpleton/wrapture),\nwhich uses exactly this technique to intercept module attribute access;\nrepro above is reduced from that.\n\n## AI Disclaimer\n\nThis was a real problem I encountered, but have had AI generate the report for me so more clearly explained. The AI did generate a suggested fix as well, but I am not in a position to evaluate whether it is correct so have not included it. If want AI generated suggested fix then let me know.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-156474\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/GrahamDumpleton","@type":"Person","name":"GrahamDumpleton"},"datePublished":"2026-08-26T04:02:30Z","url":"https://github.com/python/cpython/issues/156399"}
| 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:6d5e0851-f884-5979-088d-dae1af55edfc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9012:39588:E5CE8F:13388C5:6B064BBA |
| html-safe-nonce | cd8069309dad10dc8c5ed269fe4971691ab1271181d9068f66a4d50ff555eefe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MDEyOjM5NTg4OkU1Q0U4RjoxMzM4OEM1OjZCMDY0QkJBIiwidmlzaXRvcl9pZCI6IjQ3NDMzMzQwMTc3NTAxNTgyNjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 815e65c2b56da85883195270e0f7c0e5d6038f5c4aa56db3a783bd8574289645 |
| hovercard-subject-tag | issue:5253192809 |
| 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/cpython/156399/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6caccef90bbba706180844d9084a44c47476a2f04a30779055353eda0003f580/python/cpython/issues/156399 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6caccef90bbba706180844d9084a44c47476a2f04a30779055353eda0003f580/python/cpython/issues/156399 |
| og:image:alt | Bug report Bug description: Since 3.14, attribute loads on an instance of a types.ModuleType subclass are specialized to LOAD_ATTR_MODULE, which reads the module dictionary directly. The specializa... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GrahamDumpleton |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| 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 | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width