Skip to content

contours(): n_levels is not validated as a positive integer #2791

@brendancol

Description

@brendancol

Describe the bug

contours() in xrspatial/contour.py does not validate n_levels. When levels is not passed, n_levels goes straight into np.linspace(vmin, vmax, n_levels + 2), so bad values slip through and fail in confusing ways instead of raising a clear error:

  • n_levels=0: np.linspace(..., 2)[1:-1] returns an empty array, so you get no contours and no warning.
  • Negatives: np.linspace raises a low-level error about a negative number of samples that never mentions n_levels.
  • Floats like 2.5: np.linspace accepts the float count (n_levels + 2), so the failure surfaces deep in numpy.
  • Booleans: True/False are silently treated as 1/0, since bool subclasses int.

Expected behavior

n_levels should be a positive integer. Validate it up front and raise a clear TypeError/ValueError that names n_levels for 0, negatives, non-integer floats, and bool inputs. A valid positive integer should behave exactly as before.

Additional context

This only matters on the levels is None path. When explicit levels are passed, n_levels is ignored.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions