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

WIP: Patch open, os and os.path builtins #322

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

pjbull
Copy link
Member

@pjbull pjbull commented Feb 16, 2023

Right now, libs that use open directly, fspath or just assume paths are strings and can be manipulated don't work with CloudPath 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:

  • Libs that do any string manipulation manually still will not work (e.g. path = dir + os.sep + file)
  • Libs that pass local paths off to compiled code to do IO will still not work
  • There are probably lots of other ways that other code can do funky things with paths, so even these patches may not make things work

Other potential todos for this PR:

  • Patch the standard lib glob module as well
  • Add integration test suite that shows the patches working with libs that did not work previously (pandas has all kinds of formats, rasterio may be interesting, xarray has come up, Pillow may be a common use case)
  • Add env var for patching os functions
  • override os.fspath to allow returning CloudPath
  • Document these methods
  • Add full test suite (can we leverage what CPython does for these functions?)
  • Document that in Jupyter notebooks you have to do open = patch_open() since open exists as a local in the notebook namespace
  • Remove testing script

Curious for your thoughts @jayqi !


Experimental instructions

  • pip install git+https://github.com/drivendataorg/cloudpathlib@patch-builtins
  • Set the environment variable CLOUDPATHLIB_PATCH_OPEN=True before any import call to cloudpathlib
  • Experiment with your code that uses open or third-party libraries that use open.
  • Report the results in open(cloud_path) acts on cached local file instead of cloud file #128

@github-actions
Copy link
Contributor

CloudPath.open,
)
builtins.open = patched
CloudPath.__fspath__ = lambda x: x # turn off `fspath`
Copy link
Member Author

@pjbull pjbull Feb 16, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant