55from copy import deepcopy
66
77from bson import BSON , ObjectId
8- from pymongo import ASCENDING , MongoClient
8+ from pymongo import MongoClient
99from pymongo .collection import Collection
1010from pymongo .database import Database
1111from typing_extensions import Self
1212
1313from ..helpers .empty import Empty , EmptyObject
14- from ..types import SpecDocumentType , Specs , RawDocuments , SpecsOrRawDocuments , SubSpecBaseType , SpecBaseType
15- import typing as t
14+ from ..types import RawDocuments , SpecBaseType , SpecDocumentType , SpecsOrRawDocuments
15+
1616
1717class MongoBaseMixin (SpecBaseType ):
1818 _client : t .ClassVar [t .Optional [MongoClient [t .Any ]]] = None
@@ -38,9 +38,8 @@ def __eq__(self, other: t.Any) -> bool:
3838
3939 return self ._id == other ._id
4040
41- def __lt__ (self , other : t .Any ) -> t .Any :
42- return self ._id < other ._id
43-
41+ def __lt__ (self , other : t .Any ) -> bool :
42+ return self ._id < other ._id # type: ignore[no-any-return]
4443
4544 @classmethod
4645 def get_collection (cls ) -> Collection [t .Any ]:
@@ -76,7 +75,7 @@ def with_options(cls, **options: t.Any) -> t.Generator[t.Any, t.Any, None]:
7675 cls ._collection_context = existing_context
7776
7877 @classmethod
79- def _path_to_value (cls , path : str , parent_dict : t . MutableMapping [ str , t . Any ] ) -> t .Any :
78+ def _path_to_value (cls , path : str , parent_dict : SpecDocumentType ) -> t .Any :
8079 """Return a value from a dictionary at the given path"""
8180 keys : list [str ] = cls ._path_to_keys (path )
8281
@@ -97,7 +96,7 @@ def _path_to_keys(cls, path: str) -> list[str]:
9796 return path .split ("." )
9897
9998 @classmethod
100- def _ensure_specs (cls , documents : SpecsOrRawDocuments [ SpecDocumentType ] ) -> Specs [ SpecDocumentType ]:
99+ def _ensure_specs (cls , documents : SpecsOrRawDocuments ) -> t . Sequence [ t . Self ]:
101100 """
102101 Ensure all items in a list are specs by converting those that aren't.
103102 """
@@ -110,7 +109,7 @@ def _ensure_specs(cls, documents: SpecsOrRawDocuments[SpecDocumentType]) -> Spec
110109 return specs
111110
112111 @classmethod
113- def _apply_sub_specs (cls , documents : RawDocuments [ SpecDocumentType ] , subs : dict [str , t .Any ]) -> None :
112+ def _apply_sub_specs (cls , documents : RawDocuments , subs : dict [str , t .Any ]) -> None :
114113 """Convert embedded documents to sub-specs for one or more documents"""
115114
116115 # Dereference each reference
@@ -242,7 +241,7 @@ def _flatten_projection(
242241 return flat_projection , references , subs
243242
244243 @classmethod
245- def _dereference (cls , documents : RawDocuments [ t . Any ] , references : dict [str , t .Any ]) -> None :
244+ def _dereference (cls , documents : RawDocuments , references : dict [str , t .Any ]) -> None :
246245 """Dereference one or more documents"""
247246
248247 # Dereference each reference
@@ -320,24 +319,3 @@ def _remove_keys(cls, parent_dict: dict[str, t.Any], paths: list[str]) -> None:
320319 continue
321320
322321 child_dict .pop (keys [- 1 ], None )
323-
324-
325- def to_refs (value : t .Any ) -> t .Any :
326- """Convert all Spec instances within the given value to Ids"""
327- # Spec
328- if isinstance (value , SpecBaseType ):
329- return getattr (value , "_id" )
330-
331- # SubSpec
332- elif isinstance (value , SubSpecBaseType ):
333- return to_refs (value .to_dict ())
334-
335- # Lists
336- elif isinstance (value , (list , tuple )):
337- return [to_refs (v ) for v in value ]
338-
339- # Dictionaries
340- elif isinstance (value , dict ):
341- return {k : to_refs (v ) for k , v in value .items ()}
342-
343- return value
0 commit comments