fixed type hints of callback_data

its impotant to remeber all data saved in callback_data is text even if you pass to it integer
insofar as newbies often copy examples and modyfy this typing may help them make no mistake
This commit is contained in:
Yuriy Chebyshev 2020-08-10 13:07:46 +03:00
parent d1452b1620
commit 0103e743ea
2 changed files with 6 additions and 4 deletions

View file

@ -1,6 +1,7 @@
import logging
import random
import uuid
import typing
from aiogram import Bot, Dispatcher, executor, md, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
@ -52,7 +53,7 @@ def format_post(post_id: str, post: dict) -> (str, types.InlineKeyboardMarkup):
md.quote_html(post['body']),
'', # just new empty line
f"Votes: {post['votes']}",
sep = '\n',
sep='\n',
)
markup = types.InlineKeyboardMarkup()
@ -75,7 +76,7 @@ async def query_show_list(query: types.CallbackQuery):
@dp.callback_query_handler(posts_cb.filter(action='view'))
async def query_view(query: types.CallbackQuery, callback_data: dict):
async def query_view(query: types.CallbackQuery, callback_data: typing.Dict[str, str]):
post_id = callback_data['id']
post = POSTS.get(post_id, None)
@ -87,7 +88,7 @@ async def query_view(query: types.CallbackQuery, callback_data: dict):
@dp.callback_query_handler(posts_cb.filter(action=['like', 'dislike']))
async def query_post_vote(query: types.CallbackQuery, callback_data: dict):
async def query_post_vote(query: types.CallbackQuery, callback_data: typing.Dict[str, str]):
try:
await dp.throttle('vote', rate=1)
except Throttled:

View file

@ -4,6 +4,7 @@ For more comprehensive example see callback_data_factory.py
"""
import logging
import typing
from aiogram import Bot, Dispatcher, executor, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware
@ -38,7 +39,7 @@ async def cmd_start(message: types.Message):
@dp.callback_query_handler(vote_cb.filter(action=['up', 'down']))
async def callback_vote_action(query: types.CallbackQuery, callback_data: dict):
async def callback_vote_action(query: types.CallbackQuery, callback_data: typing.Dict[str, str]):
logging.info('Got this callback data: %r', callback_data) # callback_data contains all info from callback data
await query.answer() # don't forget to answer callback query as soon as possible
callback_data_action = callback_data['action']