✨ 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
Provenance
- Commit
de159dc- Tree
0542e55- Author
Colin Nolan <colin-nolan@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
185b2f5
7 files changed+52 added−14 removed
modified:docs/tutorial/exceptions.md+2 added-2 removed
233 unmodified lines234 ```235 236 </div>237 238-You could also achieve the same with the environment variable `_TYPER_STANDARD_TRACEBACK=1`.238+You could also achieve the same with the environment variable `TYPER_STANDARD_TRACEBACK=1` (or by setting the deprecated variable `_TYPER_STANDARD_TRACEBACK=1`).239 240 This will work for any other Typer program too, in case you need to debug a problem in a Typer program made by someone else:241 242 <div class="termy">243 244 ```console245-export _TYPER_STANDARD_TRACEBACK=1245+export TYPER_STANDARD_TRACEBACK=1246 $ python main.py247 248 249 Traceback (most recent call last):24 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial001.py+11 added-3 removed
1 unmodified line2 import subprocess3 import sys4 from pathlib import Path5 6+import pytest7 from typer.testing import CliRunner8 9 from docs_src.exceptions import tutorial001 as mod10 5 unmodified lines16 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.stderr27 28 assert "app()" not in result.stderr1 unmodified line30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" in result.stderr32 33 34+@pytest.mark.parametrize(35+ "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36+)29-def test_standard_traceback_env_var():37+def 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.stderr46 47 assert "app()" in result.stderr12 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial002.py+11 added-3 removed
1 unmodified line2 import subprocess3 import sys4 from pathlib import Path5 6+import pytest7 from typer.testing import CliRunner8 9 from docs_src.exceptions import tutorial002 as mod10 5 unmodified lines16 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.stderr27 28 assert "app()" not in result.stderr1 unmodified line30 assert 'TypeError: can only concatenate str (not "int") to str' in result.stderr31 assert "name = 'morty'" not in result.stderr32 33 34+@pytest.mark.parametrize(35+ "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"]36+)29-def test_standard_traceback_env_var():37+def 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.stderr46 47 assert "app()" in result.stderr12 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial003.py+5 added-1 removed
14 unmodified lines15 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.stderr26 27 assert "app()" in result.stderr12 unmodified linesmodified:tests/test_tutorial/test_exceptions/test_tutorial004.py+5 added-1 removed
14 unmodified lines15 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.stderr26 27 assert "app()" in result.stderr11 unmodified linesmodified:tests/test_tracebacks.py+15 added-3 removed
8 unmodified lines9 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.stderr20 21 assert "app()" in result.stderr6 unmodified lines28 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.stderr39 40 assert "app()" in result.stderr6 unmodified lines47 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 linesmodified:typer/main.py+3 added-1 removed
58 unmodified lines59 ) -> 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_enable1075 unmodified lines