-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
bpo-24132: Add direct subclassing of PurePath/Path in pathlib #26906
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
Changes from all commits
710f577
a373969
35d8f78
bab67b4
67faae0
610f41e
a0c4998
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,7 +273,7 @@ Methods and properties | |
|
||
.. testsetup:: | ||
|
||
from pathlib import PurePath, PurePosixPath, PureWindowsPath | ||
from pathlib import PurePath, PurePosixPath, PureWindowsPath, Path | ||
|
||
Pure paths provide the following methods and properties: | ||
|
||
|
@@ -1229,6 +1229,56 @@ call fails (for example because the path doesn't exist). | |
.. versionchanged:: 3.10 | ||
The *newline* parameter was added. | ||
|
||
Subclassing and Extensibility | ||
----------------------------- | ||
|
||
Both :class:`PurePath` and :class:`Path` are directly subclassable and extensible as you | ||
see fit: | ||
|
||
>>> class MyPath(Path): | ||
... def my_method(self, *args, **kwargs): | ||
... ... # Platform agnostic implementation | ||
|
||
.. note:: | ||
Unlike :class:`PurePath` or :class:`Path`, instantiating the derived | ||
class will not generate a differently named class: | ||
|
||
.. doctest:: | ||
:pyversion: > 3.11 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is surprising to me: python docs document one version, so what is this for? |
||
:skipif: is_windows | ||
|
||
>>> Path('.') # On POSIX | ||
PosixPath('.') | ||
>>> MyPath('.') | ||
MyPath('.') | ||
|
||
Despite this, the subclass will otherwise match the class that would be | ||
returned by the factory on your particular system type. For instance, | ||
when instantiated on a POSIX system: | ||
|
||
.. doctest:: | ||
:pyversion: > 3.11 | ||
:skipif: is_windows | ||
|
||
>>> [Path('/dir').is_absolute, MyPath('/dir').is_absolute()] | ||
[True, True] | ||
>>> [Path().home().drive, MyPath().home().drive] | ||
['', ''] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A minor point: the lists here are a little bit distracting (we are looking at methods/properties, not working with lists). |
||
|
||
However on Windows, the *same code* will instead return values which | ||
apply to that system: | ||
|
||
.. doctest:: | ||
:pyversion: > 3.11 | ||
:skipif: is_posix | ||
|
||
>>> [Path('/dir').is_absolute(), MyPath('/dir').is_absolute()] | ||
[False, False] | ||
>>> [Path().home().drive, MyPath().home().drive] | ||
['C:', 'C:'] | ||
|
||
.. versionadded:: 3.11 | ||
|
||
Correspondence to tools in the :mod:`os` module | ||
----------------------------------------------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or IS_POSIX/IS_WINDOWS? I am unaware of any precedence for the naming convention here. However having the ability to skip tests depending on platform, gives one the ability to actually have sphinx doctest the code in the documentation rather than just skipping it with :: and trusting that it is correct (as is the current practice throughout much of pathlib's documentation).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesnt really matter in this snippet that’s invisible to readers of the doc 🙂
I think lower case looks fine.