Skip to content

Commit 6e21760

Browse files
committed
add mypy test
1 parent ea41432 commit 6e21760

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

doc/examples/type_hints.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ Typed Collection
9393
----------------
9494

9595
You can use :py:class:`~typing.TypedDict` (Python 3.8+) when using a well-defined schema for the data in a :class:`~pymongo.collection.Collection`.
96-
Note that all `schema_validation`_ for inserts and updates is done on the server. Do not rely on type hints for schema validation, only for providing
97-
a more ergonomic interface:
96+
Note that all `schema_validation`_ for inserts and updates is done on the server. This is due to the fact that these methods automatically add
97+
an "_id" field. Do not rely on TypedDicts for schema validation, only for providing a more ergonomic interface:
9898

9999
.. doctest::
100100

test/test_collection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ def test_insert_many_invalid(self):
785785
db.test.insert_many(1) # type: ignore[arg-type]
786786

787787
with self.assertRaisesRegex(TypeError, "documents must be a non-empty list"):
788-
db.test.insert_many(RawBSONDocument(encode({"_id": 2}))) # type: ignore[arg-type]
788+
db.test.insert_many(RawBSONDocument(encode({"_id": 2})))
789789

790790
def test_delete_one(self):
791791
self.db.test.drop()

test/test_mypy.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import os
1919
import tempfile
2020
import 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

2323
try:
2424
from typing import TypedDict # type: ignore[attr-defined]
@@ -40,7 +40,15 @@ class Movie(TypedDict): # type: ignore[misc]
4040
from test import IntegrationTest
4141
from 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+
)
4452
from bson.raw_bson import RawBSONDocument
4553
from bson.son import SON
4654
from 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

Comments
 (0)