From 9bd66bedbdbbdeb662a35f8e3f25ffc3d0755178 Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Wed, 4 Sep 2024 21:38:22 +0200 Subject: [PATCH] simplify pydantic type conversion using pydantic.TypeAdapter --- typer/main.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/typer/main.py b/typer/main.py index e2890f2cf5..90acce1626 100644 --- a/typer/main.py +++ b/typer/main.py @@ -12,7 +12,7 @@ from uuid import UUID import click -from typing_extensions import Annotated, TypeAlias, get_args, get_origin +from typing_extensions import Annotated, get_args, get_origin from ._typing import is_union from .completion import get_completion_inspect_parameters @@ -68,15 +68,11 @@ def is_pydantic_type(type_: Any) -> bool: def pydantic_convertor(type_: type) -> Callable[[str], Any]: """Create a convertor for a parameter annotated with a pydantic type.""" - T: TypeAlias = type_ # type: ignore[valid-type] + adapter = pydantic.TypeAdapter(type_) - @pydantic.validate_call - def internal_convertor(value: T) -> T: - return value - - def convertor(value: str) -> T: + def convertor(value: str): try: - return internal_convertor(value) + return adapter.validate_python(value) except pydantic.ValidationError as e: error_message = e.errors( include_context=False, include_input=False, include_url=False