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

Deprecation fix: mktemp is deprecated #789

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion spectral_cube/dask_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def wrapper(self, *args, **kwargs):
raise ImportError("saving the cube to a temporary directory "
"requires the zarr and fsspec packages to "
"be installed.")
filename = tempfile.mktemp()
filenum, filename = tempfile.mkstemp()
with dask.config.set(**cube._scheduler_kwargs):
cube._data.to_zarr(filename)
cube._data = da.from_zarr(filename)
Comment on lines +83 to 86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filenum, filename = tempfile.mkstemp()
with dask.config.set(**cube._scheduler_kwargs):
cube._data.to_zarr(filename)
cube._data = da.from_zarr(filename)
tmpdir = tempfile.TemporaryDirectory()
with dask.config.set(**cube._scheduler_kwargs):
cube._data.to_zarr(tmpdir)
cube._data = da.from_zarr(tmpdir)
tmpdir.cleanup()

Without the .cleanup() the test raises a warning over the leftover directory; it's not quite clear to me if the saved data in tmp_dir are never to be accessed again...

Comment on lines +83 to 86
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filenum, filename = tempfile.mkstemp()
with dask.config.set(**cube._scheduler_kwargs):
cube._data.to_zarr(filename)
cube._data = da.from_zarr(filename)
cube._tmpdir = tempfile.TemporaryDirectory()
with dask.config.set(**cube._scheduler_kwargs):
cube._data.to_zarr(cube._tmpdir.name)
cube._data = da.from_zarr(cube._tmpdir.namefile)

Slightly different option; this will enable the TemporaryDirectory to be cleaned up on deletion of the cube by setting up a custom teardown method for DaskSpectralCubeMixin:

    def __del__(self):
        # Clean up any `tempfile.TemporaryDirectory` created by `save_to_tmp_dir`.
        if hasattr(self, '_tmpdir'):
            self._tmpdir.cleanup()

Expand Down