♻️ 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
Provenance
- Commit
458325d- Tree
84967ff- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
11a6286
6 files changed+40 added−43 removed
modified:docs/tutorial/exceptions.md+22 added-28 removed
33 unmodified lines34 <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">"__main__"</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">'morty'</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">"int"</font><b>)</b> to str40 ```41 22 unmodified lines64 ```65 66 </div>67 72-## Disable Local Variables for Security68+## Show Local Variables for Detailed Debugging69 74-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 <abbr title="a variable that lives only inside a function, its value is only visible inside of it">local variables</abbr>.70+When 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.71 76-This would be relevant in particular if your CLI application is being run on some CI (continuous integration) system that is recording the logs.72+By default, this setting is disabled (since Typer 0.23.0) to avoid showing **delicate information**, for example a **password**, a **key** or a **token**.73 78-The default errors above, when using Rich, show a section with:74+In these cases, it could be problematic if the automatic errors show the value in those local variables.75 80-```Python81-name = 'morty'82-```76+This would be relevant in particular if your CLI application is being run on some CI (continuous integration) system that is recording the logs.77 84-In this case, `name` is a local variable, it comes from a parameter passed to the function.78+However, if you do want to enable the setting, you can set the parameter `pretty_exceptions_show_locals=True` when creating the `typer.Typer()` application:79 86-But if it was something like a password, you would have liked to hide it.87-88-In that case, you can set the parameter `pretty_exceptions_show_locals=False` when creating the `typer.Typer()` application:89-80 {* docs_src/exceptions/tutorial002_py39.py hl[3] *}81 92-And now when you run it, you will see the error without the local variables:82+Now, when using Rich, you will see the error with the local variables:83 84 <div class="termy">85 86 ```console97-$ python main.py supersecret87+$ python main.py88 89 <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">"__main__"</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">"morty"</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">"__main__"</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">'morty'</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">"int"</font><b>)</b> to str105 ```106 107 </div>108 115-Note that you passed the password `supersecret`, but it's not shown anywhere in the error message.109+Being able to see the values of local variables is very **helpful** to diagnose, **debug**, and fix problems.110 117-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. 🔒111+But you should only enable it if you're not dealing with delicate information.112 113 ## Disable Short Output114 115 If 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 linesmodified:docs_src/exceptions/tutorial002_py39.py+3 added-3 removed
1 import typer2 3-app = typer.Typer(pretty_exceptions_show_locals=False)3+app = typer.Typer(pretty_exceptions_show_locals=True)4 5 6 @app.command()7-def main(password: str):8- print(password + 3)7+def main(name: str = "morty"):8+ print(name + 3)9 10 11 if __name__ == "__main__":12 app()modified:tests/test_tutorial/test_exceptions/test_tutorial001.py+1 added-1 removed
26 unmodified lines27 28 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.stderr32 33 34 @pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]24 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial002.py+5 added-5 removed
12 unmodified lines13 14 def 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 lines25 )26 assert "return get_command(self)(*args, **kwargs)" not in result.stderr27 28 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.stderr32 33 34 @pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36 )37 def 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.stderr46 47 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.stderr51 52 7 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial003.py+1 added-1 removed
25 unmodified lines26 27 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.stderr31 32 33 def test_script():34 result = subprocess.run(5 unmodified linesmodified:typer/main.py+8 added-5 removed
478 unmodified lines479 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.484 485+ If you set `pretty_exceptions_show_locals=True` it will also include the values of local variables for easy debugging.486+487+ 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.489+490 **Example**491 492 ```python493 import typer494 492- 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