With the release of Python 3.12, pathlib.Path
can now be subclassed. I want to create a subclass CustomPath(Path)
for non-os
environments (ftp, sftp, s3 storage, etc.), meaning I have to re-implemented (almost) all methods.
I want to make sure that CustomPath
is only using methods which are defined in the subclass, to prevent accidentally using methods from the parent Path
class. In order to do this, I want to use only the interface (abstract class) of Path
. (Since Path
may be updated to include new methods beyond my control.)
What is the most pythonic way of doing this? (It might be the case that it is not appropriate to subclass at all.)
Here's an example of expected behavior:
class S3Path(pathlib.Path):
@classmethod
def from_connection(cls, ...):
... # custom implementation
def read_text(self, encoding=None, errors=None):
... # custom implementation
s3path = S3Path.from_connection(...)
text = s3path.read_text()
s3path.write_text(text) # should raise NotImplementedError
I am the current pathlib maintainer. I'm looking to add pathlib ABCs in future, and in fact they already exist (privately) in CPython. For the time being you may wish to use the "pathlib-abc" PyPI package, which is a straight copy-paste of the private functionality.