Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Add path to error message in map_over_subtree #264

Merged

Conversation

TomNicholas
Copy link
Member

@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
Member 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 disabled auto-merge 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
12 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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Debug flag for map_over_subtree
2 participants