Skip to content

Commit

Permalink
simplify pydantic type conversion using pydantic.TypeAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
lachaib committed Sep 4, 2024
1 parent b70d5a6 commit 9bd66be
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9bd66be

Please sign in to comment.