Skip to content

Commit dfb21ad

Browse files
authored
🚸 Don't truncate code lines in traceback when formatted with Rich (#1695)
1 parent c9554ec commit dfb21ad

5 files changed

Lines changed: 61 additions & 2 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ classifiers = [
3535
dependencies = [
3636
"click >= 8.2.1",
3737
"shellingham >=1.3.0",
38-
"rich >=12.3.0",
38+
"rich >=13.8.0",
3939
"annotated-doc >=0.0.2",
4040
]
4141
readme = "README.md"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import typer
2+
import typer.main
3+
4+
app = typer.Typer()
5+
6+
7+
@app.command()
8+
def main(name: str = "morty"):
9+
# The line below will cause TypeError because you cannot concatenate string and integer.
10+
print(name + 3)
11+
12+
13+
if __name__ == "__main__":
14+
app()

‎tests/test_tracebacks.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,46 @@ def test_unmodified_traceback():
6262
assert "typer.main.get_command(broken_app)()" in result.stderr
6363
assert "print(name + 3)" in result.stderr
6464
assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr
65+
66+
67+
def test_rich_exceptions_dont_truncate_code_on_wide_terminal():
68+
file_path = Path(__file__).parent / "assets/type_error_rich.py"
69+
result = subprocess.run(
70+
[sys.executable, "-m", "coverage", "run", str(file_path)],
71+
capture_output=True,
72+
encoding="utf-8",
73+
env={
74+
**os.environ,
75+
"TERMINAL_WIDTH": "120",
76+
"TYPER_STANDARD_TRACEBACK": "",
77+
"_TYPER_STANDARD_TRACEBACK": "",
78+
},
79+
)
80+
81+
assert (
82+
"# The line below will cause TypeError because you cannot concatenate string and integer."
83+
in result.stderr
84+
)
85+
86+
87+
def test_rich_exceptions_wrap_words_on_small_width_terminal():
88+
file_path = Path(__file__).parent / "assets/type_error_rich.py"
89+
result = subprocess.run(
90+
[sys.executable, "-m", "coverage", "run", str(file_path)],
91+
capture_output=True,
92+
encoding="utf-8",
93+
env={
94+
**os.environ,
95+
"TERMINAL_WIDTH": "80",
96+
"TYPER_STANDARD_TRACEBACK": "",
97+
"_TYPER_STANDARD_TRACEBACK": "",
98+
},
99+
)
100+
101+
# Long line is wrapped, so the full line is not in the output, but parts of it are.
102+
assert "# The line below will cause TypeError because you cannot" in result.stderr
103+
assert "concatenate string and integer." in result.stderr
104+
assert (
105+
"# The line below will cause TypeError because you cannot concatenate string and integer."
106+
not in result.stderr
107+
)

‎typer/rich_utils.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,5 +749,7 @@ def get_traceback(
749749
show_locals=exception_config.pretty_exceptions_show_locals,
750750
suppress=internal_dir_names,
751751
width=MAX_WIDTH,
752+
code_width=None,
753+
word_wrap=True,
752754
)
753755
return rich_tb

‎uv.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)