clean code

This commit is contained in:
collerek
2020-08-11 19:54:54 +02:00
parent 45653d36c7
commit 4aadc9fac6
9 changed files with 88 additions and 73 deletions

View File

@ -1,5 +1,4 @@
from orm.models.fakepydantic import FakePydantic
from orm.models.model import Model
__all__ = [
"Model"
]
__all__ = ["FakePydantic", "Model"]

View File

@ -1,19 +1,29 @@
import inspect
import json
import uuid
from typing import TYPE_CHECKING, Dict, TypeVar, Type, Any, Optional, Callable, Set, List
from typing import (
Any,
Callable,
Dict,
List,
Optional,
Set,
TYPE_CHECKING,
Type,
TypeVar,
)
import databases
import pydantic
import sqlalchemy
from pydantic import BaseModel
import orm
import orm # noqa I100
from orm.fields import BaseField
from orm.models.metaclass import ModelMetaclass
from orm.relations import RelationshipManager
if TYPE_CHECKING: #pragma no cover
if TYPE_CHECKING: # pragma no cover
from orm.models.model import Model
@ -74,9 +84,9 @@ class FakePydantic(list, metaclass=ModelMetaclass):
item = getattr(self.values, key, None)
if (
item is not None
and self._is_conversion_to_json_needed(key)
and isinstance(item, str)
item is not None
and self._is_conversion_to_json_needed(key)
and isinstance(item, str)
):
try:
item = json.loads(item)
@ -92,7 +102,7 @@ class FakePydantic(list, metaclass=ModelMetaclass):
if self.__class__ != other.__class__: # pragma no cover
return False
return self._orm_id == other._orm_id or (
self.values is not None and other.values is not None and self.pk == other.pk
self.values is not None and other.values is not None and self.pk == other.pk
)
def __repr__(self) -> str: # pragma no cover
@ -148,7 +158,7 @@ class FakePydantic(list, metaclass=ModelMetaclass):
related_names = set()
for name, field in cls.__fields__.items():
if inspect.isclass(field.type_) and issubclass(
field.type_, pydantic.BaseModel
field.type_, pydantic.BaseModel
):
related_names.add(name)
return related_names
@ -180,7 +190,7 @@ class FakePydantic(list, metaclass=ModelMetaclass):
for field in one.__model_fields__.keys():
# print(field, one.dict(), other.dict())
if isinstance(getattr(one, field), list) and not isinstance(
getattr(one, field), orm.Model
getattr(one, field), orm.Model
):
setattr(other, field, getattr(one, field) + getattr(other, field))
elif isinstance(getattr(one, field), orm.Model):

View File

@ -1,14 +1,17 @@
import copy
from typing import Dict, Tuple, Type, Optional, List, Any
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Type
import sqlalchemy
from pydantic import BaseConfig, create_model
from pydantic.fields import ModelField
from orm import ForeignKey, ModelDefinitionError
from orm import ForeignKey, ModelDefinitionError # noqa I100
from orm.fields import BaseField
from orm.relations import RelationshipManager
if TYPE_CHECKING: # pragma no cover
from orm import Model
relationship_manager = RelationshipManager()
@ -129,4 +132,4 @@ class ModelMetaclass(type):
expand_reverse_relationships(new_model)
return new_model
return new_model

View File

@ -1,15 +1,15 @@
from typing import List, Any
from typing import Any, List
import sqlalchemy
import orm.queryset.queryset
from orm.models.fakepydantic import FakePydantic
import orm.queryset # noqa I100
from orm.models import FakePydantic # noqa I100
class Model(FakePydantic):
__abstract__ = True
objects = orm.queryset.queryset.QuerySet()
objects = orm.queryset.QuerySet()
@classmethod
def from_row(
@ -90,4 +90,4 @@ class Model(FakePydantic):
expr = self.__table__.select().where(self.pk_column == self.pk)
row = await self.__database__.fetch_one(expr)
self.from_dict(dict(row))
return self
return self