Skip to content

Sequence-based Results containers with replayable query params (proposal for #1298) - #1508

Merged
betolink merged 5 commits into
earthaccess-dev:results-objectfrom
danielfromearth:results-container-for-1298
Nov 20, 2026
Merged

betolink merged 5 commits into
earthaccess-dev:results-objectfrom
danielfromearth:results-container-for-1298

Conversation

@danielfromearth

@danielfromearth danielfromearth commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

My Description

This changes the Results class so it can hold either a list or an iterator, and so that we have at least:

This PR does not add lazy loading (searches would still get every result before returning, as they do today).

AI-generated Description

A proposal for #1298 (which addresses #847). It answers the open question there about how Results should be iterable, and it fixes the CI failures on the current branch.

What it does

  • Results is a collections.abc.Sequence. GranuleResults and CollectionResults are its typed subclasses, exported from earthaccess. The type parameter is limited to the two result types, Results[ResultItem: (DataGranule, DataCollection)], so type checkers know that search_data(...)[0] is a DataGranule.
  • Replayable inputs (should earthaccess provide a way to keep track of input query parameters? #847): search_data() and search_datasets() store their own arguments ({"count": count, **kwargs}, deep-copied), so earthaccess.search_data(**results.query_params) repeats a search. Each search returns a new object with its own query_params. The CMR hit count the search already fetches is kept as results.hits.
  • download() and open() work as before: both convert a GranuleResults to a plain list with .all() before calling Store. store.py is unchanged.
  • Results are fully loaded: search_data() and search_datasets() fetch all results (up to count) and pass them in as a list, the same requests as today. Indexing, slicing, len(), bool() and repeated iteration work as on a list.
  • Iterator input, for later use: Results also accepts an iterator, takes each item from it only when first needed, and keeps it, so the results can be iterated again without searching again. Nothing in this PR passes an iterator; this is the hook for a lazy search later (the TODO in results.py).
  • Display: __repr__, __str__ and _repr_html_ keep the layout from this branch (the same rows, labels and wording). The values are stored when the search runs, so displaying results makes no CMR request. "Query parameters" shows the arguments passed to the search, and "Query options" shows the CMR query options.
  • DataGranulesQuery.to_geopandas() and DataCollectionsQuery.to_geopandas() accept any iterable, as on this branch, including results objects.
  • GranuleResults.to_gdf() and CollectionResults.to_gdf() call those same to_geopandas() methods from Add to_geopandas() to DataGranules and DataCollections #1461, so earthaccess.search_data(...).to_gdf() gives the same GeoDataFrame.

Changes relative to results-object

  • Replaces the Results classes that hold the query object.
  • Removes the get()/get_all() overrides on the query classes. get() returns a list again, and the results objects are built in api.py.
  • Keeps the _umm_record() hardening of DataCollection, the DataCollectionsQuery docstring rename, and the to_geopandas signatures from this branch.
  • Fixes the DataGranulesQuery.to_geopandas() docstring example, which used the old name ea.DataGranules, and adds count=100 to it.
  • Updates three integration assertions that expected a list.

CI failures on results-object this addresses

  • TypeError: type 'Results' is not subscriptable at import on Python 3.12, 3.13 and in the min-deps job.
  • The 25 mypy errors: Results not generic, get() overrides conflicting with python-cmr's list[Any] stub, and __getitem__ returning a union.
  • DispatchError: _get: 0 methods found from download()/open() on Python 3.14.

Decisions for reviewers

  • count is stored in query_params, so a replay returns the same number of results. It could be left out if a replay should mean "same filters, all matches".
  • "Number of results" in the display shows CMR's total hits, as before, so a search with count=100 can show a much larger number.
  • Query-builder users (DataGranulesQuery(...).get()) get a list and no provenance.
  • isinstance(results, list) is now false. That's the breaking change.
  • Not included: lazy loading from CMR, __eq__, or __add__ (combining searches).
  • The names all(), query_params, GranuleResults and CollectionResults match SearchResults on the plugins branch, so moving to its streaming model later wouldn't rename the API. Note that len() means something different there: the number of items loaded so far.

Testing

  • mypy: no issues.
  • Unit tests: 168 passed on Python 3.12 (with all extras) and on 3.14, including 13 new tests: iteration, load-once behaviour with an iterator, query_params snapshots, separate query_params per search, display output without HTTP requests, pickling, replay, download/open passing Store a list, and to_gdf() matching to_geopandas().
  • pre-commit passes on all changed files. demo_old.ipynb has been removed from this branch.
  • The to_geopandas() and to_gdf() docstring examples were run against CMR (171 collections, 100 granules).
  • Not run: integration tests (they need EDL credentials) and the docs build.
  • CI's min-deps job passes. (My local nox environment fails on store.py:522 for an unrelated reason, on main too.)

AI usage disclosure

Using AI tools is welcome. Per our AI Policy,
we just ask that you disclose any use of generative AI tools in this contribution.

  • I have disclosed AI tool usage below, or confirmed that no AI tools were used

Claude Code (Claude Opus 5.5) drafted the implementation, the tests, the merge-conflict resolution and the "AI-Generated Description" above at my direction, and ran the checks listed under Testing.

"Ready for review" checklist

  • Place this Pull Request (PR) in draft until it is ready for review (see below)
  • Please review our Pull Request Guide
  • Mark "ready for review" after following instructions in the guide

Merge checklist

  • PR title is descriptive
  • PR body contains links to related and resolved issues (e.g. closes #1)
  • If needed, CHANGELOG.md updated
  • If needed, docs and/or README.md updated
  • If needed, unit tests added
  • All checks passing
  • At least one approval

📚 Documentation preview 📚: https://earthaccess--1508.org.readthedocs.build/en/1508/

danielfromearth and others added 2 commits November 18, 2026 16:41
- add Results, GranuleResults, CollectionResults (Sequence-based; items
  loaded once from a list or an iterator, then kept)
- search_data()/search_datasets() record their kwargs and CMR hit count
  so a search can be repeated with search_data(**results.query_params)
- download()/open() convert results to a list before calling Store, so
  Store behaviour is unchanged
- widen to_geopandas() annotations to accept Sequence
- update integration assertions that expected a plain list
Conflicts resolved in favour of the Sequence-based Results container:

- earthaccess/results.py: replace the query-holding Results classes with
  Results/GranuleResults/CollectionResults; keep the _umm_record()
  hardening of DataCollection from this branch
- earthaccess/search.py: drop the get()/get_all() overrides (get() returns
  a list again; search_data()/search_datasets() build the results); keep
  the DataCollectionsQuery docstring rename
- earthaccess/api.py: take the containers' search/download/open changes
- tests/unit/test_geo_interface.py: replace the Results(query=...) test
  with one using GranuleResults

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Binder 👈 Launch a binder notebook on this branch for commit 2dfb5c6

I will automatically update this comment whenever this PR is modified

Binder 👈 Launch a binder notebook on this branch for commit 90b7fe6

Binder 👈 Launch a binder notebook on this branch for commit 6d07fd8

Binder 👈 Launch a binder notebook on this branch for commit 0d9d9a9

Comment thread earthaccess/results.py Outdated
Comment on lines +656 to +657
these results. A deep copy is stored, so later changes to the
caller's objects do not change it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So if the user now reruns results = earthaccess.search_data(params) with different inputs, results.query_params won't get updated? My initial thought it that's a counterintuitive behavior from the user perspective and query_params should be updated to the latest submitted parameters, so they reflect the actual results associated with that object (and not some arbitrary previous results). Otherwise it sort of defeats the purpose of having the query parameters attached to the results.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rerunning will still update it, because each call returns a new GranuleResults object.

This deep copy covers a different case though: a case where a list or dict is passed to the search and then those lists or dicts are changed afterwards. If that happens, results.query_params still shows what a search actually used. I'll reword the docstring to say so and we can add a new unit test for it, one that runs two searches and checks the params.

Comment thread earthaccess/results.py Outdated
Comment on lines +661 to +662
self._items: list[T]
self._source: Iterator[T] | None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

here (and throughout) - I'm concerned about AI's prolific use of type "T" instead of being more specific. In the few cases I've encountered in other work, a specific type is easy to plug in. Here I think that would be list[DataGranule] | list[DataCollection] or similar.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that's a good point about "T". How about Results[ResultItem: (DataGranule, DataCollection)], going to give that a try.

Comment thread earthaccess/results.py Outdated
f"params={self.query.params!r}, "
f"options={self.query.options!r})"
f"{type(self).__name__}({self._loaded_label()}, hits={self._hits!r}, "
f"query_params={self._query_params!r})"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the __repr__ and other display items: I'd be keen to see some of the content/how it's displayed preserved. That was something that humans put a lot of effort into for determining readability, clarity, usability, and user-friendliness.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, good point. I will change that back to the original layout.

And apparently, from a discussion with Opus 5.5, since the values are now stored when the search runs instead of read from the query object, we can have the display of results no longer call CMR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Neat - and thanks!

Comment thread earthaccess/search.py
collections = ea.search_datasets(
keyword="above ground biomass",
)
gdf = ea.DataCollectionsQuery.to_geopandas(collections)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

flagging to test (I previously found errors in these examples) - I can't recall if we have pytest enabled on docstrings.

Comment thread tests/unit/test_geo_interface.py Outdated

granule_gdf = DataGranulesQuery.to_geopandas(granules)
collection_gdf = DataCollectionsQuery.to_geopandas(collections)
gdf = DataGranulesQuery.to_geopandas(GranuleResults(iter(granules)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

    gdf = DataGranulesQuery.to_geopandas(GranuleResults(iter(granules)))

That's kinda ugly. I'd like to just be able to pass in granules or collections.

Also, this removes the collection_gdf test, but we still have both granule- and collection- to_geopandas functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, I will work on changing this back.

@JessicaS11 JessicaS11 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

See comments throughout (with the additional note that pending some of the other changes we should review the tests again). This neatly addresses the lazy generator and in-memory approaches though.

@betolink

Copy link
Copy Markdown
Member

Unrelated: Don't we have an automatic pre-commit fix action e.g. @pre-commit fix?

@JessicaS11

Copy link
Copy Markdown
Contributor

Unrelated: Don't we have an automatic pre-commit fix action e.g. @pre-commit fix?

We do, but the check didn't pass even after I ran the fix action in the results-objects branch (#1298)

@betolink

Copy link
Copy Markdown
Member

In general I think this goes in the right direction, we are still not lazy loading results which is fine and saves us a breaking change (although, more are coming). One feature we may consider for this PR is to have a wrapper on to_gdf() directly on the results class so we can easily get a geo-dataframe from granules. @danielfromearth @JessicaS11

betolink and others added 3 commits November 20, 2026 12:12
- constrain Results type parameter to DataGranule/DataCollection (ResultItem)
- restore the original repr/str/HTML layout, using stored hits and a
  snapshot of the CMR query options instead of live CMR calls
- clarify that each search returns results with its own query_params
- restore Iterable to_geopandas signatures and fix the stale
  ea.DataGranules name in the granule example
- restore the granule and collection to_geopandas results test
- delegate to the existing DataGranulesQuery/DataCollectionsQuery
  to_geopandas() static methods (earthaccess-dev#1461), so both paths return the same
  GeoDataFrame
- add a test that to_gdf() matches to_geopandas() for both result types
@danielfromearth

Copy link
Copy Markdown
Contributor Author

@JessicaS11, @betolink,
I pushed some changes based on your comments, and added to_gdf() to GranuleResults and CollectionResults. It just calls the to_geopandas() methods from #1461.

@betolink
betolink merged commit e8f4af8 into earthaccess-dev:results-object Nov 20, 2026
10 checks passed
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.

3 participants