refactor tools: centralize error handling with explain_exc and ensure #745
+285
−82
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@jph00 What do you think about centralizing it in a single helper?
The could look like this:
I’m leaning towards an explicit
try/exceptwith a helper func, rather than a decorator, as I worry a decorator might suggest some "magic" to convert a function to a tool.Re. assert, I find them easier to reason about or skip than
ifstatements, especially with complex logic. But they can be disabled with -O. If we would like to avoid this edge case what would you say about simpleensure(b, m)helper that raiseValueErrorLike:Regarding the
explain_excit could look like:or using python matching
I've implemented rerise as it is easier to catch multiple exceptions
Here is how the current error handling looks like