diff --git a/docs/tutorial/exceptions.md b/docs/tutorial/exceptions.md index 43ca2d0f6d..2c66fbf9b6 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 8c8ab0295d..92a2ef395a 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 26b6ca943c..89af94caa8 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,16 +31,18 @@ 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 - assert "typer.run(main)" in result.stderr assert "print(name + 3)" in result.stderr assert 'TypeError: can only concatenate str (not "int") to str' 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 2fdc09e1ac..9377342f49 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