liniting, black, mypy fixes

This commit is contained in:
collerek
2020-10-31 18:22:15 +01:00
parent 7d5e291a19
commit 3c10892db7
16 changed files with 157 additions and 155 deletions

View File

@ -1,4 +1,4 @@
from typing import Any, List, Optional, TYPE_CHECKING, Type, Union, Sequence
from typing import Any, List, Optional, Sequence, TYPE_CHECKING, Type, Union
from ormar.fields import BaseField
from ormar.fields.foreign_key import ForeignKeyField
@ -50,7 +50,8 @@ def ManyToMany(
class ManyToManyField(ForeignKeyField):
through: Type["Model"]
if TYPE_CHECKING: # pragma nocover
if TYPE_CHECKING: # noqa: C901; pragma nocover
@staticmethod
async def add(item: "Model") -> None:
pass
@ -62,7 +63,7 @@ class ManyToManyField(ForeignKeyField):
from ormar import QuerySet
@staticmethod
def filter(**kwargs: Any) -> "QuerySet": # noqa: A003
def filter(**kwargs: Any) -> "QuerySet": # noqa: A003, A001
pass
@staticmethod
@ -70,15 +71,15 @@ class ManyToManyField(ForeignKeyField):
pass
@staticmethod
async def exists(self) -> bool:
return await self.queryset.exists()
async def exists() -> bool:
pass
@staticmethod
async def count(self) -> int:
return await self.queryset.count()
async def count() -> int:
pass
@staticmethod
async def clear(self) -> int:
async def clear() -> int:
pass
@staticmethod
@ -86,21 +87,21 @@ class ManyToManyField(ForeignKeyField):
pass
@staticmethod
def offset(self, offset: int) -> "QuerySet":
def offset(offset: int) -> "QuerySet":
pass
@staticmethod
async def first(self, **kwargs: Any) -> "Model":
async def first(**kwargs: Any) -> "Model":
pass
@staticmethod
async def get(self, **kwargs: Any) -> "Model":
async def get(**kwargs: Any) -> "Model":
pass
@staticmethod
async def all(self, **kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003
async def all(**kwargs: Any) -> Sequence[Optional["Model"]]: # noqa: A003, A001
pass
@staticmethod
async def create(self, **kwargs: Any) -> "Model":
async def create(**kwargs: Any) -> "Model":
pass

View File

@ -208,8 +208,14 @@ def extract_field_from_annotation_or_value(
def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
model_fields = {}
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)})
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):
@ -226,10 +232,8 @@ def populate_pydantic_default_values(attrs: Dict) -> Tuple[Dict, Dict]:
return attrs, model_fields
def extract_annotations_and_default_vals(
attrs: dict
) -> Tuple[Dict, Dict]:
key = '__annotations__'
def extract_annotations_and_default_vals(attrs: dict) -> Tuple[Dict, Dict]:
key = "__annotations__"
attrs[key] = attrs.get(key, {})
attrs, model_fields = populate_pydantic_default_values(attrs)
return attrs, model_fields

View File

@ -38,9 +38,7 @@ class Artist(ormar.Model):
first_name = ormar.String(name="fname", max_length=100)
last_name = ormar.String(name="lname", max_length=100)
born_year = ormar.Integer(name="year")
children = ormar.ManyToMany(
Child, through=ArtistChildren
)
children = ormar.ManyToMany(Child, through=ArtistChildren)
class Album(ormar.Model):

View File

