Title: module: honor module-level __dir__ (PEP 562) by JaceJung-dev · Pull Request #8271 · RustPython/RustPython · GitHub
Open Graph Title: module: honor module-level __dir__ (PEP 562) by JaceJung-dev · Pull Request #8271 · RustPython/RustPython
X Title: module: honor module-level __dir__ (PEP 562) by JaceJung-dev · Pull Request #8271 · RustPython/RustPython
Description: Closes #8231 This PR follows our AI policy Summary A module-level __dir__ function (PEP 562) was ignored: dir(module) returned the raw __dict__ keys instead of calling the module's __dir__. # mod.py def __dir__(): return ['gamma', 'alpha', 'beta'] # before: dir(mod) -> ['__builtins__', '__cached__', '__dir__', ...] # after: dir(mod) -> ['alpha', 'beta', 'gamma'] # matches CPython (dir() sorts) Cause PyModule::__dir__ (crates/vm/src/builtins/module.rs) unconditionally returned the module dict's keys, with no check for a module-level __dir__. Fix This ports the behavior of CPython's module_dir() in Objects/moduleobject.c, which is the reference implementation for this change: look up __dir__ in the module's __dict__ first and, if present, call it and return its result; otherwise fall back to list(__dict__). The lookup is a direct dict access, so neither the module-level __getattr__ nor the type's own __dir__ slot is involved. A non-callable or wrongly-signed __dir__ raises TypeError, matching CPython. The @unittest.expectedFailure # TODO: RUSTPYTHON markers on test_module_dir and test_module_dir_errors are removed since both now pass. Side effect Honoring a module-level __dir__ also fixes dir() for stdlib modules that define one, which flips two previously-failing tests to passing. Their stale @unittest.expectedFailure # TODO: RUSTPYTHON markers are removed as well (otherwise they would be reported as unexpected successes and fail CI): test_zoneinfo.test_dir_contains_all — zoneinfo.__dir__ makes dir(zoneinfo) contain everything in __all__. test_support.test_check__all__ — support.check__all__ iterates dir(module); unittest.__dir__ now surfaces IsolatedAsyncioTestCase (declared in unittest.__all__ but lazily loaded via PEP 562), so the check matches __all__. Test Verified locally: cargo run --release -- -m test test_module -v → SUCCESS (run=39) (test_module_dir / test_module_dir_errors ... ok) cargo run --release -- -m test test_zoneinfo → SUCCESS (test_dir_contains_all now passes) cargo run --release -- -m test test_support → SUCCESS (test_check__all__ now passes via dir(unittest)) cargo run --release -- -m test test_unittest test_concurrent_futures → SUCCESS (the other stdlib modules with a module-level __dir__; no regressions) Assisted-by: Claude Code:claude-opus-4-8 Summary by CodeRabbit New Features Modules can now define a custom __dir__ function to control the names returned when inspecting available attributes. Existing default behavior remains unchanged when no custom __dir__ is provided.
Open Graph Description: Closes #8231 This PR follows our AI policy Summary A module-level __dir__ function (PEP 562) was ignored: dir(module) returned the raw __dict__ keys instead of calling the module's __dir__. #...
X Description: Closes #8231 This PR follows our AI policy Summary A module-level __dir__ function (PEP 562) was ignored: dir(module) returned the raw __dict__ keys instead of calling the module's __dir_...
Opengraph URL: https://github.com/RustPython/RustPython/pull/8271
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:730bd2d9-8602-ab0a-67ff-101824cc8a19 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | B006:935FE:1265705:1A9F6CD:6A58BB70 |
| html-safe-nonce | 50e120c6e804223bb4ba5e006000eeeea318eccd24e10fa9e9e471e6ac1826ec |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMDA2OjkzNUZFOjEyNjU3MDU6MUE5RjZDRDo2QTU4QkI3MCIsInZpc2l0b3JfaWQiOiIxNTAxMzAxNzM5ODgyMDY4ODQ4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | a533f36ef160dec9d3de6d08f24eb55cf85a5770103e2605baef9c77f983b6ec |
| hovercard-subject-tag | pull_request:4044572601 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/RustPython/RustPython/pull/8271/files |
| twitter:image | https://avatars.githubusercontent.com/u/192051293?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/192051293?s=400&v=4 |
| og:image:alt | Closes #8231 This PR follows our AI policy Summary A module-level __dir__ function (PEP 562) was ignored: dir(module) returned the raw __dict__ keys instead of calling the module's __dir__. #... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width