Skip to content

Commit

Permalink
fix return type from document update (#1030)
Browse files Browse the repository at this point in the history
* fix return type from document update

---------

Co-authored-by: ᑕᗩᑭTᗩIᑎᗰᗩᖇᐯᗴᒪ <[email protected]>
  • Loading branch information
CAPITAINMARVEL and CAPITAINMARVEL authored Oct 4, 2024
1 parent b438edc commit 4318aa9
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions beanie/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import (
TYPE_CHECKING,
Any,
Awaitable,
Callable,
ClassVar,
Coroutine,
Expand Down Expand Up @@ -38,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,
Expand Down Expand Up @@ -324,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[AsyncIOMotorClientSession] = 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()
Expand Down Expand Up @@ -382,12 +381,12 @@ async def insert(
return self

async def create(
self: DocType,
self: Self,
session: Optional[AsyncIOMotorClientSession] = None,
) -> DocType:
) -> Self:
"""
The same as self.insert()
:return: Document
:return: self
"""
return await self.insert(session=session)

Expand Down Expand Up @@ -467,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[AsyncIOMotorClientSession] = 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
Expand Down Expand Up @@ -550,20 +549,20 @@ async def replace(
@save_state_after
@validate_self_before
async def save(
self: DocType,
self: Self,
session: Optional[AsyncIOMotorClientSession] = 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.
:param session: Optional[AsyncIOMotorClientSession] - motor session.
:param link_rule: WriteRules - rules how to deal with links on writing
:param ignore_revision: bool - do force save.
:return: None
:return: self
"""
if link_rule == WriteRules.WRITE:
link_fields = self.get_link_fields()
Expand Down Expand Up @@ -631,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[AsyncIOMotorClientSession] = 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: None
:return: Optional[self]
"""
if not self.is_changed:
return None
Expand Down Expand Up @@ -691,15 +690,15 @@ async def replace_many(
@wrap_with_actions(EventTypes.UPDATE)
@save_state_after
async def update(
self,
self: Self,
*args,
ignore_revision: bool = False,
session: Optional[AsyncIOMotorClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_actions: Optional[List[Union[ActionDirections, str]]] = None,
skip_sync: Optional[bool] = None,
**pymongo_kwargs,
) -> DocType:
) -> Self:
"""
Partially update the document in the database
Expand All @@ -708,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: self
"""
arguments = list(args)

Expand Down Expand Up @@ -767,13 +766,13 @@ def update_all(
)

def set(
self: DocType,
self: Self,
expression: Dict[Union[ExpressionField, str, Any], Any],
session: Optional[AsyncIOMotorClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_sync: Optional[bool] = None,
**kwargs,
) -> Awaitable[DocType]:
) -> Coroutine[None, None, Self]:
"""
Set values
Expand Down Expand Up @@ -806,13 +805,13 @@ class Sample(Document):
)

def current_date(
self,
self: Self,
expression: Dict[Union[datetime, ExpressionField, str], Any],
session: Optional[AsyncIOMotorClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_sync: Optional[bool] = None,
**kwargs,
):
) -> Coroutine[None, None, Self]:
"""
Set current date
Expand All @@ -833,13 +832,13 @@ def current_date(
)

def inc(
self,
self: Self,
expression: Dict[Union[ExpressionField, float, int, str], Any],
session: Optional[AsyncIOMotorClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_sync: Optional[bool] = None,
**kwargs,
):
) -> Coroutine[None, None, Self]:
"""
Increment
Expand Down

0 comments on commit 4318aa9

Please sign in to comment.