Most operations have Session type unknown with pyright/pylance

Hi,
I’m trying to use pymongo on a pyright/pylance using codebase (strict), but after a bunch of trying, I’ve decided to just add # type: ignore to a bunch of lines.

client: MongoClient[Dict[str, Any]] = MongoClient(settings.mongo_host)

client.db.user.create_index([("user_id", ASCENDING)], unique=True)

gets:

Type of “create_index” is "(keys: str | Sequence[Tuple[str, int | str | Mapping[str, Any]]], session: ClientSession[Unknown] | None = None, comment: Any | None = None, **kwargs: Any) → str"PylancereportUnknownMemberType

And with bulk_write, even the let gets an unknown type on mapping.

Am I missing something obvious? If not, is there a plan to fix this?

Hi @Marcin_Platek,

I tried something similar. For me it was specifing the db you want to use.
So for your example you can use:
db = client[“database”] and then try db.collection.create_index()

Hope this helps!

Greetings,
Niklas

I am specifying the database.

Anyway, I tried to rewrite the code to look more like yours and nothing changed. The ClientSession has still an Unknown in the type variable.

Hi @Marcin_Platek, I think what you’re seeing is another unfortunate limitation from Unable to specify a default value for a generic parameter · Issue #3737 · python/mypy · GitHub. The ClientSession object is generic, but we can’t provide a default value so it defaults to unknown.

1 Like

After further discussion, we opened PYTHON-3283 to track changing ClientSession to not be generic in the next minor release.

2 Likes

Thanks, great to know!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.