question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature Request] BeartypeException object metadata

See original GitHub issue

Hello 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:open
  • Created a year ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Jasha10commented, Dec 3, 2022

Hooray!

1reaction
leyceccommented, Dec 3, 2022

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! 🎅

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · beartype/beartype - GitHub
[Feature Request] BeartypeException object metadata. #180 opened on Oct 23 by Jasha10 · 8. [Feature Request] Deeply type-check set and frozenset containers.
Read more >
Feature request: Provide method to retrieve application “client ...
I have a need to vary the look and feel and functionality of the Universal Login Page based on which client the user...
Read more >
requested features [Zotero Documentation]
Provide a way to update/re-retrieve metadata for an item already in the library. Status: Planned in conjunction with improved retrieve metadata ...
Read more >
How do I file a project feature request? - Google Support
You can file a feature request by clicking "New Issue" and entering a summary and description. A feature request will not have "steps...
Read more >
Working with objects - Hitachi Vantara Knowledge
You can add custom metadata to an object in the same request as you use to store the object. To do this, you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found