[FEATURE] support Pydantic BaseModel to "collect" all kwargs, like FastAPI #679
-
First Check
Commit to Help
Example Codefrom typer import Typer
from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str | None = None
price: float
tax: float | None = None
app = Typer()
@app.command("create")
def create_item(item: Item):
return item DescriptionI might be missing something, but wouldn't it be cool if Typer could work like this FastAPI docs example. This feature request superficially resembles #111 but notice I'm seeking to use a Pydantic I struggled to write a descriptive title that would differentiate it from this other request (which is also well reasoned I think). I think the example code makes it clear what I'm thinking of. Operating SystemLinux, macOS Operating System DetailsNo response Typer Version0.9.0 Python VersionPython 3.10.11 Additional ContextI love Typer, thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
This works for me: @app.command()
class foo_cmd(BaseModel):
"""foo cmd"""
x: int
y: bool = False
def model_post_init(self, _):
print(f"{self.x=}, {self.y=}") |
Beta Was this translation helpful? Give feedback.
-
also, name argument to |
Beta Was this translation helpful? Give feedback.
-
Ahh, that's what I'm got about parameter names.
and now we get a nice help:
|
Beta Was this translation helpful? Give feedback.
Thinking about it more I came up with slightly nicer workaround: