Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 64 additions & 6 deletions docs/tutorial/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ So, the error you see will be **much clearer** and simpler, to help you detect t
$ python main.py

<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>
<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>
<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">9</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> 5 <font color="#F92672">│</font>
<font color="#F92672">│</font> 6 <font color="#FF00FF">@app</font>.command() <font color="#F92672">│</font>
<font color="#F92672">│</font> 7 <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>
<font color="#F92672">│</font> <font color="#F92672">❱ </font> 8 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>
<font color="#F92672">│</font> 9 <font color="#F92672">│</font>
<font color="#F92672">│</font> 10 <font color="#F92672">│</font>
<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>
<font color="#F92672">│</font> 8 │ # The line below will cause TypeError because you cannot <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">❱ </font> 9 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>
<font color="#F92672">│</font> 10 <font color="#F92672">│</font>
<font color="#F92672">│</font> 11 <font color="#F92672">│</font>
<font color="#F92672">│</font> 12 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>
<font color="#F92672">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#F92672"><b>TypeError: </b></font>can only concatenate str <b>(</b>not <font color="#A6E22E">&quot;int&quot;</font><b>)</b> to str
```
Expand Down Expand Up @@ -192,6 +192,64 @@ $ python main.py

</div>


## Enable word wrapping for code lines to avoid truncation

By default, Typer will truncate the code lines in the error message if they are too long.

<div class="termy">

```console
$ python main.py

<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>
<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">9</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> 6 <font color="#FF00FF">@app</font>.command() <font color="#F92672">│</font>
<font color="#F92672">│</font> 7 <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>
<font color="#F92672">│</font> 8 │ # The line below will cause TypeError because you cannot <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">❱ </font> 9 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>
<font color="#F92672">│</font> 10 <font color="#F92672">│</font>
<font color="#F92672">│</font> 11 <font color="#F92672">│</font>
<font color="#F92672">│</font> 12 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>
<font color="#F92672">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#F92672"><b>TypeError: </b></font>can only concatenate str <b>(</b>not <font color="#A6E22E">&quot;int&quot;</font><b>)</b> to str
```

</div>

Note the comment line is truncated with `# The line below will cause TypeError because you cannot ` and the rest of the line is not shown.

You can configure it to wrap the code lines instead of truncating them with the parameter `pretty_exceptions_wrap_code=True`:

{* docs_src/exceptions/tutorial005_py310.py hl[3] *}

Now when you run it, you will see the full lines:


<div class="termy">

```console
$ python main.py

<font color="#F92672">╭──────────────── </font><font color="#F92672"><b>Traceback (most recent call last)</b></font><font color="#F92672"> ────────────────╮</font>
<font color="#F92672">│</font> <font color="#A37F4E">/home/user/code/superapp/</font><font color="#F4BF75"><b>main.py</b></font>:<font color="#66D9EF">9</font> in <font color="#A6E22E">main</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">│</font>
<font color="#F92672">│</font> 6 <font color="#FF00FF">@app</font>.command() <font color="#F92672">│</font>
<font color="#F92672">│</font> 7 <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>
<font color="#F92672">│</font> 8 │ # The line below will cause TypeError because you cannot <font color="#F92672">│</font>
<font color="#F92672">│</font> concatenate string and integer. <font color="#F92672">│</font>
<font color="#F92672">│</font> <font color="#F92672">❱ </font> 9 │ <font color="#A1EFE4">print</font>(name + <font color="#66D9EF">3</font>) <font color="#F92672">│</font>
<font color="#F92672">│</font> 10 <font color="#F92672">│</font>
<font color="#F92672">│</font> 11 <font color="#F92672">│</font>
<font color="#F92672">│</font> 12 <font color="#66D9EF">if</font> <font color="#F92672">__name__</font> == <font color="#F4BF75">&quot;__main__&quot;</font>: <font color="#F92672">│</font>
<font color="#F92672">╰───────────────────────────────────────────────────────────────────╯</font>
<font color="#F92672"><b>TypeError: </b></font>can only concatenate str <b>(</b>not <font color="#A6E22E">&quot;int&quot;</font><b>)</b> to str
```

</div>


## Disable Pretty Exceptions

You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`:
Expand Down
1 change: 1 addition & 0 deletions docs_src/exceptions/tutorial001_py310.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@app.command()
def main(name: str = "morty"):
# The line below will cause TypeError because you cannot concatenate string and integer.
print(name + 3)


Expand Down
13 changes: 13 additions & 0 deletions docs_src/exceptions/tutorial005_py310.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import typer

app = typer.Typer(pretty_exceptions_word_wrap=True)


@app.command()
def main(name: str = "morty"):
# The line below will cause TypeError because you cannot concatenate string and integer.
print(name + 3)


if __name__ == "__main__":
app()
4 changes: 4 additions & 0 deletions tests/test_tutorial/test_exceptions/test_tutorial001.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def test_traceback_rich():
assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr
assert "name = 'morty'" not in result.stderr

# Code lines are not truncated to 88 characters if terminal width is large enough
CODE_LINE = " # The line below will cause TypeError because you cannot concatenate string and integer."
assert CODE_LINE.lstrip() in result.stderr


@pytest.mark.parametrize(
"env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]
Expand Down
19 changes: 19 additions & 0 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,23 @@ def callback():
"""
),
] = True,
pretty_exceptions_word_wrap: Annotated[
bool,
Doc(
"""
By default, [pretty exceptions formatted with Rich](https://typer.tiangolo.com/tutorial/exceptions/#exceptions-with-rich) truncate long lines.
If you want to disable this behavior and enable word wrapping instead, you can set the parameter `pretty_exceptions_word_wrap` to `True`:

**Example**

```python
import typer

app = typer.Typer(pretty_exceptions_word_wrap=True)
```
"""
),
] = False,
):
self._add_completion = add_completion
self.rich_markup_mode: MarkupMode = rich_markup_mode
Expand All @@ -522,6 +539,7 @@ def callback():
self.pretty_exceptions_enable = pretty_exceptions_enable
self.pretty_exceptions_show_locals = pretty_exceptions_show_locals
self.pretty_exceptions_short = pretty_exceptions_short
self.pretty_exceptions_word_wrap = pretty_exceptions_word_wrap
self.info = TyperInfo(
name=name,
cls=cls,
Expand Down Expand Up @@ -1147,6 +1165,7 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
pretty_exceptions_enable=self.pretty_exceptions_enable,
pretty_exceptions_show_locals=self.pretty_exceptions_show_locals,
pretty_exceptions_short=self.pretty_exceptions_short,
pretty_exceptions_word_wrap=self.pretty_exceptions_word_wrap,
),
)
raise e
Expand Down
2 changes: 2 additions & 0 deletions typer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,10 +634,12 @@ def __init__(
pretty_exceptions_enable: bool = True,
pretty_exceptions_show_locals: bool = True,
pretty_exceptions_short: bool = True,
pretty_exceptions_word_wrap: bool = False,
) -> None:
self.pretty_exceptions_enable = pretty_exceptions_enable
self.pretty_exceptions_show_locals = pretty_exceptions_show_locals
self.pretty_exceptions_short = pretty_exceptions_short
self.pretty_exceptions_word_wrap = pretty_exceptions_word_wrap


class TyperPath(click.Path):
Expand Down
2 changes: 2 additions & 0 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,5 +749,7 @@ def get_traceback(
show_locals=exception_config.pretty_exceptions_show_locals,
suppress=internal_dir_names,
width=MAX_WIDTH,
code_width=MAX_WIDTH,
word_wrap=exception_config.pretty_exceptions_word_wrap,
)
return rich_tb
Loading