Skip to content

Conversation

@Sakshee-D
Copy link
Contributor

This PR improves the error message raised when using NDPointIndex without scipy installed.

Previously, the missing dependency error was raised indirectly during index creation. This change wraps the scipy import in ScipyKDTreeAdapter and raises a more actionable ImportError explaining that scipy is required.

Manual testing

  • Verified behavior when scipy is not installed
  • Verified no behavior change when scipy is installed

Related to #11047

@welcome
Copy link

welcome bot commented Jan 12, 2026

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@max-sixty max-sixty added the plan to merge Final call for comments label Jan 12, 2026
@keewis
Copy link
Collaborator

keewis commented Jan 13, 2026

thanks for the PR, @Sakshee-D.

This looks good to me, but we might need a test? This might require more changes, though: right now, the dedicated test module (test_ndpoint_index.py) is skipped if scipy is not available. Are you up for that? If not, I can make the changes for you.

@Sakshee-D
Copy link
Contributor Author

Thanks for the suggestion.
Since test_nd_point_index.py is skipped entirely when SciPy isn’t available, I will add a small test in a new test file under tests/ (without importorskip("scipy")). The test will use monkeypatch to simulate SciPy being missing and display the improved error message when NDPointIndex is constructed.

I’ll include this in the PR so you can review it there and suggest any changes if needed.

Comment on lines +81 to +84
raise ImportError(
"`NDPointIndex` requires `scipy` when used with `ScipyKDTreeAdapter`. "
"Please ensure that `scipy` is installed and importable."
) from err
Copy link
Contributor

Choose a reason for hiding this comment

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

So I understand, is the only way to hit this error trying to import the defuault NDPointIndex without scipy installed?

Since there are other possible trees, not all of which necessarily require scipy, is there any those escape this warning?

Copy link
Collaborator

@keewis keewis Jan 14, 2026

Choose a reason for hiding this comment

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

NDPointIndex is a generalized class that delegates to a TreeAdapter subclass, which does the import. Thus, every adapter needs to raise its own error if the corresponding optional dependency is missing.

For example, the adapters in xoak would have to raise if sklearn or pys2index are missing.

from xarray.indexes import NDPointIndex


def test_ndpointindex_missing_scipy(monkeypatch):
Copy link
Collaborator

@keewis keewis Jan 14, 2026

Choose a reason for hiding this comment

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

instead of the monkey patching it should be enough to check on the environments that don't install scipy (also needs from xarray.tests import has_scipy):

Suggested change
def test_ndpointindex_missing_scipy(monkeypatch):
@pytest.mark.skipif(has_scipy)
def test_ndpointindex_missing_scipy():

monkeypatch.setitem(sys.modules, "scipy", None)
monkeypatch.setitem(sys.modules, "scipy.spatial", None)

xx = xr.DataArray(
Copy link
Collaborator

Choose a reason for hiding this comment

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

could you call the constructor of ScipyKDTreeAdapter directly? I believe there's less setup involved with that.

@Sakshee-D
Copy link
Contributor Author

Thanks for pointing that out .
I missed this earlier. I will update the test to rely on the no-SciPy CI environments and call ScipyKDTreeAdapter directly to keep it minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

plan to merge Final call for comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants