In Python 3.12 we've made pathlib.PurePath subclassable - see #31691.
A significant wrinkle is that subclasses inherit an __fspath__() method, which can lead to unexpected behaviour when these "pure" paths are passed to filesystem APIs:
import pathlib
class TarPath(pathlib.PurePath):
...
readme = TarPath('README.md', tarfile=...)
with open(readme) as f:
...
The with open(readme) as f: line should throw a TypeError, but instead it attempts to open a file called README.md in the current working directory (!). This sort of thing makes subclasses that implement purely virtual filesystems difficult to use safely.
Linked PRs
Reactions are currently unavailable
In Python 3.12 we've made pathlib.PurePath subclassable - see #31691.
A significant wrinkle is that subclasses inherit an __fspath__() method, which can lead to unexpected behaviour when these "pure" paths are passed to filesystem APIs:
The with open(readme) as f: line should throw a TypeError, but instead it attempts to open a file called README.md in the current working directory (!). This sort of thing makes subclasses that implement purely virtual filesystems difficult to use safely.
Linked PRs