Skip to content

Exceptions and Errors

When your code has errors and you run it, it will show the error and an exception.

Typer does some tricks to help you detect those errors quickly.

Example Broken App

Let's take this example broken app:

import typer


def main(name: str = "morty"):
    print(name + 3)


if __name__ == "__main__":
    typer.run(main)

This code is broken because you can't sum a string and a number (name + 3).

Exceptions with Rich

If you have Rich installed (for example if you installed "typer[all]"), Typer will use it to automatically show you nicely printed errors.

It will omit all the parts of the traceback (the chain of things that called your function) that come from the internal parts in Typer and Click.

So, the error you see will be much clearer and simpler, to help you detect the problem in your code quickly:

fast →python main.py
╭──────────────── Traceback (most recent call last) ────────────────╮
/home/user/code/superapp/main.py:5 in main

2
3
4 def main(name: str = "morty"):
5 │ print(name + 3)
6
7
8 if __name__ == "__main__":

╭──── locals ────╮
name = 'morty'
╰────────────────╯
╰───────────────────────────────────────────────────────────────────╯
TypeError: can only concatenate str (not "int") to str

restart ↻

Exceptions without Rich

If you don't have Rich installed, Typer will still do some tricks to show you the information as clearly as possible:

fast →python main.py
Traceback (most recent call last):

File "main.py", line 12, in
typer.run(main)

File "main.py", line 8, in main
print(name + 3)

TypeError: can only concatenate str (not "int") to str

restart ↻

Disable Local Variables for Security

If your Typer application handles delicate information, for example a password, a key, a token, then it could be problematic if the automatic errors show the value in those local variables.

This would be relevant in particular if your CLI application is being run on some CI (continuous integration) system that is recording the logs.

The default errors above, when using Rich, show a section with:

name = 'morty'

In this case, name is a local variable, it comes from a parameter passed to the function.

But if it was something like a password, you would have liked to hide it.

In that case, you can create the typer.Typer() application explicitly and set the parameter pretty_exceptions_show_locals=False:

import typer

app = typer.Typer(pretty_exceptions_show_locals=False)


@app.command()
def main(password: str):
    print(password + 3)


if __name__ == "__main__":
    app()

And now when you run it, you will see the error without the local variables:

fast →python main.py supersecret
╭──────────────── Traceback (most recent call last) ────────────────╮
/home/user/code/superapp/main.py:8 in main

5
6 @app.command()
7 def main(password: str):
8 │ print(password + 3)
9
10
11 if __name__ == "__main__":
╰───────────────────────────────────────────────────────────────────╯
TypeError: can only concatenate str (not "int") to str

restart ↻

Note that you passed the password supersecret, but it's not shown anywhere in the error message.

Being able to see the values of local variables is normally very helpful to diagnose, debug, and fix problems, but if you are dealing with delicate information, now you know how to secure it. 🔒

Disable Short Output

If you want to show the full exception, including the parts in Typer and Click, you can use the parameter pretty_exceptions_short=False:

import typer

app = typer.Typer(pretty_exceptions_short=False)


@app.command()
def main(name: str = "morty"):
    print(name + 3)


if __name__ == "__main__":
    app()

Now when you run it, you will see the whole output:

fast →python main.py
╭──────────────── Traceback (most recent call last) ────────────────╮
/home/user/code/superapp/main.py:12 in <module>

9
10
11 if __name__ == "__main__":
12 │ app()
13

╭─────────────────────────── locals ────────────────────────────╮
__annotations__ = {}
__builtins__ = <module 'builtins' (built-in)>
__cached__ = None
__doc__ = None
__file__ = 'main.py'
__loader__ = <_frozen_importlib_external.SourceFileLoad…
object at 0x7f047db1c050>
__name__ = '__main__'
__package__ = None
__spec__ = None
app = <typer.main.Typer object at 0x7f047db51d90>
main = <function main at 0x7f047db56830>
typer = <module 'typer' from
'/home/user/code/superapp/env/lib/python3.…
╰───────────────────────────────────────────────────────────────╯

