Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix return type from document update #1030

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[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()
Expand Down Expand Up @@ -382,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)

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[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

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[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.

: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: 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[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: 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[ClientSession] = 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[ClientSession] = 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[ClientSession] = 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[ClientSession] = None,
bulk_writer: Optional[BulkWriter] = None,
skip_sync: Optional[bool] = None,
**kwargs,
):
) -> Coroutine[None, None, Self]:
"""
Increment

Expand Down
Loading