@@ -1352,15 +1352,17 @@ class _OpReply:
13521352 UNPACK_FROM = struct .Struct ("<iqii" ).unpack_from
13531353 OP_CODE = 1
13541354
1355- def __init__ (self , flags : int , cursor_id : int , number_returned : int , documents : bytes ):
1355+ def __init__ (
1356+ self , flags : int , cursor_id : int , number_returned : int , documents : bytes | memoryview
1357+ ):
13561358 self .flags = flags
13571359 self .cursor_id = Int64 (cursor_id )
13581360 self .number_returned = number_returned
13591361 self .documents = documents
13601362
13611363 def raw_response (
13621364 self , cursor_id : Optional [int ] = None , user_fields : Optional [Mapping [str , Any ]] = None
1363- ) -> list [bytes ]:
1365+ ) -> list [bytes | memoryview ]:
13641366 """Check the response header from the database, without decoding BSON.
13651367
13661368 Check the response for errors and unpack.
@@ -1448,7 +1450,7 @@ def more_to_come(self) -> bool:
14481450 return False
14491451
14501452 @classmethod
1451- def unpack (cls , msg : bytes ) -> _OpReply :
1453+ def unpack (cls , msg : bytes | memoryview ) -> _OpReply :
14521454 """Construct an _OpReply from raw bytes."""
14531455 # PYTHON-945: ignore starting_from field.
14541456 flags , cursor_id , _ , number_returned = cls .UNPACK_FROM (msg )
@@ -1470,7 +1472,7 @@ class _OpMsg:
14701472 MORE_TO_COME = 1 << 1
14711473 EXHAUST_ALLOWED = 1 << 16 # Only present on requests.
14721474
1473- def __init__ (self , flags : int , payload_document : bytes ):
1475+ def __init__ (self , flags : int , payload_document : bytes | memoryview ):
14741476 self .flags = flags
14751477 self .payload_document = payload_document
14761478
@@ -1512,7 +1514,7 @@ def command_response(self, codec_options: CodecOptions[Any]) -> dict[str, Any]:
15121514 """Unpack a command response."""
15131515 return self .unpack_response (codec_options = codec_options )[0 ]
15141516
1515- def raw_command_response (self ) -> bytes :
1517+ def raw_command_response (self ) -> bytes | memoryview :
15161518 """Return the bytes of the command response."""
15171519 return self .payload_document
15181520
@@ -1522,7 +1524,7 @@ def more_to_come(self) -> bool:
15221524 return bool (self .flags & self .MORE_TO_COME )
15231525
15241526 @classmethod
1525- def unpack (cls , msg : bytes ) -> _OpMsg :
1527+ def unpack (cls , msg : bytes | memoryview ) -> _OpMsg :
15261528 """Construct an _OpMsg from raw bytes."""
15271529 flags , first_payload_type , first_payload_size = cls .UNPACK_FROM (msg )
15281530 if flags != 0 :
@@ -1541,7 +1543,7 @@ def unpack(cls, msg: bytes) -> _OpMsg:
15411543 return cls (flags , payload_document )
15421544
15431545
1544- _UNPACK_REPLY : dict [int , Callable [[bytes ], Union [_OpReply , _OpMsg ]]] = {
1546+ _UNPACK_REPLY : dict [int , Callable [[bytes | memoryview ], Union [_OpReply , _OpMsg ]]] = {
15451547 _OpReply .OP_CODE : _OpReply .unpack ,
15461548 _OpMsg .OP_CODE : _OpMsg .unpack ,
15471549}
0 commit comments