Debian Codegraph @

docs/source/vs_typer/validation/README.rst

raw
1 ==========
2 Validation
3 ==========
4 Typer has builtin argument validation for certain type annotations.
5
6
7 .. code-block:: python
8
9 import typer
10
11 typer_app = typer.Typer()
12
13 @typer_app.command()
14 def foo(age: Annotated[int, typer.Argument(min=0)]):
15 pass
16
17 This works for a select few builtins, but the Typer solution doesn't abstract out validation properly.
18 Why does the generic ``typer.Argument`` have fields that only have meaning if the annotated type is a number?
19 The ``typer.Argument`` signature has a ridiculous number of fields that only apply for certain types.
20
21 .. code-block:: python
22
23 def Argument(
24 # Parameter
25 default: Optional[Any] = ...,
26 *,
27 callback: Optional[Callable[..., Any]] = None,
28 metavar: Optional[str] = None,
29 expose_value: bool = True,
30 is_eager: bool = False,
31 envvar: Optional[Union[str, List[str]]] = None,
32 shell_complete: Optional[
33 Callable[
34 [click.Context, click.Parameter, str],
35 Union[List["click.shell_completion.CompletionItem"], List[str]],
36 ]
37 ] = None,
38 autocompletion: Optional[Callable[..., Any]] = None,
39 # Custom type
40 parser: Optional[Callable[[str], Any]] = None,
41 # TyperArgument
42 show_default: Union[bool, str] = True,
43 show_choices: bool = True,
44 show_envvar: bool = True,
45 help: Optional[str] = None,
46 hidden: bool = False,
47 # Choice
48 case_sensitive: bool = True,
49 # Numbers
50 min: Optional[Union[int, float]] = None,
51 max: Optional[Union[int, float]] = None,
52 clamp: bool = False,
53 # DateTime
54 formats: Optional[List[str]] = None,
55 # File
56 mode: Optional[str] = None,
57 encoding: Optional[str] = None,
58 errors: Optional[str] = "strict",
59 lazy: Optional[bool] = None,
60 atomic: bool = False,
61 # Path
62 exists: bool = False,
63 file_okay: bool = True,
64 dir_okay: bool = True,
65 writable: bool = False,
66 readable: bool = True,
67 resolve_path: bool = False,
68 allow_dash: bool = False,
69 path_type: Union[None, Type[str], Type[bytes]] = None,
70 # Rich settings
71 rich_help_panel: Union[str, None] = None,
72 ) -> Any:
73 ...
74
75 Cyclopts has an explicit :attr:`~.Parameter.validator` field that accepts a function:
76
77 .. code-block:: python
78
79 from cyclopts import App, parameter
80 from typing import Annotated
81
82 cyclopts_app = App()
83
84 def age_validator(type_, value: int):
85 if value < 0:
86 raise ValueError
87
88 @cyclopts_app.command()
89 def foo(age: Annotated[int, Parameter(validator=age_validator)]):
90 pass
91
92 cyclopts_app()
93
94 This solution is similar to how other libraries, like Attrs_ or Pydantic_, perform validation.
95
96 Cyclopts has builtin validators for common use-cases.
97
98 .. code-block:: python
99
100 # Typer
101 typer.Argument(file_okay=True, exists=True)
102
103 # Cyclopts
104 cyclopts.Parameter(validator=cyclopts.validators.Path(file_okay=True, exists=True))
105
106
107 .. _Attrs: https://www.attrs.org/en/stable/examples.html#validators
108 .. _Pydantic: https://docs.pydantic.dev/latest/concepts/validators/