|
61 | 61 | import struct
|
62 | 62 | import sys
|
63 | 63 | import uuid
|
64 |
| -from codecs import utf_8_decode as _utf_8_decode # type: ignore |
65 |
| -from codecs import utf_8_encode as _utf_8_encode # type: ignore |
| 64 | +from codecs import utf_8_decode as _utf_8_decode # type: ignore[attr-defined] |
| 65 | +from codecs import utf_8_encode as _utf_8_encode # type: ignore[attr-defined] |
66 | 66 | from collections import abc as _abc
|
67 |
| -from typing import (TYPE_CHECKING, Any, BinaryIO, Callable, Dict, Generator, |
| 67 | +from typing import (IO, TYPE_CHECKING, Any, BinaryIO, Callable, Dict, Generator, |
68 | 68 | Iterator, List, Mapping, MutableMapping, NoReturn,
|
69 | 69 | Sequence, Tuple, Type, TypeVar, Union, cast)
|
70 | 70 |
|
|
88 | 88 |
|
89 | 89 | # Import RawBSONDocument for type-checking only to avoid circular dependency.
|
90 | 90 | if TYPE_CHECKING:
|
| 91 | + from array import array |
| 92 | + from mmap import mmap |
91 | 93 | from bson.raw_bson import RawBSONDocument
|
92 | 94 |
|
93 | 95 |
|
94 | 96 | try:
|
95 |
| - from bson import _cbson # type: ignore |
| 97 | + from bson import _cbson # type: ignore[attr-defined] |
96 | 98 | _USE_C = True
|
97 | 99 | except ImportError:
|
98 | 100 | _USE_C = False
|
@@ -851,6 +853,7 @@ def _datetime_to_millis(dtm: datetime.datetime) -> int:
|
851 | 853 |
|
852 | 854 | _DocumentIn = Mapping[str, Any]
|
853 | 855 | _DocumentOut = Union[MutableMapping[str, Any], "RawBSONDocument"]
|
| 856 | +_ReadableBuffer = Union[bytes, memoryview, "mmap", "array"] |
854 | 857 |
|
855 | 858 |
|
856 | 859 | def encode(document: _DocumentIn, check_keys: bool = False, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> bytes:
|
@@ -880,7 +883,7 @@ def encode(document: _DocumentIn, check_keys: bool = False, codec_options: Codec
|
880 | 883 | return _dict_to_bson(document, check_keys, codec_options)
|
881 | 884 |
|
882 | 885 |
|
883 |
| -def decode(data: bytes, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> _DocumentOut: |
| 886 | +def decode(data: _ReadableBuffer, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> Dict[str, Any]: |
884 | 887 | """Decode BSON to a document.
|
885 | 888 |
|
886 | 889 | By default, returns a BSON document represented as a Python
|
@@ -912,7 +915,7 @@ def decode(data: bytes, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) ->
|
912 | 915 | return _bson_to_dict(data, codec_options)
|
913 | 916 |
|
914 | 917 |
|
915 |
| -def decode_all(data: bytes, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> List[_DocumentOut]: |
| 918 | +def decode_all(data: _ReadableBuffer, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> List[Dict[str, Any]]: |
916 | 919 | """Decode BSON data to multiple documents.
|
917 | 920 |
|
918 | 921 | `data` must be a bytes-like object implementing the buffer protocol that
|
@@ -1075,7 +1078,7 @@ def decode_iter(data: bytes, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS
|
1075 | 1078 | yield _bson_to_dict(elements, codec_options)
|
1076 | 1079 |
|
1077 | 1080 |
|
1078 |
| -def decode_file_iter(file_obj: BinaryIO, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> Iterator[_DocumentOut]: |
| 1081 | +def decode_file_iter(file_obj: Union[BinaryIO, IO], codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> Iterator[_DocumentOut]: |
1079 | 1082 | """Decode bson data from a file to multiple documents as a generator.
|
1080 | 1083 |
|
1081 | 1084 | Works similarly to the decode_all function, but reads from the file object
|
@@ -1158,7 +1161,7 @@ def encode(cls: Type["BSON"], document: _DocumentIn, check_keys: bool = False,
|
1158 | 1161 | """
|
1159 | 1162 | return cls(encode(document, check_keys, codec_options))
|
1160 | 1163 |
|
1161 |
| - def decode(self, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> _DocumentOut: # type: ignore[override] |
| 1164 | + def decode(self, codec_options: CodecOptions = DEFAULT_CODEC_OPTIONS) -> Dict[str, Any]: # type: ignore[override] |
1162 | 1165 | """Decode this BSON data.
|
1163 | 1166 |
|
1164 | 1167 | By default, returns a BSON document represented as a Python
|
|
0 commit comments