Skip to content

contours() validates return_type too late, wasting compute on invalid values #2788

@brendancol

Description

@brendancol

Describe the bug

contours() in xrspatial/contour.py checks the return_type argument only at the end of the function, after all the contour work has already run. The branch that raises ValueError for an unknown return_type sits near the bottom of the function (around line 676), past level generation, backend dispatch, and the per-segment coordinate transform.

For a Dask-backed input this means an invalid return_type triggers a full dask.compute() before the function raises. For a large NumPy raster it can hit the memory guard in _contours_numpy first. Either way the call does a lot of work before failing on what is usually a typo.

Expected behavior

An invalid return_type should fail fast with a clear ValueError before any level generation, backend dispatch, or compute happens. Passing return_type="geojson" (or any other unsupported value) should raise right away, with no Dask execution and no memory allocation.

Steps to reproduce

  • Build a Dask-backed DataArray.
  • Call contours(agg, return_type="bogus").
  • The chunks get computed before the ValueError is raised.

Additional context

Move the return_type membership check to the top of contours(), next to the existing raster validation. The late branch can then drop its else: raise since the value is already known good. Add a test asserting an invalid return_type raises without triggering a compute on a Dask input.

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