← Back to Pull requests

Fix silent byte-swapping of non-native-endian attribute values in the netcdf4 backend #11543

Closed
unknown closed this pull request without merging on Oct 16, 2026 07:01 UTC
View on GitHub
unknown unknown commented on Aug 24, 2026 21:19 UTC
Author

Description

Writing a dataset whose attributes hold non-native-endian numpy arrays silently stores byte-swapped values with the netcdf4 engine. The easiest way to get such attributes is reading a netCDF-3 file with the scipy engine, which returns attribute arrays big-endian — so this fully-default round-trip corrupts metadata on disk:

import xarray as xr
ds = xr.open_dataset("xarray/tests/data/example_1.nc.gz")  # scipy engine guessed
ds.load().to_netcdf("out.nc")                              # netcdf4 engine guessed
xr.open_dataset("out.nc")["rh"].attrs["valid_range"]
# array([0.00000e+000, 3.03865e-319])  — was [0., 1.]; 3.03865e-319 is byte-swapped 1.0

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/setncatts do 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 reach setncatts/setncattr unconverted. This PR applies the same native-endianness conversion to attribute values at both write sites (prepare_variable and set_attribute), with a regression test. The h5netcdf writer already handles identical input correctly and is unchanged.

Following review, this PR also makes the scipy engine 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 with h5netcdf) until the upstream fix (Unidata/netcdf4-python#1502) is released.

Checklist

  • Tests added

  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • AI Disclosure

  • This PR contains AI-generated content.

  • - [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 default open_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 the TestNetCDF4Data/TestScipy/TestGenericNetCDFData suites 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]

    Diff: 345 lines (+258 / -87) across 4 files Reviewer: glaziermag Review duration: 75462m
    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
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.7% · Safe to merge
    16.7%
    Repository