Skip to content

Commit 0ac6ec0

Browse files
committed
[FEATURE] Support for common pydantic types
Fixes #181
1 parent b4b6f25 commit 0ac6ec0

23 files changed

+576
-11
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Pydantic types such as [AnyUrl](https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.AnyUrl) or [EmailStr](https://docs.pydantic.dev/latest/api/networks/#pydantic.networks.EmailStr) can be very convenient to describe and validate some parameters.
2+
3+
You can add pydantic from typer's optional dependencies
4+
5+
<div class="termy">
6+
7+
```console
8+
// Pydantic comes with typer[all]
9+
$ pip install "typer[all]"
10+
---> 100%
11+
Successfully installed typer rich pydantic
12+
13+
// Alternatively, you can install Pydantic independently
14+
$ pip install pydantic
15+
---> 100%
16+
Successfully installed pydantic
17+
```
18+
19+
</div>
20+
21+
22+
You can then use them as parameter types.
23+
24+
=== "Python 3.6+ Argument"
25+
26+
```Python hl_lines="5"
27+
{!> ../docs_src/parameter_types/pydantic_types/tutorial001_an.py!}
28+
```
29+
30+
=== "Python 3.6+ Argument non-Annotated"
31+
32+
!!! tip
33+
Prefer to use the `Annotated` version if possible.
34+
35+
```Python hl_lines="4"
36+
{!> ../docs_src/parameter_types/pydantic_types/tutorial001.py!}
37+
```
38+
39+
=== "Python 3.6+ Option"
40+
41+
```Python hl_lines="5"
42+
{!> ../docs_src/parameter_types/pydantic_types/tutorial002_an.py!}
43+
```
44+
45+
=== "Python 3.6+ Option non-Annotated"
46+
47+
!!! tip
48+
Prefer to use the `Annotated` version if possible.
49+
50+
```Python hl_lines="4"
51+
{!> ../docs_src/parameter_types/pydantic_types/tutorial002.py!}
52+
```
53+
54+
These types are also supported in lists or tuples
55+
56+
=== "Python 3.6+ list"
57+
58+
```Python hl_lines="6"
59+
{!> ../docs_src/parameter_types/pydantic_types/tutorial003_an.py!}
60+
```
61+
62+
=== "Python 3.6+ list non-Annotated"
63+
64+
!!! tip
65+
Prefer to use the `Annotated` version if possible.
66+
67+
```Python hl_lines="5"
68+
{!> ../docs_src/parameter_types/pydantic_types/tutorial003.py!}
69+
```
70+
71+
=== "Python 3.6+ tuple"
72+
73+
```Python hl_lines="6"
74+
{!> ../docs_src/parameter_types/pydantic_types/tutorial004_an.py!}
75+
```
76+
77+
=== "Python 3.6+ tuple non-Annotated"
78+
79+
!!! tip
80+
Prefer to use the `Annotated` version if possible.
81+
82+
```Python hl_lines="5"
83+
{!> ../docs_src/parameter_types/pydantic_types/tutorial004.py!}
84+
```

docs_src/parameter_types/pydantic_types/__init__.py

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import typer
2+
from pydantic import EmailStr
3+
4+
5+
def main(email_arg: EmailStr):
6+
typer.echo(f"email_arg: {email_arg}")
7+
8+
9+
if __name__ == "__main__":
10+
typer.run(main)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import typer
2+
from pydantic import EmailStr
3+
from typing_extensions import Annotated
4+
5+
6+
def main(email_arg: Annotated[EmailStr, typer.Argument()]):
7+
typer.echo(f"email_arg: {email_arg}")
8+
9+
10+
if __name__ == "__main__":
11+
typer.run(main)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import typer
2+
from pydantic import EmailStr
3+
4+
5+
def main(email_opt: EmailStr = typer.Option("[email protected]")):
6+
typer.echo(f"email_opt: {email_opt}")
7+
8+
9+
if __name__ == "__main__":
10+
typer.run(main)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import typer
2+
from pydantic import EmailStr
3+
from typing_extensions import Annotated
4+
5+
6+
def main(email_opt: Annotated[EmailStr, typer.Option()] = "[email protected]"):
7+
typer.echo(f"email_opt: {email_opt}")
8+
9+
10+
if __name__ == "__main__":
11+
typer.run(main)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import List
2+
3+
import typer
4+
from pydantic import AnyHttpUrl
5+
6+
7+
def main(urls: List[AnyHttpUrl] = typer.Option([], "--url")):
8+
typer.echo(f"urls: {urls}")
9+
10+
11+
if __name__ == "__main__":
12+
typer.run(main)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from typing import List
2+
3+
import typer
4+
from pydantic import AnyHttpUrl
5+
from typing_extensions import Annotated
6+
7+
8+
def main(
9+
urls: Annotated[List[AnyHttpUrl], typer.Option("--url", default_factory=list)],
10+
):
11+
typer.echo(f"urls: {urls}")
12+
13+
14+
if __name__ == "__main__":
15+
typer.run(main)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Tuple
2+
3+
import typer
4+
from pydantic import AnyHttpUrl, EmailStr
5+
6+
7+
def main(
8+
user: Tuple[str, int, EmailStr, AnyHttpUrl] = typer.Option(
9+
..., help="User name, age, email and social media URL"
10+
),
11+
):
12+
name, age, email, url = user
13+
typer.echo(f"name: {name}")
14+
typer.echo(f"age: {age}")
15+
typer.echo(f"email: {email}")
16+
typer.echo(f"url: {url}")
17+
18+
19+
if __name__ == "__main__":
20+
typer.run(main)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Tuple
2+
3+
import typer
4+
from pydantic import AnyHttpUrl, EmailStr
5+
from typing_extensions import Annotated
6+
7+
8+
def main(
9+
user: Annotated[
10+
Tuple[str, int, EmailStr, AnyHttpUrl],
11+
typer.Option(help="User name, age, email and social media URL"),
12+
],
13+
):
14+
name, age, email, url = user
15+
typer.echo(f"name: {name}")
16+
typer.echo(f"age: {age}")
17+
typer.echo(f"email: {email}")
18+
typer.echo(f"url: {url}")
19+
20+
21+
if __name__ == "__main__":
22+
typer.run(main)

0 commit comments

Comments
 (0)