Title: gh-127337: Fix importlib.resources for legacy ResourceReader loaders by pikammmmm · Pull Request #154919 · python/cpython · GitHub
Open Graph Title: gh-127337: Fix importlib.resources for legacy ResourceReader loaders by pikammmmm · Pull Request #154919 · python/cpython
X Title: gh-127337: Fix importlib.resources for legacy ResourceReader loaders by pikammmmm · Pull Request #154919 · python/cpython
Description: CompatibilityFiles.SpecPath, .ChildPath and .OrphanPath each defined joinpath() with a single required argument, but importlib.resources.abc.Traversable declares joinpath(*descendants) and documents that passing no descendants returns self. Since 3.13 the functional API is implemented on top of files(anchor).joinpath(*path_names), so any call that passes no path names reaches joinpath() with no arguments and raises TypeError for a package whose loader provides only a legacy ResourceReader: 3.12.13 3.13.13 / 3.14.4 / 3.15.0b1 this PR contents(FakeModule) ['NAME'] TypeError ['NAME'] is_resource(FakeModule) n/a — required a name TypeError False (measured with the reproducer from the issue; 3.12 predates _functional.py, whose one-name-per-call predecessor never routed through joinpath()) This is the fix @jaraco sketched in the issue, extended to ChildPath and OrphanPath as he suggested it might need to be. It also fixes the latent multi-argument case, broken ever since the protocol gained *descendants in 3.11 without _adapters.py being updated — as @FFY00 diagnosed, "the CompatibilityFiles implementation is bad, it can only ever take one argument, and now it needs to be able to take any number of arguments." One deviation from the sketch: it used '/'.join(descendants) for SpecPath, which would make files.joinpath('a', 'b') a single ChildPath named a/b, while files / 'a' / 'b' is an OrphanPath('a', 'b'). The Traversable.joinpath docs present the chained single-argument form as the compatibility equivalent of the multi-argument form for implementations that predate it, so this PR applies descendants one at a time and the two forms agree. That also avoids '/'.join raising TypeError on a PathLike segment which single-argument joinpath() accepts today. CompatibilityFiles still does not meaningfully support os.PathLike segments; that is unchanged here and out of scope. Tests Seven new tests in test_compatibilty_files.py: zero-argument joinpath() on all three path classes (with and without a reader), the multi-argument/chained equivalence, and contents()/is_resource() through the functional API. Negative control: with _adapters.py reverted, all seven new tests fail and the 16 pre-existing tests in the file still pass. All 221 tests in test_importlib.resources pass (1 skipped). I test without a local C build — checkout Lib/ on a matching installed interpreter — which yields 88 unrelated pre-existing failures in test_importlib.metadata/frozen/extension. That set is identical with and without this patch, and none of them are in resources. Sibling fix for the same protocol violation in simple.py: #145263. @FFY00 — @jaraco assigned this to you in December 2024; happy to close this if you would rather take it yourself. Issue: gh-127337
Open Graph Description: CompatibilityFiles.SpecPath, .ChildPath and .OrphanPath each defined joinpath() with a single required argument, but importlib.resources.abc.Traversable declares joinpath(*descendants) and document...
X Description: CompatibilityFiles.SpecPath, .ChildPath and .OrphanPath each defined joinpath() with a single required argument, but importlib.resources.abc.Traversable declares joinpath(*descendants) and document...
Opengraph URL: https://github.com/python/cpython/pull/154919
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:2b1e541a-4d79-2f7f-9632-5fb6dacb0168 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | E1D8:395E6B:EF46:146E8:6B004CDF |
| html-safe-nonce | c59fa05b3dffd51c5d67f711fb79e37795e6dd2e57c714bb65eb6abb0c1ff2fa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMUQ4OjM5NUU2QjpFRjQ2OjE0NkU4OjZCMDA0Q0RGIiwidmlzaXRvcl9pZCI6IjQ5NzkzNTk3MjM1NDg1OTMzNzUiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5009b33ac54b62ddd0240ce24a68d1b686a79ffdbc7c4fb528b9e1a002458482 |
| hovercard-subject-tag | pull_request:4169031435 |
| 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/python/cpython/pull/154919/files |
| twitter:image | https://avatars.githubusercontent.com/u/124257101?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/124257101?s=400&v=4 |
| og:image:alt | CompatibilityFiles.SpecPath, .ChildPath and .OrphanPath each defined joinpath() with a single required argument, but importlib.resources.abc.Traversable declares joinpath(*descendants) and document... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width