Skip to content

Extensions 🧩

The extensions module provides a base class for extending UPath functionality while maintaining compatibility with all filesystem implementations.

ProxyUPath

ProxyUPath

ProxyUPath(
    *args: JoinablePathLike,
    protocol: str | None = None,
    **storage_options: Any,
)

ProxyUPath base class

ProxyUPath should be used when you want to extend the UPath class interface with additional methods, but still want to support all supported upath implementations.

Source code in upath/extensions.py
115
116
117
118
119
120
121
def __init__(
    self,
    *args: JoinablePathLike,
    protocol: str | None = None,
    **storage_options: Any,
) -> None:
    self.__wrapped__ = UPath(*args, protocol=protocol, **storage_options)

Usage Example

ProxyUPath allows you to extend the UPath interface with additional methods while preserving compatibility with all supported filesystem implementations. It acts as a wrapper around any UPath instance.

Creating a Custom Extension

from upath import UPath
from upath.extensions import ProxyUPath

class MyCustomPath(ProxyUPath):
    """Custom path with additional functionality"""

    def custom_method(self) -> str:
        """Add your custom functionality here"""
        return f"Custom processing for: {self.path}"

    def enhanced_read(self) -> str:
        """Enhanced read with preprocessing"""
        content = self.read_text()
        # Add custom processing
        return content.upper()

# Use with any filesystem
s3_path = MyCustomPath("s3://bucket/file.txt")
local_path = MyCustomPath("/tmp/file.txt")
gcs_path = MyCustomPath("gs://bucket/file.txt")

# All standard UPath methods work
print(s3_path.exists())
print(local_path.parent)

# Always a subclass of your class
assert isinstance(s3_path, MyCustomPath)
assert isinstance(local_path, MyCustomPath)

# Plus your custom methods
print(s3_path.custom_method())
content = local_path.enhanced_read()

See Also 🔗