101101 DEFAULT_CODEC_OPTIONS ,
102102 CodecOptions ,
103103 DatetimeConversion ,
104- _DocumentType ,
105104 _raw_document_class ,
106105)
107106from bson .datetime_ms import (
125124
126125# Import some modules for type-checking only.
127126if TYPE_CHECKING :
128- from array import array
129- from mmap import mmap
127+ from bson .typings import _DocumentIn , _DocumentType , _ReadableBuffer
130128
131129try :
132130 from bson import _cbson # type: ignore[attr-defined]
@@ -986,12 +984,8 @@ def _dict_to_bson(doc: Any, check_keys: bool, opts: CodecOptions, top_level: boo
986984_CODEC_OPTIONS_TYPE_ERROR = TypeError ("codec_options must be an instance of CodecOptions" )
987985
988986
989- _DocumentIn = Mapping [str , Any ]
990- _ReadableBuffer = Union [bytes , memoryview , "mmap" , "array" ]
991-
992-
993987def encode (
994- document : _DocumentIn ,
988+ document : " _DocumentIn" ,
995989 check_keys : bool = False ,
996990 codec_options : CodecOptions = DEFAULT_CODEC_OPTIONS ,
997991) -> bytes :
@@ -1022,8 +1016,8 @@ def encode(
10221016
10231017
10241018def decode (
1025- data : _ReadableBuffer , codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1026- ) -> _DocumentType :
1019+ data : " _ReadableBuffer" , codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1020+ ) -> " _DocumentType" :
10271021 """Decode BSON to a document.
10281022
10291023 By default, returns a BSON document represented as a Python
@@ -1056,11 +1050,13 @@ def decode(
10561050 return _bson_to_dict (data , opts )
10571051
10581052
1059- def _decode_all (data : _ReadableBuffer , opts : "CodecOptions[_DocumentType]" ) -> List [_DocumentType ]:
1053+ def _decode_all (
1054+ data : "_ReadableBuffer" , opts : "CodecOptions[_DocumentType]"
1055+ ) -> "List[_DocumentType]" :
10601056 """Decode a BSON data to multiple documents."""
10611057 data , view = get_data_and_view (data )
10621058 data_len = len (data )
1063- docs : List [_DocumentType ] = []
1059+ docs : " List[_DocumentType]" = []
10641060 position = 0
10651061 end = data_len - 1
10661062 use_raw = _raw_document_class (opts .document_class )
@@ -1091,8 +1087,8 @@ def _decode_all(data: _ReadableBuffer, opts: "CodecOptions[_DocumentType]") -> L
10911087
10921088
10931089def decode_all (
1094- data : _ReadableBuffer , codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1095- ) -> List [_DocumentType ]:
1090+ data : " _ReadableBuffer" , codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1091+ ) -> " List[_DocumentType]" :
10961092 """Decode BSON data to multiple documents.
10971093
10981094 `data` must be a bytes-like object implementing the buffer protocol that
@@ -1213,7 +1209,7 @@ def _decode_all_selective(data: Any, codec_options: CodecOptions, fields: Any) -
12131209 # Decode documents for internal use.
12141210 from bson .raw_bson import RawBSONDocument
12151211
1216- internal_codec_options = codec_options .with_options (
1212+ internal_codec_options : CodecOptions [ RawBSONDocument ] = codec_options .with_options (
12171213 document_class = RawBSONDocument , type_registry = None
12181214 )
12191215 _doc = _bson_to_dict (data , internal_codec_options )
@@ -1228,7 +1224,7 @@ def _decode_all_selective(data: Any, codec_options: CodecOptions, fields: Any) -
12281224
12291225def decode_iter (
12301226 data : bytes , codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1231- ) -> Iterator [_DocumentType ]:
1227+ ) -> " Iterator[_DocumentType]" :
12321228 """Decode BSON data to multiple documents as a generator.
12331229
12341230 Works similarly to the decode_all function, but yields one document at a
@@ -1264,7 +1260,7 @@ def decode_iter(
12641260
12651261def decode_file_iter (
12661262 file_obj : Union [BinaryIO , IO ], codec_options : "Optional[CodecOptions[_DocumentType]]" = None
1267- ) -> Iterator [_DocumentType ]:
1263+ ) -> " Iterator[_DocumentType]" :
12681264 """Decode bson data from a file to multiple documents as a generator.
12691265
12701266 Works similarly to the decode_all function, but reads from the file object
@@ -1325,7 +1321,7 @@ class BSON(bytes):
13251321 @classmethod
13261322 def encode (
13271323 cls : Type ["BSON" ],
1328- document : _DocumentIn ,
1324+ document : " _DocumentIn" ,
13291325 check_keys : bool = False ,
13301326 codec_options : CodecOptions = DEFAULT_CODEC_OPTIONS ,
13311327 ) -> "BSON" :
@@ -1352,7 +1348,7 @@ def encode(
13521348 """
13531349 return cls (encode (document , check_keys , codec_options ))
13541350
1355- def decode (self , codec_options : "CodecOptions[_DocumentType]" = DEFAULT_CODEC_OPTIONS ) -> _DocumentType : # type: ignore[override,assignment]
1351+ def decode (self , codec_options : "CodecOptions[_DocumentType]" = DEFAULT_CODEC_OPTIONS ) -> " _DocumentType" : # type: ignore[override,assignment]
13561352 """Decode this BSON data.
13571353
13581354 By default, returns a BSON document represented as a Python
0 commit comments