-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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-100479: Add optional blueprint
argument to pathlib.PurePath
#100481
Changes from 19 commits
a6fdd0e
b061747
99eb8b1
4759d01
ef6f4c3
595b8ae
dcfe70a
117fe4b
e75dedc
5a6bd3f
f2f1048
3c172fb
4637109
ae48454
e7a8fe3
7f12faa
687c764
d7e326a
a65d499
d4b15d7
958b183
1e10188
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 |
---|---|---|
|
@@ -96,7 +96,7 @@ Pure path objects provide path-handling operations which don't actually | |
access a filesystem. There are three ways to access these classes, which | ||
we also call *flavours*: | ||
|
||
.. class:: PurePath(*pathsegments) | ||
.. class:: PurePath(*pathsegments, template=None) | ||
|
||
A generic class that represents the system's path flavour (instantiating | ||
it creates either a :class:`PurePosixPath` or a :class:`PureWindowsPath`):: | ||
|
@@ -150,23 +150,49 @@ we also call *flavours*: | |
to ``PurePosixPath('bar')``, which is wrong if ``foo`` is a symbolic link | ||
to another directory) | ||
|
||
The optional *template* argument may provide another path object. It is | ||
supplied whenever a new path object is created from an existing one, such | ||
as in :attr:`parent` or :meth:`relative_to`. Subclasses may use this to | ||
pass information between path objects. For example:: | ||
|
||
from pathlib import PurePosixPath | ||
|
||
class MyPath(PurePosixPath): | ||
def __init__(self, *pathsegments, template=None, session_id=None): | ||
super().__init__(*pathsegments, template=template) | ||
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. Should we add a test to make sure diamond inheritance works? 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. I'm not sure. There's only one place in 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. Perhaps a hidden |
||
if template: | ||
self.session_id = template.session_id | ||
else: | ||
self.session_id = session_id | ||
barneygale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
etc = MyPath('/etc', session_id=42) | ||
hosts = etc / 'hosts' | ||
print(hosts.session_id) # 42 | ||
|
||
.. note:: | ||
The classes provided in this module ignore the *template* argument. | ||
barneygale marked this conversation as resolved.
Show resolved
Hide resolved
|
||
It is there purely as a hook for user-defined subclasses. | ||
|
||
.. versionadded:: 3.12 | ||
The *template* argument. | ||
|
||
Pure path objects implement the :class:`os.PathLike` interface, allowing them | ||
to be used anywhere the interface is accepted. | ||
|
||
.. versionchanged:: 3.6 | ||
Added support for the :class:`os.PathLike` interface. | ||
|
||
.. class:: PurePosixPath(*pathsegments) | ||
.. class:: PurePosixPath(*pathsegments, template=None) | ||
|
||
A subclass of :class:`PurePath`, this path flavour represents non-Windows | ||
filesystem paths:: | ||
|
||
>>> PurePosixPath('/etc') | ||
PurePosixPath('/etc') | ||
|
||
*pathsegments* is specified similarly to :class:`PurePath`. | ||
*pathsegments* and *template* are specified similarly to :class:`PurePath`. | ||
|
||
.. class:: PureWindowsPath(*pathsegments) | ||
.. class:: PureWindowsPath(*pathsegments, template=None) | ||
|
||
A subclass of :class:`PurePath`, this path flavour represents Windows | ||
filesystem paths, including `UNC paths`_:: | ||
|
@@ -176,7 +202,7 @@ we also call *flavours*: | |
>>> PureWindowsPath('//server/share/file') | ||
PureWindowsPath('//server/share/file') | ||
|
||
*pathsegments* is specified similarly to :class:`PurePath`. | ||
*pathsegments* and *template* are specified similarly to :class:`PurePath`. | ||
|
||
.. _unc paths: https://en.wikipedia.org/wiki/Path_(computing)#UNC | ||
|
||
|
@@ -530,10 +556,10 @@ Pure paths provide the following methods and properties: | |
unintended effects. | ||
|
||
|
||
.. method:: PurePath.joinpath(*other) | ||
.. method:: PurePath.joinpath(*pathsegments) | ||
|
||
Calling this method is equivalent to combining the path with each of | ||
the *other* arguments in turn:: | ||
the given *pathsegments* in turn:: | ||
|
||
>>> PurePosixPath('/etc').joinpath('passwd') | ||
PurePosixPath('/etc/passwd') | ||
|
@@ -690,7 +716,7 @@ Concrete paths are subclasses of the pure path classes. In addition to | |
operations provided by the latter, they also provide methods to do system | ||
calls on path objects. There are three ways to instantiate concrete paths: | ||
|
||
.. class:: Path(*pathsegments) | ||
.. class:: Path(*pathsegments, template=None) | ||
|
||
A subclass of :class:`PurePath`, this class represents concrete paths of | ||
the system's path flavour (instantiating it creates either a | ||
|
@@ -699,27 +725,27 @@ calls on path objects. There are three ways to instantiate concrete paths: | |
>>> Path('setup.py') | ||
PosixPath('setup.py') | ||
|
||
*pathsegments* is specified similarly to :class:`PurePath`. | ||
*pathsegments* and *template* are specified similarly to :class:`PurePath`. | ||
|
||
.. class:: PosixPath(*pathsegments) | ||
.. class:: PosixPath(*pathsegments, template=None) | ||
|
||
A subclass of :class:`Path` and :class:`PurePosixPath`, this class | ||
represents concrete non-Windows filesystem paths:: | ||
|
||
>>> PosixPath('/etc') | ||
PosixPath('/etc') | ||
|
||
*pathsegments* is specified similarly to :class:`PurePath`. | ||
*pathsegments* and *template* are specified similarly to :class:`PurePath`. | ||
|
||
.. class:: WindowsPath(*pathsegments) | ||
.. class:: WindowsPath(*pathsegments, template=None) | ||
|
||
A subclass of :class:`Path` and :class:`PureWindowsPath`, this class | ||
represents concrete Windows filesystem paths:: | ||
|
||
>>> WindowsPath('c:/Program Files/') | ||
WindowsPath('c:/Program Files') | ||
|
||
*pathsegments* is specified similarly to :class:`PurePath`. | ||
*pathsegments* and *template* are specified similarly to :class:`PurePath`. | ||
|
||
You can only instantiate the class flavour that corresponds to your system | ||
(allowing system calls on non-compatible path flavours could lead to | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Add optional *template* argument to :class:`pathlib.PurePath` and | ||
:class:`~pathlib.Path`. This argument is supplied whenever a derivative path | ||
is created, such as from :attr:`pathlib.PurePath.parent`. Subclasses may use | ||
to pass information to derivative paths. Patch by Barney Gale. |
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.
Does it make sense to specify that
template: Self | None
here? I.e. that if template is not None, it will be an instance of the current (user-defined) class.A