|
21 | 21 | from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional |
22 | 22 |
|
23 | 23 | try: |
24 | | - from typing import TypedDict # type: ignore[attr-defined] |
| 24 | + from typing_extensions import NotRequired, TypedDict |
| 25 | + |
| 26 | + from bson import ObjectId |
25 | 27 |
|
26 | 28 | # Not available in Python 3.7 |
27 | 29 | class Movie(TypedDict): # type: ignore[misc] |
| 30 | + _id: ObjectId |
| 31 | + idiot: ObjectId |
28 | 32 | name: str |
29 | 33 | year: int |
30 | 34 |
|
@@ -312,16 +316,18 @@ def test_typeddict_document_type(self) -> None: |
312 | 316 | assert retreived["year"] == 1 |
313 | 317 | assert retreived["name"] == "a" |
314 | 318 |
|
| 319 | + # TODO: mypy --install-types --non-interactive test/test_mypy.py |
| 320 | + # run just this file in CI |
315 | 321 | @only_type_check |
316 | 322 | def test_typeddict_document_type_insertion(self) -> None: |
317 | 323 | client: MongoClient[Movie] = MongoClient() |
318 | 324 | 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"}) |
| 325 | + insert = coll.insert_one(Movie(_id=ObjectId(), name="THX-1138", year=1971)) |
| 326 | + out = coll.find_one({"name": "THX-1138"}) |
321 | 327 | assert out is not None |
322 | | - assert out.name == "THX-1138" |
323 | | - assert out.year == "1971" |
324 | | - assert out.id == ObjectId() |
| 328 | + # This should fail because the output is a Movie. |
| 329 | + assert out["foo"] # type:ignore[typeddict-item] |
| 330 | + assert type(out["_id"]) == ObjectId |
325 | 331 |
|
326 | 332 | @only_type_check |
327 | 333 | def test_raw_bson_document_type(self) -> None: |
|
0 commit comments