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-110109: Add pathlib._PurePathBase #110670

Merged
merged 15 commits into from
Dec 8, 2023

Conversation

barneygale
Copy link
Contributor

@barneygale barneygale commented Oct 11, 2023

Add private pathlib._PurePathBase class: a private superclass of both PurePath and _PathBase. Unlike PurePath, it does not define any of these special methods: __fspath__, __bytes__, __reduce__, __hash__, __eq__, __lt__, __le__, __gt__, __ge__. Its initializer and path joining methods accept only strings, not os.PathLike objects more broadly.

This is important for supporting virtual paths: user subclasses of _PathBase that provide access to archive files, FTP servers, etc. In these classes, the above methods should be implemented by users only as appropriate, with due consideration for the hash/equality of any backing objects, such as file objects or sockets.

Add private `pathlib._PurePathBase` class: a private superclass of both
`PurePath` and `_PathBase`. Unlike `PurePath`, it does not define any of
these special methods: `__fspath__`, `__bytes__`, `__reduce__`, `__hash__`,
`__eq__`, `__lt__`, `__le__`, `__gt__`, `__ge__`.

This is important for supporting *virtual paths*: user subclasses of
`_PathBase` that provide access to archive files, FTP servers, etc. In
these classes, the above methods should be implemented by users only as
appropriate, with due consideration for the hash/equality of any backing
objects, such as file objects or sockets.
@barneygale
Copy link
Contributor Author

FYI @AlexWaygood. We avoided adding this class previously with your clever suggestion to set __fspath__ and __bytes__ to None in PathBase, and then restore their values in Path. (see #106082, #106337).

But I'm increasingly convinced that this extra class is needed after all - there are too many PurePath magic methods which shouldn't be inherited by PathBase. Hence this PR adds a new superclass.

@barneygale
Copy link
Contributor Author

An inheritance diagram:

image

@barneygale
Copy link
Contributor Author

barneygale commented Nov 13, 2023

Quite a lot of this patch is re-ordering methods. I've split that stuff off into a preparatory PR, which will help make this PR easier to review:

#112012

@barneygale
Copy link
Contributor Author

Once this lands, I plan to:

  1. Move _PurePathBase and _PathBase into a new pathlib._abc module (likewise their tests)
  2. Copy-paste that code into a pathlib_abc PyPI package, dropping the class underscore prefixes
  3. Engage with the community and iterate on pathlib_abc (regularly syncing changes back to CPython) for at least a few months, more likely a couple years
  4. Drop the class underscore prefixes in CPython; make pathlib_abc a backport-only package.

It's going to be a while before PurePathBase sees official public release in CPython, so I reckon we don't need to get its interface perfect in this PR. Hopefully that makes it less intimidating to review.

@barneygale barneygale removed the request for review from AlexWaygood December 8, 2023 17:29
@barneygale
Copy link
Contributor Author

Given that this change is blocking work on pathlib ABCs, and should have no observable effects on any public APIs, I'm going to merge it without waiting any longer for review.

If that doesn't seem right to anyone, please let me know and I can revert it. I'm never quite sure where to draw the line on these things.

@barneygale barneygale merged commit 76929fd into python:main Dec 8, 2023
29 checks passed
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
Add private `pathlib._PurePathBase` class: a private superclass of both `PurePath` and `_PathBase`. Unlike `PurePath`, it does not define any of these special methods: `__fspath__`, `__bytes__`, `__reduce__`, `__hash__`, `__eq__`, `__lt__`, `__le__`, `__gt__`, `__ge__`. Its initializer and path joining methods accept only strings, not os.PathLike objects more broadly.

This is important for supporting *virtual paths*: user subclasses of `_PathBase` that provide access to archive files, FTP servers, etc. In these classes, the above methods should be implemented by users only as appropriate, with due consideration for the hash/equality of any backing objects, such as file objects or sockets.
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.

None yet

1 participant