Skip to content
Myr

✨ Add support for standard tracebacks via the env `TYPER_STANDARD_TRACEBACK` (#1299)

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

* Adds `TYPER_STANDARD_TRACEBACK` variable Keeps support fo `_TYPER_STANDARD_TRACEBACK` but notes as deprecated because: - It is non-standard for an advertised env to start with an underscore. - Environment variables that start with an underscore have been observed as causing issues in some execution environments (e.g. with AWS Lambda). See: https://github.com/fastapi/typer/discussions/1284 * Test new and legacy envs for setting standard tb

authored by Colin Nolan · committed by GitHub · parent 185b2f5 · Browse files · 7 files+52 added−14 removed
This commit

State

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

Diff stats

7 files+52 added−14 removed

  • tests:+47 added−11 removed
  • other:+5 added−3 removed

7 files+52 added−14 removed

modified:docs/tutorial/exceptions.md+2 added-2 removed
233 unmodified lines
234```235236</div>237238You could also achieve the same with the environment variable `_TYPER_STANDARD_TRACEBACK=1`.238You could also achieve the same with the environment variable `TYPER_STANDARD_TRACEBACK=1` (or by setting the deprecated variable `_TYPER_STANDARD_TRACEBACK=1`).239240This will work for any other Typer program too, in case you need to debug a problem in a Typer program made by someone else:241242<div class="termy">243244```console245export _TYPER_STANDARD_TRACEBACK=1245export TYPER_STANDARD_TRACEBACK=1246$ python main.py247248249Traceback (most recent call last):
24 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial001.py+11 added-3 removed
1 unmodified line
2import subprocess3import sys4from pathlib import Path56import pytest7from typer.testing import CliRunner89from docs_src.exceptions import tutorial001 as mod10
5 unmodified lines
16 result = subprocess.run(17 [sys.executable, "-m", "coverage", "run", str(file_path)],18 capture_output=True,19 encoding="utf-8",19 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},20 env={21 **os.environ,22 "TYPER_STANDARD_TRACEBACK": "",23 "_TYPER_STANDARD_TRACEBACK": "",24 },25 )26 assert "return get_command(self)(*args, **kwargs)" not in result.stderr2728 assert "app()" not in result.stderr
1 unmodified line
30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" in result.stderr323334@pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36)29def test_standard_traceback_env_var():37def test_standard_traceback_env_var(env_var: str):38 file_path = Path(mod.__file__)39 result = subprocess.run(40 [sys.executable, "-m", "coverage", "run", str(file_path)],41 capture_output=True,42 encoding="utf-8",35 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"},43 env={**os.environ, env_var: "1"},44 )45 assert "return get_command(self)(*args, **kwargs)" in result.stderr4647 assert "app()" in result.stderr
12 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial002.py+11 added-3 removed
1 unmodified line
2import subprocess3import sys4from pathlib import Path56import pytest7from typer.testing import CliRunner89from docs_src.exceptions import tutorial002 as mod10
5 unmodified lines
16 result = subprocess.run(17 [sys.executable, "-m", "coverage", "run", str(file_path), "secret"],18 capture_output=True,19 encoding="utf-8",19 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},20 env={21 **os.environ,22 "TYPER_STANDARD_TRACEBACK": "",23 "_TYPER_STANDARD_TRACEBACK": "",24 },25 )26 assert "return get_command(self)(*args, **kwargs)" not in result.stderr2728 assert "app()" not in result.stderr
1 unmodified line
30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" not in result.stderr323334@pytest.mark.parametrize(35 "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36)29def test_standard_traceback_env_var():37def test_standard_traceback_env_var(env_var: str):38 file_path = Path(mod.__file__)39 result = subprocess.run(40 [sys.executable, "-m", "coverage", "run", str(file_path), "secret"],41 capture_output=True,42 encoding="utf-8",35 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"},43 env={**os.environ, env_var: "1"},44 )45 assert "return get_command(self)(*args, **kwargs)" in result.stderr4647 assert "app()" in result.stderr
12 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial003.py+5 added-1 removed
14 unmodified lines
15 result = subprocess.run(16 [sys.executable, "-m", "coverage", "run", str(file_path)],17 capture_output=True,18 encoding="utf-8",19 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},19 env={20 **os.environ,21 "TYPER_STANDARD_TRACEBACK": "",22 "_TYPER_STANDARD_TRACEBACK": "",23 },24 )25 assert "return get_command(self)(*args, **kwargs)" not in result.stderr2627 assert "app()" in result.stderr
12 unmodified lines
modified:tests/test_tutorial/test_exceptions/test_tutorial004.py+5 added-1 removed
14 unmodified lines
15 result = subprocess.run(16 [sys.executable, "-m", "coverage", "run", str(file_path)],17 capture_output=True,18 encoding="utf-8",19 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},19 env={20 **os.environ,21 "TYPER_STANDARD_TRACEBACK": "",22 "_TYPER_STANDARD_TRACEBACK": "",23 },24 )25 assert "return get_command(self)(*args, **kwargs)" in result.stderr2627 assert "app()" in result.stderr
11 unmodified lines
modified:tests/test_tracebacks.py+15 added-3 removed
8 unmodified lines
9 result = subprocess.run(10 [sys.executable, "-m", "coverage", "run", str(file_path)],11 capture_output=True,12 encoding="utf-8",13 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},13 env={14 **os.environ,15 "TYPER_STANDARD_TRACEBACK": "",16 "_TYPER_STANDARD_TRACEBACK": "",17 },18 )19 assert "return get_command(self)(*args, **kwargs)" not in result.stderr2021 assert "app()" in result.stderr
6 unmodified lines
28 result = subprocess.run(29 [sys.executable, "-m", "coverage", "run", str(file_path)],30 capture_output=True,31 encoding="utf-8",28 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},32 env={33 **os.environ,34 "TYPER_STANDARD_TRACEBACK": "",35 "_TYPER_STANDARD_TRACEBACK": "",36 },37 )38 assert "return get_command(self)(*args, **kwargs)" not in result.stderr3940 assert "app()" in result.stderr
6 unmodified lines
47 result = subprocess.run(48 [sys.executable, "-m", "coverage", "run", str(file_path)],49 capture_output=True,50 encoding="utf-8",43 env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""},51 env={52 **os.environ,53 "TYPER_STANDARD_TRACEBACK": "",54 "_TYPER_STANDARD_TRACEBACK": "",55 },56 )57 assert "morty" in result.stdout, "the call to the first app should work normally"58 assert "return callback(**use_params)" in result.stderr, (59 "calling outside of Typer should show the normal traceback, "
5 unmodified lines
modified:typer/main.py+3 added-1 removed
58 unmodified lines
59) -> None:60 exception_config: Union[DeveloperExceptionConfig, None] = getattr(61 exc_value, _typer_developer_exception_attr_name, None62 )63 standard_traceback = os.getenv("_TYPER_STANDARD_TRACEBACK")63 standard_traceback = os.getenv(64 "TYPER_STANDARD_TRACEBACK", os.getenv("_TYPER_STANDARD_TRACEBACK")65 )66 if (67 standard_traceback68 or not exception_config69 or not exception_config.pretty_exceptions_enable
1075 unmodified lines