diff --git a/pymongo/asynchronous/network.py b/pymongo/asynchronous/network.py index a7696af44..bd51397eb 100644 --- a/pymongo/asynchronous/network.py +++ b/pymongo/asynchronous/network.py @@ -40,7 +40,7 @@ from pymongo.errors import ( NotPrimaryError, OperationFailure, ) -from pymongo.message import _OpMsg +from pymongo.message import _OpMsg, _OpReply from pymongo.monitoring import _is_speculative_authenticate if TYPE_CHECKING: @@ -83,7 +83,7 @@ async def _network_command_core( cursor_id: Optional[int] = None, orig: Optional[MutableMapping[str, Any]] = None, speculative_hello: bool = False, -) -> tuple[list[_DocumentOut], Optional[_OpMsg], datetime.timedelta]: +) -> tuple[list[_DocumentOut], Optional[Union[_OpReply, _OpMsg]], datetime.timedelta]: """Send/receive a command and return (docs, raw_reply, duration). Handles APM logging, send/receive, unpacking, response processing, @@ -92,7 +92,7 @@ async def _network_command_core( """ publish = listeners is not None and listeners.enabled_for_commands name = next(iter(spec)) - reply: Optional[_OpMsg] = None + reply: Optional[Union[_OpReply, _OpMsg]] = None docs: list[_DocumentOut] = [] if client is not None: @@ -213,7 +213,7 @@ async def _network_command_core( if client and client._encrypter and reply is not None: decrypted = await client._encrypter.decrypt(reply.raw_command_response()) decrypt_fields = _CURSOR_DOC_FIELDS if unpack_res is not None else user_fields - docs = list(_decode_all_selective(decrypted, codec_options, decrypt_fields)) + docs = list(_decode_all_selective(decrypted, codec_options, decrypt_fields)) # type: ignore[arg-type] return docs, reply, duration diff --git a/pymongo/asynchronous/server.py b/pymongo/asynchronous/server.py index 3f881de2f..855cc04a8 100644 --- a/pymongo/asynchronous/server.py +++ b/pymongo/asynchronous/server.py @@ -155,7 +155,7 @@ class Server: start = datetime.now() operation.use_command(conn) - more_to_come = operation.conn_mgr and operation.conn_mgr.more_to_come + more_to_come = bool(operation.conn_mgr and operation.conn_mgr.more_to_come) cmd, dbn = await self.operation_to_command(operation, conn, True) if more_to_come: request_id = 0 @@ -186,6 +186,7 @@ class Server: cursor_id=operation.cursor_id, ) + assert reply is not None response: Response client = operation.client # type: ignore[assignment] if client._should_pin_cursor(operation.session) or operation.exhaust: # type: ignore[arg-type] diff --git a/pymongo/synchronous/network.py b/pymongo/synchronous/network.py index 93a5d0c6e..1099af24d 100644 --- a/pymongo/synchronous/network.py +++ b/pymongo/synchronous/network.py @@ -40,7 +40,7 @@ from pymongo.errors import ( NotPrimaryError, OperationFailure, ) -from pymongo.message import _OpMsg +from pymongo.message import _OpMsg, _OpReply from pymongo.monitoring import _is_speculative_authenticate if TYPE_CHECKING: @@ -83,7 +83,7 @@ def _network_command_core( cursor_id: Optional[int] = None, orig: Optional[MutableMapping[str, Any]] = None, speculative_hello: bool = False, -) -> tuple[list[_DocumentOut], Optional[_OpMsg], datetime.timedelta]: +) -> tuple[list[_DocumentOut], Optional[Union[_OpReply, _OpMsg]], datetime.timedelta]: """Send/receive a command and return (docs, raw_reply, duration). Handles APM logging, send/receive, unpacking, response processing, @@ -92,7 +92,7 @@ def _network_command_core( """ publish = listeners is not None and listeners.enabled_for_commands name = next(iter(spec)) - reply: Optional[_OpMsg] = None + reply: Optional[Union[_OpReply, _OpMsg]] = None docs: list[_DocumentOut] = [] if client is not None: @@ -213,7 +213,7 @@ def _network_command_core( if client and client._encrypter and reply is not None: decrypted = client._encrypter.decrypt(reply.raw_command_response()) decrypt_fields = _CURSOR_DOC_FIELDS if unpack_res is not None else user_fields - docs = list(_decode_all_selective(decrypted, codec_options, decrypt_fields)) + docs = list(_decode_all_selective(decrypted, codec_options, decrypt_fields)) # type: ignore[arg-type] return docs, reply, duration diff --git a/pymongo/synchronous/server.py b/pymongo/synchronous/server.py index 212b5e5b4..a6964518f 100644 --- a/pymongo/synchronous/server.py +++ b/pymongo/synchronous/server.py @@ -155,7 +155,7 @@ class Server: start = datetime.now() operation.use_command(conn) - more_to_come = operation.conn_mgr and operation.conn_mgr.more_to_come + more_to_come = bool(operation.conn_mgr and operation.conn_mgr.more_to_come) cmd, dbn = self.operation_to_command(operation, conn, True) if more_to_come: request_id = 0 @@ -186,6 +186,7 @@ class Server: cursor_id=operation.cursor_id, ) + assert reply is not None response: Response client = operation.client # type: ignore[assignment] if client._should_pin_cursor(operation.session) or operation.exhaust: # type: ignore[arg-type]