-
Notifications
You must be signed in to change notification settings - Fork 64
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
WIP: Patch open, os and os.path builtins #322
base: master
Are you sure you want to change the base?
Conversation
cloudpathlib/patches.py
Outdated
CloudPath.open, | ||
) | ||
builtins.open = patched | ||
CloudPath.__fspath__ = lambda x: x # turn off `fspath` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the idea here is that even if code uses fspath
to try to get the local path, we give back the CloudPath
instead so it works downstream. This may break code that assumes fspath
returns a string, which is what it is supposed to do.
(Also, note: this no longer caches on the call here so we only populate the cache on open
).
5308425
to
7bf3460
Compare
Right now, libs that use
open
directly,fspath
or just assume paths are strings and can be manipulated don't work withCloudPath
objects. (Related issues: #315, #73, #128)Totally WIP and not sure that we want to introduce and support this, but it may increase compatibility significantly to have the option at least for users to patch the builtin functions.
The biggest limitations are:
path = dir + os.sep + file
)Other potential todos for this PR:
glob
module as wellpandas
has all kinds of formats,rasterio
may be interesting,xarray
has come up,Pillow
may be a common use case)os
functionsos.fspath
to allow returningCloudPath
open = patch_open()
sinceopen
exists as alocal
in the notebook namespaceCurious for your thoughts @jayqi !
Experimental instructions
pip install git+https://github.com/drivendataorg/cloudpathlib@patch-builtins
CLOUDPATHLIB_PATCH_OPEN=True
before any import call tocloudpathlib
open
or third-party libraries that use open.