@ -53,9 +53,7 @@ class Item(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
categories = ormar.ManyToMany(
Category, through=ItemsXCategories
)
categories = ormar.ManyToMany(Category, through=ItemsXCategories)
@pytest.fixture(autouse=True, scope="module")

View File

@ -49,10 +49,8 @@ class Post(ormar.Model):
id = ormar.Integer(primary_key=True)
title = ormar.String(max_length=200)
categories = ormar.ManyToMany(
Category, through=PostCategory
)
author= ormar.ForeignKey(Author)
categories = ormar.ManyToMany(Category, through=PostCategory)
author = ormar.ForeignKey(Author)
@pytest.fixture(scope="module")

View File

@ -139,6 +139,7 @@ def test_sqlalchemy_table_is_created(example):
@typing.no_type_check
def test_no_pk_in_model_definition(): # type: ignore
with pytest.raises(ModelDefinitionError): # type: ignore
class ExampleModel2(Model): # type: ignore
class Meta:
tablename = "example2"
@ -150,6 +151,7 @@ def test_no_pk_in_model_definition(): # type: ignore
@typing.no_type_check
def test_two_pks_in_model_definition():
with pytest.raises(ModelDefinitionError):
@typing.no_type_check
class ExampleModel2(Model):
class Meta:
@ -163,6 +165,7 @@ def test_two_pks_in_model_definition():
@typing.no_type_check
def test_setting_pk_column_as_pydantic_only_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
class Meta:
tablename = "example4"
@ -174,6 +177,7 @@ def test_setting_pk_column_as_pydantic_only_in_model_definition():
@typing.no_type_check
def test_decimal_error_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
class Meta:
tablename = "example5"
@ -185,6 +189,7 @@ def test_decimal_error_in_model_definition():
@typing.no_type_check
def test_string_error_in_model_definition():
with pytest.raises(ModelDefinitionError):
class ExampleModel2(Model):
class Meta:
tablename = "example6"

View File

@ -23,7 +23,7 @@ class JsonSample(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
test_json= ormar.JSON(nullable=True)
test_json = ormar.JSON(nullable=True)
class UUIDSample(ormar.Model):
@ -32,7 +32,7 @@ class UUIDSample(ormar.Model):
metadata = metadata
database = database
id= ormar.UUID(primary_key=True, default=uuid.uuid4)
id = ormar.UUID(primary_key=True, default=uuid.uuid4)
test_text = ormar.Text()
@ -55,8 +55,8 @@ class Product(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
rating = ormar.Integer(minimum=1, maximum=5)
in_stock= ormar.Boolean(default=False)
last_delivery= ormar.Date(default=datetime.now)
in_stock = ormar.Boolean(default=False)
last_delivery = ormar.Date(default=datetime.now)
country_name_choices = ("Canada", "Algeria", "United States")
@ -71,10 +71,8 @@ class Country(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
name = ormar.String(
max_length=9, choices=country_name_choices, default="Canada",
)
taxed= ormar.Boolean(choices=country_taxed_choices, default=True)
name = ormar.String(max_length=9, choices=country_name_choices, default="Canada",)
taxed = ormar.Boolean(choices=country_taxed_choices, default=True)
country_code = ormar.Integer(
minimum=0, maximum=1000, choices=country_country_code_choices, default=1
)

View File

@ -40,7 +40,7 @@ class Category(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
department= ormar.ForeignKey(Department, nullable=False)
department = ormar.ForeignKey(Department, nullable=False)
class Student(ormar.Model):
@ -51,8 +51,8 @@ class Student(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass= ormar.ForeignKey(SchoolClass)
category= ormar.ForeignKey(Category, nullable=True)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
class Teacher(ormar.Model):
@ -63,8 +63,8 @@ class Teacher(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass= ormar.ForeignKey(SchoolClass)
category= ormar.ForeignKey(Category, nullable=True)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
@pytest.fixture(scope="module")

View File

@ -29,7 +29,7 @@ class Track(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album)
album = ormar.ForeignKey(Album)
title = ormar.String(max_length=100)
position = ormar.Integer()
@ -41,7 +41,7 @@ class Cover(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
album= ormar.ForeignKey(Album, related_name="cover_pictures")
album = ormar.ForeignKey(Album, related_name="cover_pictures")
title = ormar.String(max_length=100)
@ -62,7 +62,7 @@ class Team(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
org= ormar.ForeignKey(Organisation)
org = ormar.ForeignKey(Organisation)
name = ormar.String(max_length=100)
@ -73,7 +73,7 @@ class Member(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
team= ormar.ForeignKey(Team)
team = ormar.ForeignKey(Team)
email = ormar.String(max_length=100)

View File

@ -36,7 +36,7 @@ class ToDo(ormar.Model):
id = ormar.Integer(primary_key=True)
text = ormar.String(max_length=500)
completed= ormar.Boolean(default=False)
completed = ormar.Boolean(default=False)
class Category(ormar.Model):
@ -57,7 +57,7 @@ class Note(ormar.Model):
id = ormar.Integer(primary_key=True)
text = ormar.String(max_length=500)
category= ormar.ForeignKey(Category)
category = ormar.ForeignKey(Category)
@pytest.fixture(autouse=True, scope="module")

View File

@ -30,7 +30,7 @@ class SchoolClass(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
department= ormar.ForeignKey(Department, nullable=False)
department = ormar.ForeignKey(Department, nullable=False)
class Category(ormar.Model):
@ -51,8 +51,8 @@ class Student(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass= ormar.ForeignKey(SchoolClass)
category= ormar.ForeignKey(Category, nullable=True)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
class Teacher(ormar.Model):
@ -63,8 +63,8 @@ class Teacher(ormar.Model):
id = ormar.Integer(primary_key=True)
name = ormar.String(max_length=100)
schoolclass= ormar.ForeignKey(SchoolClass)
category= ormar.ForeignKey(Category, nullable=True)
schoolclass = ormar.ForeignKey(SchoolClass)
category = ormar.ForeignKey(Category, nullable=True)
@pytest.fixture(scope="module")

View File

@ -30,7 +30,7 @@ class Car(ormar.Model):
database = database
id = ormar.Integer(primary_key=True)
manufacturer= ormar.ForeignKey(Company)
manufacturer = ormar.ForeignKey(Company)
name = ormar.String(max_length=100)
year = ormar.Integer(nullable=True)
gearbox_type = ormar.String(max_length=20, nullable=True)

View File

@ -24,7 +24,7 @@ class Product(ormar.Model):
name = ormar.String(max_length=100)
company = ormar.String(max_length=200, server_default="Acme")
sort_order = ormar.Integer(server_default=text("10"))
created= ormar.DateTime(server_default=func.now())
created = ormar.DateTime(server_default=func.now())
@pytest.fixture(scope="module")