Skip to content

Move Xarray custom exceptions into a new xarray.errors module - #10285

Open
benbovy wants to merge 3 commits into
pydata:mainfrom
benbovy:add-errors-module
Open

Move Xarray custom exceptions into a new xarray.errors module#10285
benbovy wants to merge 3 commits into
pydata:mainfrom
benbovy:add-errors-module

Conversation

@benbovy

Copy link
Copy Markdown
Member

A bit cleaner, especially when displayed in error tracebacks.

@benbovy

Copy link
Copy Markdown
MemberAuthor

I think this is ready. The warning emitted in the doc build seems unrelated.

All errors are still exposed in Xarray's root namespace for backward compatibility, although we might want to remove this at some point and only access them via xarray.errors? How best to let users know that this is deprecated? I updated all references in the doc already (hopefully I didn't miss any).

@dcherian

Copy link
Copy Markdown
Contributor

Looks like we need to update this PR to include CoordinateValidationError from #10137

@dcherian

Copy link
Copy Markdown
Contributor

Seems good to me.

@keewis do you have any ideas for handling the deprecation cycle here?

@mathause

mathause commented May 8, 2025

Copy link
Copy Markdown
Collaborator

You could use a module level __getattr__ - add the following at the end of __init__.py (untested):

importwarningsdef__getattr__(attr):
moved_errors= (
"MergeError",
)
importxarray.errorsifattrinmoved_errors:
warnings.warn(f"Please import {attr} from xarray.errors", FutureWarning)
# NOTE: could maybe use importlib.import_module() but it registers the function # in sys.modules such that the warning is only called oncereturngetattr(xarray.errors, attr)
# required for ipython tab completionraiseAttributeError(f"module {__name__!r} has no attribute {attr!r}")

Sign up for freeto 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.

3 participants

@benbovy@dcherian@mathause