Skip to content
Myr

🚸 Don't truncate code lines in traceback when formatted with Rich (#1695)

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

authored by Motov Yurii · committed by GitHub · parent c9554ec · Browse files · 5 files+61 added−2 removed
This commit

State

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

Diff stats

5 files+61 added−2 removed

  • tests:+57 added−0 removed
  • other:+4 added−2 removed

5 files+61 added−2 removed

added:tests/assets/type_error_rich.py+14 added
1import typer2import typer.main34app = typer.Typer()567@app.command()8def main(name: str = "morty"):9 # The line below will cause TypeError because you cannot concatenate string and integer.10 print(name + 3)111213if __name__ == "__main__":14 app()
modified:tests/test_tracebacks.py+43 added
60 unmodified lines
61 )62 assert "typer.main.get_command(broken_app)()" in result.stderr63 assert "print(name + 3)" in result.stderr64 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr656667def 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 )8081 assert (82 "# The line below will cause TypeError because you cannot concatenate string and integer."83 in result.stderr84 )858687def 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 )100101 # 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.stderr103 assert "concatenate string and integer." in result.stderr104 assert (105 "# The line below will cause TypeError because you cannot concatenate string and integer."106 not in result.stderr107 )
modified:typer/rich_utils.py+2 added
747 unmodified lines
748 exc.__traceback__,749 show_locals=exception_config.pretty_exceptions_show_locals,750 suppress=internal_dir_names,751 width=MAX_WIDTH,752 code_width=None,753 word_wrap=True,754 )755 return rich_tb
modified:pyproject.toml+1 added-1 removed
33 unmodified lines
34]35dependencies = [36 "click >= 8.2.1",37 "shellingham >=1.3.0",38 "rich >=12.3.0",38 "rich >=13.8.0",39 "annotated-doc >=0.0.2",40]41readme = "README.md"42
172 unmodified lines
modified:uv.lock+1 added-1 removed
1915 unmodified lines
1916[package.metadata]1917requires-dist = [1918 { name = "annotated-doc", specifier = ">=0.0.2" },1919 { name = "click", specifier = ">=8.2.1" },1920 { name = "rich", specifier = ">=12.3.0" },1920 { name = "rich", specifier = ">=13.8.0" },1921 { name = "shellingham", specifier = ">=1.3.0" },1922]19231924[package.metadata.requires-dev]
127 unmodified lines