/home/user/code/superapp/env/lib/python3.7/site-packages/typer/ma
in.py:328 in __call__

/home/user/code/superapp/env/lib/python3.7/site-packages/typer/ma
in.py:311 in __call__

/home/user/code/superapp/env/lib/python3.7/site-packages/click/co
re.py:1130 in __call__

/home/user/code/superapp/env/lib/python3.7/site-packages/typer/co
re.py:723 in main

/home/user/code/superapp/env/lib/python3.7/site-packages/typer/co
re.py:216 in _main

/home/user/code/superapp/env/lib/python3.7/site-packages/click/co
re.py:1404 in invoke

/home/user/code/superapp/env/lib/python3.7/site-packages/click/co
re.py:760 in invoke

/home/user/code/superapp/env/lib/python3.7/site-packages/typer/ma
in.py:683 in wrapper

/home/user/code/superapp/main.py:8 in main

5
6 @app.command()
7 def main(name: str = "morty"):
8 │ print(name + 3)
9
10
11 if __name__ == "__main__":

╭──── locals ────╮
name = 'morty'
╰────────────────╯
╰───────────────────────────────────────────────────────────────────╯
TypeError: can only concatenate str (not "int") to str

restart ↻

Set Output Width

If you want to control the width of the yellow and red Rich exception borders, you can set the parameter pretty_exceptions_width to a specific integer (it's 100 by default):

import typer

app = typer.Typer(pretty_exceptions_width=120)


@app.command()
def main(name: str = "morty"):
    deep_dict_or_json = {
        "this_is_a_long_key": {
            "this_is_the_next_long_key": {
                "this_is_the_next_long_key": {
                    "this_is_the_next_long_key": {
                        "this_is_the_next_long_key": {
                            "this_is_the_next_long_key": {
                                "this_is_the_next_long_key": {
                                    "this_is_the_next_long_key": {
                                        "this_is_the_next_long_key": {
                                            "this_is_the_next_long_key": {
                                                "and_once_again_a_very_long_key": {
                                                    "but_this_is_not_the_end": {
                                                        "end": True
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    print(name + 3)
    print(deep_dict_or_json)


if __name__ == "__main__":
    app()

This prevents artificial line breaks in cases where there is sufficient horizontal space on the console.

Disable Pretty Exceptions

You can also entirely disable pretty exceptions with the parameter pretty_exceptions_enable=False:

import typer

app = typer.Typer(pretty_exceptions_enable=False)


@app.command()
def main(name: str = "morty"):
    print(name + 3)


if __name__ == "__main__":
    app()

And now you will see the full standard exception as with any other Python program:

fast →python main.py
Traceback (most recent call last):
File "main.py", line 12, in
app()
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 328, in __call__
raise e
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 311, in __call__
return get_command(self)(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/core.py", line 723, in main
**extra,
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/core.py", line 216, in _main
rv = self.invoke(ctx)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 683, in wrapper
return callback(**use_params) # type: ignore
File "main.py", line 8, in main
print(name + 3)
TypeError: can only concatenate str (not "int") to str

restart ↻

You could also achieve the same with the environment variable _TYPER_STANDARD_TRACEBACK=1.

This will work for any other Typer program too, in case you need to debug a problem in a Typer program made by someone else:

fast →export _TYPER_STANDARD_TRACEBACK=1python main.py

Traceback (most recent call last):
File "main.py", line 12, in
app()
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 328, in __call__
raise e
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 311, in __call__
return get_command(self)(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 1130, in __call__
return self.main(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/core.py", line 723, in main
**extra,
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/core.py", line 216, in _main
rv = self.invoke(ctx)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/click/core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "/home/user/code/superapp/env/lib/python3.7/site-packages/typer/main.py", line 683, in wrapper
return callback(**use_params) # type: ignore
File "main.py", line 8, in main
print(name + 3)
TypeError: can only concatenate str (not "int") to str

restart ↻