Skip to content
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

Add path to error message in map_over_subtree #264

Merged

Conversation

TomNicholas
Copy link
Collaborator

@TomNicholas TomNicholas commented Oct 23, 2023

return func(*args, **kwargs)
except Exception as e:
# Add the context information to the error message
e.add_note(f"Raised whilst mapping function over node with path {path}")
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Adding notes to exceptions is a new feature in python 3.11.

Perhaps we should wait before merging this? If I understand xarray's deprecation policy correctly then 3.11 will only be the minimum supported version of python 24 months after 3.11 is released, which is 12 months from now...

Copy link
Contributor

Choose a reason for hiding this comment

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

you could use a helper for this:

def add_note(err: BaseException, msg: str) -> None:
    # TODO: remove once python 3.10 can be dropped
    if sys.version_info < (3, 11):
        err.__notes__ = getattr(err, "__notes__", []) + [msg]
    else:
        err.add_note(msg)

@TomNicholas TomNicholas enabled auto-merge (squash) October 24, 2023 00:06
@TomNicholas TomNicholas enabled auto-merge (squash) October 24, 2023 00:43
@TomNicholas TomNicholas merged commit dffd32c into xarray-contrib:main Oct 24, 2023
14 checks passed
@TomNicholas TomNicholas deleted the error_context_map_over_subtree branch October 24, 2023 01:40
flamingbear pushed a commit to flamingbear/rewritten-datatree that referenced this pull request Jan 19, 2024
…#264

* test

* implementation

* formatting

* add version check, if not using 3.11 then you just won't get the extra info in the error message

* whatsnew

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* use better helper function

* xfail test, because this does actually work...

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

Debug flag for map_over_subtree
2 participants