revert docs changes, pin databases, fix mypy

This commit is contained in:
collerek
2022-01-14 17:36:18 +01:00
parent bf3b36194b
commit 53a2636421
13 changed files with 61 additions and 119 deletions

View File

@ -3,18 +3,18 @@ repos:
rev: 21.12b0 rev: 21.12b0
hooks: hooks:
- id: black - id: black
exclude: docs_src exclude: ^(docs_src/|examples/)
- repo: https://github.com/pycqa/flake8 - repo: https://github.com/pycqa/flake8
rev: 3.9.2 rev: 3.9.2
hooks: hooks:
- id: flake8 - id: flake8
exclude: docs_src exclude: ^(docs_src/|examples/|tests/)
args: [ '--max-line-length=88' ] args: [ '--max-line-length=88' ]
- repo: https://github.com/pre-commit/mirrors-mypy - repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910 rev: v0.910
hooks: hooks:
- id: mypy - id: mypy
exclude: docs_src exclude: ^(docs_src/|examples/)
args: [--no-strict-optional, --ignore-missing-imports] args: [--no-strict-optional, --ignore-missing-imports]
additional_dependencies: [ additional_dependencies: [
types-ujson>=0.1.1, types-ujson>=0.1.1,

View File

@ -26,9 +26,7 @@ class Course(ormar.Model):
id: int = ormar.Integer(primary_key=True) id: int = ormar.Integer(primary_key=True)
name: str = ormar.String(max_length=100) name: str = ormar.String(max_length=100)
completed: bool = ormar.Boolean(default=False) completed: bool = ormar.Boolean(default=False)
department: Optional[Department] = ormar.ForeignKey( department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses")
Department, related_name="my_courses"
)
department = Department(name="Science") department = Department(name="Science")

View File

@ -8,9 +8,7 @@ metadata = sqlalchemy.MetaData()
class Course(ormar.Model): class Course(ormar.Model):
class Meta( class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy
ormar.ModelMeta
): # note you don't have to subclass - but it's recommended for ide completion and mypy
database = database database = database
metadata = metadata metadata = metadata

View File

@ -16,5 +16,4 @@ class Course(ormar.Model):
name: ormar.String(max_length=100) name: ormar.String(max_length=100)
completed: ormar.Boolean(default=False) completed: ormar.Boolean(default=False)
c1 = Course()
c1 = Course()

View File

@ -15,17 +15,14 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True) id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200) title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100) author: str = ormar.String(max_length=100)
genre: str = ormar.String( genre: str = ormar.String(max_length=100, default='Fiction',
max_length=100, choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure") await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction") await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction") await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.update(each=True, genre="Fiction") await Book.objects.update(each=True, genre='Fiction')
all_books = await Book.objects.filter(genre="Fiction").all() all_books = await Book.objects.filter(genre='Fiction').all()
assert len(all_books) == 3 assert len(all_books) == 3

View File

@ -15,23 +15,18 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True) id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200) title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100) author: str = ormar.String(max_length=100)
genre: str = ormar.String( genre: str = ormar.String(max_length=100, default='Fiction',
max_length=100, choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure") await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create(title="War and Peace", author="Tolstoy, Leo", genre="Fiction") await Book.objects.create(title='War and Peace', author="Tolstoy, Leo", genre='Fiction')
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction") await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
# if not exist the instance will be persisted in db # if not exist the instance will be persisted in db
vol2 = await Book.objects.update_or_create( vol2 = await Book.objects.update_or_create(title="Volume II", author='Anonymous', genre='Fiction')
title="Volume II", author="Anonymous", genre="Fiction"
)
assert await Book.objects.count() == 1 assert await Book.objects.count() == 1
# if pk or pkname passed in kwargs (like id here) the object will be updated # if pk or pkname passed in kwargs (like id here) the object will be updated
assert await Book.objects.update_or_create(id=vol2.id, genre="Historic") assert await Book.objects.update_or_create(id=vol2.id, genre='Historic')
assert await Book.objects.count() == 1 assert await Book.objects.count() == 1

View File

@ -15,21 +15,16 @@ class Book(ormar.Model):
id: int = ormar.Integer(primary_key=True) id: int = ormar.Integer(primary_key=True)
title: str = ormar.String(max_length=200) title: str = ormar.String(max_length=200)
author: str = ormar.String(max_length=100) author: str = ormar.String(max_length=100)
genre: str = ormar.String( genre: str = ormar.String(max_length=100, default='Fiction',
max_length=100, choices=['Fiction', 'Adventure', 'Historic', 'Fantasy'])
default="Fiction",
choices=["Fiction", "Adventure", "Historic", "Fantasy"],
)
await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure") await Book.objects.create(title='Tom Sawyer', author="Twain, Mark", genre='Adventure')
await Book.objects.create( await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy')
title="War and Peace in Space", author="Tolstoy, Leo", genre="Fantasy" await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction')
)
await Book.objects.create(title="Anna Karenina", author="Tolstoy, Leo", genre="Fiction")
# delete accepts kwargs that will be used in filter # delete accepts kwargs that will be used in filter
# acting in same way as queryset.filter(**kwargs).delete() # acting in same way as queryset.filter(**kwargs).delete()
await Book.objects.delete(genre="Fantasy") # delete all fantasy books await Book.objects.delete(genre='Fantasy') # delete all fantasy books
all_books = await Book.objects.all() all_books = await Book.objects.all()
assert len(all_books) == 2 assert len(all_books) == 2

View File

@ -36,27 +36,10 @@ class Car(ormar.Model):
# build some sample data # build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937) toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create( await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
manufacturer=toyota, aircon_type='Manual')
name="Corolla", await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
year=2020, aircon_type='Manual')
gearbox_type="Manual", await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
gears=5, aircon_type='Auto')
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Yaris",
year=2019,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Supreme",
year=2020,
gearbox_type="Auto",
gears=6,
aircon_type="Auto",
)

