Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pathlib.Path.from_uri() classmethod #107465

Closed
barneygale opened this issue Jul 30, 2023 · 1 comment
Closed

Add pathlib.Path.from_uri() classmethod #107465

barneygale opened this issue Jul 30, 2023 · 1 comment
Labels
topic-pathlib type-feature A feature request or enhancement

Comments

@barneygale
Copy link
Contributor

barneygale commented Jul 30, 2023

Feature or enhancement

Add pathlib.Path.from_uri() classmethod that creates a path objects from a 'file' URI, like file:///c:/windows. This method should accept RFC 8089 file: URIs, including variant forms.

Pitch

The proposed method is the counterpart of pathlib.Path.as_uri(). As we continue to open up pathlib for subclassing, user subclasses of path classes will begin to appear with their own as_uri() methods returning URIs like s3://, ftp://. These subclasses will likely also support parsing URIs to create paths. However, there is currently no defined interface for doing this in pathlib, and users will be tempted to accept URIs in initialisers, which produces a confusing interface. If pathlib instead defines a from_uri() classmethod, there is one clear and obvious method that subclasses may override.

Previous discussion

See https://discuss.python.org/t/make-pathlib-extensible/3428/136 and subsequent posts

Linked PRs

@barneygale barneygale added type-feature A feature request or enhancement topic-pathlib labels Jul 30, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Aug 4, 2023
This method supports file URIs (including variants) as described in
RFC 8089, such as URIs generated by `pathlib.Path.as_uri()` and
`urllib.request.pathname2url`.

The method is added to `Path` rather than `PurePath` because it uses
`os.fsdecode()`, and so its results vary from system to system. I intend to
deprecate `PurePath.as_uri()` and move it to `Path` for the same reason.
barneygale added a commit to barneygale/cpython that referenced this issue Aug 4, 2023
This method supports file URIs (including variants) as described in
RFC 8089, such as URIs generated by `pathlib.Path.as_uri()` and
`urllib.request.pathname2url`.

The method is added to `Path` rather than `PurePath` because it uses
`os.fsdecode()`, and so its results vary from system to system. I intend to
deprecate `PurePath.as_uri()` and move it to `Path` for the same reason.
barneygale added a commit to barneygale/cpython that referenced this issue Sep 2, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Sep 24, 2023
barneygale added a commit to barneygale/cpython that referenced this issue Sep 30, 2023
barneygale added a commit that referenced this issue Oct 1, 2023
This method supports file URIs (including variants) as described in RFC 8089, such as URIs generated by `pathlib.Path.as_uri()` and `urllib.request.pathname2url()`.

The method is added to `Path` rather than `PurePath` because it uses `os.fsdecode()`, and so its results vary from system to system. I intend to deprecate `PurePath.as_uri()` and move it to `Path` for the same reason.

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
@barneygale
Copy link
Contributor Author

Implemented in 3.13 / 15de493 / #107640

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-pathlib type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants
@barneygale and others