Fix silent byte-swapping of non-native-endian attribute values in the netcdf4 backend #11543
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.42) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.42 · 345 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.350
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #11543"
general
weight: 0.35
glaziermag
Description
Writing a dataset whose attributes hold non-native-endian numpy arrays silently stores byte-swapped values with the
netcdf4engine. The easiest way to get such attributes is reading a netCDF-3 file with thescipyengine, which returns attribute arrays big-endian — so this fully-default round-trip corrupts metadata on disk:The same happens for integer attributes (
>i4[1, 2]→[16777216, 33554432]), for both variable and global attributes, and in NETCDF4 as well as NETCDF3 formats via this engine. No warning or error is raised; inspecting the written file with h5py shows the byte-swapped values are committed to disk at write time.Cause: netCDF4-python's
setncattr/setncattsdo not byte-swap. The backend already converts variable data to native endianness for exactly this reason (_force_native_endianness, whose comment notes this "is not supported by the netCDF4 python library"), but attribute values reachsetncatts/setncattrunconverted. This PR applies the same native-endianness conversion to attribute values at both write sites (prepare_variableandset_attribute), with a regression test. Theh5netcdfwriter already handles identical input correctly and is unchanged.Following review, this PR also makes the
scipyengine return attribute arrays in native byte order on read, as it already does for variable data. The write-side guard stays for big-endian attributes from other sources (in-memory arrays, files read withh5netcdf) until the upstream fix (Unidata/netcdf4-python#1502) is released.Checklist
whats-new.rstAI Disclosure
- [x] I have tested any AI-generated content in my PR.
- [x] I take responsibility for any AI-generated content in my PR.
Tools: Claude (Claude Code). The defect was found by round-tripping the committed files in
xarray/tests/data/through defaultopen_dataset/to_netcdf. The repro above was confirmed twice in a clean environment at 551ced5 (netCDF4 1.7.4, scipy 1.18.1, numpy 2.5.2), the fix verified to clear it, and theTestNetCDF4Data/TestScipy/TestGenericNetCDFDatasuites run against the patch (427 passed). Prompt: a standing instruction to find, verify, and fix one upstream defect.[This is Claude Code on behalf of glaziermag]