-
-
Notifications
You must be signed in to change notification settings - Fork 725
✨ Add option to set the width of the Rich exception output #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
637a6a1
f4b44a4
0d209d8
0460e47
3e81d23
0c82ef0
6d87cfd
cb05984
45e9736
b9d9fc0
ea2d475
1039c11
f037896
96c0b5e
4c4d1d5
91c79fb
fc4f3a6
7b0679b
5914d78
e1eec6d
1c58bdf
8872822
be51839
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -202,6 +202,17 @@ $ python main.py | |
|
||
</div> | ||
|
||
|
||
## 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): | ||
|
||
```Python hl_lines="3" | ||
{!../docs_src/exceptions/tutorial005.py!} | ||
``` | ||
|
||
This prevents artificial line breaks in cases where there is sufficient horizontal space on the console. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tiangolo: I'm not sure whether we want to show output here in a |
||
## Disable Pretty Exceptions | ||
|
||
You can also entirely disable pretty exceptions with the parameter `pretty_exceptions_enable=False`: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.exceptions import tutorial005 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_traceback_rich_pretty_width(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test simply checks that the program runs like expected, but it doesn't (yet) actually test the Rich borders length as I wasn't quite sure how to do that 🤔 |
||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", str(file_path)], | ||
capture_output=True, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, | ||
) | ||
print(result.stderr) | ||
|
||
assert "print(name + 3)" in result.stderr | ||
assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr | ||
assert "name = 'morty'" in result.stderr | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
capture_output=True, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, should we rename tutorial004 to 005 and call the new one from this PR 004?