fsspec / universal_pathlib

pathlib api extended to use fsspec backends
MIT License
251 stars 44 forks source link

UPath instance check #29

Closed brl0 closed 3 years ago

brl0 commented 3 years ago

I think it would be nice if something like this worked:

from upath import UPath

path = UPath(".")
assert isinstance(path, UPath)  # this currently returns false

This issue occurs for local and remote paths, so for pathlib and UniversalPath objects.

Thanks to UPath and various implementations inheriting from pathlib.Path, here is one basic approach that seems to allow this to work:

from abc import ABCMeta

class UPathMeta(ABCMeta):
    def __instancecheck__(self, instance):
        return isinstance(instance, Path)

    def __subclasscheck__(self, subclass):
        return issubclass(subclass, Path)

class UPath(Path, metaclass=UPathMeta):
    ...

I am working to submit a PR for #25 and #26, so I am planning to also include this change, but can take it out if undesired or preferred in a separate PR.