Typer extension to enable pydantic support
Project description
Pydantic Typer
Typer extension to enable pydantic support
Table of Contents
Installation
pip install pydantic-typer
Note: pydantic-typer
comes with pydantic
and typer
as dependencies, so you don't need to install anything else.
Usage
All the code blocks below can be copied and used directly (they are tested Python files).
To run any of the examples, copy the code to a file main.py
, and run it:
python main.py
Basic Usage
Simply add the @enable_pydantic
decorator to any function that you use with typer.run
:
import pydantic
import typer
from pydantic_typer import enable_pydantic
class User(pydantic.BaseModel):
id: int
name: str = "Jane Doe"
@enable_pydantic
def main(num: int, user: User):
print(num, type(num))
print(user, type(user))
if __name__ == "__main__":
typer.run(main)
Usage with nested models
@enable_pydantic
also works with nested pydantic models:
from __future__ import annotations
from typing import Optional
import pydantic
import typer
from pydantic_typer import enable_pydantic
class Pet(pydantic.BaseModel):
name: str
species: str
class Person(pydantic.BaseModel):
name: str
age: Optional[float] = None # noqa: UP007 typer does not support float | None yet, see https://github.com/tiangolo/typer/pull/548
pet: Pet
@enable_pydantic
def main(person: Person):
print(person, type(person))
if __name__ == "__main__":
typer.run(main)
Use pydantic
models with typer.Argument
You can annotate the parameters with typer.Argument
to make all model fields CLI arguments:
from __future__ import annotations
import pydantic
import typer
from typing_extensions import Annotated
from pydantic_typer import enable_pydantic
class User(pydantic.BaseModel):
id: int
name: str
@enable_pydantic
def main(num: Annotated[int, typer.Option()], user: Annotated[User, typer.Argument()]):
print(num, type(num))
print(user, type(user))
if __name__ == "__main__":
typer.run(main)
*Note: You can also override annotations directly on the pydantic model fields:*
from __future__ import annotations
import pydantic
import typer
from typing_extensions import Annotated
from pydantic_typer import enable_pydantic
class User(pydantic.BaseModel):
id: Annotated[int, typer.Argument(metavar="THE_ID")]
name: Annotated[str, typer.Option()]
@enable_pydantic
def main(num: Annotated[int, typer.Option()], user: Annotated[User, typer.Argument()]):
print(num, type(num))
print(user, type(user))
if __name__ == "__main__":
typer.run(main)
Here, User
is a typer.Argument
, but we manually override the fields again:
- We override the
metavar
of toUser.id
beTHE_ID
- And
User.name
to be atyper.Option
License
pydantic-typer
is distributed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pydantic_typer-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d8ecefaebfd48b545b5466519541ba4020569eb5a41ca395e8834cac265a5a3 |
|
MD5 | cbe4e494ce3c1a3a3badd671f8755e9a |
|
BLAKE2b-256 | 087358bdd2839a47f74eb6598fabb716e70238395a118c3cb421de4a264b8923 |