Skip to content

Check that {Inline, Reply}Keyboards are of the correct type #2657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions telegram/inline/inlinekeyboardmarkup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions telegram/replykeyboardmarkup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion telegram/replymarkup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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
8 changes: 8 additions & 0 deletions tests/test_inlinekeyboardmarkup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_replykeyboardmarkup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down