Skip to content

feat: sanitise attributes for HDF5#80

Open
LDeakin wants to merge 4 commits into
mainfrom
ld/feat/sanitise_hdf5_attrs
Open

feat: sanitise attributes for HDF5#80
LDeakin wants to merge 4 commits into
mainfrom
ld/feat/sanitise_hdf5_attrs

Conversation

@LDeakin

@LDeakin LDeakin commented Apr 20, 2026

Copy link
Copy Markdown
Member

Addresses TypeError: Object dtype dtype('O') has no native HDF5 equivalent on setncattr, which I was hitting running the cylinder unwrapping CLI.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves NetCDF/HDF5 attribute writing by sanitizing attribute values before calling setncattr, addressing failures when attributes contain unsupported (e.g., object) dtypes.

Changes:

  • Added helpers to detect HDF5-compatible NumPy dtypes and to coerce unsupported attribute values to HDF5-storable types.
  • Updated global attribute writing to route all attributes (including history) through the sanitizer.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/anu_ctlab_io/netcdf/_writer.py Outdated
Comment thread src/anu_ctlab_io/netcdf/_writer.py Outdated
Comment on lines +166 to +168
# Everything else: encode to string bytes
str_value = value if isinstance(value, str) else str(value)
return np.bytes_(str_value.encode("ascii"))

Copilot AI Apr 20, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str_value.encode("ascii") will raise UnicodeEncodeError for any non-ASCII attribute (e.g., filenames, user-supplied metadata). Consider adding an error strategy (e.g., errors="backslashreplace"/"replace") or falling back to UTF-8 encoding so NetCDF writing can’t crash on valid Unicode metadata.

Copilot uses AI. Check for mistakes.
Comment thread src/anu_ctlab_io/netcdf/_writer.py
@LDeakin
LDeakin marked this pull request as ready for review April 23, 2026 23:35
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.

2 participants