Skip to content

contours(): return_type validated too late; all-non-finite path silently accepts bad values #2891

@brendancol

Description

@brendancol

Describe the bug

contours() validates return_type too late. The only check is at the end of the
function (xrspatial/contour.py:691), inside the dispatch chain:

if return_type == "numpy":
    return results
elif return_type == "geopandas":
    ...
else:
    raise ValueError(...)

That runs only after the raster has been validated, the levels computed, and the
contours extracted. Two problems fall out of the ordering:

  1. A bad return_type isn't rejected until the whole extraction has already run, so
    the caller pays for work that was always going to fail.

  2. The all-non-finite early return at xrspatial/contour.py:653-656 short-circuits
    before that check ever happens:

     if not np.isfinite(vmin) or not np.isfinite(vmax):
         if return_type == "numpy":
             return []
         return _to_geopandas([], crs=agg.attrs.get('crs', None))
    

    Any non-"numpy" value falls through to the GeoDataFrame branch. So
    contours(all_nan_raster, return_type="bad") hands back an empty GeoDataFrame
    instead of raising. The bad argument is silently accepted.

Expected behavior

return_type should be checked near the top of contours(), right after
_validate_raster and before any level computation or data work. A bad value should
raise ValueError no matter what the input raster looks like, including the
all-non-finite case.

Additional context

Fix: add an explicit return_type check (allowed values 'numpy' and 'geopandas')
at the top of the function. The existing late dispatch stays put but becomes
unreachable for bad values.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions