-
Hello, I came to Pydantic via FastAPI, which uses Pydantic at its core. One feature I'm enamoured with is the idea of query parameter models, and it works like this: Say you have a model with This got me thinking: say I have two models describing parameters: class One(BaseModel):
a: str
class Two(BaseModel):
b: int Assume furthermore I want to expose a function that takes def some_function(**kwargs: Unpack[Concat[One, Two]]) -> None:
... and that would mean that type checkers would type-check Looking forward to any insights or ideas how to achieve the above. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
This is not possible in the current Python typing system. It would require a way to express a |
Beta Was this translation helpful? Give feedback.
-
One thing I just thought about: couldn't we use typing information from elsewhere to compose a model? Unfortunately, it's not possible to derive from a class ParamsType(TypedDict):
b: str
class Params(ParamsType, BaseModel): ... This yields:
And a class ParamsType(TypedDict):
a: int
Params = TypeAdapter(ParamsType)
def myfun(**kwargs: Unpack[Params]):
print(kwargs)
myfun(a=5) # ok
myfun(b="str") # also ok, no typing or validation happening Would it be possible to somehow construct a class Params(typedict=Params): ... ? |
Beta Was this translation helpful? Give feedback.
This is not possible in the current Python typing system. It would require a way to express a
TypedDict
from a Pydantic model (or more generally a dataclass-like type). There is some discussion in this thread.