Skip to content
Myr

♻️ When printing error tracebacks with Rich, default to not showing locals, which are sometimes verbose (#1072)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 11a6286 · Browse files · 6 files+40 added−43 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: svlandeg <sofie.vanlandeghem@gmail.com>
  • Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
  • Co-authored-by: svlandeg <svlandeg@github.com>

Diff stats

6 files+40 added−43 removed

  • tests:+7 added−7 removed
  • other:+33 added−36 removed

6 files+40 added−43 removed

modified:docs/tutorial/exceptions.md+22 added-28 removed
33 unmodified lines
34<font color="#F92672">│</font> <font color="#F92672">❱ </font> 8 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>35<font color="#F92672">│</font> 9 <font color="#F92672">│</font>36<font color="#F92672">│</font> 10 <font color="#F92672">│</font>37<font color="#F92672">│</font> 11 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>38<font color="#F92672">│</font> <font color="#F92672">│</font>39<font color="#F92672">│</font> <font color="#F4BF75">╭──── locals ────╮</font> <font color="#F92672">│</font>40<font color="#F92672">│</font> <font color="#F4BF75">│</font> name = <font color="#F4BF75">&apos;morty&apos;</font> <font color="#F4BF75">│</font> <font color="#F92672">│</font>41<font color="#F92672">│</font> <font color="#F4BF75">╰────────────────╯</font> <font color="#F92672">│</font>38<font color="#F92672">╰───────────────────────────────────────────────────────────────────╯</font>39<font color="#F92672"><b>TypeError: </b></font>can only concatenate str <b>(</b>not <font color="#A6E22E">&quot;int&quot;</font><b>)</b> to str40```41
22 unmodified lines
64```6566</div>6772## Disable Local Variables for Security68## Show Local Variables for Detailed Debugging6974If 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 <abbr title="a variable that lives only inside a function, its value is only visible inside of it">local variables</abbr>.70When using Rich, you can get more verbose output by printing the values of the <abbr title="a variable that lives only inside a function, its value is only visible inside of it">local variables</abbr> as part of the error message.7176This would be relevant in particular if your CLI application is being run on some CI (continuous integration) system that is recording the logs.72By default, this setting is disabled (since Typer 0.23.0) to avoid showing **delicate information**, for example a **password**, a **key** or a **token**.7378The default errors above, when using Rich, show a section with:74In these cases, it could be problematic if the automatic errors show the value in those local variables.7580```Python81name = 'morty'82```76This would be relevant in particular if your CLI application is being run on some CI (continuous integration) system that is recording the logs.7784In this case, `name` is a local variable, it comes from a parameter passed to the function.78However, if you do want to enable the setting, you can set the parameter `pretty_exceptions_show_locals=True` when creating the `typer.Typer()` application:7986But if it was something like a password, you would have liked to hide it.8788In that case, you can set the parameter `pretty_exceptions_show_locals=False` when creating the `typer.Typer()` application:8980{* docs_src/exceptions/tutorial002_py39.py hl[3] *}8192And now when you run it, you will see the error without the local variables:82Now, when using Rich, you will see the error with the local variables:8384<div class="termy">8586```console97$ python main.py supersecret87$ python main.py8889<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>100<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">8</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>90<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">5</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>91<font color="#F92672">│</font> <font color="#F92672">│</font>102<font color="#F92672">│</font> 5 <font color="#F92672">│</font>103<font color="#F92672">│</font> 6 <font color="#AE81FF"><b>@app</b></font>.command() <font color="#F92672">│</font>104<font color="#F92672">│</font> 7 <font color="#66D9EF">def</font> <font color="#A6E22E">main</font>(password: <font color="#A1EFE4">str</font>): <font color="#F92672">│</font>105<font color="#F92672">│</font> <font color="#F92672">❱ </font> 8 │ <font color="#A1EFE4">print</font>(password + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>106<font color="#F92672">│</font> 9 <font color="#F92672">│</font>107<font color="#F92672">│</font> 10 <font color="#F92672">│</font>108<font color="#F92672">│</font> 11 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>92<font color="#F92672">│</font> 2 <font color="#F92672">│</font>93<font color="#F92672">│</font> 3 <font color="#F92672">│</font>94<font color="#F92672">│</font> 4 <font color="#66D9EF">def</font> <font color="#A6E22E">main</font>(name: <font color="#A1EFE4">str</font> = <font color="#F4BF75">&quot;morty&quot;</font>): <font color="#F92672">│</font>95<font color="#F92672">│</font> <font color="#F92672">❱ </font> 5 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>96<font color="#F92672">│</font> 6 <font color="#F92672">│</font>97<font color="#F92672">│</font> 7 <font color="#F92672">│</font>98<font color="#F92672">│</font> 8 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>99<font color="#F92672">│</font> <font color="#F92672">│</font>100<font color="#F92672">│</font> <font color="#F4BF75">╭──── locals ────╮</font> <font color="#F92672">│</font>101<font color="#F92672">│</font> <font color="#F4BF75">│</font> name = <font color="#F4BF75">&apos;morty&apos;</font> <font color="#F4BF75">│</font> <font color="#F92672">│</font>102<font color="#F92672">│</font> <font color="#F4BF75">╰────────────────╯</font> <font color="#F92672">│</font>103<font color="#F92672">╰───────────────────────────────────────────────────────────────────╯</font>104<font color="#F92672"><b>TypeError: </b></font>can only concatenate str <b>(</b>not <font color="#A6E22E">&quot;int&quot;</font><b>)</b> to str105```106107</div>108115Note that you passed the password `supersecret`, but it's not shown anywhere in the error message.109Being able to see the values of local variables is very **helpful** to diagnose, **debug**, and fix problems.110117Being 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. 🔒111But you should only enable it if you're not dealing with delicate information.112113## Disable Short Output114115If you want to show the full exception, including the parts in Typer and Click, you can use the parameter `pretty_exceptions_short=False`:
154 unmodified lines
modified:docs_src/exceptions/tutorial002_py39.py+3 added-3 removed
1import typer23app = typer.Typer(pretty_exceptions_show_locals=False)3app = typer.Typer(pretty_exceptions_show_locals=True)456@app.command()7def main(password: str):8 print(password + 3)7def main(name: str = "morty"):8 print(name + 3)91011if __name__ == "__main__":12 app()
modified:tests/test_tutorial/test_exceptions/test_tutorial001.py+1 added-1 removed
26 unmodified lines
2728 assert "app()" not in result.stderr29 assert "print(name + 3)" in result.stderr30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" in result.stderr31 assert "name = 'morty'" not in result.stderr323334@pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]
24 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial002.py+5 added-5 removed
12 unmodified lines
1314def test_traceback_rich():15 file_path = Path(mod.__file__)16 result = subprocess.run(17 [sys.executable, "-m", "coverage", "run", str(file_path), "secret"],17 [sys.executable, "-m", "coverage", "run", str(file_path)],18 capture_output=True,19 encoding="utf-8",20 env={21 **os.environ,
3 unmodified lines
25 )26 assert "return get_command(self)(*args, **kwargs)" not in result.stderr2728 assert "app()" not in result.stderr29 assert "print(password + 3)" in result.stderr29 assert "print(name + 3)" in result.stderr30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" not in result.stderr31 assert "name = 'morty'" in result.stderr323334@pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36)37def test_standard_traceback_env_var(env_var: str):38 file_path = Path(mod.__file__)39 result = subprocess.run(40 [sys.executable, "-m", "coverage", "run", str(file_path), "secret"],40 [sys.executable, "-m", "coverage", "run", str(file_path)],41 capture_output=True,42 encoding="utf-8",43 env={**os.environ, env_var: "1"},44 )45 assert "return get_command(self)(*args, **kwargs)" in result.stderr4647 assert "app()" in result.stderr48 assert "print(password + 3)" in result.stderr48 assert "print(name + 3)" in result.stderr49 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr50 assert "name = 'morty'" not in result.stderr5152
7 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial003.py+1 added-1 removed
25 unmodified lines
2627 assert "app()" in result.stderr28 assert "print(name + 3)" in result.stderr29 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr30 assert "name = 'morty'" in result.stderr30 assert "name = 'morty'" not in result.stderr313233def test_script():34 result = subprocess.run(
5 unmodified lines
modified:typer/main.py+8 added-5 removed
478 unmodified lines
479 bool,480 Doc(481 """482 If Rich is installed, [error messages](https://typer.tiangolo.com/tutorial/exceptions/#exceptions-and-errors)483 will be nicely printed and include the values of local variables for easy debugging.484 However, if such a variable contains delicate information, you should consider setting `pretty_exceptions_show_locals` to `False`485 to enhance security.483 will be nicely printed.484485 If you set `pretty_exceptions_show_locals=True` it will also include the values of local variables for easy debugging.486487 However, if such a variable contains delicate information, you should consider leaving `pretty_exceptions_show_locals=False`488 (the default) to `False` to enhance security.489490 **Example**491492 ```python493 import typer494492 app = typer.Typer(pretty_exceptions_show_locals=False)495 app = typer.Typer(pretty_exceptions_show_locals=True)496 ```497 """498 ),496 ] = True,499 ] = False,500 pretty_exceptions_short: Annotated[501 bool,502 Doc(503 """
1512 unmodified lines