Pathlib Abstract Base Classes
pathlib-abc is a Python library that provides abstract base classes (ABCs) for `pathlib.Path`-like objects. It serves as a backport and preview of `pathlib` functionality intended for future Python standard library releases, specifically designed to enable path implementations for non-local filesystems (e.g., archives, remote storage). The library is currently at version 0.5.2 and is under active development, requiring Python 3.9+.
Warnings
- gotcha The `pathlib-abc` classes (`JoinablePath`, `ReadablePath`, `WritablePath`) are currently not direct parent classes of the standard library `pathlib.Path` classes. Therefore, `isinstance(custom_path_object, pathlib.Path)` will return `False`.
- breaking These base classes are under active development and are a 'preview' of future Python functionality. Their interface and behavior might change in minor versions as they evolve, and they may eventually be integrated into the standard library's public API, potentially making this package a backport.
- gotcha The `pathlib` ABCs exist in Python 3.13+ within the *private* `pathlib._abc` module. Direct import or reliance on this private standard library module is discouraged and not officially supported.
Install
-
pip install pathlib-abc
Imports
- JoinablePath
from pathlib_abc import JoinablePath
- ReadablePath
from pathlib_abc import ReadablePath
- WritablePath
from pathlib_abc import WritablePath
Quickstart
from pathlib_abc import JoinablePath, PathInfo
class CustomPath(JoinablePath):
_parser = JoinablePath._pathmod.posixpath # Example: use POSIX path parsing
def __init__(self, path_str):
self._path = path_str
def __str__(self):
return self._path
@property
def info(self) -> PathInfo:
# In a real implementation, this would return actual file system info.
# For this example, we return a basic PathInfo instance.
return PathInfo(is_dir=True, is_file=False, is_symlink=False)
def _with_segments(self, segments):
return CustomPath(self._parser.join(*segments))
# Example usage:
base = CustomPath('/my/virtual/root')
file_path = base / 'folder' / 'document.txt'
print(f"Base path: {base}")
print(f"File path: {file_path}")
print(f"Parent of file_path: {file_path.parent}")
print(f"Name of file_path: {file_path.name}")