Skip to content

Cloud paths should be PathLike #47

Closed

Description

@pjbull

Lots of Python codebases use os.PathLike to indicate a file that can be read/opened/etc. We want CloudPath to be treated like a path in these instances so that when it is passed to other libraries they call trust its .open() method and work with the file content as expected.

Right now

import os
from pathlib import Path

p = Path("file.txt")
isinstance(p, os.PathLike)  # True

from cloudpathlib import S3Path
s3p = S3Path("s3://bucket/file.txt")
isinstance(s3p, os.PathLike)  # False

In a previous iteration, we had inherited from the PurePath type as below, which I believe made this work. There may be other implementation options as well:

class CloudPath(type(PurePath()):

Activity

  1. jayqi commented on Sep 2, 2020

    @jayqi
    Member

    This actually looks like it's pretty easy. All we have to do is add an __fspath__ instance method.

    https://docs.python.org/3/library/os.html#os.PathLike

    >>> class Foo:
    ...     def __fspath__(self):
    ...         return ""
    ... 
    >>> foo = Foo()
    >>> foo.__fspath__()
    ''
    >>> isinstance(foo, os.PathLike)
    True
    
  2. added
    good first issue
    on Sep 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions