From f0c06aa9b8b116869c3008c1f7102f1f01170d5e Mon Sep 17 00:00:00 2001 From: floordiv Date: Fri, 9 Apr 2021 03:29:43 +0300 Subject: [PATCH 1/6] aiogram.types.reply_keyboard: implemented __iadd__ method (allows adding buttons using += operator instead of calling .add() method), __add__ (get new keyboard from 2+ other keyboards by adding them) --- aiogram/types/reply_keyboard.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py index ffe07ae1..271906bd 100644 --- a/aiogram/types/reply_keyboard.py +++ b/aiogram/types/reply_keyboard.py @@ -90,6 +90,16 @@ class ReplyKeyboardMarkup(base.TelegramObject): self.add(button) return self + def __iadd__(self, other): + return self.add(other) + + def __add__(self, other): + return ReplyKeyboardMarkup( + keyboard=self.keyboard + other.keyboard, resize_keyboard=self.resize_keyboard, + one_time_keyboard=self.one_time_keyboard, selective=self.selective, + row_width=self.row_width + ) + class KeyboardButton(base.TelegramObject): """ From efbef64cd335dc381076f9d7f4d0a8a5110870b7 Mon Sep 17 00:00:00 2001 From: floordiv Date: Fri, 9 Apr 2021 12:26:57 +0300 Subject: [PATCH 2/6] aiogram.types.inline_keyboard: added all the same behavior for InlineKeyboardMarkup as ReplyKeyboardMarkup's operators overloading --- aiogram/types/inline_keyboard.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index 97ad35da..8ba946e7 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -23,6 +23,7 @@ class InlineKeyboardMarkup(base.TelegramObject): conf = kwargs.pop('conf', {}) or {} conf['row_width'] = row_width + self.other_kwargs = kwargs super(InlineKeyboardMarkup, self).__init__(**kwargs, conf=conf, @@ -82,6 +83,17 @@ class InlineKeyboardMarkup(base.TelegramObject): self.add(button) return self + def __iadd__(self, other): + return self.add(other) + + def __add__(self, other): + return InlineKeyboardMarkup( + inline_keyboard=self.inline_keyboard, + row_width=self.row_width, + conf=self.conf, + **self.other_kwargs + ) + class InlineKeyboardButton(base.TelegramObject): """ From 46de5ecb0f9e43dc78b991874a5f18b8e42d14bc Mon Sep 17 00:00:00 2001 From: floordiv Date: Mon, 12 Apr 2021 15:20:01 +0300 Subject: [PATCH 3/6] aiogram.types.inline_keyboard: fixed adding other.keyboard for a new keyboard --- aiogram/types/inline_keyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index 8ba946e7..d3d28a74 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -88,7 +88,7 @@ class InlineKeyboardMarkup(base.TelegramObject): def __add__(self, other): return InlineKeyboardMarkup( - inline_keyboard=self.inline_keyboard, + inline_keyboard=self.inline_keyboard + other.keyboard, row_width=self.row_width, conf=self.conf, **self.other_kwargs From 6a342dd0a8dd0d1cdb588711dd12a80bf920dda4 Mon Sep 17 00:00:00 2001 From: floordiv Date: Mon, 12 Apr 2021 15:22:19 +0300 Subject: [PATCH 4/6] aiogram.types.inline_keyboard: using `self.__class__` instead of explicit InlineKeyboardMarkup class initializing --- aiogram/types/inline_keyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index d3d28a74..a9545f61 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -87,7 +87,7 @@ class InlineKeyboardMarkup(base.TelegramObject): return self.add(other) def __add__(self, other): - return InlineKeyboardMarkup( + return self.__class__( inline_keyboard=self.inline_keyboard + other.keyboard, row_width=self.row_width, conf=self.conf, From 8589aa1572fa5025e17fd9a72e0581f280523268 Mon Sep 17 00:00:00 2001 From: floordiv Date: Mon, 12 Apr 2021 15:22:57 +0300 Subject: [PATCH 5/6] aiogram.types.reply_keyboard: using `self.__class__` instead of explicit ReplyKeyboardMarkup class initializing --- aiogram/types/reply_keyboard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py index 271906bd..30a6017c 100644 --- a/aiogram/types/reply_keyboard.py +++ b/aiogram/types/reply_keyboard.py @@ -94,7 +94,7 @@ class ReplyKeyboardMarkup(base.TelegramObject): return self.add(other) def __add__(self, other): - return ReplyKeyboardMarkup( + return self.__class__( keyboard=self.keyboard + other.keyboard, resize_keyboard=self.resize_keyboard, one_time_keyboard=self.one_time_keyboard, selective=self.selective, row_width=self.row_width From 447942989aeb159bd1b52db3258b428c57c76376 Mon Sep 17 00:00:00 2001 From: floordiv Date: Tue, 13 Apr 2021 00:34:17 +0300 Subject: [PATCH 6/6] aiogram.types.inline_keyboard: fixed getting attribute `keyboard` from `other` but InlineKeyboardMarkup has `inline_keyboard` attribute instead --- aiogram/types/inline_keyboard.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index a9545f61..9ed21085 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -86,9 +86,9 @@ class InlineKeyboardMarkup(base.TelegramObject): def __iadd__(self, other): return self.add(other) - def __add__(self, other): + def __add__(self, other: self.__class__): return self.__class__( - inline_keyboard=self.inline_keyboard + other.keyboard, + inline_keyboard=self.inline_keyboard + other.inline_keyboard, row_width=self.row_width, conf=self.conf, **self.other_kwargs