Skip to content

Commit b438edc

Browse files
fix type hint using pymongo client session instead of motor client session (#1022)
* fix beanie type hint using pymongo client session instead of AsyncIOMotorClientSession --------- Co-authored-by: ᑕᗩᑭTᗩIᑎᗰᗩᖇᐯᗴᒪ <CAPITAINMARVEL@users.noreply.github.com>
1 parent 71438c1 commit b438edc

File tree

10 files changed

+145
-147
lines changed

10 files changed

+145
-147
lines changed

beanie/odm/bulk.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any, Dict, List, Mapping, Optional, Type, Union
22

3+
from motor.motor_asyncio import AsyncIOMotorClientSession
34
from pydantic import BaseModel, Field
45
from pymongo import (
56
DeleteMany,
@@ -9,7 +10,6 @@
910
UpdateMany,
1011
UpdateOne,
1112
)
12-
from pymongo.client_session import ClientSession
1313
from pymongo.results import BulkWriteResult
1414

1515
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2
@@ -44,7 +44,9 @@ class Config:
4444

4545
class BulkWriter:
4646
def __init__(
47-
self, session: Optional[ClientSession] = None, ordered: bool = True
47+
self,
48+
session: Optional[AsyncIOMotorClientSession] = None,
49+
ordered: bool = True,
4850
):
4951
self.operations: List[Operation] = []
5052
self.session = session

beanie/odm/documents.py

+37-37
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from bson import DBRef, ObjectId
2525
from lazy_model import LazyModel
26+
from motor.motor_asyncio import AsyncIOMotorClientSession
2627
from pydantic import (
2728
ConfigDict,
2829
Field,
@@ -32,7 +33,6 @@
3233
from pydantic.class_validators import root_validator
3334
from pydantic.main import BaseModel
3435
from pymongo import InsertOne
35-
from pymongo.client_session import ClientSession
3636
from pymongo.errors import DuplicateKeyError
3737
from pymongo.results import (
3838
DeleteResult,
@@ -249,7 +249,7 @@ def fill_back_refs(cls, values):
249249
async def get(
250250
cls: Type["DocType"],
251251
document_id: Any,
252-
session: Optional[ClientSession] = None,
252+
session: Optional[AsyncIOMotorClientSession] = None,
253253
ignore_cache: bool = False,
254254
fetch_links: bool = False,
255255
with_children: bool = False,
@@ -261,7 +261,7 @@ async def get(
261261
Get document by id, returns None if document does not exist
262262
263263
:param document_id: PydanticObjectId - document id
264-
:param session: Optional[ClientSession] - pymongo session
264+
:param session: Optional[AsyncIOMotorClientSession] - motor session
265265
:param ignore_cache: bool - ignore cache (if it is turned on)
266266
:param **pymongo_kwargs: pymongo native parameters for find operation
267267
:return: Union["Document", None]
@@ -327,7 +327,7 @@ async def insert(
327327
self: DocType,
328328
*,
329329
link_rule: WriteRules = WriteRules.DO_NOTHING,
330-
session: Optional[ClientSession] = None,
330+
session: Optional[AsyncIOMotorClientSession] = None,
331331
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
332332
) -> DocType:
333333
"""
@@ -383,7 +383,7 @@ async def insert(
383383

384384
async def create(
385385
self: DocType,
386-
session: Optional[ClientSession] = None,
386+
session: Optional[AsyncIOMotorClientSession] = None,
387387
) -> DocType:
388388
"""
389389
The same as self.insert()
@@ -395,14 +395,14 @@ async def create(
395395
async def insert_one(
396396
cls: Type[DocType],
397397
document: DocType,
398-
session: Optional[ClientSession] = None,
398+
session: Optional[AsyncIOMotorClientSession] = None,
399399
bulk_writer: Optional["BulkWriter"] = None,
400400
link_rule: WriteRules = WriteRules.DO_NOTHING,
401401
) -> Optional[DocType]:
402402
"""
403403
Insert one document to the collection
404404
:param document: Document - document to insert
405-
:param session: ClientSession - pymongo session
405+
:param session: AsyncIOMotorClientSession - motor session
406406
:param bulk_writer: "BulkWriter" - Beanie bulk writer
407407
:param link_rule: InsertRules - hot to manage link fields
408408
:return: DocType
@@ -435,15 +435,15 @@ async def insert_one(
435435
async def insert_many(
436436
cls: Type[DocType],
437437
documents: Iterable[DocType],
438-
session: Optional[ClientSession] = None,
438+
session: Optional[AsyncIOMotorClientSession] = None,
439439
link_rule: WriteRules = WriteRules.DO_NOTHING,
440440
**pymongo_kwargs,
441441
) -> InsertManyResult:
442442
"""
443443
Insert many documents to the collection
444444
445445
:param documents: List["Document"] - documents to insert
446-
:param session: ClientSession - pymongo session
446+
:param session: AsyncIOMotorClientSession - motor session
447447
:param link_rule: InsertRules - how to manage link fields
448448
:return: InsertManyResult
449449
"""
@@ -469,15 +469,15 @@ async def insert_many(
469469
async def replace(
470470
self: DocType,
471471
ignore_revision: bool = False,
472-
session: Optional[ClientSession] = None,
472+
session: Optional[AsyncIOMotorClientSession] = None,
473473
bulk_writer: Optional[BulkWriter] = None,
474474
link_rule: WriteRules = WriteRules.DO_NOTHING,
475475
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
476476
) -> DocType:
477477
"""
478478
Fully update the document in the database
479479
480-
:param session: Optional[ClientSession] - pymongo session.
480+
:param session: Optional[AsyncIOMotorClientSession] - motor session.
481481
:param ignore_revision: bool - do force replace.
482482
Used when revision based protection is turned on.
483483
:param bulk_writer: "BulkWriter" - Beanie bulk writer
@@ -551,7 +551,7 @@ async def replace(
551551
@validate_self_before
552552
async def save(
553553
self: DocType,
554-
session: Optional[ClientSession] = None,
554+
session: Optional[AsyncIOMotorClientSession] = None,
555555
link_rule: WriteRules = WriteRules.DO_NOTHING,
556556
ignore_revision: bool = False,
557557
**kwargs,
@@ -560,7 +560,7 @@ async def save(
560560
Update an existing model in the database or
561561
insert it if it does not yet exist.
562562
563-
:param session: Optional[ClientSession] - pymongo session.
563+
:param session: Optional[AsyncIOMotorClientSession] - motor session.
564564
:param link_rule: WriteRules - rules how to deal with links on writing
565565
:param ignore_revision: bool - do force save.
566566
:return: None
@@ -633,7 +633,7 @@ async def save(
633633
async def save_changes(
634634
self: DocType,
635635
ignore_revision: bool = False,
636-
session: Optional[ClientSession] = None,
636+
session: Optional[AsyncIOMotorClientSession] = None,
637637
bulk_writer: Optional[BulkWriter] = None,
638638
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
639639
) -> Optional[DocType]:
@@ -668,13 +668,13 @@ async def save_changes(
668668
async def replace_many(
669669
cls: Type[DocType],
670670
documents: List[DocType],
671-
session: Optional[ClientSession] = None,
671+
session: Optional[AsyncIOMotorClientSession] = None,
672672
) -> None:
673673
"""
674674
Replace list of documents
675675
676676
:param documents: List["Document"]
677-
:param session: Optional[ClientSession] - pymongo session.
677+
:param session: Optional[AsyncIOMotorClientSession] - motor session.
678678
:return: None
679679
"""
680680
ids_list = [document.id for document in documents]
@@ -694,7 +694,7 @@ async def update(
694694
self,
695695
*args,
696696
ignore_revision: bool = False,
697-
session: Optional[ClientSession] = None,
697+
session: Optional[AsyncIOMotorClientSession] = None,
698698
bulk_writer: Optional[BulkWriter] = None,
699699
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
700700
skip_sync: Optional[bool] = None,
@@ -704,7 +704,7 @@ async def update(
704704
Partially update the document in the database
705705
706706
:param args: *Union[dict, Mapping] - the modifications to apply.
707-
:param session: ClientSession - pymongo session.
707+
:param session: AsyncIOMotorClientSession - motor session.
708708
:param ignore_revision: bool - force update. Will update even if revision id is not the same, as stored
709709
:param bulk_writer: "BulkWriter" - Beanie bulk writer
710710
:param pymongo_kwargs: pymongo native parameters for update operation
@@ -749,15 +749,15 @@ async def update(
749749
def update_all(
750750
cls,
751751
*args: Union[dict, Mapping],
752-
session: Optional[ClientSession] = None,
752+
session: Optional[AsyncIOMotorClientSession] = None,
753753
bulk_writer: Optional[BulkWriter] = None,
754754
**pymongo_kwargs,
755755
) -> UpdateMany:
756756
"""
757757
Partially update all the documents
758758
759759
:param args: *Union[dict, Mapping] - the modifications to apply.
760-
:param session: ClientSession - pymongo session.
760+
:param session: AsyncIOMotorClientSession - motor session.
761761
:param bulk_writer: "BulkWriter" - Beanie bulk writer
762762
:param **pymongo_kwargs: pymongo native parameters for find operation
763763
:return: UpdateMany query
@@ -769,7 +769,7 @@ def update_all(
769769
def set(
770770
self: DocType,
771771
expression: Dict[Union[ExpressionField, str, Any], Any],
772-
session: Optional[ClientSession] = None,
772+
session: Optional[AsyncIOMotorClientSession] = None,
773773
bulk_writer: Optional[BulkWriter] = None,
774774
skip_sync: Optional[bool] = None,
775775
**kwargs,
@@ -792,7 +792,7 @@ class Sample(Document):
792792
793793
:param expression: Dict[Union[ExpressionField, str, Any], Any] - keys and
794794
values to set
795-
:param session: Optional[ClientSession] - pymongo session
795+
:param session: Optional[AsyncIOMotorClientSession] - motor session
796796
:param bulk_writer: Optional[BulkWriter] - bulk writer
797797
:param skip_sync: bool - skip doc syncing. Available for the direct instances only
798798
:return: self
@@ -808,7 +808,7 @@ class Sample(Document):
808808
def current_date(
809809
self,
810810
expression: Dict[Union[datetime, ExpressionField, str], Any],
811-
session: Optional[ClientSession] = None,
811+
session: Optional[AsyncIOMotorClientSession] = None,
812812
bulk_writer: Optional[BulkWriter] = None,
813813
skip_sync: Optional[bool] = None,
814814
**kwargs,
@@ -819,7 +819,7 @@ def current_date(
819819
Uses [CurrentDate operator](operators/update.md#currentdate)
820820
821821
:param expression: Dict[Union[datetime, ExpressionField, str], Any]
822-
:param session: Optional[ClientSession] - pymongo session
822+
:param session: Optional[AsyncIOMotorClientSession] - motor session
823823
:param bulk_writer: Optional[BulkWriter] - bulk writer
824824
:param skip_sync: bool - skip doc syncing. Available for the direct instances only
825825
:return: self
@@ -835,7 +835,7 @@ def current_date(
835835
def inc(
836836
self,
837837
expression: Dict[Union[ExpressionField, float, int, str], Any],
838-
session: Optional[ClientSession] = None,
838+
session: Optional[AsyncIOMotorClientSession] = None,
839839
bulk_writer: Optional[BulkWriter] = None,
840840
skip_sync: Optional[bool] = None,
841841
**kwargs,
@@ -857,7 +857,7 @@ class Sample(Document):
857857
Uses [Inc operator](operators/update.md#inc)
858858
859859
:param expression: Dict[Union[ExpressionField, float, int, str], Any]
860-
:param session: Optional[ClientSession] - pymongo session
860+
:param session: Optional[AsyncIOMotorClientSession] - motor session
861861
:param bulk_writer: Optional[BulkWriter] - bulk writer
862862
:param skip_sync: bool - skip doc syncing. Available for the direct instances only
863863
:return: self
@@ -873,7 +873,7 @@ class Sample(Document):
873873
@wrap_with_actions(EventTypes.DELETE)
874874
async def delete(
875875
self,
876-
session: Optional[ClientSession] = None,
876+
session: Optional[AsyncIOMotorClientSession] = None,
877877
bulk_writer: Optional[BulkWriter] = None,
878878
link_rule: DeleteRules = DeleteRules.DO_NOTHING,
879879
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
@@ -882,7 +882,7 @@ async def delete(
882882
"""
883883
Delete the document
884884
885-
:param session: Optional[ClientSession] - pymongo session.
885+
:param session: Optional[AsyncIOMotorClientSession] - motor session.
886886
:param bulk_writer: "BulkWriter" - Beanie bulk writer
887887
:param link_rule: DeleteRules - rules for link fields
888888
:param **pymongo_kwargs: pymongo native parameters for delete operation
@@ -930,14 +930,14 @@ async def delete(
930930
@classmethod
931931
async def delete_all(
932932
cls,
933-
session: Optional[ClientSession] = None,
933+
session: Optional[AsyncIOMotorClientSession] = None,
934934
bulk_writer: Optional[BulkWriter] = None,
935935
**pymongo_kwargs,
936936
) -> Optional[DeleteResult]:
937937
"""
938938
Delete all the documents
939939
940-
:param session: Optional[ClientSession] - pymongo session.
940+
:param session: Optional[AsyncIOMotorClientSession] - motor session.
941941
:param bulk_writer: "BulkWriter" - Beanie bulk writer
942942
:param **pymongo_kwargs: pymongo native parameters for delete operation
943943
:return: Optional[DeleteResult] - pymongo DeleteResult instance.
@@ -1112,7 +1112,7 @@ def get_settings(cls) -> DocumentSettings:
11121112

11131113
@classmethod
11141114
async def inspect_collection(
1115-
cls, session: Optional[ClientSession] = None
1115+
cls, session: Optional[AsyncIOMotorClientSession] = None
11161116
) -> InspectionResult:
11171117
"""
11181118
Check, if documents, stored in the MongoDB collection
@@ -1202,7 +1202,7 @@ async def distinct(
12021202
cls,
12031203
key: str,
12041204
filter: Optional[Mapping[str, Any]] = None,
1205-
session: Optional[ClientSession] = None,
1205+
session: Optional[AsyncIOMotorClientSession] = None,
12061206
**kwargs: Any,
12071207
) -> list:
12081208
return await cls.get_motor_collection().distinct(
@@ -1223,7 +1223,7 @@ def is_deleted(self) -> bool:
12231223

12241224
async def hard_delete(
12251225
self,
1226-
session: Optional[ClientSession] = None,
1226+
session: Optional[AsyncIOMotorClientSession] = None,
12271227
bulk_writer: Optional[BulkWriter] = None,
12281228
link_rule: DeleteRules = DeleteRules.DO_NOTHING,
12291229
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
@@ -1239,7 +1239,7 @@ async def hard_delete(
12391239

12401240
async def delete(
12411241
self,
1242-
session: Optional[ClientSession] = None,
1242+
session: Optional[AsyncIOMotorClientSession] = None,
12431243
bulk_writer: Optional[BulkWriter] = None,
12441244
link_rule: DeleteRules = DeleteRules.DO_NOTHING,
12451245
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
@@ -1257,7 +1257,7 @@ def find_many_in_all( # type: ignore
12571257
skip: Optional[int] = None,
12581258
limit: Optional[int] = None,
12591259
sort: Union[None, str, List[Tuple[str, SortDirection]]] = None,
1260-
session: Optional[ClientSession] = None,
1260+
session: Optional[AsyncIOMotorClientSession] = None,
12611261
ignore_cache: bool = False,
12621262
fetch_links: bool = False,
12631263
with_children: bool = False,
@@ -1289,7 +1289,7 @@ def find_many( # type: ignore
12891289
skip: Optional[int] = None,
12901290
limit: Optional[int] = None,
12911291
sort: Union[None, str, List[Tuple[str, SortDirection]]] = None,
1292-
session: Optional[ClientSession] = None,
1292+
session: Optional[AsyncIOMotorClientSession] = None,
12931293
ignore_cache: bool = False,
12941294
fetch_links: bool = False,
12951295
with_children: bool = False,
@@ -1321,7 +1321,7 @@ def find_one( # type: ignore
13211321
cls: Type[FindType],
13221322
*args: Union[Mapping[str, Any], bool],
13231323
projection_model: Optional[Type["DocumentProjectionType"]] = None,
1324-
session: Optional[ClientSession] = None,
1324+
session: Optional[AsyncIOMotorClientSession] = None,
13251325
ignore_cache: bool = False,
13261326
fetch_links: bool = False,
13271327
with_children: bool = False,

0 commit comments

Comments
 (0)