From 496c51402af20370c67881a6c920848eb58d63af Mon Sep 17 00:00:00 2001 From: ducklet Date: Sat, 18 Mar 2023 00:29:29 +0100 Subject: [PATCH] fix using deprecated SQLAlchemy mapping access --- scripts/tests | 1 + unwind/db.py | 16 ++++++++-------- unwind/models.py | 3 ++- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/scripts/tests b/scripts/tests index 4237558..df8b5a0 100755 --- a/scripts/tests +++ b/scripts/tests @@ -10,5 +10,6 @@ trap 'rm "$dbfile"' EXIT TERM INT QUIT [ -z "${DEBUG:-}" ] || set -x +SQLALCHEMY_WARN_20=1 \ UNWIND_STORAGE="$dbfile" \ python -m pytest "$@" diff --git a/unwind/db.py b/unwind/db.py index 4a4fc68..968111e 100644 --- a/unwind/db.py +++ b/unwind/db.py @@ -263,7 +263,7 @@ async def get( query += f" ORDER BY {order_by}" async with locked_connection() as conn: row = await conn.fetch_one(query=query, values=values) - return fromplain(model, row, serialized=True) if row else None + return fromplain(model, row._mapping, serialized=True) if row else None async def get_many(model: Type[ModelType], **kwds) -> Iterable[ModelType]: @@ -283,7 +283,7 @@ async def get_many(model: Type[ModelType], **kwds) -> Iterable[ModelType]: query = f"SELECT {fields_} FROM {model._table} WHERE {cond}" async with locked_connection() as conn: rows = await conn.fetch_all(query=query, values=values) - return (fromplain(model, row, serialized=True) for row in rows) + return (fromplain(model, row._mapping, serialized=True) for row in rows) async def get_all(model: Type[ModelType], **kwds) -> Iterable[ModelType]: @@ -294,7 +294,7 @@ async def get_all(model: Type[ModelType], **kwds) -> Iterable[ModelType]: query = f"SELECT {fields_} FROM {model._table} WHERE {cond}" async with locked_connection() as conn: rows = await conn.fetch_all(query=query, values=values) - return (fromplain(model, row, serialized=True) for row in rows) + return (fromplain(model, row._mapping, serialized=True) for row in rows) async def update(item): @@ -467,7 +467,7 @@ async def find_ratings( """ async with locked_connection() as conn: rows = await conn.fetch_all(bindparams(query, values)) - movie_ids = tuple(r["movie_id"] for r in rows) + movie_ids = tuple(r._mapping["movie_id"] for r in rows) if include_unrated and len(movie_ids) < limit_rows: sqlin, sqlin_vals = sql_in("id", movie_ids, not_=True) @@ -486,7 +486,7 @@ async def find_ratings( {**values, **sqlin_vals, "limit_rows": limit_rows - len(movie_ids)}, ) ) - movie_ids += tuple(r["movie_id"] for r in rows) + movie_ids += tuple(r._mapping["movie_id"] for r in rows) return await ratings_for_movie_ids(ids=movie_ids) @@ -528,7 +528,7 @@ async def ratings_for_movie_ids( async with locked_connection() as conn: rows = await conn.fetch_all(bindparams(query, vals)) - return tuple(dict(r) for r in rows) + return tuple(dict(r._mapping) for r in rows) def sql_fields(tp: Type): @@ -584,7 +584,7 @@ async def ratings_for_movies( async with locked_connection() as conn: rows = await conn.fetch_all(query, values) - return (fromplain(Rating, row, serialized=True) for row in rows) + return (fromplain(Rating, row._mapping, serialized=True) for row in rows) async def find_movies( @@ -651,7 +651,7 @@ async def find_movies( async with locked_connection() as conn: rows = await conn.fetch_all(bindparams(query, values)) - movies = [fromplain(Movie, row, serialized=True) for row in rows] + movies = [fromplain(Movie, row._mapping, serialized=True) for row in rows] if not user_ids: return ((m, []) for m in movies) diff --git a/unwind/models.py b/unwind/models.py index b34d6c3..4480307 100644 --- a/unwind/models.py +++ b/unwind/models.py @@ -10,6 +10,7 @@ from typing import ( ClassVar, Container, Literal, + Mapping, Type, TypeVar, Union, @@ -144,7 +145,7 @@ def asplain( return d -def fromplain(cls: Type[T], d: dict[str, Any], *, serialized: bool = False) -> T: +def fromplain(cls: Type[T], d: Mapping, *, serialized: bool = False) -> T: """Return an instance of the given model using the given data. If `serialized` is `True`, collection types (lists, dicts, etc.) will be