Title: Support for sharing state between pathlib subclasses · Issue #100479 · python/cpython · GitHub
Open Graph Title: Support for sharing state between pathlib subclasses · Issue #100479 · python/cpython
X Title: Support for sharing state between pathlib subclasses · Issue #100479 · python/cpython
Description: Feature or enhancement This enhancement proposes that we allow state to be shared between related instances of subclasses of pathlib.PurePath and pathlib.Path. Pitch Now that #68320 is resolved, users can subclass pathlib.PurePath and pa...
Open Graph Description: Feature or enhancement This enhancement proposes that we allow state to be shared between related instances of subclasses of pathlib.PurePath and pathlib.Path. Pitch Now that #68320 is resolved, us...
X Description: Feature or enhancement This enhancement proposes that we allow state to be shared between related instances of subclasses of pathlib.PurePath and pathlib.Path. Pitch Now that #68320 is resolved, us...
Opengraph URL: https://github.com/python/cpython/issues/100479
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support for sharing state between pathlib subclasses","articleBody":"# Feature or enhancement\r\n\r\nThis enhancement proposes that we allow state to be shared between related instances of subclasses of `pathlib.PurePath` and `pathlib.Path`.\r\n\r\n# Pitch\r\n\r\nNow that #68320 is resolved, users can subclass `pathlib.PurePath` and `pathlib.Path` directly:\r\n\r\n```python\r\nimport pathlib\r\n\r\nclass MyPath(pathlib.Path):\r\n def my_custom_method(self):\r\n pass\r\n\r\netc = MyPath('/etc')\r\netc_hosts = etc / 'hosts'\r\netc_hosts.my_custom_method()\r\n```\r\n\r\nHowever, some user implementations of classes - such as `TarPath` or `S3Path` - would require underlying `tarfile.TarFile` or `botocore.Resource` objects to be shared between path objects (*etc* and *etc_hosts* in the example above). Such sharing of resources is presently rather difficult, as there's no single instance method used to derive new path objects.\r\n\r\nThis feature request proposes that we add a new `PurePath.makepath()` method, which is called whenever one path object is derived from another, such as in `joinpath()`, `iterdir()`, etc. The default implementation of this method looks something like:\r\n\r\n```python\r\ndef makepath(self, *args):\r\n return type(self)(*args)\r\n```\r\n\r\nUsers may redefine this method in a subclass, in conjunction with a customized initializer:\r\n\r\n```python\r\nclass SessionPath(pathlib.Path):\r\n def __init__(self, *args, session_id):\r\n super().__init__(*args)\r\n self.session_id = session_id\r\n\r\n def makepath(self, *args):\r\n return type(self)(*args, session_id=self.session_id)\r\n\r\netc = SessionPath('/etc', session_id=42)\r\netc_hosts = etc / 'hosts'\r\nprint(etc_hosts.session_id) # 42\r\n```\r\n\r\nI propose the name \"makepath\" for this method due to its close relationship with the existing \"joinpath\" method: `a.joinpath(b) == a.makepath(a, b)`.\r\n\r\n# Performance\r\n\r\nedit: this change has been taken care of elsewhere, and so implementing this feature request should no longer have much affect on performance.\r\n\r\n~~This change will affect the performance of some pathlib operations, because it requires us to remove the `_from_parsed_parts()` constructor, which is an internal optimization used in cases where path parsing and normalization can be skipped (for example, in the `parents` sequence). I suggest that, within the standard library, pathlib is not a particularly performance-sensitive module -- few folks reach to pathlib for reason of speed. Within pathlib itself, the savings from optimizing these \"pure\" methods are usually drowned out by the I/O costs of \"impure\" methods. With the appeal of this feature in mind, I believe the performance cost is justified.~~\r\n\r\n~~However, if the performance degradation is considered unacceptable, there's a possible alternative: add a *normalize* keyword argument to the path initializer and to `makepath()`. This would require some serious internal surgery to make work, and might be difficult to communicate to users, so at this stage it's not my preferred route forward.~~\r\n\r\n# Previous discussion\r\n\r\nhttps://discuss.python.org/t/make-pathlib-extensible/3428/47 (and replies)\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-100481\r\n* gh-103975\n* gh-104215\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/barneygale","@type":"Person","name":"barneygale"},"datePublished":"2022-12-23T23:07:23.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":10},"url":"https://github.com/100479/cpython/issues/100479"}
| 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:4207e81a-fc73-3506-99c8-5f9271187b44 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E8CE:1CD637:2AD3C:39EA3:696963FE |
| html-safe-nonce | b030d28605a84433ff440214bc66bb0900b19fe4225ca0beee9b90c7135edde9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOENFOjFDRDYzNzoyQUQzQzozOUVBMzo2OTY5NjNGRSIsInZpc2l0b3JfaWQiOiIzNjI4MjYzMTg4MDE3NTM4MDQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e8266ec69dacb8494d34259d525933d2cd81460c4592e916e8389dc9e4e1d8a2 |
| hovercard-subject-tag | issue:1509838808 |
| 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/100479/issue_layout |
| twitter:image | https://opengraph.githubassets.com/24ebb0fcdcd54f5e46b0d69619dff0a2cb0db8c8d94494d0d82e23f588062020/python/cpython/issues/100479 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/24ebb0fcdcd54f5e46b0d69619dff0a2cb0db8c8d94494d0d82e23f588062020/python/cpython/issues/100479 |
| og:image:alt | Feature or enhancement This enhancement proposes that we allow state to be shared between related instances of subclasses of pathlib.PurePath and pathlib.Path. Pitch Now that #68320 is resolved, us... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | barneygale |
| hostname | github.com |
| expected-hostname | github.com |
| None | 48487c1ad776a7975b7132d95f4240ff3ae37cd5b8e3cb597102a4edb76738f1 |
| 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 | 669463fcc54773a88c1f5a44eef6b99a5504b9c7 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width