[Feature Request] BeartypeException object metadata
See original GitHub issueHello hello @leycec!
I have an idea to improve the debugging experience by adding metadata to beartype’s exceptions. In a nutshell the proposal is to add attributes to the raised beartype.roar.BeartypeException
instance so that, with a try: ... except
, it’s easy to inspect the inputs and beartyped function that caused the exception.
Motivating example
Here’s a typical beartype exception:
beartype.roar.BeartypeCallHintParamViolation: @beartyped my_module.MyClass.__init__() parameter my_array="array([[False, False],
[False, False],
[ True, False],
[ True, False],
... violates type hint NArray[Shape['11 foo'], bool], as <protocol "numpy.ndarray"> "array([[False, False],
[False, False],
[ True, False],
[ True, False],
... not instance of <class "ntyping.common.NArray[Shape['11 foo'], bool]">.
(for context, ntyping
is my personal fork of ramonhagenaars/nptyping
.)
To debug this error, I need to know the shape and dtype of the my_array
array. This would be easy if the BeartypeCallHintParamViolation
exception instance would expose an interface for accessing the my_array
argument:
try:
... # bad code
except roar.BeartypeCallHintParamViolation as exc:
bad_arg = exc.interface_exposing_the_input_that_violated_your_type_hint
print(f"{bad_arg.shape=}")
print(f"{bad_arg.dtype=}")
...
Of course, simply calling pdb.post_mortem()
in the except
block works well enough. Also, I know that implementing something like this might have implications for e.g. memory usage; perhaps keeping a reference to my_array
in the exception object will prevent that array from being garbage collected. There might be other tradeoffs I’m not aware of. Anyway, I wanted to throw this idea out there; do with it what you will 😛
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top GitHub Comments
Hooray!
Oh ho ho. I’m currently digging deep into this ancient feature request, which resonates into the future like a time capsule from a Halcyon Age long past. Implementing this is proving surprisingly non-trivial. It’s just as well that nobody did nuthin’ until now. Laziness prevails, friends.
Nonetheless, this is slated for our upcoming
beartype 0.12.0
release. Let’s see what Santa Bearclaws brings to nice Pythonistas this Christmas. We deserve something more than raw salmon in our stockings for once! 🎅