From 16a04b61e972b1080ff4f4bf57475dc844e0614a Mon Sep 17 00:00:00 2001 From: NagIzaazShaik <176337212+shaikn6@users.noreply.github.com> Date: Wed, 9 Sep 2026 15:02:26 -0400 Subject: [PATCH] Accept target_values=None in surface_distance trio (#3712) surface_distance, surface_allocation and surface_direction declared target_values: list = [] while proximity, allocation, direction and cost_distance use None and normalize to [] in the body. Passing target_values=None (common when wrapper code threads an optional arg through) reached np.asarray(None) and failed before the numba kernel. Change the three signatures to None and add the sibling 'if target_values is None: target_values = []' guard in the shared _compute dispatcher. [] and None both mean 'every non-zero finite pixel is a source', so callers passing an explicit list are unaffected. Also removes the mutable default argument the siblings already avoid. --- xrspatial/surface_distance.py | 9 ++++++--- xrspatial/tests/test_surface_distance.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/xrspatial/surface_distance.py b/xrspatial/surface_distance.py index dd2bf33b7..fcb46cb19 100644 --- a/xrspatial/surface_distance.py +++ b/xrspatial/surface_distance.py @@ -1383,6 +1383,9 @@ def _compute(raster, elevation, x, y, target_values, max_distance, signed_cellsize_x = cellsize_x * _coord_step_sign(raster, x) signed_cellsize_y = cellsize_y * _coord_step_sign(raster, y) + if target_values is None: + target_values = [] + target_values = np.asarray(target_values, dtype=np.float64) if target_values.ndim != 1: raise ValueError( @@ -1528,7 +1531,7 @@ def surface_distance( elevation: xr.DataArray, x: str = "x", y: str = "y", - target_values: list = [], + target_values: list = None, max_distance: float = np.inf, connectivity: int = 8, method: str = 'planar', @@ -1584,7 +1587,7 @@ def surface_allocation( elevation: xr.DataArray, x: str = "x", y: str = "y", - target_values: list = [], + target_values: list = None, max_distance: float = np.inf, connectivity: int = 8, method: str = 'planar', @@ -1621,7 +1624,7 @@ def surface_direction( elevation: xr.DataArray, x: str = "x", y: str = "y", - target_values: list = [], + target_values: list = None, max_distance: float = np.inf, connectivity: int = 8, method: str = 'planar', diff --git a/xrspatial/tests/test_surface_distance.py b/xrspatial/tests/test_surface_distance.py index 499b689ea..b925215c5 100644 --- a/xrspatial/tests/test_surface_distance.py +++ b/xrspatial/tests/test_surface_distance.py @@ -613,6 +613,16 @@ def test_invalid_target_values_shape(bad): surface_distance(source, elev, target_values=bad) +@pytest.mark.parametrize("func", [surface_distance, surface_allocation, surface_direction]) +def test_target_values_none_matches_empty(func): + """target_values=None must behave like the [] default (issue #3712).""" + source = _make_raster(np.array([[0.0, 1.0, 0.0, 2.0, 0.0]], dtype=np.float64)) + elev = _make_raster(np.zeros((1, 5), dtype=np.float64)) + expected = _compute(func(source, elev)) + got = _compute(func(source, elev, target_values=None)) + np.testing.assert_array_equal(np.asarray(got), np.asarray(expected)) + + # --------------------------------------------------------------------------- # Tests — dask-specific # ---------------------------------------------------------------------------