Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strictly follow PEP3119 for AnyPath __subclasscheck__ implementation #247

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions cloudpathlib/anypath.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class AnyPathMeta(type):
[PEP 3119](https://www.python.org/dev/peps/pep-3119/#overloading-isinstance-and-issubclass)."""

def __instancecheck__(cls, inst):
return isinstance(inst, CloudPath) or isinstance(inst, Path)
return any(cls.__subclasscheck__(c) for c in {type(inst), inst.__class__})

def __subclasscheck__(cls, sub):
return issubclass(sub, CloudPath) or issubclass(sub, Path)
candidates = cls.__dict__.get("__subclass__", set()) | {cls}
return any(c in candidates for c in sub.mro())


class AnyPath(metaclass=AnyPathMeta):
Expand All @@ -28,6 +29,8 @@ class AnyPath(metaclass=AnyPathMeta):
constructor and dispatch to CloudPath or Path.
"""

__subclass__ = {CloudPath, Path}

def __new__(cls, *args, **kwargs) -> Union[CloudPath, Path]: # type: ignore
try:
return CloudPath(*args, **kwargs) # type: ignore
Expand Down