switch to equals in most of the code, fix dependencies, clean tests, make all not relation fields work with type hints

This commit is contained in:
collerek
2020-10-31 18:11:48 +01:00
parent 8fba94efa1
commit 7d5e291a19
47 changed files with 558 additions and 438 deletions

View File

@ -1,4 +1,4 @@
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union, Sequence
from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField
@ -10,13 +10,13 @@ REF_PREFIX = "#/components/schemas/"
def ManyToMany(
to: Type["Model"],
through: Type["Model"],
*,
name: str = None,
unique: bool = False,
virtual: bool = False,
**kwargs: Any
to: Type["Model"],
through: Type["Model"],
*,
name: str = None,
unique: bool = False,
virtual: bool = False,
**kwargs: Any
) -> Type["ManyToManyField"]:
to_field = to.Meta.model_fields[to.Meta.pkname]
related_name = kwargs.pop("related_name", None)
@ -49,3 +49,58 @@ def ManyToMany(
class ManyToManyField(ForeignKeyField):
through: Type["Model"]
if TYPE_CHECKING: # 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
pass
@staticmethod
def select_related(related: Union[List, str]) -> "QuerySet":
pass
@staticmethod
async def exists(self) -> bool:
return await self.queryset.exists()
@staticmethod
async def count(self) -> int:
return await self.queryset.count()
@staticmethod
async def clear(self) -> int:
pass
@staticmethod
def limit(limit_count: int) -> "QuerySet":
pass
@staticmethod
def offset(self, offset: int) -> "QuerySet":
pass
@staticmethod
async def first(self, **kwargs: Any) -> "Model":
pass
@staticmethod
async def get(self, **kwargs: Any) -> "Model":
pass
@staticmethod
async def all(self, **kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003
pass
@staticmethod
async def create(self, **kwargs: Any) -> "Model":
pass

View File

@ -12,7 +12,7 @@ from ormar.fields.base import BaseField # noqa I101
def is_field_nullable(
nullable: Optional[bool], default: Any, server_default: Any
nullable: Optional[bool], default: Any, server_default: Any
) -> bool:
if nullable is None:
return default is not None or server_default is not None
@ -58,20 +58,20 @@ class ModelFieldFactory:
pass
class String(ModelFieldFactory):
class String(ModelFieldFactory, str):
_type = str
_pydantic_type = pydantic.ConstrainedStr
def __new__( # type: ignore # noqa CFQ002
cls,
*,
allow_blank: bool = True,
strip_whitespace: bool = False,
min_length: int = None,
max_length: int = None,
curtail_length: int = None,
regex: str = None,
**kwargs: Any
cls,
*,
allow_blank: bool = True,
strip_whitespace: bool = False,
min_length: int = None,
max_length: int = None,
curtail_length: int = None,
regex: str = None,
**kwargs: Any
) -> Type[BaseField]: # type: ignore
kwargs = {
**kwargs,
@ -97,17 +97,17 @@ class String(ModelFieldFactory):
)
class Integer(ModelFieldFactory):
class Integer(ModelFieldFactory, int):
_type = int
_pydantic_type = pydantic.ConstrainedInt
def __new__( # type: ignore
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
) -> Type[BaseField]:
autoincrement = kwargs.pop("autoincrement", None)
autoincrement = (
@ -132,12 +132,12 @@ class Integer(ModelFieldFactory):
return sqlalchemy.Integer()
class Text(ModelFieldFactory):
class Text(ModelFieldFactory, str):
_type = str
_pydantic_type = pydantic.ConstrainedStr
def __new__( # type: ignore
cls, *, allow_blank: bool = True, strip_whitespace: bool = False, **kwargs: Any
cls, *, allow_blank: bool = True, strip_whitespace: bool = False, **kwargs: Any
) -> Type[BaseField]:
kwargs = {
**kwargs,
@ -155,17 +155,17 @@ class Text(ModelFieldFactory):
return sqlalchemy.Text()
class Float(ModelFieldFactory):
class Float(ModelFieldFactory, float):
_type = float
_pydantic_type = pydantic.ConstrainedFloat
def __new__( # type: ignore
cls,
*,
minimum: float = None,
maximum: float = None,
multiple_of: int = None,
**kwargs: Any
cls,
*,
minimum: float = None,
maximum: float = None,
multiple_of: int = None,
**kwargs: Any
) -> Type[BaseField]:
kwargs = {
**kwargs,
@ -184,7 +184,7 @@ class Float(ModelFieldFactory):
return sqlalchemy.Float()
class Boolean(ModelFieldFactory):
class Boolean(ModelFieldFactory, int):
_type = bool
_pydantic_type = bool
@ -193,7 +193,7 @@ class Boolean(ModelFieldFactory):
return sqlalchemy.Boolean()
class DateTime(ModelFieldFactory):
class DateTime(ModelFieldFactory, datetime.datetime):
_type = datetime.datetime
_pydantic_type = datetime.datetime
@ -202,7 +202,7 @@ class DateTime(ModelFieldFactory):
return sqlalchemy.DateTime()
class Date(ModelFieldFactory):
class Date(ModelFieldFactory, datetime.date):
_type = datetime.date
_pydantic_type = datetime.date
@ -211,7 +211,7 @@ class Date(ModelFieldFactory):
return sqlalchemy.Date()
class Time(ModelFieldFactory):
class Time(ModelFieldFactory, datetime.time):
_type = datetime.time
_pydantic_type = datetime.time
@ -220,7 +220,7 @@ class Time(ModelFieldFactory):
return sqlalchemy.Time()
class JSON(ModelFieldFactory):
class JSON(ModelFieldFactory, pydantic.Json):
_type = pydantic.Json
_pydantic_type = pydantic.Json
@ -229,17 +229,17 @@ class JSON(ModelFieldFactory):
return sqlalchemy.JSON()
class BigInteger(Integer):
class BigInteger(Integer, int):
_type = int
_pydantic_type = pydantic.ConstrainedInt
def __new__( # type: ignore
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
cls,
*,
minimum: int = None,
maximum: int = None,
multiple_of: int = None,
**kwargs: Any
) -> Type[BaseField]:
autoincrement = kwargs.pop("autoincrement", None)
autoincrement = (
@ -264,21 +264,21 @@ class BigInteger(Integer):
return sqlalchemy.BigInteger()
class Decimal(ModelFieldFactory):
class Decimal(ModelFieldFactory, decimal.Decimal):
_type = decimal.Decimal
_pydantic_type = pydantic.ConstrainedDecimal
def __new__( # type: ignore # noqa CFQ002
cls,
*,
minimum: float = None,
maximum: float = None,
multiple_of: int = None,
precision: int = None,
scale: int = None,
max_digits: int = None,
decimal_places: int = None,
**kwargs: Any
cls,
*,
minimum: float = None,
maximum: float = None,
multiple_of: int = None,
precision: int = None,
scale: int = None,
max_digits: int = None,
decimal_places: int = None,
**kwargs: Any
) -> Type[BaseField]:
kwargs = {
**kwargs,
@ -319,7 +319,7 @@ class Decimal(ModelFieldFactory):
)
class UUID(ModelFieldFactory):
class UUID(ModelFieldFactory, uuid.UUID):
_type = uuid.UUID
_pydantic_type = uuid.UUID

View File

@ -42,7 +42,7 @@ def register_relation_on_build(table_name: str, field: Type[ForeignKeyField]) ->
def register_many_to_many_relation_on_build(
table_name: str, field: Type[ManyToManyField]
table_name: str, field: Type[ManyToManyField]
) -> None:
alias_manager.add_relation_type(field.through.Meta.tablename, table_name)
alias_manager.add_relation_type(
@ -51,11 +51,11 @@ def register_many_to_many_relation_on_build(
def reverse_field_not_already_registered(
child: Type["Model"], child_model_name: str, parent_model: Type["Model"]
child: Type["Model"], child_model_name: str, parent_model: Type["Model"]
) -> bool:
return (
child_model_name not in parent_model.__fields__
and child.get_name() not in parent_model.__fields__
child_model_name not in parent_model.__fields__
and child.get_name() not in parent_model.__fields__
)
@ -66,7 +66,7 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
parent_model = model_field.to
child = model
if reverse_field_not_already_registered(
child, child_model_name, parent_model
child, child_model_name, parent_model
):
register_reverse_model_fields(
parent_model, child, child_model_name, model_field
@ -74,10 +74,10 @@ def expand_reverse_relationships(model: Type["Model"]) -> None:
def register_reverse_model_fields(
model: Type["Model"],
child: Type["Model"],
child_model_name: str,
model_field: Type["ForeignKeyField"],
model: Type["Model"],
child: Type["Model"],
child_model_name: str,
model_field: Type["ForeignKeyField"],
) -> None:
if issubclass(model_field, ManyToManyField):
model.Meta.model_fields[child_model_name] = ManyToMany(
@ -92,7 +92,7 @@ def register_reverse_model_fields(
def adjust_through_many_to_many_model(
model: Type["Model"], child: Type["Model"], model_field: Type[ManyToManyField]
model: Type["Model"], child: Type["Model"], model_field: Type[ManyToManyField]
) -> None:
model_field.through.Meta.model_fields[model.get_name()] = ForeignKey(
model, name=model.get_name(), ondelete="CASCADE"
@ -109,7 +109,7 @@ def adjust_through_many_to_many_model(
def create_pydantic_field(
field_name: str, model: Type["Model"], model_field: Type[ManyToManyField]
field_name: str, model: Type["Model"], model_field: Type[ManyToManyField]
) -> None:
model_field.through.__fields__[field_name] = ModelField(
name=field_name,
@ -121,7 +121,7 @@ def create_pydantic_field(
def create_and_append_m2m_fk(
model: Type["Model"], model_field: Type[ManyToManyField]
model: Type["Model"], model_field: Type[ManyToManyField]
) -> None:
column = sqlalchemy.Column(
model.get_name(),
@ -137,7 +137,7 @@ def create_and_append_m2m_fk(
def check_pk_column_validity(
field_name: str, field: BaseField, pkname: Optional[str]
field_name: str, field: BaseField, pkname: Optional[str]
) -> Optional[str]:
if pkname is not None:
raise ModelDefinitionError("Only one primary key column is allowed.")
@ -147,7 +147,7 @@ def check_pk_column_validity(
def sqlalchemy_columns_from_model_fields(
model_fields: Dict, table_name: str
model_fields: Dict, table_name: str
) -> Tuple[Optional[str], List[sqlalchemy.Column]]:
columns = []
pkname = None
@ -161,9 +161,9 @@ def sqlalchemy_columns_from_model_fields(
if field.primary_key:
pkname = check_pk_column_validity(field_name, field, pkname)
if (
not field.pydantic_only
and not field.virtual
and not issubclass(field, ManyToManyField)
not field.pydantic_only
and not field.virtual
and not issubclass(field, ManyToManyField)
):
columns.append(field.get_column(field_name))
register_relation_in_alias_manager(table_name, field)
@ -171,7 +171,7 @@ def sqlalchemy_columns_from_model_fields(
def register_relation_in_alias_manager(
table_name: str, field: Type[ForeignKeyField]
table_name: str, field: Type[ForeignKeyField]
) -> None:
if issubclass(field, ManyToManyField):
register_many_to_many_relation_on_build(table_name, field)
@ -180,7 +180,7 @@ def register_relation_in_alias_manager(
def populate_default_pydantic_field_value(
ormar_field: Type[BaseField], field_name: str, attrs: dict
ormar_field: Type[BaseField], field_name: str, attrs: dict
) -> dict:
curr_def_value = attrs.get(field_name, ormar.Undefined)
if lenient_issubclass(curr_def_value, ormar.fields.BaseField):
@ -193,7 +193,7 @@ def populate_default_pydantic_field_value(
def check_if_field_annotation_or_value_is_ormar(
field: Any, field_name: str, attrs: Dict
field: Any, field_name: str, attrs: Dict
) -> bool:
return lenient_issubclass(field, BaseField) or issubclass(
attrs.get(field_name, type), BaseField
@ -201,14 +201,16 @@ def check_if_field_annotation_or_value_is_ormar(
def extract_field_from_annotation_or_value(
field: Any, field_name: str, attrs: Dict
field: Any, field_name: str, attrs: Dict
) -> Type[ormar.fields.BaseField]:
return field if lenient_issubclass(field, BaseField) else attrs.get(field_name)
def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
model_fields = {}
for field_name, field in attrs["__annotations__"].items():
potential_fields = {k: v for k, v in attrs["__annotations__"].items() if lenient_issubclass(v, BaseField)}
potential_fields.update({k: v for k, v in attrs.items() if lenient_issubclass(v, BaseField)})
for field_name, field in potential_fields.items():
# ormar fields can be used as annotation or as default value
if check_if_field_annotation_or_value_is_ormar(field, field_name, attrs):
ormar_field = extract_field_from_annotation_or_value(
@ -225,17 +227,16 @@ def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
def extract_annotations_and_default_vals(
attrs: dict, bases: Tuple
attrs: dict
) -> Tuple[Dict, Dict]:
attrs["__annotations__"] = attrs.get("__annotations__") or bases[0].__dict__.get(
"__annotations__", {}
)
key = '__annotations__'
attrs[key] = attrs.get(key, {})
attrs, model_fields = populate_pydantic_default_values(attrs)
return attrs, model_fields
def populate_meta_tablename_columns_and_pk(
name: str, new_model: Type["Model"]
name: str, new_model: Type["Model"]
) -> Type["Model"]:
tablename = name.lower() + "s"
new_model.Meta.tablename = (
@ -261,7 +262,7 @@ def populate_meta_tablename_columns_and_pk(
def populate_meta_sqlalchemy_table_if_required(
new_model: Type["Model"],
new_model: Type["Model"],
) -> Type["Model"]:
if not hasattr(new_model.Meta, "table"):
new_model.Meta.table = sqlalchemy.Table(
@ -276,7 +277,7 @@ def populate_meta_sqlalchemy_table_if_required(
def get_pydantic_base_orm_config() -> Type[BaseConfig]:
class Config(BaseConfig):
orm_mode = True
arbitrary_types_allowed = True
# arbitrary_types_allowed = True
return Config
@ -303,7 +304,7 @@ def choices_validator(cls: Type["Model"], values: Dict[str, Any]) -> Dict[str, A
def populate_choices_validators( # noqa CCR001
model: Type["Model"], attrs: Dict
model: Type["Model"], attrs: Dict
) -> None:
if model_initialized_and_has_model_fields(model):
for _, field in model.Meta.model_fields.items():
@ -316,11 +317,11 @@ def populate_choices_validators( # noqa CCR001
class ModelMetaclass(pydantic.main.ModelMetaclass):
def __new__( # type: ignore
mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict
mcs: "ModelMetaclass", name: str, bases: Any, attrs: dict
) -> "ModelMetaclass":
attrs["Config"] = get_pydantic_base_orm_config()
attrs["__name__"] = name
attrs, model_fields = extract_annotations_and_default_vals(attrs, bases)
attrs, model_fields = extract_annotations_and_default_vals(attrs)
new_model = super().__new__( # type: ignore
mcs, name, bases, attrs
)

View File

@ -24,6 +24,9 @@ def group_related_list(list_: List) -> Dict:
return test_dict
if TYPE_CHECKING: # pragma nocover
from ormar import QuerySet
T = TypeVar("T", bound="Model")
@ -31,6 +34,7 @@ class Model(NewBaseModel):
__abstract__ = False
if TYPE_CHECKING: # pragma nocover
Meta: ModelMeta
objects: "QuerySet"
def __repr__(self) -> str: # pragma nocover
attrs_to_include = ["tablename", "columns", "pkname"]
@ -41,12 +45,12 @@ class Model(NewBaseModel):
@classmethod
def from_row( # noqa CCR001
cls: Type[T],
row: sqlalchemy.engine.ResultProxy,
select_related: List = None,
related_models: Any = None,
previous_table: str = None,
fields: List = None,
cls: Type[T],
row: sqlalchemy.engine.ResultProxy,
select_related: List = None,
related_models: Any = None,
previous_table: str = None,
fields: List = None,
) -> Optional[T]:
item: Dict[str, Any] = {}
@ -56,9 +60,9 @@ class Model(NewBaseModel):
related_models = group_related_list(select_related)
if (
previous_table
and previous_table in cls.Meta.model_fields
and issubclass(cls.Meta.model_fields[previous_table], ManyToManyField)
previous_table
and previous_table in cls.Meta.model_fields
and issubclass(cls.Meta.model_fields[previous_table], ManyToManyField)
):
previous_table = cls.Meta.model_fields[
previous_table
@ -86,12 +90,12 @@ class Model(NewBaseModel):
@classmethod
def populate_nested_models_from_row(
cls,
item: dict,
row: sqlalchemy.engine.ResultProxy,
related_models: Any,
previous_table: sqlalchemy.Table,
fields: List = None,
cls,
item: dict,
row: sqlalchemy.engine.ResultProxy,
related_models: Any,
previous_table: sqlalchemy.Table,
fields: List = None,
) -> dict:
for related in related_models:
if isinstance(related_models, dict) and related_models[related]:
@ -115,12 +119,12 @@ class Model(NewBaseModel):
@classmethod
def extract_prefixed_table_columns( # noqa CCR001
cls,
item: dict,
row: sqlalchemy.engine.result.ResultProxy,
table_prefix: str,
fields: List = None,
nested: bool = False,
cls,
item: dict,
row: sqlalchemy.engine.result.ResultProxy,
table_prefix: str,
fields: List = None,
nested: bool = False,
) -> dict:
# databases does not keep aliases in Record for postgres, change to raw row

View File

@ -136,7 +136,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
def _extract_related_model_instead_of_field(
self, item: str
) -> Optional[Union[T, Sequence[T]]]:
) -> Optional[Union["T", Sequence["T"]]]:
alias = self.get_column_alias(item)
if alias in self._orm:
return self._orm.get(alias)
@ -173,7 +173,7 @@ class NewBaseModel(pydantic.BaseModel, ModelTableProxy, metaclass=ModelMetaclass
def db_backend_name(cls) -> str:
return cls.Meta.database._backend._dialect.name
def remove(self, name: T) -> None:
def remove(self, name: "T") -> None:
self._orm.remove_parent(self, name)
def dict( # noqa A003

View File

@ -39,7 +39,7 @@ class QuerySet:
def __get__(
self,
instance: Union["QuerySet", "QuerysetProxy"],
instance: Optional[Union["QuerySet", "QuerysetProxy"]],
owner: Union[Type["Model"], Type["QuerysetProxy"]],
) -> "QuerySet":
if issubclass(owner, ormar.Model):

View File

@ -28,28 +28,28 @@ class QuerysetProxy:
def queryset(self, value: "QuerySet") -> None:
self._queryset = value
def _assign_child_to_parent(self, child: Optional[T]) -> 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[T, Sequence[Optional[T]]]) -> 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)
assert isinstance(child, ormar.Model)
self._assign_child_to_parent(child)
async def create_through_instance(self, child: T) -> 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: T) -> 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()

View File

@ -25,15 +25,15 @@ class Relation:
self,
manager: "RelationsManager",
type_: RelationType,
to: Type[T],
through: Type[T] = None,
to: Type["T"],
through: Type["T"] = None,
) -> None:
self.manager = manager
self._owner: "Model" = manager.owner
self._type: RelationType = type_
self.to: Type[T] = to
self.through: Optional[Type[T]] = through
self.related_models: Optional[Union[RelationProxy, T]] = (
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
@ -52,7 +52,7 @@ class Relation:
self.related_models.pop(ind)
return None
def add(self, child: T) -> 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
@ -79,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[T], T]]:
def get(self) -> Optional[Union[List["T"], "T"]]:
return self.related_models
def __repr__(self) -> str: # pragma no cover

View File

@ -48,7 +48,7 @@ class RelationsManager:
def __contains__(self, item: str) -> bool:
return item in self._related_names
def get(self, name: str) -> Optional[Union[T, Sequence[T]]]:
def get(self, name: str) -> Optional[Union["T", Sequence["T"]]]:
relation = self._relations.get(name, None)
if relation is not None:
return relation.get()