finish mypy checks add reqs and linting
This commit is contained in:
@ -27,6 +27,7 @@ script:
|
||||
- DATABASE_URL=postgresql://localhost/test_database scripts/test.sh
|
||||
- DATABASE_URL=mysql://localhost/test_database scripts/test.sh
|
||||
- DATABASE_URL=sqlite:///test.db scripts/test.sh
|
||||
- mypy --config-file mypy.ini ormar
|
||||
|
||||
after_script:
|
||||
- codecov
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Union, Type
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
|
||||
|
||||
import sqlalchemy
|
||||
from pydantic import Field, typing
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union, Generator
|
||||
from typing import Any, Generator, List, Optional, TYPE_CHECKING, Type, Union
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
@ -76,7 +76,10 @@ class ForeignKeyField(BaseField):
|
||||
def _extract_model_from_sequence(
|
||||
cls, value: List, child: "Model", to_register: bool
|
||||
) -> List["Model"]:
|
||||
return [cls.expand_relationship(val, child, to_register) for val in value] # type: ignore
|
||||
return [
|
||||
cls.expand_relationship(val, child, to_register) # type: ignore
|
||||
for val in value
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def _register_existing_model(
|
||||
|
||||
@ -293,7 +293,9 @@ def populate_choices_validators( # noqa CCR001
|
||||
|
||||
|
||||
class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
def __new__(mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict) -> "ModelMetaclass": # type: ignore
|
||||
def __new__( # type: ignore
|
||||
mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict
|
||||
) -> "ModelMetaclass":
|
||||
attrs["Config"] = get_pydantic_base_orm_config()
|
||||
attrs["__name__"] = name
|
||||
attrs = extract_annotations_and_default_vals(attrs, bases)
|
||||
@ -312,7 +314,9 @@ class ModelMetaclass(pydantic.main.ModelMetaclass):
|
||||
field_name = new_model.Meta.pkname
|
||||
field = Integer(name=field_name, primary_key=True)
|
||||
attrs["__annotations__"][field_name] = field
|
||||
populate_default_pydantic_field_value(field, field_name, attrs) # type: ignore
|
||||
populate_default_pydantic_field_value(
|
||||
field, field_name, attrs # type: ignore
|
||||
)
|
||||
|
||||
new_model = super().__new__( # type: ignore
|
||||
mcs, name, bases, attrs
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import itertools
|
||||
from typing import Any, List, Dict, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import sqlalchemy
|
||||
from databases.backends.postgres import Record
|
||||
@ -28,7 +28,7 @@ class Model(NewBaseModel):
|
||||
__abstract__ = False
|
||||
|
||||
@classmethod
|
||||
def from_row(
|
||||
def from_row( # noqa CCR001
|
||||
cls,
|
||||
row: sqlalchemy.engine.ResultProxy,
|
||||
select_related: List = None,
|
||||
@ -57,7 +57,7 @@ class Model(NewBaseModel):
|
||||
previous_table, cls.Meta.table.name
|
||||
)
|
||||
else:
|
||||
table_prefix = ''
|
||||
table_prefix = ""
|
||||
previous_table = cls.Meta.table.name
|
||||
|
||||
item = cls.populate_nested_models_from_row(
|
||||
@ -142,6 +142,8 @@ class Model(NewBaseModel):
|
||||
expr = self.Meta.table.select().where(self.pk_column == self.pk)
|
||||
row = await self.Meta.database.fetch_one(expr)
|
||||
if not row: # pragma nocover
|
||||
raise ValueError('Instance was deleted from database and cannot be refreshed')
|
||||
raise ValueError(
|
||||
"Instance was deleted from database and cannot be refreshed"
|
||||
)
|
||||
self.from_dict(dict(row))
|
||||
return self
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import inspect
|
||||
from typing import List, Optional, Set, TYPE_CHECKING, Type, TypeVar, Union, Dict
|
||||
from typing import Dict, List, Set, TYPE_CHECKING, Type, TypeVar, Union
|
||||
|
||||
import ormar
|
||||
from ormar.exceptions import RelationshipInstanceError
|
||||
@ -94,7 +94,8 @@ class ModelTableProxy:
|
||||
|
||||
@staticmethod
|
||||
def resolve_relation_name(
|
||||
item: Union["NewBaseModel", Type["NewBaseModel"]], related: Union["NewBaseModel", Type["NewBaseModel"]]
|
||||
item: Union["NewBaseModel", Type["NewBaseModel"]],
|
||||
related: Union["NewBaseModel", Type["NewBaseModel"]],
|
||||
) -> str:
|
||||
for name, field in item.Meta.model_fields.items():
|
||||
if issubclass(field, ForeignKeyField):
|
||||
|
||||
@ -84,7 +84,9 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
for k, v in kwargs.items()
|
||||
}
|
||||
|
||||
values, fields_set, validation_error = pydantic.validate_model(self, kwargs) # type: ignore
|
||||
values, fields_set, validation_error = pydantic.validate_model(
|
||||
self, kwargs # type: ignore
|
||||
)
|
||||
if validation_error and not pk_only:
|
||||
raise validation_error
|
||||
|
||||
@ -218,7 +220,9 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
|
||||
condition = (
|
||||
isinstance(value, str) if op == "loads" else not isinstance(value, str)
|
||||
)
|
||||
operand: Callable[[Any], Any] = json.loads if op == "loads" else json.dumps
|
||||
operand: Callable[[Any], Any] = (
|
||||
json.loads if op == "loads" else json.dumps # type: ignore
|
||||
)
|
||||
|
||||
if condition:
|
||||
try:
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import List, TYPE_CHECKING, Tuple, Type, Optional
|
||||
from typing import List, Optional, TYPE_CHECKING, Tuple, Type
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import text
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Any, List, Mapping, TYPE_CHECKING, Type, Union, Optional
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
|
||||
|
||||
import databases
|
||||
import sqlalchemy
|
||||
@ -14,6 +14,7 @@ from ormar.queryset.query import Query
|
||||
if TYPE_CHECKING: # pragma no cover
|
||||
from ormar import Model
|
||||
from ormar.models.metaclass import ModelMeta
|
||||
from ormar.relations.querysetproxy import QuerysetProxy
|
||||
|
||||
|
||||
class QuerySet:
|
||||
@ -34,8 +35,15 @@ class QuerySet:
|
||||
self.query_offset = offset
|
||||
self.order_bys = None
|
||||
|
||||
def __get__(self, instance: "QuerySet", owner: Type["Model"]) -> "QuerySet":
|
||||
def __get__(
|
||||
self,
|
||||
instance: Union["QuerySet", "QuerysetProxy"],
|
||||
owner: Union[Type["Model"], Type["QuerysetProxy"]],
|
||||
) -> "QuerySet":
|
||||
if issubclass(owner, ormar.Model):
|
||||
return self.__class__(model_cls=owner)
|
||||
else: # pragma nocover
|
||||
return self.__class__()
|
||||
|
||||
@property
|
||||
def model_meta(self) -> "ModelMeta":
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import string
|
||||
import uuid
|
||||
from random import choices
|
||||
from typing import List, Dict
|
||||
from typing import Dict, List
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy import text
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import Any, List, TYPE_CHECKING, Tuple, Union
|
||||
from typing import Any, List, Optional, TYPE_CHECKING, Union
|
||||
|
||||
import ormar
|
||||
|
||||
@ -14,14 +14,25 @@ class QuerysetProxy:
|
||||
|
||||
def __init__(self, relation: "Relation") -> None:
|
||||
self.relation: Relation = relation
|
||||
self.queryset: "QuerySet"
|
||||
self._queryset: Optional["QuerySet"] = None
|
||||
|
||||
def _assign_child_to_parent(self, child: "Model") -> None:
|
||||
@property
|
||||
def queryset(self) -> "QuerySet":
|
||||
if not self._queryset:
|
||||
raise AttributeError
|
||||
return self._queryset
|
||||
|
||||
@queryset.setter
|
||||
def queryset(self, value: "QuerySet") -> None:
|
||||
self._queryset = value
|
||||
|
||||
def _assign_child_to_parent(self, child: Optional["Model"]) -> 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["Model"]]) -> None:
|
||||
def _register_related(self, child: Union["Model", List[Optional["Model"]]]) -> None:
|
||||
if isinstance(child, list):
|
||||
for subchild in child:
|
||||
self._assign_child_to_parent(subchild)
|
||||
@ -40,13 +51,13 @@ class QuerysetProxy:
|
||||
owner_column = self.relation._owner.get_name()
|
||||
child_column = child.get_name()
|
||||
kwargs = {owner_column: self.relation._owner, child_column: child}
|
||||
link_instance = await queryset.filter(**kwargs).get()
|
||||
link_instance = await queryset.filter(**kwargs).get() # type: ignore
|
||||
await link_instance.delete()
|
||||
|
||||
def filter(self, **kwargs: Any) -> "QuerySet": # noqa: A003
|
||||
return self.queryset.filter(**kwargs)
|
||||
|
||||
def select_related(self, related: Union[List, Tuple, str]) -> "QuerySet":
|
||||
def select_related(self, related: Union[List, str]) -> "QuerySet":
|
||||
return self.queryset.select_related(related)
|
||||
|
||||
async def exists(self) -> bool:
|
||||
@ -59,7 +70,7 @@ class QuerysetProxy:
|
||||
queryset = ormar.QuerySet(model_cls=self.relation.through)
|
||||
owner_column = self.relation._owner.get_name()
|
||||
kwargs = {owner_column: self.relation._owner}
|
||||
return await queryset.delete(**kwargs)
|
||||
return await queryset.delete(**kwargs) # type: ignore
|
||||
|
||||
def limit(self, limit_count: int) -> "QuerySet":
|
||||
return self.queryset.limit(limit_count)
|
||||
@ -77,7 +88,7 @@ class QuerysetProxy:
|
||||
self._register_related(get)
|
||||
return get
|
||||
|
||||
async def all(self, **kwargs: Any) -> List["Model"]: # noqa: A003
|
||||
async def all(self, **kwargs: Any) -> List[Optional["Model"]]: # noqa: A003
|
||||
all_items = await self.queryset.all(**kwargs)
|
||||
self._register_related(all_items)
|
||||
return all_items
|
||||
|
||||
@ -37,7 +37,9 @@ class Relation:
|
||||
else None
|
||||
)
|
||||
|
||||
def _find_existing(self, child: "Model") -> Optional[int]:
|
||||
def _find_existing(
|
||||
self, child: Union["NewBaseModel", Type["NewBaseModel"]]
|
||||
) -> Optional[int]:
|
||||
if not isinstance(self.related_models, RelationProxy): # pragma nocover
|
||||
raise ValueError("Cannot find existing models in parent relation type")
|
||||
for ind, relation_child in enumerate(self.related_models[:]):
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import List, Optional, TYPE_CHECKING, Type, Union, Dict
|
||||
from typing import Dict, List, Optional, TYPE_CHECKING, Type, Union
|
||||
from weakref import proxy
|
||||
|
||||
from ormar.fields import BaseField
|
||||
@ -73,13 +73,17 @@ class RelationsManager:
|
||||
if child_relation:
|
||||
child_relation.add(parent)
|
||||
|
||||
def remove(self, name: str, child: Union["NewBaseModel", Type["NewBaseModel"]]) -> None:
|
||||
def remove(
|
||||
self, name: str, child: Union["NewBaseModel", Type["NewBaseModel"]]
|
||||
) -> None:
|
||||
relation = self._get(name)
|
||||
if relation:
|
||||
relation.remove(child)
|
||||
|
||||
@staticmethod
|
||||
def remove_parent(item: Union["NewBaseModel", Type["NewBaseModel"]], name: "Model") -> None:
|
||||
def remove_parent(
|
||||
item: Union["NewBaseModel", Type["NewBaseModel"]], name: "Model"
|
||||
) -> None:
|
||||
related_model = name
|
||||
rel_name = item.resolve_relation_name(item, related_model)
|
||||
if rel_name in item._orm:
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
from typing import TYPE_CHECKING, Tuple, Type, Optional
|
||||
from typing import Optional, TYPE_CHECKING, Tuple, Type
|
||||
from weakref import proxy
|
||||
|
||||
import ormar
|
||||
|
||||
@ -16,6 +16,7 @@ psycopg2-binary
|
||||
mysqlclient
|
||||
|
||||
# Testing
|
||||
mypy
|
||||
pytest
|
||||
pytest-cov
|
||||
codecov
|
||||
|
||||
Reference in New Issue
Block a user