-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New decorator/context manager to catch exceptions when not in strict …
…mode. (#3531) ### What Resolves: #3513 The permutations and indirections in our new API style made this fairly involved. In an effort to be as-forgiving-as-possible I wanted to be able to handle exceptions down multiple levels in the construction hierarchy. For example: ``` rr.log("points", rr.Points3D(positions, colors='RED')) ``` Should produce an error: ``` my_points3d.py:12: RerunWarning: ColorBatch: ValueError(expected sequence of length of 3 or 4, received 1) ``` But still successfully log positions; just not colors. However, if something else goes wrong in that constructor, or the things using it, the next level up in the stack, for example `log` should catch the error instead. In this case no data will be sent, but we'll still get a warning and not crash the program. To handle this I introduced a hybrid decorator / context manager: `catch_and_log_exceptions` which tries to behave as sanely as possible in the context of a situation where we might recursively call multiple levels of functions, any one of which might produce an exception we would like to handle. The basic idea is that the deepest context manager to see the error will handle it and produce the message. This means we can add the `catch_and_log_exceptions` decorator or context manager anywhere we know that we can try to recover sanely. We'll get an error produced from that location, but the code higher up in the stack will still run. To improve the user experience, errors are appended to a warning list as they are encountered, and finally we output them when exiting the top-most context manager. This means we get warning lines that point to the user call-site rather than some arbitrary location within the guts of rerun SDK. One irritation I discovered is that decorating `__init__` methods causes two major issues. For one, it leaves the object in a potentially uninitialized state, just waiting to cascade into more exceptions in future. Additionally, it wreaks havoc on the type-checker. To accommodate this,`__init__` functions follow a pattern where they use the context-manager directly and then fallback on a hopefully safer code-path. The Archetype constructors now generally call `__attr_clear__` which is a new helper function designed to call `__attrs_init__` with `None` for all the known params. I also code-gen a `_clear` helper to use as a similar fallback path in builder functions. Recommend reviewing with: ![image](https://github.com/rerun-io/rerun/assets/3312232/6f05d3b8-8c5c-48ff-a227-c515a92735ea) Since a number of big snippets got indented when adding the context managers. ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/3531) (if applicable) - [PR Build Summary](https://build.rerun.io/pr/3531) - [Docs preview](https://rerun.io/preview/41f3a7dae887503cf098e063121e54bcdeb7378d/docs) <!--DOCS-PREVIEW--> - [Examples preview](https://rerun.io/preview/41f3a7dae887503cf098e063121e54bcdeb7378d/examples) <!--EXAMPLES-PREVIEW--> - [Recent benchmark results](https://ref.rerun.io/dev/bench/) - [Wasm size tracking](https://ref.rerun.io/dev/sizes/)
- Loading branch information
Showing
50 changed files
with
1,428 additions
and
423 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.