'How can I type annotate a PyMongo Collection?

I'm currently using:

from typing import Type
from pymongo.collection import Collection

def collection_type_test(example_collection: Type[Collection]) -> list:
    return list(example_collection.find())

Is this the correct approach, will I get any linter benefits from this / any benefits at all?



Solution 1:[1]

For me it went to use Collection directly:

from pymongo.collection import Collection

def collection_type_test(example_collection: Collection) -> list:
    return list(example_collection.find())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adrian Panaintescu