From afcff83ebcb532456505cf2e4c895bfb179eab9b Mon Sep 17 00:00:00 2001 From: Harshil <37377066+harshil21@users.noreply.github.com> Date: Wed, 15 Sep 2021 19:04:47 +0400 Subject: [PATCH] Add User Friendly Type Check For Init Of {Inline, Reply}KeyboardMarkup (#2657) --- telegram/inline/inlinekeyboardmarkup.py | 5 +++++ telegram/replykeyboardmarkup.py | 6 ++++++ telegram/replymarkup.py | 11 ++++++++++- tests/test_inlinekeyboardmarkup.py | 8 ++++++++ tests/test_replykeyboardmarkup.py | 6 ++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/telegram/inline/inlinekeyboardmarkup.py b/telegram/inline/inlinekeyboardmarkup.py index eff0e0f11..64d5a9070 100644 --- a/telegram/inline/inlinekeyboardmarkup.py +++ b/telegram/inline/inlinekeyboardmarkup.py @@ -48,6 +48,11 @@ class InlineKeyboardMarkup(ReplyMarkup): __slots__ = ('inline_keyboard',) def __init__(self, inline_keyboard: List[List[InlineKeyboardButton]], **_kwargs: Any): + if not self._check_keyboard_type(inline_keyboard): + raise ValueError( + "The parameter `inline_keyboard` should be a list of " + "list of InlineKeyboardButtons" + ) # Required self.inline_keyboard = inline_keyboard diff --git a/telegram/replykeyboardmarkup.py b/telegram/replykeyboardmarkup.py index 9895d9874..2a1783755 100644 --- a/telegram/replykeyboardmarkup.py +++ b/telegram/replykeyboardmarkup.py @@ -92,6 +92,12 @@ class ReplyKeyboardMarkup(ReplyMarkup): input_field_placeholder: str = None, **_kwargs: Any, ): + if not self._check_keyboard_type(keyboard): + raise ValueError( + "The parameter `keyboard` should be a list of list of " + "strings or KeyboardButtons" + ) + # Required self.keyboard = [] for row in keyboard: diff --git a/telegram/replymarkup.py b/telegram/replymarkup.py index 081a73a9a..c8bc4c7c3 100644 --- a/telegram/replymarkup.py +++ b/telegram/replymarkup.py @@ -17,7 +17,6 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """Base class for Telegram ReplyMarkup Objects.""" - from telegram import TelegramObject @@ -31,3 +30,13 @@ class ReplyMarkup(TelegramObject): """ __slots__ = () + + @staticmethod + def _check_keyboard_type(keyboard: object) -> bool: + """Checks if the keyboard provided is of the correct type - A list of lists.""" + if not isinstance(keyboard, list): + return False + for row in keyboard: + if not isinstance(row, list): + return False + return True diff --git a/tests/test_inlinekeyboardmarkup.py b/tests/test_inlinekeyboardmarkup.py index 9c388c123..944a1a170 100644 --- a/tests/test_inlinekeyboardmarkup.py +++ b/tests/test_inlinekeyboardmarkup.py @@ -81,6 +81,14 @@ class TestInlineKeyboardMarkup: def test_expected_values(self, inline_keyboard_markup): assert inline_keyboard_markup.inline_keyboard == self.inline_keyboard + def test_wrong_keyboard_inputs(self): + with pytest.raises(ValueError): + InlineKeyboardMarkup( + [[InlineKeyboardButton('b1', '1')], InlineKeyboardButton('b2', '2')] + ) + with pytest.raises(ValueError): + InlineKeyboardMarkup(InlineKeyboardButton('b1', '1')) + def test_expected_values_empty_switch(self, inline_keyboard_markup, bot, monkeypatch): def test( url, diff --git a/tests/test_replykeyboardmarkup.py b/tests/test_replykeyboardmarkup.py index 6486af0a3..d7627860c 100644 --- a/tests/test_replykeyboardmarkup.py +++ b/tests/test_replykeyboardmarkup.py @@ -102,6 +102,12 @@ class TestReplyKeyboardMarkup: assert reply_keyboard_markup.selective == self.selective assert reply_keyboard_markup.input_field_placeholder == self.input_field_placeholder + def test_wrong_keyboard_inputs(self): + with pytest.raises(ValueError): + ReplyKeyboardMarkup([[KeyboardButton('b1')], 'b2']) + with pytest.raises(ValueError): + ReplyKeyboardMarkup(KeyboardButton('b1')) + def test_to_dict(self, reply_keyboard_markup): reply_keyboard_markup_dict = reply_keyboard_markup.to_dict()