Skip to content

Use add_note() to annotate exceptions when encoding fails #10464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025

Conversation

shoyer
Copy link
Member

@shoyer shoyer commented Jun 29, 2025

We now always add variable names and contents when encoding fails, in contrast to the current practice where variable names and values are only sometimes specified, based on the name passed into VariableCoder.encode(). This provides better debugging experience for users.

In the future, we might remove name from VariableCoder.encode() because this makes it redundant.

For example, attempting to save a int64 fail in netCDF3 file now shows something like the following:

ValueError: could not safely cast array from int64 to int32...
Raised while encoding variable 'invalid' with value <xarray.Variable (invalid: 1)> Size: 8B
array([9223372036854775807])

Note that Exception.add_note() is a Python 3.11+ feature, so this PR will need to wait until #10438 is submitted.

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

We now always add variable names and contents when encoding fails, in
contrast to the current practice where variable names and values are
only sometimes specified, based on the `name` passed into
`VariableCoder.encode()`. This provides better debugging experience for
users.

In the future, we might remove `name` from `VariableCoder.encode()`
because this makes it redundant.

For example, attempting to save a int64 fail in netCDF3 file now shows
something like the following:

    ValueError: could not safely cast array from int64 to int32...
    Raised while encoding variable 'invalid' with value <xarray.Variable (invalid: 1)> Size: 8B
    array([9223372036854775807])

Note that `Exception.add_note()` is a Python 3.11+ feature, so this PR
will need to wait until pydata#10438 is submitted.
@kmuehlbauer
Copy link
Contributor

kmuehlbauer commented Jun 29, 2025

That's great for Errors, but for Warnings we would still need the name-kwarg, or am I missing something?

@keewis
Copy link
Collaborator

keewis commented Jun 29, 2025

technically, warnings.warn also accepts Warning instances and Warning inherits from Exception, so this should be possible:

import warnings

w = UserWarning("message")
w.add_note("note")

warnings.warn(w)

However, the note on warnings is usually not printed (unless the warning is raised as an exception), so yes, I think we still need the name for warnings.

@kmuehlbauer
Copy link
Contributor

However, the note on warnings is usually not printed (unless the warning is raised as an exception), so yes, I think we still need the name for warnings.

I've digged a bit deeper. So a warning equipped with add_note(), this additional note isn't printed when emitted as warning unfortunately. It only gets printed when emitted as error.

It would be possible to achieve this by a custom warning like SerializationWarning for all encoding/decoding warnings. In with warnings.catch_warnings() we can elevate the custom warning to error, catch elevated custom warning, rewrite the message with the added note and re-emit the warning. It would be much easier, if the notes would get printed for warnings, too. I didn't find anything related on the CPython issue tracker, most likely because of my mediocre search-fu.

@dcherian dcherian added the plan to merge Final call for comments label Jul 2, 2025
Copy link
Member

@spencerkclark spencerkclark left a comment

Choose a reason for hiding this comment

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

This is nice, thanks!

@dcherian dcherian merged commit 0a4b378 into pydata:main Jul 2, 2025
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants