From 53a26364215f0ae0227e8ee2851130976658b755 Mon Sep 17 00:00:00 2001 From: collerek Date: Fri, 14 Jan 2022 17:36:18 +0100 Subject: [PATCH] revert docs changes, pin databases, fix mypy --- .pre-commit-config.yaml | 6 ++-- docs_src/fields/docs002.py | 4 +-- docs_src/models/docs004.py | 4 +-- docs_src/models/docs011.py | 3 +- docs_src/queries/docs002.py | 17 +++++----- docs_src/queries/docs003.py | 19 +++++------- docs_src/queries/docs005.py | 17 ++++------ docs_src/queries/docs006.py | 31 +++++-------------- docs_src/queries/docs008.py | 60 +++++++++--------------------------- ormar/fields/model_fields.py | 2 -- ormar/queryset/queryset.py | 13 +++++++- poetry.lock | 2 +- pyproject.toml | 2 +- 13 files changed, 61 insertions(+), 119 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d720888..7c8f35d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -3,18 +3,18 @@ repos: rev: 21.12b0 hooks: - id: black - exclude: docs_src + exclude: ^(docs_src/|examples/) - repo: https://github.com/pycqa/flake8 rev: 3.9.2 hooks: - id: flake8 - exclude: docs_src + exclude: ^(docs_src/|examples/|tests/) args: [ '--max-line-length=88' ] - repo: https://github.com/pre-commit/mirrors-mypy rev: v0.910 hooks: - id: mypy - exclude: docs_src + exclude: ^(docs_src/|examples/) args: [--no-strict-optional, --ignore-missing-imports] additional_dependencies: [ types-ujson>=0.1.1, diff --git a/docs_src/fields/docs002.py b/docs_src/fields/docs002.py index 19df0d6..2432856 100644 --- a/docs_src/fields/docs002.py +++ b/docs_src/fields/docs002.py @@ -26,9 +26,7 @@ class Course(ormar.Model): id: int = ormar.Integer(primary_key=True) name: str = ormar.String(max_length=100) completed: bool = ormar.Boolean(default=False) - department: Optional[Department] = ormar.ForeignKey( - Department, related_name="my_courses" - ) + department: Optional[Department] = ormar.ForeignKey(Department, related_name="my_courses") department = Department(name="Science") diff --git a/docs_src/models/docs004.py b/docs_src/models/docs004.py index 4fe548a..cc8bce8 100644 --- a/docs_src/models/docs004.py +++ b/docs_src/models/docs004.py @@ -8,9 +8,7 @@ metadata = sqlalchemy.MetaData() class Course(ormar.Model): - class Meta( - ormar.ModelMeta - ): # note you don't have to subclass - but it's recommended for ide completion and mypy + class Meta(ormar.ModelMeta): # note you don't have to subclass - but it's recommended for ide completion and mypy database = database metadata = metadata diff --git a/docs_src/models/docs011.py b/docs_src/models/docs011.py index 1f948ba..962de1d 100644 --- a/docs_src/models/docs011.py +++ b/docs_src/models/docs011.py @@ -16,5 +16,4 @@ class Course(ormar.Model): name: ormar.String(max_length=100) completed: ormar.Boolean(default=False) - -c1 = Course() +c1 = Course() \ No newline at end of file diff --git a/docs_src/queries/docs002.py b/docs_src/queries/docs002.py index 7d18aa7..d9cdeff 100644 --- a/docs_src/queries/docs002.py +++ b/docs_src/queries/docs002.py @@ -15,17 +15,14 @@ class Book(ormar.Model): id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) author: str = ormar.String(max_length=100) - genre: str = ormar.String( - max_length=100, - default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], - ) + genre: str = ormar.String(max_length=100, default='Fiction', + choices=['Fiction', 'Adventure', 'Historic', 'Fantasy']) -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="Anna Karenina", author="Tolstoy, Leo", genre="Fiction") +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='Anna Karenina', author="Tolstoy, Leo", genre='Fiction') -await Book.objects.update(each=True, genre="Fiction") -all_books = await Book.objects.filter(genre="Fiction").all() +await Book.objects.update(each=True, genre='Fiction') +all_books = await Book.objects.filter(genre='Fiction').all() assert len(all_books) == 3 diff --git a/docs_src/queries/docs003.py b/docs_src/queries/docs003.py index d8b3b65..58e4f1b 100644 --- a/docs_src/queries/docs003.py +++ b/docs_src/queries/docs003.py @@ -15,23 +15,18 @@ class Book(ormar.Model): id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) author: str = ormar.String(max_length=100) - genre: str = ormar.String( - max_length=100, - default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], - ) + genre: str = ormar.String(max_length=100, default='Fiction', + choices=['Fiction', 'Adventure', 'Historic', 'Fantasy']) -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="Anna Karenina", author="Tolstoy, Leo", genre="Fiction") +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='Anna Karenina', author="Tolstoy, Leo", genre='Fiction') # if not exist the instance will be persisted in db -vol2 = await Book.objects.update_or_create( - title="Volume II", author="Anonymous", genre="Fiction" -) +vol2 = await Book.objects.update_or_create(title="Volume II", author='Anonymous', genre='Fiction') assert await Book.objects.count() == 1 # 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 diff --git a/docs_src/queries/docs005.py b/docs_src/queries/docs005.py index 0111123..dca0757 100644 --- a/docs_src/queries/docs005.py +++ b/docs_src/queries/docs005.py @@ -15,21 +15,16 @@ class Book(ormar.Model): id: int = ormar.Integer(primary_key=True) title: str = ormar.String(max_length=200) author: str = ormar.String(max_length=100) - genre: str = ormar.String( - max_length=100, - default="Fiction", - choices=["Fiction", "Adventure", "Historic", "Fantasy"], - ) + genre: str = ormar.String(max_length=100, default='Fiction', + choices=['Fiction', 'Adventure', 'Historic', 'Fantasy']) -await Book.objects.create(title="Tom Sawyer", author="Twain, Mark", genre="Adventure") -await Book.objects.create( - 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='Tom Sawyer', author="Twain, Mark", genre='Adventure') +await Book.objects.create(title='War and Peace in Space', author="Tolstoy, Leo", genre='Fantasy') +await Book.objects.create(title='Anna Karenina', author="Tolstoy, Leo", genre='Fiction') # delete accepts kwargs that will be used in filter # 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() assert len(all_books) == 2 diff --git a/docs_src/queries/docs006.py b/docs_src/queries/docs006.py index 01a23f7..13143d2 100644 --- a/docs_src/queries/docs006.py +++ b/docs_src/queries/docs006.py @@ -36,27 +36,10 @@ class Car(ormar.Model): # build some sample data toyota = await Company.objects.create(name="Toyota", founded=1937) -await Car.objects.create( - manufacturer=toyota, - name="Corolla", - year=2020, - gearbox_type="Manual", - gears=5, - 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", -) +await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5, + 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') + diff --git a/docs_src/queries/docs008.py b/docs_src/queries/docs008.py index e90912b..2c79b61 100644 --- a/docs_src/queries/docs008.py +++ b/docs_src/queries/docs008.py @@ -36,65 +36,33 @@ class Car(ormar.Model): # build some sample data toyota = await Company.objects.create(name="Toyota", founded=1937) -await Car.objects.create( - manufacturer=toyota, - name="Corolla", - year=2020, - gearbox_type="Manual", - gears=5, - 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", -) +await Car.objects.create(manufacturer=toyota, name="Corolla", year=2020, gearbox_type='Manual', gears=5, + 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} -all_cars = ( - await Car.objects.select_related("manufacturer") - .exclude_fields( - ["year", "gearbox_type", "gears", "aircon_type", "company__founded"] - ) - .all() -) +all_cars = await Car.objects.select_related('manufacturer').exclude_fields( + ['year', 'gearbox_type', 'gears', 'aircon_type', 'company__founded']).all() for car in all_cars: # excluded columns will yield None - assert all( - getattr(car, x) is None - for x in ["year", "gearbox_type", "gears", "aircon_type"] - ) + assert all(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 # 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 assert car.manufacturer.founded is None # 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 -all_cars = ( - await Car.objects.select_related("manufacturer") - .exclude_fields("year") - .exclude_fields(["gear", "gearbox_type"]) - .all() -) +all_cars = await Car.objects.select_related('manufacturer').exclude_fields('year').exclude_fields( + ['gear', 'gearbox_type']).all() # 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 # 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( - [{"company": {"name"}}] -).all() +await Car.objects.select_related('manufacturer').exclude_fields([{'company': {'name'}}]).all() # will raise pydantic ValidationError as company.name is required diff --git a/ormar/fields/model_fields.py b/ormar/fields/model_fields.py index a819b61..7a10c25 100644 --- a/ormar/fields/model_fields.py +++ b/ormar/fields/model_fields.py @@ -381,7 +381,6 @@ if TYPE_CHECKING: # pragma: nocover def Boolean(**kwargs: Any) -> bool: pass - else: class Boolean(ModelFieldFactory, int): @@ -545,7 +544,6 @@ if TYPE_CHECKING: # pragma: nocover # noqa: C901 ) -> Union[str, bytes]: pass - else: class LargeBinary(ModelFieldFactory, bytes): diff --git a/ormar/queryset/queryset.py b/ormar/queryset/queryset.py index 7ecb6fa..0326a5d 100644 --- a/ormar/queryset/queryset.py +++ b/ormar/queryset/queryset.py @@ -18,6 +18,15 @@ import databases import sqlalchemy 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 from ormar import MultipleMatches, NoMatch from ormar.exceptions import ModelPersistenceError, QueryDefinitionError @@ -605,7 +614,9 @@ class QuerySet(Generic[T]): model_cls=self.model_cls, # type: ignore 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 = [ {column_map.get(k): v for k, v in dict(x).items() if k in column_map} for x in rows diff --git a/poetry.lock b/poetry.lock index 4150d5a..5bcdf7e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1557,7 +1557,7 @@ sqlite = [] [metadata] lock-version = "1.1" python-versions = "^3.6.2" -content-hash = "e225b046ea8842875754f54a879c18d9e9ffb724758f5be9db2bbe88e3f59967" +content-hash = "7f70457628e806c602066d934eeac8f5550e53107b31906a5ec3a20bca9dbbda" [metadata.files] aiocontextvars = [ diff --git a/pyproject.toml b/pyproject.toml index 927675d..d5c40c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ classifiers = [ [tool.poetry.dependencies] 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" SQLAlchemy = ">=1.3.18,<=1.4.29" asyncpg = { version = ">=0.24,<0.26", optional = true }