Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
5 changes: 4 additions & 1 deletion tests/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
20 changes: 18 additions & 2 deletions tests/test_terminal_control_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"),
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions typer/_display.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any

from ._click.utils import _escape_control_characters


def describe_number_range(
min: int | float | None,
Expand All @@ -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
Loading