-
-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add pretty error tracebacks for user errors and support for Rich (#412
- Loading branch information
Showing
9 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import typer | ||
import typer.main | ||
|
||
typer.main.rich = None | ||
|
||
|
||
def main(name: str = "morty"): | ||
print(name + 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import typer | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command() | ||
def main(name: str = "morty"): | ||
print(name) | ||
|
||
|
||
broken_app = typer.Typer() | ||
|
||
|
||
@broken_app.command() | ||
def broken(name: str = "morty"): | ||
print(name + 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
app(standalone_mode=False) | ||
|
||
typer.main.get_command(broken_app)() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import typer | ||
|
||
|
||
def main(name: str = "morty"): | ||
print(name + 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import subprocess | ||
from pathlib import Path | ||
|
||
|
||
def test_traceback_rich(): | ||
file_path = Path(__file__).parent / "assets/type_error_rich.py" | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" not in result.stderr | ||
|
||
assert "typer.run(main)" in result.stderr | ||
assert "print(name + 3)" in result.stderr | ||
|
||
# TODO: when deprecating Python 3.6, remove second option | ||
assert ( | ||
'TypeError: can only concatenate str (not "int") to str' in result.stderr | ||
or "TypeError: must be str, not int" in result.stderr | ||
) | ||
assert "name = 'morty'" in result.stderr | ||
|
||
|
||
def test_traceback_no_rich(): | ||
file_path = Path(__file__).parent / "assets/type_error_no_rich.py" | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" not in result.stderr | ||
|
||
assert "typer.run(main)" in result.stderr | ||
assert "print(name + 3)" in result.stderr | ||
# TODO: when deprecating Python 3.6, remove second option | ||
assert ( | ||
'TypeError: can only concatenate str (not "int") to str' in result.stderr | ||
or "TypeError: must be str, not int" in result.stderr | ||
) | ||
|
||
|
||
def test_unmodified_traceback(): | ||
file_path = Path(__file__).parent / "assets/type_error_normal_traceback.py" | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "morty" in result.stdout, "the call to the first app should work normally" | ||
assert "return callback(**use_params)" in result.stderr, ( | ||
"calling outside of Typer should show the normal traceback, " | ||
"even after the hook is installed" | ||
) | ||
assert "typer.main.get_command(broken_app)()" in result.stderr | ||
assert "print(name + 3)" in result.stderr | ||
# TODO: when deprecating Python 3.6, remove second option | ||
assert ( | ||
'TypeError: can only concatenate str (not "int") to str' in result.stderr | ||
or "TypeError: must be str, not int" in result.stderr | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters