diff --git a/pyproject.toml b/pyproject.toml index 17030e2b69..e349a4ef0c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -203,7 +203,7 @@ keep-runtime-typing = true [tool.ruff.lint.flake8-tidy-imports] # Import these from within functions (lazy), not at the module level (TID253) -banned-module-level-imports = ["typer.rich_utils", "typer.adapters", "pydantic"] +banned-module-level-imports = ["typer.rich_utils", "typer._adapters", "pydantic"] [tool.ruff.lint.flake8-tidy-imports.banned-api] "rich".msg = "Use 'typer.rich_utils' instead of importing from 'rich' directly." diff --git a/tests/test_coercion.py b/tests/test_coercion.py index 0f21158444..9015c57d92 100644 --- a/tests/test_coercion.py +++ b/tests/test_coercion.py @@ -3,7 +3,6 @@ import pytest import typer -from typer._adapters import disabled_pydantic_plugins from typer.core import _TyperParameter from typer.testing import CliRunner @@ -204,6 +203,8 @@ def main(val: Widget = typer.Option("42", parser=parse_widget)): def test_plugin_when_previously_unset( monkeypatch: pytest.MonkeyPatch, ) -> None: + from typer._adapters import disabled_pydantic_plugins + monkeypatch.delenv("PYDANTIC_DISABLE_PLUGINS", raising=False) with disabled_pydantic_plugins(): assert os.environ["PYDANTIC_DISABLE_PLUGINS"] == "1" @@ -213,6 +214,8 @@ def test_plugin_when_previously_unset( def test_plugin_when_previously_set( monkeypatch: pytest.MonkeyPatch, ) -> None: + from typer._adapters import disabled_pydantic_plugins + monkeypatch.setenv("PYDANTIC_DISABLE_PLUGINS", "0") with disabled_pydantic_plugins(): assert os.environ["PYDANTIC_DISABLE_PLUGINS"] == "1" diff --git a/tests/test_terminal_control_characters.py b/tests/test_terminal_control_characters.py index ed29509180..b4aacc8a5a 100644 --- a/tests/test_terminal_control_characters.py +++ b/tests/test_terminal_control_characters.py @@ -27,6 +27,15 @@ def parse( ) -> None: pass # pragma: no cover + def parse_value(value: str) -> str: + raise ValueError(f"Invalid item: {value}") + + @app.command() + def parse_custom( + value: Annotated[str, typer.Argument(parser=parse_value)], + ) -> None: + pass # pragma: no cover + return app @@ -38,8 +47,15 @@ def parse( ("no-args", "--", "No such option"), ("read-file", "", "Invalid value"), ("parse", "", "Invalid value"), + ("parse-custom", "", "Value error, Invalid item:"), + ], + ids=[ + "extra-argument", + "unknown-option", + "file-open", + "custom-parser", + "custom-parser-message", ], - ids=["extra-argument", "unknown-option", "file-open", "custom-parser"], ) @pytest.mark.parametrize( ("value", "escaped"), @@ -79,7 +95,7 @@ def test_error_values( output = re.sub(r"\x1b\[[0-9;]*m", "", result.stderr) assert message in output if command == "parse": - # Custom parsers fail via Pydantic; the value shows up via repr() + # int() already uses repr() for the input in its error message. assert repr(prefix + "input-" + value) in output else: assert prefix + "input-" + escaped in output diff --git a/typer/_display.py b/typer/_display.py index d7154e7fd7..d64b2b0006 100644 --- a/typer/_display.py +++ b/typer/_display.py @@ -1,5 +1,7 @@ from typing import Any +from ._click.utils import _escape_control_characters + def describe_number_range( min: int | float | None, @@ -15,8 +17,8 @@ def describe_number_range( def get_error_msg(exc: Any) -> str: - """Get a string representation of the (first) validation error.""" + """Get the first validation error message with terminal controls escaped.""" errors = exc.errors() if errors: - return str(errors[0]["msg"]) - return str(exc) # pragma: no cover + return _escape_control_characters(str(errors[0]["msg"])) + return _escape_control_characters(str(exc)) # pragma: no cover