View File

@ -36,65 +36,33 @@ class Car(ormar.Model):
# build some sample data # build some sample data
toyota = await Company.objects.create(name="Toyota", founded=1937) toyota = await Company.objects.create(name="Toyota", founded=1937)
await Car.objects.create( await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5,
manufacturer=toyota, aircon_type='Manual')
name="Corolla", await Car.objects.create(manufacturer=toyota, name="Yaris", year=2019, gearbox_type='Manual', gears=5,
year=2020, aircon_type='Manual')
gearbox_type="Manual", await Car.objects.create(manufacturer=toyota, name="Supreme", year=2020, gearbox_type='Auto', gears=6,
gears=5, aircon_type='Auto')
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Yaris",
year=2019,
gearbox_type="Manual",
gears=5,
aircon_type="Manual",
)
await Car.objects.create(
manufacturer=toyota,
name="Supreme",
year=2020,
gearbox_type="Auto",
gears=6,
aircon_type="Auto",
)
# select manufacturer but only name - to include related models use notation {model_name}__{column} # select manufacturer but only name - to include related models use notation {model_name}__{column}
all_cars = ( all_cars = await Car.objects.select_related('manufacturer').exclude_fields(
await Car.objects.select_related("manufacturer") ['year', 'gearbox_type', 'gears', 'aircon_type', 'company__founded']).all()
.exclude_fields(
["year", "gearbox_type", "gears", "aircon_type", "company__founded"]
)
.all()
)
for car in all_cars: for car in all_cars:
# excluded columns will yield None # excluded columns will yield None
assert all( assert all(getattr(car, x) is None for x in ['year', 'gearbox_type', 'gears', 'aircon_type'])
getattr(car, x) is None
for x in ["year", "gearbox_type", "gears", "aircon_type"]
)
# included column on related models will be available, pk column is always included # included column on related models will be available, pk column is always included
# even if you do not include it in fields list # even if you do not include it in fields list
assert car.manufacturer.name == "Toyota" assert car.manufacturer.name == 'Toyota'
# also in the nested related models - you cannot exclude pk - it's always auto added # also in the nested related models - you cannot exclude pk - it's always auto added
assert car.manufacturer.founded is None assert car.manufacturer.founded is None
# fields() can be called several times, building up the columns to select # fields() can be called several times, building up the columns to select
# models selected in select_related but with no columns in fields list implies all fields # models selected in select_related but with no columns in fields list implies all fields
all_cars = ( all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year').exclude_fields(
await Car.objects.select_related("manufacturer") ['gear', 'gearbox_type']).all()
.exclude_fields("year")
.exclude_fields(["gear", "gearbox_type"])
.all()
)
# all fiels from company model are selected # all fiels from company model are selected
assert all_cars[0].manufacturer.name == "Toyota" assert all_cars[0].manufacturer.name == 'Toyota'
assert all_cars[0].manufacturer.founded == 1937 assert all_cars[0].manufacturer.founded == 1937
# cannot exclude mandatory model columns - company__name in this example - note usage of dict/set this time # cannot exclude mandatory model columns - company__name in this example - note usage of dict/set this time
await Car.objects.select_related("manufacturer").exclude_fields( await Car.objects.select_related('manufacturer').exclude_fields([{'company': {'name'}}]).all()
[{"company": {"name"}}]
).all()
# will raise pydantic ValidationError as company.name is required # will raise pydantic ValidationError as company.name is required

