Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
bpo-24132: Document direct subclassing of PurePath/Path in pathlib
Add documentation highlighting the new direct subclass functionality of
PurePath and Path. Also add new is_posix & is_windows doctest conditions
so that system specific doctest code can still be tested instead of just
being taken as a literal block and always skipped.
  • Loading branch information
kfollstad committed Jun 24, 2021
commit a0c4998ffd2fdb32aa8901a7ba872a4d9ddaabba
4 changes: 4 additions & 0 deletions Doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import _tkinter
except ImportError:
_tkinter = None
from os import name as system_name
is_posix = system_name == 'posix'
is_windows = system_name == 'nt'
del system_name

@kfollstad kfollstad Jun 25, 2021 •

Copy link
Copy Markdown
Contributor Author

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).

Copy link
Copy Markdown
Member

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.

'''

manpages_url = 'https://manpages.debian.org/{path}'
Expand Down
52 changes: 51 additions & 1 deletion Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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?
(also > and not >=?)

: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]
['', '']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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).
Could use separate lines, or >>> Path().home().drive, MyPath().home().drive (which will have parens in the output but that’s fine, we’re used to commas being sometimes multiple things, sometimes a tuple object).


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
-----------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ Matt Fleming
Hernán Martínez Foffani
Benjamin Fogle
Artem Fokin
Kevin Follstad
Arnaud Fontaine
Michael Foord
Amaury Forgeot d'Arc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add a mechanism and support for direct subclassing of :class:`PurePath`
and :class:`Path` in pathlib.