Describe the bug
contours() doesn't validate its n_levels parameter. The value is only used on the auto-level branch (when levels isn't passed), at xrspatial/contour.py:659:
levels = np.linspace(vmin, vmax, n_levels + 2)[1:-1]
Two things go wrong:
n_levels=0 or n_levels=-1 silently returns no contours. np.linspace(vmin, vmax, 2)[1:-1] and np.linspace(vmin, vmax, 1)[1:-1] give back an empty or degenerate level set, so the caller gets an empty result with no hint that the argument was bad.
n_levels=2.5 (or any non-integer) raises a raw TypeError from inside numpy ('float' object cannot be interpreted as an integer), which leaks an implementation detail instead of pointing at the parameter.
Expected behavior
n_levels should have an explicit contract: an integer >= 1. Invalid values raise a clear error up front:
- non-integer (e.g.
2.5, or a bool) -> TypeError that names n_levels
- integer < 1 (e.g.
0, -1) -> ValueError that names n_levels
Since n_levels is ignored when explicit levels are supplied, the check should only fire on the auto-level branch, so contours(agg, levels=[...], n_levels=0) isn't rejected for no reason.
Additional context
Convention in the codebase: TypeError for wrong type, ValueError for out-of-range. See landforms (inner_radius must be >= 1) in terrain_metrics.py and max_iterations must be >= 1 in balanced_allocation.py.
Describe the bug
contours()doesn't validate itsn_levelsparameter. The value is only used on the auto-level branch (whenlevelsisn't passed), atxrspatial/contour.py:659:Two things go wrong:
n_levels=0orn_levels=-1silently returns no contours.np.linspace(vmin, vmax, 2)[1:-1]andnp.linspace(vmin, vmax, 1)[1:-1]give back an empty or degenerate level set, so the caller gets an empty result with no hint that the argument was bad.n_levels=2.5(or any non-integer) raises a rawTypeErrorfrom inside numpy ('float' object cannot be interpreted as an integer), which leaks an implementation detail instead of pointing at the parameter.Expected behavior
n_levelsshould have an explicit contract: an integer >= 1. Invalid values raise a clear error up front:2.5, or a bool) ->TypeErrorthat namesn_levels0,-1) ->ValueErrorthat namesn_levelsSince
n_levelsis ignored when explicitlevelsare supplied, the check should only fire on the auto-level branch, socontours(agg, levels=[...], n_levels=0)isn't rejected for no reason.Additional context
Convention in the codebase:
TypeErrorfor wrong type,ValueErrorfor out-of-range. Seelandforms(inner_radius must be >= 1) in terrain_metrics.py andmax_iterations must be >= 1in balanced_allocation.py.