Replies: 1 comment
-
One alternative I've found (although I'm not sure if this is intended) is to use the @app.get("/users/")
async def list_users() -> list[BaseUser]:
return await UserIn.find(projection_model=BaseUser).limit(100) and this happily works for the type-checking, but it does mean that we are projecting at run-time, instead of letting |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
FastAPI provides some nice documentation on a "Response" model: https://fastapi.tiangolo.com/tutorial/response-model/ with something like
Now, with
beanie
, we have theDocument
class. Most of the examples I've seen so far do a split where they defineUserBase
as the response schema, andUserIn
as the database model. The question then is, do we addDocument
as a parent class inUserIn
or onUserBase
? So we have two options here (I think):We replace the response model with a
beanie.Document
and the rest of the code should work as expected. The other option which I think is a little cleaner in terms of separation of concerns (but seems to run into issues with type-checking) iswhere
UserIn
contains theDocument
instead ofUserBase
. That means one cannot useUserBase
to query the database, and onlyUserIn
, but this is the intention (I think). However, when doing this, we have a slight problem because of the subclassingwhere
mypy
complains about thelist_users
function here:Now this is a funny issue, because I'm not sure if the intention of
beanie
was to replace all models (including the response model), or just the database-facing models. That said, I'm not sure what the correct thing to do here is as the type-checking is throwing me off a little bit.One thing I just realized, in all of these examples, if we use
Document.get
where we're not returning some sort of list of items, then there's no issue in the return type(!). It must be something about whether we should uselist
orsequence
to type-hint the end-result here.Beta Was this translation helpful? Give feedback.
All reactions