Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Generics on document type #11

Open
Doekeb opened this issue Dec 3, 2023 · 1 comment
Open

Generics on document type #11

Doekeb opened this issue Dec 3, 2023 · 1 comment

Comments

@Doekeb
Copy link
Contributor

Doekeb commented Dec 3, 2023

Thanks again for the stubs!

There is a slight fundamental difference between typing in pymongo and this package. Namely, many pymongo classes are generic on the document type. See pymongo.MongoClient as the main example. The client is created with the document_class keyword argument which defaults to dict. This allows methods like Collection.find_one to be very specific about their return type (e.g. if the collection belongs to a client where the document class is dict, then the return type of Collection.find_one is dict[str, Any]).

Currently, this package is set up to always return _Document = typing.Mapping[str, typing.Any] (https://github.com/L0RD-ZER0/Motor-Types/blob/8ffaa4fb9c0e9e93ef0425ea761b47d0502b1d87/motor-stubs/core.pyi#L31C1-L31C44). While this behavior is not incorrect per se, it does present a major development experience difference between the two packages (and after all, motor is built to very closely mirror pymongo).

For what it's worth, I never use anything other than dict as the document class in either package and I find the constant need for defining everything to be generic on the document class to be quite cumbersome. Nevertheless, I thought I would create this issue to hopefully spark a discussion around the topic 😄

@L0RD-ZER0
Copy link
Owner

Since you mentioned this one, if you were to see that I made a _Document instead of directly plugging in a dict, it was intended for that very purpose. Unfortunately in actual runtime, doing something like MotorClient[...] was producing errors as these classes don't support generics themselves. So either we get a monkey patch or we wait for the og repo to add generics in the classes. Then we can simply add the typevars and generic[_doc] and we good!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants