|
17 | 17 |
|
18 | 18 | import os |
19 | 19 | import tempfile |
| 20 | +import typing |
20 | 21 | import unittest |
21 | | -from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, Optional |
| 22 | +from typing import ( |
| 23 | + TYPE_CHECKING, |
| 24 | + Any, |
| 25 | + Callable, |
| 26 | + Dict, |
| 27 | + Iterable, |
| 28 | + Iterator, |
| 29 | + List, |
| 30 | + Optional, |
| 31 | + Sequence, |
| 32 | + Tuple, |
| 33 | +) |
22 | 34 |
|
23 | 35 | try: |
24 | 36 | from typing_extensions import NotRequired, TypedDict |
@@ -67,6 +79,7 @@ class MovieWithoutId(TypedDict): # type: ignore[misc] |
67 | 79 | from pymongo.collection import Collection |
68 | 80 | from pymongo.operations import InsertOne |
69 | 81 | from pymongo.read_preferences import ReadPreference |
| 82 | +from pymongo.results import BulkWriteResult, InsertManyResult, InsertOneResult |
70 | 83 |
|
71 | 84 | TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "mypy_fails") |
72 | 85 |
|
@@ -326,59 +339,11 @@ def test_typeddict_document_type(self) -> None: |
326 | 339 |
|
327 | 340 | @only_type_check |
328 | 341 | def test_typeddict_document_type_insertion(self) -> None: |
329 | | - client: MongoClient[Movie] = MongoClient() |
330 | | - coll: Collection[Movie] = client.test.test |
331 | | - mov = Movie(_id=ObjectId(), name="THX-1138", year=1971) |
332 | | - for meth, arg in [ |
333 | | - (coll.insert_many, [mov]), |
334 | | - (coll.insert_one, mov), |
335 | | - (coll.bulk_write, [InsertOne(mov)]), |
336 | | - ]: |
337 | | - meth(arg) # type:ignore[operator] |
338 | | - out = coll.find_one({"name": "THX-1138"}) |
339 | | - assert out is not None |
340 | | - # This should fail because the output is a Movie. |
341 | | - assert out["foo"] # type:ignore[typeddict-item] |
342 | | - assert type(out["_id"]) == ObjectId |
343 | | - coll.drop() |
344 | | - |
345 | | - # This should work the same as the test above, but this time using NotRequired to allow |
346 | | - # automatic insertion of the _id field by insert_one. |
347 | | - @only_type_check |
348 | | - def test_typeddict_document_type_not_required(self) -> None: |
349 | | - client: MongoClient[ImplicitMovie] = MongoClient() |
350 | | - coll: Collection[ImplicitMovie] = client.test.test |
351 | | - mov = ImplicitMovie(name="THX-1138", year=1971) |
352 | | - for meth, arg in [ |
353 | | - (coll.insert_many, [mov]), |
354 | | - (coll.insert_one, mov), |
355 | | - (coll.bulk_write, [InsertOne(mov)]), |
356 | | - ]: |
357 | | - meth(arg) # type:ignore[operator] |
358 | | - out = coll.find_one({"name": "THX-1138"}) |
359 | | - assert out is not None |
360 | | - # This should fail because the output is a Movie. |
361 | | - assert out["foo"] # type:ignore[typeddict-item] |
362 | | - assert type(out["_id"]) == ObjectId |
363 | | - coll.drop() |
364 | | - |
365 | | - @only_type_check |
366 | | - def test_typeddict_document_type_empty(self) -> None: |
367 | | - client: MongoClient[MovieWithoutId] = MongoClient() |
368 | | - coll: Collection[MovieWithoutId] = client.test.test |
369 | | - mov = MovieWithoutId(name="THX-1138", year=1971) |
370 | | - for meth, arg in [ |
371 | | - (coll.insert_many, [mov]), |
372 | | - (coll.insert_one, mov), |
373 | | - (coll.bulk_write, [InsertOne(mov)]), |
374 | | - ]: |
375 | | - meth(arg) # type:ignore[operator] |
376 | | - out = coll.find_one({"name": "THX-1138"}) |
377 | | - assert out is not None |
378 | | - # This should fail because the output is a Movie. |
379 | | - assert out["foo"] # type:ignore[typeddict-item] |
380 | | - # This should fail because _id is not included in our TypedDict definition. |
381 | | - assert type(out["_id"]) == ObjectId # type:ignore[typeddict-item] |
| 342 | + coll: Collection[Movie] = self.db.test |
| 343 | + mov = Movie(name="THX-1138", year=1971) |
| 344 | + coll.insert_one(mov) |
| 345 | + coll.insert_many([mov]) |
| 346 | + coll.bulk_write([InsertOne(mov)]) |
382 | 347 |
|
383 | 348 | @only_type_check |
384 | 349 | def test_raw_bson_document_type(self) -> None: |
|
0 commit comments