1818import os
1919import tempfile
2020import unittest
21- from typing import TYPE_CHECKING , Any , Dict , Iterable , Iterator , List
21+ from typing import TYPE_CHECKING , Any , Dict , Iterable , Iterator , List , Optional
2222
2323try :
2424 from typing import TypedDict # type: ignore[attr-defined]
@@ -40,7 +40,15 @@ class Movie(TypedDict): # type: ignore[misc]
4040from test import IntegrationTest
4141from test .utils import rs_or_single_client
4242
43- from bson import CodecOptions , decode , decode_all , decode_file_iter , decode_iter , encode
43+ from bson import (
44+ CodecOptions ,
45+ ObjectId ,
46+ decode ,
47+ decode_all ,
48+ decode_file_iter ,
49+ decode_iter ,
50+ encode ,
51+ )
4452from bson .raw_bson import RawBSONDocument
4553from bson .son import SON
4654from pymongo import ASCENDING , MongoClient
@@ -304,6 +312,17 @@ def test_typeddict_document_type(self) -> None:
304312 assert retreived ["year" ] == 1
305313 assert retreived ["name" ] == "a"
306314
315+ @only_type_check
316+ def test_typeddict_document_type_insertion (self ) -> None :
317+ client : MongoClient [Movie ] = MongoClient ()
318+ coll : Collection [Movie ] = client .test .test
319+ insert = coll .insert_one (Movie (name = "THX-1138" , year = 1971 ))
320+ out : Optional [Movie ] = coll .find_one ({"name" : "THX-1138" })
321+ assert out is not None
322+ assert out .name == "THX-1138"
323+ assert out .year == "1971"
324+ assert out .id == ObjectId ()
325+
307326 @only_type_check
308327 def test_raw_bson_document_type (self ) -> None :
309328 client = MongoClient (document_class = RawBSONDocument )
0 commit comments