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

GH-100502: Add pathlib.PurePath.pathmod attribute #106533

Merged
merged 7 commits into from
Jul 19, 2023

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Jul 7, 2023

This class attribute stores the implementation of os.path used for low-level path operations: either posixpath or ntpath.


📚 Documentation preview 📚: https://cpython-previews--106533.org.readthedocs.build/

This instance attribute stores the implementation of `os.path` used for
low-level path operations: either `posixpath` or `ntpath`.

The `PurePath` and `Path` initialisers gain a *flavour* keyword-only
argument. This argument is not available in the Posix- and Windows-specific
subclasses, and as the `PurePath` and `Path` classes are not directly
instantiable, it is available therefore only in user subclasses of
`PurePath` and `Path`. Such subclasses may determine their flavour in
`__init__()` and supply the flavour to `super()`.
@barneygale barneygale marked this pull request as ready for review July 7, 2023 21:32
Doc/library/pathlib.rst Outdated Show resolved Hide resolved
@barneygale barneygale changed the title GH-100502: Add pathlib.PurePath.flavour attribute GH-100502: Add pathlib.PurePath.pathmod attribute Jul 9, 2023
@barneygale barneygale requested a review from merwok July 14, 2023 21:13
@barneygale
Copy link
Contributor Author

Thank you for the review, @merwok!

@Schamper
Copy link

Schamper commented Jan 4, 2024

Pardon the bump, but I figured this PR might be a good place to ask. @barneygale is there a specific reason why e.g. absolute(), is_mount(), etc use os.path with this patch instead of self.pathmod (or self._pathmod with the revert)? Especially since before this change they did use self._flavour. Using self._pathmod there would make it easier for custom subclasses to provide a different implementation.

@barneygale
Copy link
Contributor Author

barneygale commented Jan 4, 2024

os.path is the only valid value for pathmod in subclasses of Path, because Path is specifically concerned with the local filesystem. Many Path methods call os functions which can't be replaced by swapping out pathmod, and so it seemed silly to support customizing a small handful of other methods via pathmod. Instead, I think you should override Path methods as you need, and leave pathmod alone. Does that make sense?

Alternatively, if your subclass is not concerned with the local filesystem, you could subclass from pathlib_abc.PathBase, which doesn't use os.path: https://pypi.org/project/pathlib-abc/

@Schamper
Copy link

Schamper commented Jan 4, 2024

Thanks for the explanation, that makes sense. Curiosity satisfied :) I must admit that our use-case and restrictions are quite unique, overriding the methods as we need is indeed what we do now.

Thanks for linking pathlib_abc, I didn't know about that and definitely looks useful to look into for the future!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants