allow change to build in type hints
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Union
|
||||
from typing import Any, List, Optional, Sequence, TYPE_CHECKING, TypeVar, Union
|
||||
|
||||
import ormar
|
||||
|
||||
@ -7,6 +7,8 @@ if TYPE_CHECKING: # pragma no cover
|
||||
from ormar.models import Model
|
||||
from ormar.queryset import QuerySet
|
||||
|
||||
T = TypeVar("T", bound=Model)
|
||||
|
||||
|
||||
class QuerysetProxy:
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
@ -26,27 +28,28 @@ class QuerysetProxy:
|
||||
def queryset(self, value: "QuerySet") -> None:
|
||||
self._queryset = value
|
||||
|
||||
def _assign_child_to_parent(self, child: Optional["Model"]) -> None:
|
||||
def _assign_child_to_parent(self, child: Optional[T]) -> None:
|
||||
if child:
|
||||
owner = self.relation._owner
|
||||
rel_name = owner.resolve_relation_name(owner, child)
|
||||
setattr(owner, rel_name, child)
|
||||
|
||||
def _register_related(self, child: Union["Model", List[Optional["Model"]]]) -> None:
|
||||
def _register_related(self, child: Union[T, Sequence[Optional[T]]]) -> None:
|
||||
if isinstance(child, list):
|
||||
for subchild in child:
|
||||
self._assign_child_to_parent(subchild)
|
||||
else:
|
||||
assert isinstance(child, Model)
|
||||
self._assign_child_to_parent(child)
|
||||
|
||||
async def create_through_instance(self, child: "Model") -> None:
|
||||
async def create_through_instance(self, child: T) -> None:
|
||||
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
||||
owner_column = self.relation._owner.get_name()
|
||||
child_column = child.get_name()
|
||||
kwargs = {owner_column: self.relation._owner, child_column: child}
|
||||
await queryset.create(**kwargs)
|
||||
|
||||
async def delete_through_instance(self, child: "Model") -> None:
|
||||
async def delete_through_instance(self, child: T) -> None:
|
||||
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
||||
owner_column = self.relation._owner.get_name()
|
||||
child_column = child.get_name()
|
||||
@ -88,7 +91,7 @@ class QuerysetProxy:
|
||||
self._register_related(get)
|
||||
return get
|
||||
|
||||
async def all(self, **kwargs: Any) -> List[Optional["Model"]]: # noqa: A003
|
||||
async def all(self, **kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003
|
||||
all_items = await self.queryset.all(**kwargs)
|
||||
self._register_related(all_items)
|
||||
return all_items
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
from enum import Enum
|
||||
from typing import List, Optional, TYPE_CHECKING, Type, Union
|
||||
from typing import List, Optional, TYPE_CHECKING, Type, TypeVar, Union
|
||||
|
||||
import ormar # noqa I100
|
||||
from ormar.exceptions import RelationshipInstanceError # noqa I100
|
||||
@ -11,6 +11,8 @@ if TYPE_CHECKING: # pragma no cover
|
||||
from ormar.relations import RelationsManager
|
||||
from ormar.models import NewBaseModel
|
||||
|
||||
T = TypeVar("T", bound=Model)
|
||||
|
||||
|
||||
class RelationType(Enum):
|
||||
PRIMARY = 1
|
||||
@ -23,15 +25,15 @@ class Relation:
|
||||
self,
|
||||
manager: "RelationsManager",
|
||||
type_: RelationType,
|
||||
to: Type["Model"],
|
||||
through: Type["Model"] = None,
|
||||
to: Type[T],
|
||||
through: Type[T] = None,
|
||||
) -> None:
|
||||
self.manager = manager
|
||||
self._owner: "Model" = manager.owner
|
||||
self._type: RelationType = type_
|
||||
self.to: Type["Model"] = to
|
||||
self.through: Optional[Type["Model"]] = through
|
||||
self.related_models: Optional[Union[RelationProxy, "Model"]] = (
|
||||
self.to: Type[T] = to
|
||||
self.through: Optional[Type[T]] = through
|
||||
self.related_models: Optional[Union[RelationProxy, T]] = (
|
||||
RelationProxy(relation=self)
|
||||
if type_ in (RelationType.REVERSE, RelationType.MULTIPLE)
|
||||
else None
|
||||
@ -50,7 +52,7 @@ class Relation:
|
||||
self.related_models.pop(ind)
|
||||
return None
|
||||
|
||||
def add(self, child: "Model") -> None:
|
||||
def add(self, child: T) -> None:
|
||||
relation_name = self._owner.resolve_relation_name(self._owner, child)
|
||||
if self._type == RelationType.PRIMARY:
|
||||
self.related_models = child
|
||||
@ -77,7 +79,7 @@ class Relation:
|
||||
self.related_models.pop(position) # type: ignore
|
||||
del self._owner.__dict__[relation_name][position]
|
||||
|
||||
def get(self) -> Optional[Union[List["Model"], "Model"]]:
|
||||
def get(self) -> Optional[Union[List[T], T]]:
|
||||
return self.related_models
|
||||
|
||||
def __repr__(self) -> str: # pragma no cover
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Dict, List, Optional, TYPE_CHECKING, Type, Union
|
||||
from typing import Dict, List, Optional, Sequence, TYPE_CHECKING, Type, TypeVar, Union
|
||||
from weakref import proxy
|
||||
|
||||
from ormar.fields import BaseField
|
||||
@ -14,6 +14,8 @@ if TYPE_CHECKING: # pragma no cover
|
||||
from ormar import Model
|
||||
from ormar.models import NewBaseModel
|
||||
|
||||
T = TypeVar("T", bound=Model)
|
||||
|
||||
|
||||
class RelationsManager:
|
||||
def __init__(
|
||||
@ -46,7 +48,7 @@ class RelationsManager:
|
||||
def __contains__(self, item: str) -> bool:
|
||||
return item in self._related_names
|
||||
|
||||
def get(self, name: str) -> Optional[Union[List["Model"], "Model"]]:
|
||||
def get(self, name: str) -> Optional[Union[T, Sequence[T]]]:
|
||||
relation = self._relations.get(name, None)
|
||||
if relation is not None:
|
||||
return relation.get()
|
||||
|
||||
@ -72,6 +72,6 @@ class RelationProxy(list):
|
||||
if self.relation._type == ormar.RelationType.MULTIPLE:
|
||||
await self.queryset_proxy.create_through_instance(item)
|
||||
rel_name = item.resolve_relation_name(item, self._owner)
|
||||
if rel_name not in item._orm:
|
||||
if rel_name not in item._orm: # pragma nocover
|
||||
item._orm._add_relation(item.Meta.model_fields[rel_name])
|
||||
setattr(item, rel_name, self._owner)
|
||||
|
||||
Reference in New Issue
Block a user