From 811fbaa972e620117d0b03124a718404a512c21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=91=95=E1=97=A9=E1=91=ADT=E1=97=A9I=E1=91=8E=E1=97=B0?= =?UTF-8?q?=E1=97=A9=E1=96=87=E1=90=AF=E1=97=B4=E1=92=AA?= <91954476+CAPITAINMARVEL@users.noreply.github.com> Date: Wed, 25 Sep 2024 11:04:57 +0200 Subject: [PATCH 1/3] fix return type from document update --- beanie/odm/documents.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/beanie/odm/documents.py b/beanie/odm/documents.py index e6a63d9f..6044597d 100644 --- a/beanie/odm/documents.py +++ b/beanie/odm/documents.py @@ -5,7 +5,6 @@ from typing import ( TYPE_CHECKING, Any, - Awaitable, Callable, ClassVar, Coroutine, @@ -691,7 +690,7 @@ async def replace_many( @wrap_with_actions(EventTypes.UPDATE) @save_state_after async def update( - self, + self: DocType, *args, ignore_revision: bool = False, session: Optional[ClientSession] = None, @@ -767,13 +766,13 @@ def update_all( ) def set( - self: DocType, + self, expression: Dict[Union[ExpressionField, str, Any], Any], session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, skip_sync: Optional[bool] = None, **kwargs, - ) -> Awaitable[DocType]: + ): """ Set values From 9f073b60c5473730d44258d74a76dbbc6780d395 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=91=95=E1=97=A9=E1=91=ADT=E1=97=A9I=E1=91=8E=E1=97=B0?= =?UTF-8?q?=E1=97=A9=E1=96=87=E1=90=AF=E1=97=B4=E1=92=AA?= <91954476+CAPITAINMARVEL@users.noreply.github.com> Date: Wed, 2 Oct 2024 15:13:14 +0200 Subject: [PATCH 2/3] Update documents.py fix type hint --- beanie/odm/documents.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/beanie/odm/documents.py b/beanie/odm/documents.py index 6044597d..47a7d1b7 100644 --- a/beanie/odm/documents.py +++ b/beanie/odm/documents.py @@ -562,7 +562,7 @@ async def save( :param session: Optional[ClientSession] - pymongo session. :param link_rule: WriteRules - rules how to deal with links on writing :param ignore_revision: bool - do force save. - :return: None + :return: DocType """ if link_rule == WriteRules.WRITE: link_fields = self.get_link_fields() @@ -642,7 +642,7 @@ async def save_changes( :param ignore_revision: bool - ignore revision id, if revision is turned on :param bulk_writer: "BulkWriter" - Beanie bulk writer - :return: None + :return: Optional[DocType] """ if not self.is_changed: return None @@ -707,7 +707,7 @@ async def update( :param ignore_revision: bool - force update. Will update even if revision id is not the same, as stored :param bulk_writer: "BulkWriter" - Beanie bulk writer :param pymongo_kwargs: pymongo native parameters for update operation - :return: None + :return: DocType """ arguments = list(args) From 2d4ab4cb9928cfd2b918838bd22379981ef4fb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=91=95=E1=97=A9=E1=91=ADT=E1=97=A9I=E1=91=8E=E1=97=B0?= =?UTF-8?q?=E1=97=A9=E1=96=87=E1=90=AF=E1=97=B4=E1=92=AA?= Date: Wed, 2 Oct 2024 17:36:21 +0200 Subject: [PATCH 3/3] using self --- beanie/odm/documents.py | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/beanie/odm/documents.py b/beanie/odm/documents.py index 47a7d1b7..515e6e04 100644 --- a/beanie/odm/documents.py +++ b/beanie/odm/documents.py @@ -37,7 +37,7 @@ DeleteResult, InsertManyResult, ) -from typing_extensions import Concatenate, ParamSpec, TypeAlias +from typing_extensions import Concatenate, ParamSpec, Self, TypeAlias from beanie.exceptions import ( CollectionWasNotInitialized, @@ -323,15 +323,15 @@ async def sync(self, merge_strategy: MergeStrategy = MergeStrategy.remote): @save_state_after @validate_self_before async def insert( - self: DocType, + self: Self, *, link_rule: WriteRules = WriteRules.DO_NOTHING, session: Optional[ClientSession] = None, skip_actions: Optional[List[Union[ActionDirections, str]]] = None, - ) -> DocType: + ) -> Self: """ Insert the document (self) to the collection - :return: Document + :return: self """ if self.get_settings().use_revision: self.revision_id = uuid4() @@ -381,12 +381,12 @@ async def insert( return self async def create( - self: DocType, + self: Self, session: Optional[ClientSession] = None, - ) -> DocType: + ) -> Self: """ The same as self.insert() - :return: Document + :return: self """ return await self.insert(session=session) @@ -466,13 +466,13 @@ async def insert_many( @save_state_after @validate_self_before async def replace( - self: DocType, + self: Self, ignore_revision: bool = False, session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, link_rule: WriteRules = WriteRules.DO_NOTHING, skip_actions: Optional[List[Union[ActionDirections, str]]] = None, - ) -> DocType: + ) -> Self: """ Fully update the document in the database @@ -549,12 +549,12 @@ async def replace( @save_state_after @validate_self_before async def save( - self: DocType, + self: Self, session: Optional[ClientSession] = None, link_rule: WriteRules = WriteRules.DO_NOTHING, ignore_revision: bool = False, **kwargs, - ) -> DocType: + ) -> Self: """ Update an existing model in the database or insert it if it does not yet exist. @@ -562,7 +562,7 @@ async def save( :param session: Optional[ClientSession] - pymongo session. :param link_rule: WriteRules - rules how to deal with links on writing :param ignore_revision: bool - do force save. - :return: DocType + :return: self """ if link_rule == WriteRules.WRITE: link_fields = self.get_link_fields() @@ -630,19 +630,19 @@ async def save( @wrap_with_actions(EventTypes.SAVE_CHANGES) @validate_self_before async def save_changes( - self: DocType, + self: Self, ignore_revision: bool = False, session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, skip_actions: Optional[List[Union[ActionDirections, str]]] = None, - ) -> Optional[DocType]: + ) -> Optional[Self]: """ Save changes. State management usage must be turned on :param ignore_revision: bool - ignore revision id, if revision is turned on :param bulk_writer: "BulkWriter" - Beanie bulk writer - :return: Optional[DocType] + :return: Optional[self] """ if not self.is_changed: return None @@ -690,7 +690,7 @@ async def replace_many( @wrap_with_actions(EventTypes.UPDATE) @save_state_after async def update( - self: DocType, + self: Self, *args, ignore_revision: bool = False, session: Optional[ClientSession] = None, @@ -698,7 +698,7 @@ async def update( skip_actions: Optional[List[Union[ActionDirections, str]]] = None, skip_sync: Optional[bool] = None, **pymongo_kwargs, - ) -> DocType: + ) -> Self: """ Partially update the document in the database @@ -707,7 +707,7 @@ async def update( :param ignore_revision: bool - force update. Will update even if revision id is not the same, as stored :param bulk_writer: "BulkWriter" - Beanie bulk writer :param pymongo_kwargs: pymongo native parameters for update operation - :return: DocType + :return: self """ arguments = list(args) @@ -766,13 +766,13 @@ def update_all( ) def set( - self, + self: Self, expression: Dict[Union[ExpressionField, str, Any], Any], session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, skip_sync: Optional[bool] = None, **kwargs, - ): + ) -> Coroutine[None, None, Self]: """ Set values @@ -805,13 +805,13 @@ class Sample(Document): ) def current_date( - self, + self: Self, expression: Dict[Union[datetime, ExpressionField, str], Any], session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, skip_sync: Optional[bool] = None, **kwargs, - ): + ) -> Coroutine[None, None, Self]: """ Set current date @@ -832,13 +832,13 @@ def current_date( ) def inc( - self, + self: Self, expression: Dict[Union[ExpressionField, float, int, str], Any], session: Optional[ClientSession] = None, bulk_writer: Optional[BulkWriter] = None, skip_sync: Optional[bool] = None, **kwargs, - ): + ) -> Coroutine[None, None, Self]: """ Increment