View File

@ -381,7 +381,6 @@ if TYPE_CHECKING: # pragma: nocover
def Boolean(**kwargs: Any) -> bool: def Boolean(**kwargs: Any) -> bool:
pass pass
else: else:
class Boolean(ModelFieldFactory, int): class Boolean(ModelFieldFactory, int):
@ -545,7 +544,6 @@ if TYPE_CHECKING: # pragma: nocover # noqa: C901
) -> Union[str, bytes]: ) -> Union[str, bytes]:
pass pass
else: else:
class LargeBinary(ModelFieldFactory, bytes): class LargeBinary(ModelFieldFactory, bytes):

View File

@ -18,6 +18,15 @@ import databases
import sqlalchemy import sqlalchemy
from sqlalchemy import bindparam from sqlalchemy import bindparam
try:
from sqlalchemy.engine import LegacyRow
except ImportError:
if TYPE_CHECKING:
class LegacyRow(dict): # type: ignore
pass
import ormar # noqa I100 import ormar # noqa I100
from ormar import MultipleMatches, NoMatch from ormar import MultipleMatches, NoMatch
from ormar.exceptions import ModelPersistenceError, QueryDefinitionError from ormar.exceptions import ModelPersistenceError, QueryDefinitionError
@ -605,7 +614,9 @@ class QuerySet(Generic[T]):
model_cls=self.model_cls, # type: ignore model_cls=self.model_cls, # type: ignore
exclude_through=exclude_through, exclude_through=exclude_through,
) )
column_map = alias_resolver.resolve_columns(columns_names=list(rows[0].keys())) column_map = alias_resolver.resolve_columns(
columns_names=list(cast(LegacyRow, rows[0]).keys())
)
result = [ result = [
{column_map.get(k): v for k, v in dict(x).items() if k in column_map} {column_map.get(k): v for k, v in dict(x).items() if k in column_map}
for x in rows for x in rows

2
poetry.lock generated
View File

@ -1557,7 +1557,7 @@ sqlite = []
[metadata] [metadata]
lock-version = "1.1" lock-version = "1.1"
python-versions = "^3.6.2" python-versions = "^3.6.2"
content-hash = "e225b046ea8842875754f54a879c18d9e9ffb724758f5be9db2bbe88e3f59967" content-hash = "7f70457628e806c602066d934eeac8f5550e53107b31906a5ec3a20bca9dbbda"
[metadata.files] [metadata.files]
aiocontextvars = [ aiocontextvars = [

View File

@ -42,7 +42,7 @@ classifiers = [
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.6.2" python = "^3.6.2"
databases = ">=0.5.4,<0.5.5" databases = ">=0.3.2,!=0.5.0,!=0.5.1,!=0.5.2,!=0.5.3,<0.5.5"
pydantic = ">=1.6.1,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<=1.9.1" pydantic = ">=1.6.1,!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<=1.9.1"
SQLAlchemy = ">=1.3.18,<=1.4.29" SQLAlchemy = ">=1.3.18,<=1.4.29"
asyncpg = { version = ">=0.24,<0.26", optional = true } asyncpg = { version = ">=0.24,<0.26", optional = true }