Title: Un-deprecate functional API for importlib resources & add subdirectory support · Issue #116608 · python/cpython · GitHub
Open Graph Title: Un-deprecate functional API for importlib resources & add subdirectory support · Issue #116608 · python/cpython
X Title: Un-deprecate functional API for importlib resources & add subdirectory support · Issue #116608 · python/cpython
Description: Feature or enhancement Proposal: The importlib.resources functions {open,read}_{text,binary}, path, is_resource and contents, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed by quite a few users. They provide a si...
Open Graph Description: Feature or enhancement Proposal: The importlib.resources functions {open,read}_{text,binary}, path, is_resource and contents, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed...
X Description: Feature or enhancement Proposal: The importlib.resources functions {open,read}_{text,binary}, path, is_resource and contents, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed...
Opengraph URL: https://github.com/python/cpython/issues/116608
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Un-deprecate functional API for importlib resources \u0026 add subdirectory support","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\n\r\nThe `importlib.resources` functions `{open,read}_{text,binary}`, `path`, `is_resource` and `contents`, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed by quite a few users.\r\nThey provide a simple API for simple tasks, while the full-featured `Traversable` API is better suited for complex ones -- especially for implementing new resources-aware loaders.\r\n\r\nI'm now in a position where I can add these functions back and support them.\r\n\r\nTheir main drawback -- not allowing subdirectories -- can be solved by taking multiple path components as positional arguments, for example:\r\n```python\r\nimportlib.resources.read_text('modulename', 'subdirectory', 'subsubdir', 'resource.txt')\r\n```\r\n\r\nThe additional arguments (*encoding* and *errors*) would become keyword-only.\r\n\r\n---\r\n\r\nThere is a wrinkle in this: in Python 3.9-3.11, the above would mean:\r\n```python\r\nimportlib.resources.read_text(\r\n 'modulename', 'subdirectory',\r\n encoding='subsubdir',\r\n errors='resource.txt',\r\n)\r\n```\r\n~~I believe that this is acceptable, since:~~\r\n- ~~pragmatically: typical file names do not match typical encoding/errorhandler names~~\r\n- ~~lawyerly: the functions have already been deprecated for 2 releases; no one is using them now, right?~~\r\n\r\n~~However, if this is a problem, I can~~\r\n\r\n[edit: This is solved by:]\r\n\r\n- make the `encoding` argument *required* if a text-reading function more than one path component is given.\r\n- plan to lift this limitation around 3.15.\r\n\r\n\r\n```python\r\nimportlib.resources.read_text(\r\n 'modulename', 'subdirectory', 'subsubdir', 'resource.txt',\r\n encoding='utf-8',\r\n)\r\nimportlib.resources.read_text('modulename', 'resource.txt') # OK\r\nimportlib.resources.read_text('modulename', 'subdirectory', 'utf-8') # error\r\n```\r\n\r\n\r\n### Has this already been discussed elsewhere?\r\n\r\nI have already discussed this feature proposal on Discourse\r\n\r\n### Links to previous discussion of this feature:\r\n\r\nhttps://discuss.python.org/t/deprecating-importlib-resources-legacy-api/11386/29\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-116609\r\n* gh-117569\n* gh-123028\n* gh-123051\n* gh-123072\n* gh-123074\n* gh-132206\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/encukou","@type":"Person","name":"encukou"},"datePublished":"2024-03-11T16:03:03.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":28},"url":"https://github.com/116608/cpython/issues/116608"}
| 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:f0710c20-e003-f0d4-8b49-e1702d2df8c5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCE6:4B247:BA330:105160:696A0AA5 |
| html-safe-nonce | ce40efb62d3d29e47fdfb756c48235faf7a3facf7a9816161f1a9595737c5416 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0U2OjRCMjQ3OkJBMzMwOjEwNTE2MDo2OTZBMEFBNSIsInZpc2l0b3JfaWQiOiIxNzE3MzI0OTU4MzM0MDYxMjIxIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f8be8dfc39e697800d903f807a81b9d5e4ec6c5b9210d0dbb28aa1292c66ea22 |
| hovercard-subject-tag | issue:2179510555 |
| 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/116608/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9064eea60ed15db3f3d942405d60b99081f20df5167deff77e8568388d211542/python/cpython/issues/116608 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9064eea60ed15db3f3d942405d60b99081f20df5167deff77e8568388d211542/python/cpython/issues/116608 |
| og:image:alt | Feature or enhancement Proposal: The importlib.resources functions {open,read}_{text,binary}, path, is_resource and contents, deprecated in 3.11 and removed in 3.13 alphas, are, anecdotally, missed... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | encukou |
| hostname | github.com |
| expected-hostname | github.com |
| None | 699227a00bbb7fe1eec276d2ae1c3a93068bc5ba483bd9dc4b2a27a8f4f2f595 |
| 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 | 7266b2d935baa1c6474b16dd9feaa5ca30607261 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width