Skip to content

Commit

Permalink
Fix: FindInterface type-hints break on View models (#819)
Browse files Browse the repository at this point in the history
* fix: FindInterface type-hints break on View models

* fix: find interface methods for views

---------

Co-authored-by: Guy Tsitsiashvili <georgit@rapyd.net>
  • Loading branch information
GuyGooL5 and Guy Tsitsiashvili committed Jan 22, 2024
1 parent 9056fbb commit 63542eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 27 deletions.
3 changes: 2 additions & 1 deletion beanie/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,8 @@ def get_previous_changes(self) -> Dict[str, Any]:
return {}

return self._collect_updates(
self._previous_saved_state, self._saved_state # type: ignore
self._previous_saved_state,
self._saved_state, # type: ignore
)

@saved_state_needed
Expand Down
54 changes: 28 additions & 26 deletions beanie/odm/interfaces/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
from beanie.odm.settings.base import ItemSettings

if TYPE_CHECKING:
from beanie.odm.documents import DocType
from beanie.odm.documents import Document
from beanie.odm.views import View

DocumentProjectionType = TypeVar("DocumentProjectionType", bound=BaseModel)
FindType = TypeVar("FindType", bound=Union["Document", "View"])


class FindInterface:
Expand All @@ -54,21 +56,21 @@ def get_settings(cls) -> ItemSettings:
@overload
@classmethod
def find_one( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: None = None,
session: Optional[ClientSession] = None,
ignore_cache: bool = False,
fetch_links: bool = False,
with_children: bool = False,
**pymongo_kwargs,
) -> FindOne["DocType"]:
) -> FindOne[FindType]:
...

@overload
@classmethod
def find_one( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Type["DocumentProjectionType"],
session: Optional[ClientSession] = None,
Expand All @@ -81,15 +83,15 @@ def find_one( # type: ignore

@classmethod
def find_one( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Optional[Type["DocumentProjectionType"]] = None,
session: Optional[ClientSession] = None,
ignore_cache: bool = False,
fetch_links: bool = False,
with_children: bool = False,
**pymongo_kwargs,
) -> Union[FindOne["DocType"], FindOne["DocumentProjectionType"]]:
) -> Union[FindOne[FindType], FindOne["DocumentProjectionType"]]:
"""
Find one document by criteria.
Returns [FindOne](query.md#findone) query object.
Expand All @@ -115,7 +117,7 @@ def find_one( # type: ignore
@overload
@classmethod
def find_many( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: None = None,
skip: Optional[int] = None,
Expand All @@ -127,13 +129,13 @@ def find_many( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> FindMany["DocType"]:
) -> FindMany[FindType]:
...

@overload
@classmethod
def find_many( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Optional[Type["DocumentProjectionType"]] = None,
skip: Optional[int] = None,
Expand All @@ -150,7 +152,7 @@ def find_many( # type: ignore

@classmethod
def find_many( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Optional[Type["DocumentProjectionType"]] = None,
skip: Optional[int] = None,
Expand All @@ -162,7 +164,7 @@ def find_many( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> Union[FindMany["DocType"], FindMany["DocumentProjectionType"]]:
) -> Union[FindMany[FindType], FindMany["DocumentProjectionType"]]:
"""
Find many documents by criteria.
Returns [FindMany](query.md#findmany) query object
Expand Down Expand Up @@ -195,7 +197,7 @@ def find_many( # type: ignore
@overload
@classmethod
def find( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: None = None,
skip: Optional[int] = None,
Expand All @@ -207,13 +209,13 @@ def find( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> FindMany["DocType"]:
) -> FindMany[FindType]:
...

@overload
@classmethod
def find( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Type["DocumentProjectionType"],
skip: Optional[int] = None,
Expand All @@ -230,7 +232,7 @@ def find( # type: ignore

@classmethod
def find( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
*args: Union[Mapping[str, Any], bool],
projection_model: Optional[Type["DocumentProjectionType"]] = None,
skip: Optional[int] = None,
Expand All @@ -242,7 +244,7 @@ def find( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> Union[FindMany["DocType"], FindMany["DocumentProjectionType"]]:
) -> Union[FindMany[FindType], FindMany["DocumentProjectionType"]]:
"""
The same as find_many
"""
Expand All @@ -263,7 +265,7 @@ def find( # type: ignore
@overload
@classmethod
def find_all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Union[None, str, List[Tuple[str, SortDirection]]] = None,
Expand All @@ -273,13 +275,13 @@ def find_all( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> FindMany["DocType"]:
) -> FindMany[FindType]:
...

@overload
@classmethod
def find_all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Union[None, str, List[Tuple[str, SortDirection]]] = None,
Expand All @@ -294,7 +296,7 @@ def find_all( # type: ignore

@classmethod
def find_all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
skip: Optional[int] = None,
limit: Optional[int] = None,
sort: Union[None, str, List[Tuple[str, SortDirection]]] = None,
Expand All @@ -304,7 +306,7 @@ def find_all( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> Union[FindMany["DocType"], FindMany["DocumentProjectionType"]]:
) -> Union[FindMany[FindType], FindMany["DocumentProjectionType"]]:
"""
Get all the documents
Expand Down Expand Up @@ -332,7 +334,7 @@ def find_all( # type: ignore
@overload
@classmethod
def all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
projection_model: None = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
Expand All @@ -342,13 +344,13 @@ def all( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> FindMany["DocType"]:
) -> FindMany[FindType]:
...

@overload
@classmethod
def all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
projection_model: Type["DocumentProjectionType"],
skip: Optional[int] = None,
limit: Optional[int] = None,
Expand All @@ -363,7 +365,7 @@ def all( # type: ignore

@classmethod
def all( # type: ignore
cls: Type["DocType"],
cls: Type[FindType],
projection_model: Optional[Type["DocumentProjectionType"]] = None,
skip: Optional[int] = None,
limit: Optional[int] = None,
Expand All @@ -373,7 +375,7 @@ def all( # type: ignore
with_children: bool = False,
lazy_parse: bool = False,
**pymongo_kwargs,
) -> Union[FindMany["DocType"], FindMany["DocumentProjectionType"]]:
) -> Union[FindMany[FindType], FindMany["DocumentProjectionType"]]:
"""
the same as find_all
"""
Expand Down

0 comments on commit 63542eb

Please sign in to comment.