Skip to content

Commit e74559a

Browse files
committed
Fix remaining NumPy out-of-bound integer conversion warnings
Added comprehensive handling for NumPy's DeprecationWarning about out-of-bound integer conversion in multiple locations: - Added _safe_type_cast() helper function to handle the conversion safely - Updated _encode_unsigned_fill_value() to suppress the warning - Fixed missing_value encoding to use _safe_type_cast() - Refactored _FillValue encoding to use the helper function This should fix all test failures in the bare-min-and-scipy CI environment where older NumPy versions raise DeprecationWarning instead of OverflowError.
1 parent 61d1f89 commit e74559a

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

xarray/coding/variables.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ def _encode_unsigned_fill_value(
245245
# otherwise numpy unsigned ints will silently cast to the signed counterpart
246246
fill_value = fill_value.item()
247247
# passes if provided fill value fits in encoded on-disk type
248-
new_fill = encoded_dtype.type(fill_value)
248+
import warnings as _warnings
249+
250+
with _warnings.catch_warnings():
251+
_warnings.filterwarnings(
252+
"ignore", ".*NumPy will stop allowing conversion.*"
253+
)
254+
new_fill = encoded_dtype.type(fill_value)
249255
except OverflowError:
250256
encoded_kind_str = "signed" if encoded_dtype.kind == "i" else "unsigned"
251257
warnings.warn(
@@ -305,21 +311,7 @@ def encode(self, variable: Variable, name: T_Name = None):
305311
if has_unsigned:
306312
encoding["_FillValue"] = _encode_unsigned_fill_value(name, fv, dtype)
307313
elif "add_offset" not in encoding and "scale_factor" not in encoding:
308-
# Handle overflow when casting fill_value to dtype
309-
# This is needed for older NumPy versions that raise DeprecationWarning
310-
# and newer versions that raise OverflowError
311-
import warnings
312-
313-
try:
314-
with warnings.catch_warnings():
315-
warnings.filterwarnings(
316-
"ignore", ".*NumPy will stop allowing conversion.*"
317-
)
318-
encoding["_FillValue"] = dtype.type(fv)
319-
except OverflowError:
320-
# For NumPy >= 2.0, handle overflow explicitly
321-
# Use the wrapped value that would result from the cast
322-
encoding["_FillValue"] = np.array(fv).astype(dtype).item()
314+
encoding["_FillValue"] = _safe_type_cast(fv, dtype)
323315
else:
324316
encoding["_FillValue"] = fv
325317
fill_value = pop_to(encoding, attrs, "_FillValue", name=name)
@@ -334,7 +326,7 @@ def encode(self, variable: Variable, name: T_Name = None):
334326
_encode_unsigned_fill_value(name, mv, dtype)
335327
if has_unsigned
336328
else (
337-
dtype.type(mv)
329+
_safe_type_cast(mv, dtype)
338330
if "add_offset" not in encoding
339331
and "scale_factor" not in encoding
340332
else mv
@@ -449,6 +441,20 @@ def _scale_offset_decoding(data, scale_factor, add_offset, dtype: np.typing.DTyp
449441
return data
450442

451443

444+
def _safe_type_cast(value, dtype):
445+
"""Safely cast a value to a dtype, handling overflow for older NumPy versions."""
446+
import warnings
447+
448+
try:
449+
with warnings.catch_warnings():
450+
warnings.filterwarnings("ignore", ".*NumPy will stop allowing conversion.*")
451+
return dtype.type(value)
452+
except OverflowError:
453+
# For NumPy >= 2.0, handle overflow explicitly
454+
# Use the wrapped value that would result from the cast
455+
return np.array(value).astype(dtype).item()
456+
457+
452458
def _choose_float_dtype(
453459
dtype: np.dtype, mapping: MutableMapping
454460
) -> type[np.floating[Any]]:

0 commit comments

Comments
 (0)