🚸 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
Provenance
- Commit
dfb21ad- Tree
40f106d- Author
Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c9554ec
5 files changed+61 added−2 removed
added:tests/assets/type_error_rich.py+14 added
1+import typer2+import typer.main3+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()modified:tests/test_tracebacks.py+43 added
60 unmodified lines61 )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.stderr65+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.stderr84+ )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.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 lines748 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_tbmodified:pyproject.toml+1 added-1 removed
33 unmodified lines34 ]35 dependencies = [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 ]41 readme = "README.md"42 172 unmodified linesmodified:uv.lock+1 added-1 removed
1915 unmodified lines1916 [package.metadata]1917 requires-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 ]1923 1924 [package.metadata.requires-dev]127 unmodified lines