BeanieODM / beanie

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

ClientSession Type mismatch #706

Closed QSHolzner closed 7 months ago

QSHolzner commented 11 months ago

ClienSession type error Since the update to motor 3.3 I get type errors. In motor 3.3 additional type hints have been added to the public API. The type returned by AsyncIOMotorClient start_session() is AsyncIOMotorClientSession or AgnosticClientSession. The find_one function of beanie Document expects a ClientSession for the session parameter. I think the parameter type should be changed to AsyncIOMotorClientSession.

The AsyncIOMotorClientSession is a Container for the encapsulated PyMongo ClientSession. When motor then executes Db actions the ClientSession contained in the AsyncIOMotorClientSession is extracted and passed to PyMongo.

I hope in the following example this becomes clear.

With the assignment session=s the following error message comes: An argument of type "AgnosticClientSession" cannot be assigned to the parameter "session" of type "ClientSession | None" in the function "find_one".

import asyncio
from typing import Optional

from beanie import Document, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient

class Product(Document):
    name: str
    description: Optional[str] = None
    price: float

async def example():
    # Beanie uses Motor async client under the hood
    client = AsyncIOMotorClient(
        "mongodb://localhost:27017",
        directConnection=True,
    )

    await init_beanie(database=client.mydb, document_models=[Product])
    async with await client.start_session() as s:  # s is of type AgnosticClientSession
        async with s.start_transaction():
        # find_one expects session of type CientSession
            product: Product | None = await Product.find_one(
                Product.price < 10, session=s
            )
            if product:
                print(product.name)

if __name__ == "__main__":
    asyncio.run(example())
roman-right commented 11 months ago

Hi! Thank you for the catch. I'll check and fix it this/next week.

roman-right commented 9 months ago

Hi @QSHolzner , Unfortunately, I can not reproduce it. Could you please provide you system version, beanie version and other nessasary details to me be able to reproduce correctly. I use Ubuntu 22.04, beanie 1.23.6

QSHolzner commented 9 months ago

Hi Roman, sorry I think I missed some context in the description. There is no runtime error in this example. I get the type error in VSCode from the pylance plugin. Pylance uses pyright for type checking. If I start pyright from command-line I get the following message:

/workspaces/api-server/scripts/beanie_demo.py:26:45 - error: Argument of type "AgnosticClientSession" cannot be assigned to parameter "session" of type "ClientSession | None" in function "find_one" Type "AgnosticClientSession" cannot be assigned to type "ClientSession | None" "AgnosticClientSession" is incompatible with "ClientSession" "AgnosticClientSession" is incompatible with "None" (reportGeneralTypeIssues)

Used Versions: beanie==1.23.6 motor==3.3.2

Hope this helps. Thank you

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 30 days with no activity.

github-actions[bot] commented 7 months ago

This issue was closed because it has been stalled for 14 days with no activity.