BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
2.06k stars 217 forks source link

Typing error is shown with get method of Document subclasses #29

Closed oliverandrich closed 3 years ago

oliverandrich commented 3 years ago

I am working through the cocktail api tutorial on https://developer.mongodb.com/article/beanie-odm-fastapi-cocktails/. Basically, everything works fine, but I get a strange typing error from Pylance in VSCode. I am not certain, if I should be afraid of it or not.

For this function...

async def get_cocktail(cocktail_id: PydanticObjectId) -> Cocktail:
    """Helper function to look up a cocktail by id"""

    cocktail = await Cocktail.get(cocktail_id)
    if cocktail is None:
        raise HTTPException(status_code=404, detail="Cocktail not found")
    return cocktail

... I get the following warning.

CleanShot 2021-05-13 at 18 32 19@2x

From my point of view PyLance highlights a typing problem. Cocktail.get returns a Document and not the subclass Cocktail.

So, as I am just 30 minutes into the whole Beanie adventure, I might be entirely wrong. Or PyLance has an issue here?

roman-right commented 3 years ago

Hello @oliverandrich ,

Yes, I saw this warning too. It is annoying. I plan to fix it soon

Also, please be aware that the article was written around 2 months ago and the current Beanie has a new major version now. There are many updates, like Query Builder. But also there are some breaking changes. The current doc could be found here - https://roman-right.github.io/beanie/

Thank you for the feedback. I'll update you here when this will be fixed.

roman-right commented 3 years ago

Hello @oliverandrich It looks like I fixed this issue. Please, try 1.0.1

oliverandrich commented 3 years ago

@roman-right Yes, this solved it. Now I have to look at the source to understand how you fixed it to learn.

roman-right commented 3 years ago

I followed Guidos's solution for this: https://github.com/python/typing/issues/271