From 91ffcb85df471f587219a43f5a9428744c82b758 Mon Sep 17 00:00:00 2001 From: Untone Date: Tue, 12 Mar 2024 10:52:32 +0300 Subject: [PATCH] typechecker-column-fix --- resolvers/author.py | 7 +++---- resolvers/follower.py | 22 +++++++++++----------- services/cache.py | 12 ++++++------ 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 4523d723..7c40c41e 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -2,7 +2,7 @@ import asyncio import json import time -from sqlalchemy import select, or_, and_, text, desc +from sqlalchemy import select, or_, and_, text, desc, literal from sqlalchemy.orm import aliased from sqlalchemy_searchable import search @@ -52,7 +52,7 @@ async def get_author(_, _info, slug='', author_id=None): if author_id: cache = await redis.execute('GET', f'id:{author_id}:author') logger.debug(f'result from cache: {cache}') - q = select(Author).where(Author.id == author_id) + q = select(Author).where(Author.id == literal(author_id)) author_dict = None if cache: author_dict = json.loads(cache) @@ -257,7 +257,6 @@ async def get_author_followers(_, _info, slug: str): ) if author_id: cached = await redis.execute('GET', f'author:{author_id}:followers') - results = [] if not cached: author_follower_alias = aliased(AuthorFollower, name='af') q = select(Author).join( @@ -287,6 +286,6 @@ async def get_author_followers(_, _info, slug: str): @query.field('search_authors') -def search_authors(_, info, what: str): +def search_authors(_, _info, what: str): q = search(select(Author), what) return get_with_stat(q) diff --git a/resolvers/follower.py b/resolvers/follower.py index c5c729e9..76a63bd7 100644 --- a/resolvers/follower.py +++ b/resolvers/follower.py @@ -2,7 +2,7 @@ import json import time from typing import List -from sqlalchemy import select, or_ +from sqlalchemy import select, or_, column from sqlalchemy.sql import and_ from orm.author import Author, AuthorFollower @@ -34,7 +34,7 @@ async def follow(_, info, what, slug): user_id = info.context.get('user_id') if not user_id: return {"error": "unauthorized"} - [follower] = get_with_stat(select(Author).select_from(Author).filter(Author.user == user_id)) + [follower] = get_with_stat(select(Author).select_from(Author).filter(column(Author.user) == user_id)) if not follower: return {"error": "cant find follower"} @@ -42,7 +42,7 @@ async def follow(_, info, what, slug): error = author_follow(follower.id, slug) if not error: logger.debug(f'@{follower.slug} followed @{slug}') - [author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug)) + [author] = get_with_stat(select(Author).select_from(Author).where(column(Author.slug) == slug)) if not author: return {"error": "author is not found"} follows = await update_follows_for_author(follower, 'author', author.dict(), True) @@ -52,7 +52,7 @@ async def follow(_, info, what, slug): elif what == 'TOPIC': error = topic_follow(follower.id, slug) if not error: - [topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) + [topic] = get_with_stat(select(Topic).where(column(Topic.slug) == slug)) if not topic: return {"error": "topic is not found"} follows = await update_follows_for_author(follower, 'topic', topic.dict(), True) @@ -63,7 +63,7 @@ async def follow(_, info, what, slug): elif what == 'SHOUT': error = reactions_follow(follower.id, slug) if not error: - [shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) + [shout] = local_session().execute(select(Shout).where(column(Shout.slug) == slug)) if not shout: return {"error": "cant find shout"} follows = await update_follows_for_author(follower, 'shout', shout.dict(), True) @@ -79,7 +79,7 @@ async def unfollow(_, info, what, slug): user_id = info.context.get('user_id') if not user_id: return {"error": "unauthorized"} - follower_query = select(Author).filter(Author.user == user_id) + follower_query = select(Author).filter(column(Author.user) == user_id) [follower] = get_with_stat(follower_query) if not follower: return {"error": "follower profile is not found"} @@ -88,7 +88,7 @@ async def unfollow(_, info, what, slug): error = author_unfollow(follower.id, slug) if not error: logger.info(f'@{follower.slug} unfollowing @{slug}') - [author] = get_with_stat(select(Author).where(Author.slug == slug)) + [author] = get_with_stat(select(Author).where(column(Author.slug) == slug)) if not author: return {"error": "cant find author"} _followers = await update_followers_for_author(follower, author, False) @@ -99,7 +99,7 @@ async def unfollow(_, info, what, slug): error = topic_unfollow(follower.id, slug) if not error: logger.info(f'@{follower.slug} unfollowing §{slug}') - [topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) + [topic] = get_with_stat(select(Topic).where(column(Topic.slug) == slug)) if not topic: return {"error": "cant find topic"} follows = await update_follows_for_author(follower, 'topic', topic.dict(), False) @@ -111,7 +111,7 @@ async def unfollow(_, info, what, slug): error = reactions_unfollow(follower.id, slug) if not error: logger.info(f'@{follower.slug} unfollowing §{slug}') - [shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) + [shout] = local_session().execute(select(Shout).where(column(Shout.slug) == slug)) if not shout: return {"error": "cant find shout"} if not error: @@ -276,8 +276,8 @@ def author_unfollow(follower_id, slug): def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]: q = select(Author) q = ( - q.join(TopicFollower, TopicFollower.follower == Author.id) - .join(Topic, Topic.id == TopicFollower.topic) + q.join(TopicFollower, column(TopicFollower.follower) == Author.id) + .join(Topic, column(Topic.id) == column(TopicFollower.topic)) .filter(or_(Topic.slug == slug, Topic.id == topic_id)) ) return get_with_stat(q) diff --git a/services/cache.py b/services/cache.py index 474ddf5d..da29fdb2 100644 --- a/services/cache.py +++ b/services/cache.py @@ -98,7 +98,7 @@ async def update_followers_for_author( @event.listens_for(Shout, 'after_insert') @event.listens_for(Shout, 'after_update') -def after_shouts_update(mapper, connection, shout: Shout): +def after_shouts_update(_mapper, _connection, shout: Shout): # Main query to get authors associated with the shout through ShoutAuthor authors_query = ( select(Author) @@ -155,7 +155,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction): @event.listens_for(Author, 'after_insert') @event.listens_for(Author, 'after_update') -def after_author_update(mapper, connection, author: Author): +def after_author_update(_mapper, _connection, author: Author): q = select(Author).where(Author.id == author.id) result = get_with_stat(q) if result: @@ -164,28 +164,28 @@ def after_author_update(mapper, connection, author: Author): @event.listens_for(TopicFollower, 'after_insert') -def after_topic_follower_insert(mapper, connection, target: TopicFollower): +def after_topic_follower_insert(_mapper, _connection, target: TopicFollower): asyncio.create_task( handle_topic_follower_change(target.topic, target.follower, True) ) @event.listens_for(TopicFollower, 'after_delete') -def after_topic_follower_delete(mapper, connection, target: TopicFollower): +def after_topic_follower_delete(_mapper, _connection, target: TopicFollower): asyncio.create_task( handle_topic_follower_change(target.topic, target.follower, False) ) @event.listens_for(AuthorFollower, 'after_insert') -def after_author_follower_insert(mapper, connection, target: AuthorFollower): +def after_author_follower_insert(_mapper, _connection, target: AuthorFollower): asyncio.create_task( handle_author_follower_change(target.author, target.follower, True) ) @event.listens_for(AuthorFollower, 'after_delete') -def after_author_follower_delete(mapper, connection, target: AuthorFollower): +def after_author_follower_delete(_mapper, _connection, target: AuthorFollower): asyncio.create_task( handle_author_follower_change(target.author, target.follower, False) )