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

Cloud paths should be PathLike #47

Closed
pjbull opened this issue Aug 28, 2020 · 1 comment · Fixed by #72
Closed

Cloud paths should be PathLike #47

pjbull opened this issue Aug 28, 2020 · 1 comment · Fixed by #72
Labels
good first issue Good for newcomers

Comments

@pjbull
Copy link
Member

pjbull commented Aug 28, 2020

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()):
@jayqi
Copy link
Member

jayqi commented Sep 2, 2020

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

@jayqi jayqi added the good first issue Good for newcomers label Sep 4, 2020
@pjbull pjbull added the backlog label Sep 4, 2020
@cszc cszc added in progress and removed backlog labels Sep 4, 2020
@jayqi jayqi closed this as completed in #72 Oct 2, 2020
@pjbull pjbull removed the in progress label Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants