Configuring Pretty Exceptions
You can customize how unhandled exceptions are displayed in the terminal by configuring the "Pretty Exceptions" feature. This feature uses the Rich library to provide colorized, readable stack traces that can optionally include local variable values and filter out internal library frames.
Enable Local Variables in Tracebacks
To see the values of local variables in your stack trace (useful for debugging), set pretty_exceptions_show_locals=True when initializing your Typer app.
import typer
# Initialize app with local variables enabled in exceptions
app = typer.Typer(pretty_exceptions_show_locals=True)
@app.command()
def calculate(x: int, y: int):
# If y is 0, this will raise ZeroDivisionError
# The traceback will show the values of x and y
result = x / y
print(result)
if __name__ == "__main__":
app()
Configuration Parameters
The Typer class accepts three main parameters that populate a DeveloperExceptionConfig object (defined in typer/models.py) to control exception formatting:
pretty_exceptions_enable(default:True): Toggles the Rich-formatted exception handler. IfFalse, Typer uses the standard Python traceback.pretty_exceptions_show_locals(default:False): WhenTrue, includes the values of local variables for every frame in the stack trace.pretty_exceptions_short(default:True): WhenTrue, hides internal Typer and Click library frames from the traceback to focus on your code.
Show Full Tracebacks
By default, Typer shortens tracebacks to hide internal library calls. To see the full stack trace including Typer and Click internals, set pretty_exceptions_short=False.
import typer
# Show the full stack trace including internal library frames
app = typer.Typer(pretty_exceptions_short=False)
@app.command()
def main():
raise RuntimeError("Something went wrong")
if __name__ == "__main__":
app()
Disable Pretty Exceptions
If you prefer the standard Python traceback or are using a different exception handler, you can disable the feature entirely.
import typer
# Disable Rich-formatted exceptions
app = typer.Typer(pretty_exceptions_enable=False)
@app.command()
def main():
1 / 0
if __name__ == "__main__":
app()
Global Override via Environment Variables
You can force Typer to use standard Python tracebacks without changing your code by setting the TYPER_STANDARD_TRACEBACK environment variable.
# Force standard tracebacks for any Typer app
export TYPER_STANDARD_TRACEBACK=1
python my_app.py
Troubleshooting and Security
- Security Warning: Be cautious when enabling
pretty_exceptions_show_locals=Truein production environments. If your application handles sensitive data (like passwords or API keys) in local variables, they will be printed to the terminal if an unhandled exception occurs. - Execution Context: Pretty exceptions only trigger for exceptions raised during the execution of a Typer app (inside the
__call__method). Exceptions raised during module import or app initialization will use the standard Python hook. - Rich Dependency: This feature requires the
richlibrary. Ifrichis not installed, Typer will fall back to standard tracebacks regardless of these settings.