How to *not* set sys.excepthook? #1024
Replies: 2 comments
-
|
I've done some debugging and found I can capture |
Beta Was this translation helpful? Give feedback.
-
|
Hi @charmoniumQ, and thanks for the debugging, @djachenko - that capture/restore trick makes sense as a workaround, and it actually points at exactly what's going on under the hood. I dug into Typer's source (main.py) to see exactly what's happening. Two things combine to cause this: 1st---> Typer only saves the ORIGINAL sys.excepthook once at import time - literally the first time Put those together and your example has the ordering backwards: import typer runs first so Typer captures the plain stdlib hook as its fallback - before your rich.traceback.install() call ever runs. That's exactly why you saw the stdlib traceback instead of your Rich config even when trying to disable Typer's own renderer. The fix is to flip the order so Typer's snapshot captures your Rich hook instead: import rich.traceback
rich.traceback.install(
show_locals=False,
suppress=[
# your config here
],
)
import typer # import typer AFTER installing your rich hook
app = typer.Typer(pretty_exceptions_enable=False)
@app.command()
def main():
x = 2
raise ValueError()
if __name__ == "__main__":
app()With pretty_exceptions_enable=False, Typer's hook falls straight through to the "original" hook it saved - which is now your fully-configured Rich installer, locals/suppress/width and all, instead of stdlib or Typer's own version. One caveat worth knowing: this only works if nothing else in your dependency chain imports typer before your script does. If some other library imports it first, the snapshot happens then instead, and you'd need @djachenko's capture/restore approach as a fallback for that case. Also worth linking: #508 is tracking this exact friction as an open issue, if you want a supported programmatic override down the line rather than relying on import order. Hope this gets you the traceback config you were after! Let me know if the reordering works on your end. (And if this clears things up, I'd really appreciate it if you could mark this as the answer! ) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
It seems that Typer overwrites the
sys.excepthookhere and it's impossible to disable that functionality.This is important because I have a nice
rich.tracebackconfiguration that suppresses stackframes, customizes width, etc. Typer callsrich.traceback, but lacks these customization options I want set.Even passing
pretty_exceptions_short=Falsestill sets thesys.excepthookto the (ugly) stdlib excepthook here. Arguably, that flag or perhaps a new flag should just avoid settingsys.excepthookon line 318.I can implement this in a PR, but I was wondering if it would be worth it for others, or if that is a feature that would be accepted.
rich_markup_mode=None is not right either; it disables all of Rich markup; I just want Typer to not overwrite the
sys.excepthook.Operating System
Linux
Operating System Details
NixOS
Typer Version
0.12.3
Python Version
Python 3.12.5
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions