diff --git a/telegram/inline/inlinekeyboardmarkup.py b/telegram/inline/inlinekeyboardmarkup.py index cff50391bac..634105296b8 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 28eb87047e8..7b59dc0dbc4 100644 --- a/telegram/replykeyboardmarkup.py +++ b/telegram/replykeyboardmarkup.py @@ -92,6 +92,12 @@ def __init__( 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 4f2c01d2710..5c2ddf33f1d 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 8d4e35daaa5..0e19d7931c5 100644 --- a/tests/test_inlinekeyboardmarkup.py +++ b/tests/test_inlinekeyboardmarkup.py @@ -81,6 +81,14 @@ def test_from_column(self): 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 b95cdec8c05..d0a4532a27e 100644 --- a/tests/test_replykeyboardmarkup.py +++ b/tests/test_replykeyboardmarkup.py @@ -102,6 +102,12 @@ def test_expected_values(self, reply_keyboard_markup): 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()