refactor many2many typehints into protocols

This commit is contained in:
collerek
2020-11-01 08:29:06 +01:00
parent 79cf225ddc
commit 9175f6004c
6 changed files with 66 additions and 62 deletions

View File

@ -1,5 +1,6 @@
from ormar.exceptions import ModelDefinitionError, ModelNotSet, MultipleMatches, NoMatch from ormar.exceptions import ModelDefinitionError, ModelNotSet, MultipleMatches, NoMatch
from ormar.fields import ( from ormar.protocols import QuerySetProtocol, RelationProtocol # noqa: I100
from ormar.fields import ( # noqa: I100
BigInteger, BigInteger,
Boolean, Boolean,
Date, Date,
@ -28,8 +29,7 @@ class UndefinedType: # pragma no cover
Undefined = UndefinedType() Undefined = UndefinedType()
__version__ = "0.4.0"
__version__ = "0.3.11"
__all__ = [ __all__ = [
"Integer", "Integer",
"BigInteger", "BigInteger",
@ -54,4 +54,6 @@ __all__ = [
"Undefined", "Undefined",
"UUID", "UUID",
"UniqueColumns", "UniqueColumns",
"QuerySetProtocol",
"RelationProtocol",
] ]

View File

@ -1,5 +1,6 @@
from typing import Any, List, Optional, Sequence, TYPE_CHECKING, Type, Union from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
import ormar
from ormar.fields import BaseField from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField from ormar.fields.foreign_key import ForeignKeyField
@ -47,61 +48,5 @@ def ManyToMany(
return type("ManyToMany", (ManyToManyField, BaseField), namespace) return type("ManyToMany", (ManyToManyField, BaseField), namespace)
class ManyToManyField(ForeignKeyField): class ManyToManyField(ForeignKeyField, ormar.QuerySetProtocol, ormar.RelationProtocol):
through: Type["Model"] through: Type["Model"]
if TYPE_CHECKING: # noqa: C901; #pragma nocover
@staticmethod
async def add(item: "Model") -> None:
pass
@staticmethod
async def remove(item: "Model") -> None:
pass
from ormar import QuerySet
@staticmethod
def filter(**kwargs: Any) -> "QuerySet": # noqa: A003, A001
pass
@staticmethod
def select_related(related: Union[List, str]) -> "QuerySet":
pass
@staticmethod
async def exists() -> bool:
pass
@staticmethod
async def count() -> int:
pass
@staticmethod
async def clear() -> int:
pass
@staticmethod
def limit(limit_count: int) -> "QuerySet":
pass
@staticmethod
def offset(offset: int) -> "QuerySet":
pass
@staticmethod
async def first(**kwargs: Any) -> "Model":
pass
@staticmethod
async def get(**kwargs: Any) -> "Model":
pass
@staticmethod
async def all(**kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003, A001
pass
@staticmethod
async def create(**kwargs: Any) -> "Model":
pass

View File

@ -0,0 +1,4 @@
from ormar.protocols.queryset_protocol import QuerySetProtocol
from ormar.protocols.relation_protocol import RelationProtocol
__all__ = ["QuerySetProtocol", "RelationProtocol"]

View File

@ -0,0 +1,41 @@
from typing import Any, List, Optional, Protocol, Sequence, TYPE_CHECKING, Union
if TYPE_CHECKING: # noqa: C901; #pragma nocover
from ormar import QuerySet, Model
class QuerySetProtocol(Protocol): # pragma: nocover
def filter(self, **kwargs: Any) -> "QuerySet": # noqa: A003, A001
...
def select_related(self, related: Union[List, str]) -> "QuerySet":
...
async def exists(self) -> bool:
...
async def count(self) -> int:
...
async def clear(self) -> int:
...
def limit(self, limit_count: int) -> "QuerySet":
...
def offset(self, offset: int) -> "QuerySet":
...
async def first(self, **kwargs: Any) -> "Model":
...
async def get(self, **kwargs: Any) -> "Model":
...
async def all( # noqa: A003, A001
self, **kwargs: Any
) -> Sequence[Optional["Model"]]:
...
async def create(self, **kwargs: Any) -> "Model":
...

View File

@ -0,0 +1,12 @@
from typing import Protocol, TYPE_CHECKING, Type, Union
if TYPE_CHECKING: # pragma: nocover
from ormar import Model
class RelationProtocol(Protocol): # pragma: nocover
def add(self, child: "Model") -> None:
...
def remove(self, child: Union["Model", Type["Model"]]) -> None:
...

View File

@ -10,7 +10,7 @@ if TYPE_CHECKING: # pragma no cover
T = TypeVar("T", bound=Model) T = TypeVar("T", bound=Model)
class QuerysetProxy: class QuerysetProxy(ormar.QuerySetProtocol):
if TYPE_CHECKING: # pragma no cover if TYPE_CHECKING: # pragma no cover
relation: "Relation" relation: "Relation"