Check for 3D Arrays in check_keyboard_type (#3514)

This commit is contained in:
Aditya 2023-01-22 16:41:52 +05:30 committed by GitHub
parent 9782510779
commit 359232e6c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 1 deletions

View file

@ -31,7 +31,7 @@ from collections.abc import Sequence
def check_keyboard_type(keyboard: object) -> bool:
"""Checks if the keyboard provided is of the correct type - A list of lists.
"""Checks if the keyboard provided is of the correct type - A sequence of sequences.
Implicitly tested in the init-tests of `{Inline, Reply}KeyboardMarkup`
"""
# string and bytes may actually be used for ReplyKeyboardMarkup in which case each button
@ -39,7 +39,11 @@ def check_keyboard_type(keyboard: object) -> bool:
# allow it here.
if not isinstance(keyboard, Sequence) or isinstance(keyboard, (str, bytes)):
return False
for row in keyboard:
if not isinstance(row, Sequence) or isinstance(row, (str, bytes)):
return False
for inner in row:
if isinstance(inner, Sequence) and not isinstance(inner, str):
return False
return True

View file

@ -101,6 +101,8 @@ class TestInlineKeyboardMarkup:
InlineKeyboardMarkup(["strings_are_not_allowed_in_the_rows_either"])
with pytest.raises(ValueError):
InlineKeyboardMarkup(InlineKeyboardButton("b1", "1"))
with pytest.raises(ValueError):
InlineKeyboardMarkup([[[InlineKeyboardButton("only_2d_array_is_allowed", "1")]]])
async def test_expected_values_empty_switch(self, inline_keyboard_markup, bot, monkeypatch):
async def make_assertion(

View file

@ -116,6 +116,8 @@ class TestReplyKeyboardMarkup:
ReplyKeyboardMarkup(["strings_are_not_allowed_in_the_rows_either"])
with pytest.raises(ValueError):
ReplyKeyboardMarkup(KeyboardButton("button1"))
with pytest.raises(ValueError):
ReplyKeyboardMarkup([[["button1"]]])
def test_to_dict(self, reply_keyboard_markup):
reply_keyboard_markup_dict = reply_keyboard_markup.to_dict()