Skip to content

Silence netCDF4 ndarray.shape DeprecationWarning at each write site - #419

Merged
jmccreight merged 2 commits into
DOI-USGS:developfrom
jmccreight:nc4_dep_flood
Sep 12, 2026
Merged

jmccreight merged 2 commits into
DOI-USGS:developfrom
jmccreight:nc4_dep_flood

Conversation

@jmccreight

@jmccreight jmccreight commented Sep 12, 2026 •

Copy link
Copy Markdown
Collaborator

With netCDF4 <= 1.7.4 and NumPy >= 2.5 every netCDF variable write emits
Setting the shape on a NumPy array has been deprecated (netCDF4 assigns
to ndarray.shape in _netCDF4.pyx; fixed upstream in
Unidata/netcdf4-python#1469, unreleased). The pytest.ini filter hid it
in the test suite only: the example notebooks (run by
autotest_exs/test_notebooks.py via ipython in a subprocess) and any user
script still got one warning per variable per timestep.

A global filter cannot fix this. flopy 3.11 runs
warnings.simplefilter("always", DeprecationWarning) at import
(flopy/utils/rasters.py:10, flopy/mf6/utils/binarygrid_util.py:14,
flopy/export/vtk.py:17), which is inserted ahead of any filter set
earlier, including PYTHONWARNINGS, and import pywatershed imports
flopy. Reported informally to the flopy developers.

Changes:
nc4_shape_warning_filter, a warnings.catch_warnings context manager
in pywatershed/base/data_model.py, wraps every pywatershed netCDF4
write: NetCdfWrite (coordinate writes, add_simulation_time,
add_data, add_all_data), Budget.output_netcdf, and
dd_to_nc4_ds. It is inserted at write time, so import order does
not matter.
The pytest.ini line is kept only for xarray's own to_netcdf path
(xarray/backends/netCDF4
.py, setitem), which pywatershed
calls from about a dozen places and does not wrap.
MAINTENANCE.md lists every site (and a grep to find them) for
removal once the netCDF4 release carrying the fix is required.

Verified: 01_multi-process_models.ipynb runs clean;
pytest test_prms_canopy.py test_model.py --domain drb_2yr shows zero
occurrences and passes with -W error::DeprecationWarning:pywatershed.

Docs build for this PR: https://app.readthedocs.org/projects/pywatershed/builds/34520888/
Rendered: https://pywatershed--419.org.readthedocs.build/en/419/

Checklist

[x] Closes #418
[ ] Tests added
[ ] Performance benchmarks added or run
[x] User visible changes (including notable bug fixes) are documented in whats-new.rst
[x] The (:pull:XXX) placeholder in whats-new.rst is replaced with this PR's number
[ ] New public classes/functions are exported in pywatershed/init.py and listed in doc/api/*.rst

flopy sets warnings.simplefilter('always', DeprecationWarning) at import,
overriding pytest.ini/PYTHONWARNINGS filters and reaching notebooks and
user scripts. Wrap every pywatershed netCDF4 write in
nc4_shape_warning_filter (warnings.catch_warnings); keep the pytest.ini
line only for xarray's own to_netcdf path. MAINTENANCE.md lists every
site for removal once netCDF4 ships PR #1469.
@jmccreight
jmccreight merged commit d96a747 into DOI-USGS:develop Sep 12, 2026
33 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

netCDF4 ndarray.shape DeprecationWarning floods example-notebook runs (pytest.ini filter doesn't reach them)

1 participant