Title: dir(module) ignores module-level __dir__ (PEP 562) · Issue #8231 · RustPython/RustPython · GitHub
Open Graph Title: dir(module) ignores module-level __dir__ (PEP 562) · Issue #8231 · RustPython/RustPython
X Title: dir(module) ignores module-level __dir__ (PEP 562) · Issue #8231 · RustPython/RustPython
Description: Summary A module-level __dir__ function (PEP 562) is ignored: dir(module) returns the raw __dict__ keys instead of calling the module's __dir__. # mod.py import sys def __dir__(): return ['gamma', 'alpha', 'beta'] print(dir(sys.modules[_...
Open Graph Description: Summary A module-level __dir__ function (PEP 562) is ignored: dir(module) returns the raw __dict__ keys instead of calling the module's __dir__. # mod.py import sys def __dir__(): return ['gamma', ...
X Description: Summary A module-level __dir__ function (PEP 562) is ignored: dir(module) returns the raw __dict__ keys instead of calling the module's __dir__. # mod.py import sys def __dir__(): return ['...
Opengraph URL: https://github.com/RustPython/RustPython/issues/8231
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"dir(module) ignores module-level __dir__ (PEP 562)","articleBody":"# Summary\n\nA module-level `__dir__` function (PEP 562) is ignored: `dir(module)` returns the raw `__dict__` keys instead of calling the module's `__dir__`.\n\n```python\n# mod.py\nimport sys\n\ndef __dir__():\n return ['gamma', 'alpha', 'beta']\n\nprint(dir(sys.modules[__name__]))\n```\n\n- CPython 3.14.2: `['alpha', 'beta', 'gamma']` (calls the function, `dir()` sorts the result)\n- RustPython: `['__builtins__', '__cached__', '__dir__', ..., 'sys']`\n\nVerified on `main` @ 7044fdcc8. PEP 562's other half, module-level `__getattr__`, works — only `__dir__` is missing.\n\n# Cause\n\n`PyModule.__dir__` (`crates/vm/src/builtins/module.rs:300`) unconditionally returns the module dict's keys:\n\n```rust\nfn __dir__(zelf: \u0026Py\u003cSelf\u003e, vm: \u0026VirtualMachine) -\u003e PyResult\u003cVec\u003cPyObjectRef\u003e\u003e {\n let dict_attr = zelf.as_object().get_attr(identifier!(vm, __dict__), vm)?;\n ...\n Ok(dict.into_iter().map(|(k, _v)| k).collect())\n}\n```\n\nCPython's `module___dir___impl` (Objects/moduleobject.c) first looks up `__dir__` in the module's `__dict__` and, if present, calls it and returns its result; only otherwise does it fall back to `list(__dict__)`.\n\n---\n*Found while running synthetic benchmark/compat suites; researched and reported by Claude on behalf of @youknowone.*\n","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-07-06T15:34:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/8231/RustPython/issues/8231"}
| 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:76a8f731-f58b-7666-8777-7b56fea9bcdc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9252:400B9:2A87953:3C9231D:6A59024C |
| html-safe-nonce | c83b9a2c807685087061a1d4f4af6c0bc29c9ae5101871251bd832cc517a23dc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MjUyOjQwMEI5OjJBODc5NTM6M0M5MjMxRDo2QTU5MDI0QyIsInZpc2l0b3JfaWQiOiI0ODkwNjU1OTc0NDI2NzM5Mjc3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4a258f9ccbbec1a97dee3e5b23de8e91e0bebe290f79ba9aa665e3a04167a428 |
| hovercard-subject-tag | issue:4820664337 |
| 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/RustPython/RustPython/8231/issue_layout |
| twitter:image | https://opengraph.githubassets.com/39a1095d89e5c388a361d53a67834eef953e4cce37f1c365b1169b30419836f1/RustPython/RustPython/issues/8231 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/39a1095d89e5c388a361d53a67834eef953e4cce37f1c365b1169b30419836f1/RustPython/RustPython/issues/8231 |
| og:image:alt | Summary A module-level __dir__ function (PEP 562) is ignored: dir(module) returns the raw __dict__ keys instead of calling the module's __dir__. # mod.py import sys def __dir__(): return ['gamma', ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | youknowone |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| 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 | 9842457e8d7e3b2e1635a63af28da35614845510 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width