Type hints don't work on Motor?

The docs say that type hints are supported. So I tried that.

client: AsyncIOMotorClient = AsyncIOMotorClient()
collection: AsyncIOMotorCollection[Movie] = client.test.test

However, mypy gives me:

“AsyncIOMotorCollection” expects no type arguments, but 1 given

Pylance also gives a similar message. Did they remove it at some point? For some reason, I can’t find any mention of this on the internet. Apologies if this has been brought up before.

Here are my versions:
Python 3.9.21
pymongo 4.6.3
motor 3.4.0

What version of mypy are you using?

Here’s what I see locally with the latest mypy (1.10.1):

$ cat test/check_runtime_types.py 
from typing import Any, Dict

from motor.motor_asyncio import (
    AsyncIOMotorChangeStream,
    AsyncIOMotorClient,
    AsyncIOMotorClientEncryption,
    AsyncIOMotorCollection,
    AsyncIOMotorCursor,
    AsyncIOMotorDatabase,
)

client: AsyncIOMotorClient[Dict[str, Any]]
db: AsyncIOMotorDatabase[Dict[str, Any]]
cur: AsyncIOMotorCursor[Dict[str, Any]]
coll: AsyncIOMotorCollection[Dict[str, Any]]
cs: AsyncIOMotorChangeStream[Dict[str, Any]]
enc: AsyncIOMotorClientEncryption[Dict[str, Any]]
$ mypy --strict test/check_runtime_types.py
Success: no issues found in 1 source file

Running mypy 1.15.0. It’s actually running inside a python:3.9.21-slim-bookworm docker container if that’s any help. Here’s the output for that file.

test.py:12: error: “AsyncIOMotorClient” expects no type arguments, but 1 given [type-arg]
test.py:13: error: “AsyncIOMotorDatabase” expects no type arguments, but 1 given [type-arg]
test.py:14: error: “AsyncIOMotorCursor” expects no type arguments, but 1 given [type-arg]
test.py:15: error: “AsyncIOMotorCollection” expects no type arguments, but 1 given [type-arg]
test.py:16: error: “AsyncIOMotorChangeStream” expects no type arguments, but 1 given [type-arg]
test.py:17: error: “AsyncIOMotorClientEncryption” expects no type arguments, but 1 given [type-arg]
Found 6 errors in 1 file (checked 1 source file)

And what motor version are you using? Are you sure it’s 3.4?

Yes. Both pip list and poetry show motor tell me it’s 3.4.0. I’m not sure what’s gone wrong. Could it be the python version?

Oh now I just feel silly. Seems all I needed was to just update pymongo and motor to the latest versions. The dockerfile I used had something to do with it as well. I would install the dependencies inside a non-slim python image and then copy over the venv folder to the slim python image. For whatever reason, that breaks in the older versions. I should note that those older versions did work as long as I didn’t do the venv copying.

All good now. Thanks for your time.