diff --git a/docs/tutorial/exceptions.md b/docs/tutorial/exceptions.md index ad0a9a137a..5bcecd4db1 100644 --- a/docs/tutorial/exceptions.md +++ b/docs/tutorial/exceptions.md @@ -235,14 +235,14 @@ TypeError: can only concatenate str (not "int") to str -You could also achieve the same with the environment variable `_TYPER_STANDARD_TRACEBACK=1`. +You could also achieve the same with the environment variable `TYPER_STANDARD_TRACEBACK=1` (or by setting the deprecated variable `_TYPER_STANDARD_TRACEBACK=1`). 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:
```console -export _TYPER_STANDARD_TRACEBACK=1 +export TYPER_STANDARD_TRACEBACK=1 $ python main.py diff --git a/tests/test_tracebacks.py b/tests/test_tracebacks.py index e1b674f74b..170522c3de 100644 --- a/tests/test_tracebacks.py +++ b/tests/test_tracebacks.py @@ -10,7 +10,11 @@ def test_traceback_no_rich(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" not in result.stderr @@ -25,7 +29,11 @@ def test_traceback_no_rich_short_disable(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" not in result.stderr @@ -40,7 +48,11 @@ def test_unmodified_traceback(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "morty" in result.stdout, "the call to the first app should work normally" assert "return callback(**use_params)" in result.stderr, ( diff --git a/tests/test_tutorial/test_exceptions/test_tutorial001.py b/tests/test_tutorial/test_exceptions/test_tutorial001.py index 5267b15393..ca66f05344 100644 --- a/tests/test_tutorial/test_exceptions/test_tutorial001.py +++ b/tests/test_tutorial/test_exceptions/test_tutorial001.py @@ -3,6 +3,7 @@ import sys from pathlib import Path +import pytest from typer.testing import CliRunner from docs_src.exceptions import tutorial001 as mod @@ -16,7 +17,11 @@ def test_traceback_rich(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" not in result.stderr @@ -26,13 +31,16 @@ def test_traceback_rich(): assert "name = 'morty'" in result.stderr -def test_standard_traceback_env_var(): +@pytest.mark.parametrize( + "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"] +) +def test_standard_traceback_env_var(env_var: str): file_path = Path(mod.__file__) result = subprocess.run( [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"}, + env={**os.environ, env_var: "1"}, ) assert "return get_command(self)(*args, **kwargs)" in result.stderr diff --git a/tests/test_tutorial/test_exceptions/test_tutorial002.py b/tests/test_tutorial/test_exceptions/test_tutorial002.py index 0bcf3671f8..3f088b2de7 100644 --- a/tests/test_tutorial/test_exceptions/test_tutorial002.py +++ b/tests/test_tutorial/test_exceptions/test_tutorial002.py @@ -3,6 +3,7 @@ import sys from pathlib import Path +import pytest from typer.testing import CliRunner from docs_src.exceptions import tutorial002 as mod @@ -16,7 +17,11 @@ def test_traceback_rich(): [sys.executable, "-m", "coverage", "run", str(file_path), "secret"], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" not in result.stderr @@ -26,13 +31,16 @@ def test_traceback_rich(): assert "name = 'morty'" not in result.stderr -def test_standard_traceback_env_var(): +@pytest.mark.parametrize( + "env_var", ["TYPER_STANDARD_TRACEBACK", "_TYPER_STANDARD_TRACEBACK"] +) +def test_standard_traceback_env_var(env_var: str): file_path = Path(mod.__file__) result = subprocess.run( [sys.executable, "-m", "coverage", "run", str(file_path), "secret"], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": "1"}, + env={**os.environ, env_var: "1"}, ) assert "return get_command(self)(*args, **kwargs)" in result.stderr diff --git a/tests/test_tutorial/test_exceptions/test_tutorial003.py b/tests/test_tutorial/test_exceptions/test_tutorial003.py index 2c3ed634cd..a7ec4dbf77 100644 --- a/tests/test_tutorial/test_exceptions/test_tutorial003.py +++ b/tests/test_tutorial/test_exceptions/test_tutorial003.py @@ -16,7 +16,11 @@ def test_traceback_rich_pretty_short_disable(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" not in result.stderr diff --git a/tests/test_tutorial/test_exceptions/test_tutorial004.py b/tests/test_tutorial/test_exceptions/test_tutorial004.py index c48c4bad61..17675f49d3 100644 --- a/tests/test_tutorial/test_exceptions/test_tutorial004.py +++ b/tests/test_tutorial/test_exceptions/test_tutorial004.py @@ -16,7 +16,11 @@ def test_rich_pretty_exceptions_disable(): [sys.executable, "-m", "coverage", "run", str(file_path)], capture_output=True, encoding="utf-8", - env={**os.environ, "_TYPER_STANDARD_TRACEBACK": ""}, + env={ + **os.environ, + "TYPER_STANDARD_TRACEBACK": "", + "_TYPER_STANDARD_TRACEBACK": "", + }, ) assert "return get_command(self)(*args, **kwargs)" in result.stderr diff --git a/typer/main.py b/typer/main.py index 71a25e6c4b..2e3b1223cc 100644 --- a/typer/main.py +++ b/typer/main.py @@ -60,7 +60,9 @@ def except_hook( exception_config: Union[DeveloperExceptionConfig, None] = getattr( exc_value, _typer_developer_exception_attr_name, None ) - standard_traceback = os.getenv("_TYPER_STANDARD_TRACEBACK") + standard_traceback = os.getenv( + "TYPER_STANDARD_TRACEBACK", os.getenv("_TYPER_STANDARD_TRACEBACK") + ) if ( standard_traceback or not exception_config