earthaccess icon indicating copy to clipboard operation
earthaccess copied to clipboard

`download` returns inconsistent types (`Path`s or `str`s) depending on `in_region`

Open itcarroll opened this issue 1 year ago • 5 comments

The download docs say that the return value shall be a list of strings. When running in_region however, the result is a list of pathlib.PosixPath objects.

import earthaccess
import pathlib

assert earthaccess.__version__ == "0.9.0"
assert earthaccess.__store__.in_region

local = pathlib.Path("data").mkdir(exist_ok=True)
results = earthaccess.search_data(short_name="PACE_OCI_L2_BGC_NRT", count=1)
paths = earthaccess.download(results, local_path=local)
assert isinstance(paths[0], str)

The last assert raises an exception.

itcarroll avatar Jun 10 '24 19:06 itcarroll

Nice catch! Why isn't our type checker catching this though?

https://github.com/nsidc/earthaccess/blob/9f43047f6d2f6845d33a9367814262b43d9bcc46/earthaccess/api.py#L177

I love pathlib though, should we update download() to always return Paths? :)

mfisher87 avatar Jun 10 '24 19:06 mfisher87

If you want to be fancy, you could return the same type given to you in the local_path argument.

itcarroll avatar Jun 10 '24 19:06 itcarroll

I like the idea of returning a list of Path objects. I think this would avoid cross platform issues.

andypbarrett avatar Jun 11 '24 23:06 andypbarrett

I would like to upvote this for earthaccess.download() to be consistently returning an object of the same type, whether it be Posix or string (probably should be Posix for cross platform compatibility although several folks may have developed code already assuming the string list). In my script, I evaluate the earthaccess.download() returned object within my script to check whether I am in region or not and then apply list comprehension line to convert the Posix object to a string list since another function is expecting a string list.

files = earthaccess.download(granules, path)
if earthaccess.__store__.in_region == True:  files =  [str(Path(f)) for f in files]       

meteodave avatar Sep 14 '24 14:09 meteodave

although several folks may have developed code already assuming the string list

Excellent point. Let's please keep in mind as we address this that we also have work in progress to establish a compatibility policy: https://github.com/nsidc/earthaccess/pull/763/files We should include a prominent notification of breaking change and a migration guide with code examples.

mfisher87 avatar Sep 14 '24 15:09 mfisher87

@mfisher87 I want to work on this one. Should we proceed forward with the suggestion above to return Posix whether in region or out of region?

Sherwin-14 avatar May 18 '25 14:05 Sherwin-14

Yes, I think we can safely decide to return a Path (not a PosixPath, some users are on Windows!). We already switched to returning Path in some cases, and I think that was driven by a general preference for Paths over strs. Thanks, Sherwin!

mfisher87 avatar May 18 '25 15:05 mfisher87

Closing - this was merged via #1031

asteiker avatar Aug 19 '25 18:08 asteiker