Skip to content

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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

benbovy
Copy link
Member

@benbovy benbovy commented May 5, 2025

A bit cleaner, especially when displayed in error tracebacks.

@benbovy
Copy link
Member Author

benbovy commented May 6, 2025

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
Contributor

dcherian commented May 6, 2025

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

@dcherian
Copy link
Contributor

dcherian commented May 8, 2025

Seems good to me.

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

@mathause
Copy link
Collaborator

mathause commented May 8, 2025

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

import warnings

def __getattr__(attr):

    moved_errors = (
        "MergeError",
    )

    import xarray.errors

    if attr in moved_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 once

        return getattr(xarray.errors, attr)

    # required for ipython tab completion
    raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")

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.

3 participants