-
-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Refactor and document handling pretty exceptions (#422)
* ♻️ Rename pretty_errors_ to pretty_exceptions_ to avoid confusion with validation errors * ✨ Add source examples for pretty_exceptions * 📝 Add docs for pretty exceptions * 🔧 Add Exceptions docs to MkDocs * ✅ Add tests for tutorial about exceptions * ✅ Update trackeback tests, remove tests covered by tutorial * ✅ Fix test for module
- Loading branch information
Showing
15 changed files
with
531 additions
and
117 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
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 | ||
|
||
app = typer.Typer(pretty_exceptions_show_locals=False) | ||
|
||
|
||
@app.command() | ||
def main(password: str): | ||
print(password + 3) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
2 changes: 1 addition & 1 deletion
2
.../assets/type_error_rich_pretty_disable.py → docs_src/exceptions/tutorial003.py
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
2 changes: 1 addition & 1 deletion
2
...s/assets/type_error_rich_short_disable.py → docs_src/exceptions/tutorial004.py
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
Empty file.
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,63 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.exceptions import tutorial001 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_traceback_rich(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" not in result.stderr | ||
|
||
assert "typer.run(main)" not 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_standard_traceback_env_var(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" 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'" not in result.stderr | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
["coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
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,63 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.exceptions import tutorial002 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_traceback_rich(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path), "secret"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" not in result.stderr | ||
|
||
assert "app()" not in result.stderr | ||
assert "print(password + 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'" not in result.stderr | ||
|
||
|
||
def test_standard_traceback_env_var(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path), "secret"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" in result.stderr | ||
|
||
assert "app()" in result.stderr | ||
assert "print(password + 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'" not in result.stderr | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
["coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
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,41 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.exceptions import tutorial003 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_traceback_rich_pretty_short_disable(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" not in result.stderr | ||
|
||
assert "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 | ||
) | ||
assert "name = 'morty'" in result.stderr | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
["coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
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,39 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
|
||
from typer.testing import CliRunner | ||
|
||
from docs_src.exceptions import tutorial004 as mod | ||
|
||
runner = CliRunner() | ||
|
||
|
||
def test_rich_pretty_exceptions_disable(): | ||
file_path = Path(mod.__file__) | ||
result = subprocess.run( | ||
["coverage", "run", str(file_path)], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, | ||
) | ||
assert "return get_command(self)(*args, **kwargs)" in result.stderr | ||
|
||
assert "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 | ||
) | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
["coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
Oops, something went wrong.