7474 _IndexKeyHint ,
7575 _IndexList ,
7676)
77- from pymongo .read_preferences import (
78- Primary ,
79- PrimaryPreferred ,
80- ReadPreference ,
81- _ServerMode ,
82- )
77+ from pymongo .read_preferences import ReadPreference , _ServerMode
8378from pymongo .results import (
8479 BulkWriteResult ,
8580 DeleteResult ,
@@ -264,7 +259,7 @@ def __init__(
264259
265260 def _conn_for_reads (
266261 self , session : ClientSession
267- ) -> ContextManager [Tuple [Connection , Union [ PrimaryPreferred , Primary ] ]]:
262+ ) -> ContextManager [Tuple [Connection , _ServerMode ]]:
268263 return self .__database .client ._conn_for_reads (self ._read_preference_for (session ), session )
269264
270265 def _conn_for_writes (self , session : Optional [ClientSession ]) -> ContextManager [Connection ]:
@@ -433,6 +428,24 @@ def database(self) -> Database[_DocumentType]:
433428 """
434429 return self .__database
435430
431+ # @overload
432+ # def with_options(
433+ # self,
434+ # codec_options: None = None,
435+ # read_preference: Optional[_ServerMode] = None,
436+ # write_concern: Optional[WriteConcern] = None,
437+ # read_concern: Optional[ReadConcern] = None,
438+ # ) -> Collection[Dict[str, Any]]: ...
439+
440+ # @overload
441+ # def with_options(
442+ # self,
443+ # codec_options: bson.CodecOptions[_DocumentType],
444+ # read_preference: Optional[_ServerMode] = None,
445+ # write_concern: Optional[WriteConcern] = None,
446+ # read_concern: Optional[ReadConcern] = None,
447+ # ) -> Collection[_DocumentType]: ...
448+
436449 def with_options (
437450 self ,
438451 codec_options : Optional [bson .CodecOptions [_DocumentTypeArg ]] = None ,
@@ -597,7 +610,7 @@ def _insert_one(
597610 command ["comment" ] = comment
598611
599612 def _insert_command (
600- session : ClientSession , conn : Connection , retryable_write : bool
613+ session : Optional [ ClientSession ] , conn : Connection , retryable_write : bool
601614 ) -> None :
602615 if bypass_doc_val :
603616 command ["bypassDocumentValidation" ] = True
@@ -861,7 +874,7 @@ def _update_retryable(
861874 session : Optional [ClientSession ] = None ,
862875 let : Optional [Mapping [str , Any ]] = None ,
863876 comment : Optional [Any ] = None ,
864- ) -> Mapping [str , Any ]:
877+ ) -> Optional [ Mapping [str , Any ] ]:
865878 """Internal update / replace helper."""
866879
867880 def _update (
@@ -1737,7 +1750,7 @@ def find_raw_batches(self, *args: Any, **kwargs: Any) -> RawBatchCursor[_Documen
17371750
17381751 def _count_cmd (
17391752 self ,
1740- session : ClientSession ,
1753+ session : Optional [ ClientSession ] ,
17411754 conn : Connection ,
17421755 read_preference : Optional [_ServerMode ],
17431756 cmd : Mapping [str , Any ],
@@ -1766,7 +1779,7 @@ def _aggregate_one_result(
17661779 read_preference : Optional [_ServerMode ],
17671780 cmd : Mapping [str , Any ],
17681781 collation : Optional [_CollationIn ],
1769- session : ClientSession ,
1782+ session : Optional [ ClientSession ] ,
17701783 ) -> Optional [Mapping [str , Any ]]:
17711784 """Internal helper to run an aggregate that returns a single result."""
17721785 result = self ._command (
@@ -1819,7 +1832,7 @@ def estimated_document_count(self, comment: Optional[Any] = None, **kwargs: Any)
18191832 kwargs ["comment" ] = comment
18201833
18211834 def _cmd (
1822- session : ClientSession ,
1835+ session : Optional [ ClientSession ] ,
18231836 server : Server ,
18241837 conn : Connection ,
18251838 read_preference : Optional [_ServerMode ],
@@ -1908,7 +1921,7 @@ def count_documents(
19081921 cmd .update (kwargs )
19091922
19101923 def _cmd (
1911- session : ClientSession ,
1924+ session : Optional [ ClientSession ] ,
19121925 server : Server ,
19131926 conn : Connection ,
19141927 read_preference : Optional [_ServerMode ],
@@ -1922,7 +1935,7 @@ def _cmd(
19221935
19231936 def _retryable_non_cursor_read (
19241937 self ,
1925- func : Callable [[ClientSession , Server , Connection , Optional [_ServerMode ]], T ],
1938+ func : Callable [[Optional [ ClientSession ] , Server , Connection , Optional [_ServerMode ]], T ],
19261939 session : Optional [ClientSession ],
19271940 ) -> T :
19281941 """Non-cursor read helper to handle implicit session creation."""
@@ -2276,18 +2289,19 @@ def list_indexes(
22762289 .. versionadded:: 3.0
22772290 """
22782291 codec_options : CodecOptions = CodecOptions (SON )
2279- coll = self .with_options (
2280- codec_options = codec_options , read_preference = ReadPreference .PRIMARY
2292+ coll = cast (
2293+ Collection [MutableMapping [str , Any ]],
2294+ self .with_options (codec_options = codec_options , read_preference = ReadPreference .PRIMARY ),
22812295 )
22822296 read_pref = (session and session ._txn_read_preference ()) or ReadPreference .PRIMARY
22832297 explicit_session = session is not None
22842298
22852299 def _cmd (
2286- session : ClientSession ,
2300+ session : Optional [ ClientSession ] ,
22872301 server : Server ,
22882302 conn : Connection ,
22892303 read_preference : _ServerMode ,
2290- ) -> CommandCursor [_DocumentType ]:
2304+ ) -> CommandCursor [MutableMapping [ str , Any ] ]:
22912305 cmd = SON ([("listIndexes" , self .__name ), ("cursor" , {})])
22922306 if comment is not None :
22932307 cmd ["comment" ] = comment
@@ -2404,7 +2418,7 @@ def list_search_indexes(
24042418
24052419 return self .__database .client ._retryable_read (
24062420 cmd .get_cursor ,
2407- cmd .get_read_preference (session ),
2421+ cmd .get_read_preference (session ), # type: ignore[arg-type]
24082422 session ,
24092423 retryable = not cmd ._performs_write ,
24102424 )
@@ -2618,7 +2632,7 @@ def _aggregate(
26182632 let : Optional [Mapping [str , Any ]] = None ,
26192633 comment : Optional [Any ] = None ,
26202634 ** kwargs : Any ,
2621- ) -> Union [ CommandCursor [_DocumentType ], RawBatchCursor [ _DocumentType ] ]:
2635+ ) -> CommandCursor [_DocumentType ]:
26222636 if comment is not None :
26232637 kwargs ["comment" ] = comment
26242638 cmd = aggregation_command (
@@ -2633,7 +2647,7 @@ def _aggregate(
26332647
26342648 return self .__database .client ._retryable_read (
26352649 cmd .get_cursor ,
2636- cmd .get_read_preference (session ),
2650+ cmd .get_read_preference (session ), # type: ignore[arg-type]
26372651 session ,
26382652 retryable = not cmd ._performs_write ,
26392653 )
@@ -2724,18 +2738,15 @@ def aggregate(
27242738 https://mongodb.com/docs/manual/reference/command/aggregate
27252739 """
27262740 with self .__database .client ._tmp_session (session , close = False ) as s :
2727- return cast (
2728- CommandCursor [_DocumentType ],
2729- self ._aggregate (
2730- _CollectionAggregationCommand ,
2731- pipeline ,
2732- CommandCursor ,
2733- session = s ,
2734- explicit_session = session is not None ,
2735- let = let ,
2736- comment = comment ,
2737- ** kwargs ,
2738- ),
2741+ return self ._aggregate (
2742+ _CollectionAggregationCommand ,
2743+ pipeline ,
2744+ CommandCursor ,
2745+ session = s ,
2746+ explicit_session = session is not None ,
2747+ let = let ,
2748+ comment = comment ,
2749+ ** kwargs ,
27392750 )
27402751
27412752 def aggregate_raw_batches (
@@ -3047,7 +3058,7 @@ def distinct(
30473058 cmd ["comment" ] = comment
30483059
30493060 def _cmd (
3050- session : ClientSession ,
3061+ session : Optional [ ClientSession ] ,
30513062 server : Server ,
30523063 conn : Connection ,
30533064 read_preference : Optional [_ServerMode ],
@@ -3112,7 +3123,7 @@ def __find_and_modify(
31123123 write_concern = self ._write_concern_for_cmd (cmd , session )
31133124
31143125 def _find_and_modify (
3115- session : ClientSession , conn : Connection , retryable_write : bool
3126+ session : Optional [ ClientSession ] , conn : Connection , retryable_write : bool
31163127 ) -> Any :
31173128 acknowledged = write_concern .acknowledged
31183129 if array_filters is not None :
0 commit comments