some types improvements
This commit is contained in:
@ -302,7 +302,7 @@ class QuerysetProxy(Generic[T]):
|
||||
self._register_related(get)
|
||||
return get
|
||||
|
||||
async def all(self, **kwargs: Any) -> Sequence[Optional["T"]]: # noqa: A003
|
||||
async def all(self, **kwargs: Any) -> List[Optional["T"]]: # noqa: A003
|
||||
"""
|
||||
Returns all rows from a database for given model for set filter options.
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
from enum import Enum
|
||||
from typing import List, Optional, Set, TYPE_CHECKING, Type, Union
|
||||
from typing import Generic, List, Optional, Set, TYPE_CHECKING, Type, TypeVar, Union, \
|
||||
cast
|
||||
|
||||
import ormar # noqa I100
|
||||
from ormar.exceptions import RelationshipInstanceError # noqa I100
|
||||
@ -7,7 +8,10 @@ from ormar.relations.relation_proxy import RelationProxy
|
||||
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar.relations import RelationsManager
|
||||
from ormar.models import Model, NewBaseModel
|
||||
from ormar.models import Model, NewBaseModel, T
|
||||
from ormar.relations.relation_proxy import RelationProxy
|
||||
else:
|
||||
T = TypeVar("T", bound="Model")
|
||||
|
||||
|
||||
class RelationType(Enum):
|
||||
@ -25,7 +29,7 @@ class RelationType(Enum):
|
||||
THROUGH = 4
|
||||
|
||||
|
||||
class Relation:
|
||||
class Relation(Generic[T]):
|
||||
"""
|
||||
Keeps related Models and handles adding/removing of the children.
|
||||
"""
|
||||
@ -35,7 +39,7 @@ class Relation:
|
||||
manager: "RelationsManager",
|
||||
type_: RelationType,
|
||||
field_name: str,
|
||||
to: Type["Model"],
|
||||
to: Type["T"],
|
||||
through: Type["Model"] = None,
|
||||
) -> None:
|
||||
"""
|
||||
@ -59,7 +63,7 @@ class Relation:
|
||||
self._owner: "Model" = manager.owner
|
||||
self._type: RelationType = type_
|
||||
self._to_remove: Set = set()
|
||||
self.to: Type["Model"] = to
|
||||
self.to: Type["T"] = to
|
||||
self._through = through
|
||||
self.field_name: str = field_name
|
||||
self.related_models: Optional[Union[RelationProxy, "Model"]] = (
|
||||
@ -73,7 +77,8 @@ class Relation:
|
||||
self.related_models = None
|
||||
self._owner.__dict__[self.field_name] = None
|
||||
elif self.related_models is not None:
|
||||
self.related_models._clear()
|
||||
related_models = cast("RelationProxy", self.related_models)
|
||||
related_models._clear()
|
||||
self._owner.__dict__[self.field_name] = None
|
||||
|
||||
@property
|
||||
|
||||
@ -27,11 +27,11 @@ class RelationProxy(Generic[T], list):
|
||||
data_: Any = None,
|
||||
) -> None:
|
||||
super().__init__(data_ or ())
|
||||
self.relation: "Relation" = relation
|
||||
self.relation: "Relation[T]" = relation
|
||||
self.type_: "RelationType" = type_
|
||||
self.field_name = field_name
|
||||
self._owner: "Model" = self.relation.manager.owner
|
||||
self.queryset_proxy: QuerysetProxy = QuerysetProxy(
|
||||
self.queryset_proxy: QuerysetProxy[T] = QuerysetProxy[T](
|
||||
relation=self.relation, to=to, type_=type_
|
||||
)
|
||||
self._related_field_name: Optional[str] = None
|
||||
@ -70,7 +70,7 @@ class RelationProxy(Generic[T], list):
|
||||
return getattr(self.queryset_proxy, item)
|
||||
return super().__getattribute__(item)
|
||||
|
||||
def __getattr__(self, item: str) -> "T":
|
||||
def __getattr__(self, item: str) -> Any:
|
||||
"""
|
||||
Delegates calls for non existing attributes to QuerySetProxy.
|
||||
|
||||
@ -114,7 +114,7 @@ class RelationProxy(Generic[T], list):
|
||||
"You cannot query relationships from unsaved model."
|
||||
)
|
||||
|
||||
def _set_queryset(self) -> "QuerySet":
|
||||
def _set_queryset(self) -> "QuerySet[T]":
|
||||
"""
|
||||
Creates new QuerySet with relation model and pre filters it with currents
|
||||
parent model primary key, so all queries by definition are already related
|
||||
@ -138,7 +138,7 @@ class RelationProxy(Generic[T], list):
|
||||
return queryset
|
||||
|
||||
async def remove( # type: ignore
|
||||
self, item: "Model", keep_reversed: bool = True
|
||||
self, item: "T", keep_reversed: bool = True
|
||||
) -> None:
|
||||
"""
|
||||
Removes the related from relation with parent.
|
||||
@ -189,7 +189,7 @@ class RelationProxy(Generic[T], list):
|
||||
relation_name=self.field_name,
|
||||
)
|
||||
|
||||
async def add(self, item: "Model", **kwargs: Any) -> None:
|
||||
async def add(self, item: "T", **kwargs: Any) -> None:
|
||||
"""
|
||||
Adds child model to relation.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user