remove kwargs in favor of named keyword arguments, validate argument types

This commit is contained in:
Jannes Höke 2016-02-21 11:33:34 +01:00
parent ca526fba73
commit efd10507d0
8 changed files with 193 additions and 134 deletions

View file

@ -27,6 +27,7 @@ from telegram import (User, Message, Update, UserProfilePhotos, File,
TelegramError, ReplyMarkup, TelegramObject, NullHandler)
from telegram.error import InvalidToken
from telegram.utils import request
from telegram.utils.validate import validate_string
H = NullHandler()
logging.getLogger(__name__).addHandler(H)
@ -595,7 +596,7 @@ class Bot(TelegramObject):
"""Use this method to reply to an inline query.
Args:
inline_query_id (int):
inline_query_id (str):
Unique identifier for answered query
results (list[InlineQueryResult]):
A list of results for the inline query
@ -617,6 +618,9 @@ class Bot(TelegramObject):
A boolean if answering was successful
"""
validate_string(inline_query_id, 'inline_query_id')
validate_string(inline_query_id, 'next_offset')
url = '%s/answerInlineQuery' % self.base_url
results = [res.to_dict() for res in results]
@ -625,9 +629,9 @@ class Bot(TelegramObject):
'results': results}
if cache_time is not None:
data['cache_time'] = cache_time
data['cache_time'] = int(cache_time)
if is_personal is not None:
data['is_personal'] = is_personal
data['is_personal'] = bool(is_personal)
if next_offset is not None:
data['next_offset'] = next_offset

View file

@ -46,8 +46,7 @@ class ChosenInlineResult(TelegramObject):
def __init__(self,
result_id,
from_user,
query,
**kwargs):
query):
# Required
self.result_id = result_id
self.from_user = from_user
@ -64,8 +63,8 @@ class ChosenInlineResult(TelegramObject):
"""
if not data:
return None
data['from_user'] = User.de_json(data.get('from'))
data = data.copy()
data['from_user'] = User.de_json(data.pop('from'))
return ChosenInlineResult(**data)

View file

@ -64,11 +64,10 @@ class InlineQuery(TelegramObject):
"""
if not data:
return None
data = data.copy()
data['from_user'] = User.de_json(data.pop('from'))
data_ = data.copy() # Copy data so we can pop 'from'
data_['from_user'] = User.de_json(data_.pop('from'))
return InlineQuery(**data_)
return InlineQuery(**data)
def to_dict(self):
"""

View file

@ -22,6 +22,7 @@ https://core.telegram.org/bots/api#inline-mode
"""
from telegram import TelegramObject
from telegram.utils.validate import validate_string
class InlineQueryResult(TelegramObject):
@ -95,25 +96,38 @@ class InlineQueryResultArticle(InlineQueryResult):
id,
title,
message_text,
**kwargs):
parse_mode=None,
disable_web_page_preview=None,
url=None,
hide_url=None,
description=None,
thumb_url=None,
thumb_width=None,
thumb_height=None):
validate_string(title, 'title')
validate_string(message_text, 'message_text')
validate_string(url, 'url')
validate_string(description, 'description')
validate_string(thumb_url, 'thumb_url')
validate_string(parse_mode, 'parse_mode')
# Required
super(InlineQueryResultArticle, self).__init__('article', id)
self.title = title
self.message_text = message_text
# Optional
self.parse_mode = kwargs.get('parse_mode', '')
self.disable_web_page_preview = kwargs.get('disable_web_page_preview',
False)
self.url = kwargs.get('url', '')
self.hide_url = kwargs.get('hide_url', False)
self.description = kwargs.get('description', '')
self.thumb_url = kwargs.get('thumb_url', '')
self.parse_mode = kwargs.get('parse_mode', '')
if 'thumb_width' in kwargs:
self.thumb_width = int(kwargs['thumb_width'])
if 'thumb_height' in kwargs:
self.thumb_height = int(kwargs['thumb_height'])
self.parse_mode = parse_mode
self.disable_web_page_preview = bool(disable_web_page_preview)
self.url = url
self.hide_url = bool(hide_url)
self.description = description
self.thumb_url = thumb_url
if thumb_width is not None:
self.thumb_width = int(thumb_width)
if thumb_height is not None:
self.thumb_height = int(thumb_height)
@staticmethod
def de_json(data):
@ -126,6 +140,8 @@ class InlineQueryResultArticle(InlineQueryResult):
"""
if not data:
return None
data = data.copy()
data.pop('type', None)
return InlineQueryResultArticle(**data)
@ -168,25 +184,42 @@ class InlineQueryResultPhoto(InlineQueryResult):
id,
photo_url,
thumb_url,
**kwargs):
mime_type=None,
photo_width=None,
photo_height=None,
title=None,
description=None,
caption=None,
message_text=None,
parse_mode=None,
disable_web_page_preview=None):
validate_string(photo_url, 'photo_url')
validate_string(thumb_url, 'thumb_url')
validate_string(mime_type, 'mime_type')
validate_string(title, 'title')
validate_string(description, 'description')
validate_string(caption, 'caption')
validate_string(message_text, 'message_text')
validate_string(parse_mode, 'parse_mode')
# Required
super(InlineQueryResultPhoto, self).__init__('photo', id)
self.photo_url = photo_url
self.thumb_url = thumb_url
# Optional
self.mime_type = kwargs.get('mime_type', 'image/jpeg')
if 'photo_width' in kwargs:
self.photo_width = int(kwargs['photo_width'])
if 'photo_height' in kwargs:
self.photo_height = int(kwargs['photo_height'])
self.title = kwargs.get('title', '')
self.description = kwargs.get('description', '')
self.caption = kwargs.get('caption', '')
self.message_text = kwargs.get('message_text', '')
self.parse_mode = kwargs.get('parse_mode', '')
self.disable_web_page_preview = kwargs.get('disable_web_page_preview',
False)
self.mime_type = mime_type
if photo_width is not None:
self.photo_width = int(photo_width)
if photo_height is not None:
self.photo_height = int(photo_height)
self.title = title
self.description = description
self.caption = caption
self.message_text = message_text
self.parse_mode = parse_mode
self.disable_web_page_preview = bool(disable_web_page_preview)
@staticmethod
def de_json(data):
@ -199,6 +232,8 @@ class InlineQueryResultPhoto(InlineQueryResult):
"""
if not data:
return None
data = data.copy()
data.pop('type', None)
return InlineQueryResultPhoto(**data)
@ -237,23 +272,36 @@ class InlineQueryResultGif(InlineQueryResult):
id,
gif_url,
thumb_url,
**kwargs):
gif_width=None,
gif_height=None,
title=None,
caption=None,
message_text=None,
parse_mode=None,
disable_web_page_preview=None):
validate_string(gif_url, 'gif_url')
validate_string(thumb_url, 'thumb_url')
validate_string(title, 'title')
validate_string(caption, 'caption')
validate_string(message_text, 'message_text')
validate_string(parse_mode, 'parse_mode')
# Required
super(InlineQueryResultGif, self).__init__('gif', id)
self.gif_url = gif_url
self.thumb_url = thumb_url
# Optional
if 'gif_width' in kwargs:
self.gif_width = int(kwargs['gif_width'])
if 'gif_height' in kwargs:
self.gif_height = int(kwargs['gif_height'])
self.title = kwargs.get('title', '')
self.caption = kwargs.get('caption', '')
self.message_text = kwargs.get('message_text', '')
self.parse_mode = kwargs.get('parse_mode', '')
self.disable_web_page_preview = kwargs.get('disable_web_page_preview',
False)
if gif_width is not None:
self.gif_width = int(gif_width)
if gif_height is not None:
self.gif_height = int(gif_height)
self.title = title
self.caption = caption
self.message_text = message_text
self.parse_mode = parse_mode
self.disable_web_page_preview = bool(disable_web_page_preview)
@staticmethod
def de_json(data):
@ -266,6 +314,8 @@ class InlineQueryResultGif(InlineQueryResult):
"""
if not data:
return None
data = data.copy()
data.pop('type', None)
return InlineQueryResultGif(**data)
@ -304,23 +354,36 @@ class InlineQueryResultMpeg4Gif(InlineQueryResult):
id,
mpeg4_url,
thumb_url,
**kwargs):
mpeg4_width=None,
mpeg4_height=None,
title=None,
caption=None,
message_text=None,
parse_mode=None,
disable_web_page_preview=None):
validate_string(mpeg4_url, 'mpeg4_url')
validate_string(thumb_url, 'thumb_url')
validate_string(title, 'title')
validate_string(caption, 'caption')
validate_string(message_text, 'message_text')
validate_string(parse_mode, 'parse_mode')
# Required
super(InlineQueryResultMpeg4Gif, self).__init__('mpeg4_gif', id)
self.mpeg4_url = mpeg4_url
self.thumb_url = thumb_url
# Optional
if 'mpeg4_width' in kwargs:
self.mpeg4_width = int(kwargs['mpeg4_width'])
if 'mpeg4_height' in kwargs:
self.mpeg4_height = int(kwargs['mpeg4_height'])
self.title = kwargs.get('title', '')
self.caption = kwargs.get('caption', '')
self.message_text = kwargs.get('message_text', '')
self.parse_mode = kwargs.get('parse_mode', '')
self.disable_web_page_preview = kwargs.get('disable_web_page_preview',
False)
if mpeg4_width is not None:
self.mpeg4_width = int(mpeg4_width)
if mpeg4_height is not None:
self.mpeg4_height = int(mpeg4_height)
self.title = title
self.caption = caption
self.message_text = message_text
self.parse_mode = parse_mode
self.disable_web_page_preview = bool(disable_web_page_preview)
@staticmethod
def de_json(data):
@ -333,6 +396,8 @@ class InlineQueryResultMpeg4Gif(InlineQueryResult):
"""
if not data:
return None
data = data.copy()
data.pop('type', None)
return InlineQueryResultMpeg4Gif(**data)
@ -380,7 +445,23 @@ class InlineQueryResultVideo(InlineQueryResult):
thumb_url,
title,
message_text,
**kwargs):
video_width=None,
video_height=None,
video_duration=None,
description=None,
caption=None,
parse_mode=None,
disable_web_page_preview=None):
validate_string(video_url, 'video_url')
validate_string(mime_type, 'mime_type')
validate_string(thumb_url, 'thumb_url')
validate_string(title, 'title')
validate_string(message_text, 'message_text')
validate_string(description, 'description')
validate_string(caption, 'caption')
validate_string(parse_mode, 'parse_mode')
# Required
super(InlineQueryResultVideo, self).__init__('video', id)
self.video_url = video_url
@ -390,17 +471,16 @@ class InlineQueryResultVideo(InlineQueryResult):
self.message_text = message_text
# Optional
if 'video_width' in kwargs:
self.video_width = int(kwargs['video_width'])
if 'video_height' in kwargs:
self.video_height = int(kwargs['video_height'])
if 'video_duration' in kwargs:
self.video_duration = int(kwargs['video_duration'])
self.description = kwargs.get('description', '')
self.caption = kwargs.get('caption', '')
self.parse_mode = kwargs.get('parse_mode', '')
self.disable_web_page_preview = kwargs.get('disable_web_page_preview',
False)
if video_width is not None:
self.video_width = int(video_width)
if video_height is not None:
self.video_height = int(video_height)
if video_duration is not None:
self.video_duration = int(video_duration)
self.description = description
self.caption = caption
self.parse_mode = parse_mode
self.disable_web_page_preview = bool(disable_web_page_preview)
@staticmethod
def de_json(data):
@ -413,5 +493,7 @@ class InlineQueryResultVideo(InlineQueryResult):
"""
if not data:
return None
data = data.copy()
data.pop('type', None)
return InlineQueryResultVideo(**data)

View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2016
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
"""This module contains functions to validate function arguments"""
try:
type(basestring)
except NameError:
basestring = str
def validate_string(arg, name):
"""
Validate a string argument. Raises a ValueError if `arg` is neither an
instance of basestring (Python 2) or str (Python 3) nor None.
Args:
arg (basestring): The value to be tested
name (str): The name of the argument, for the error message
"""
if not isinstance(arg, basestring) and arg is not None:
raise ValueError(name + " is not a string")

View file

@ -51,9 +51,6 @@ class ChosenInlineResultTest(BaseTest, unittest.TestCase):
}
def test_choseninlineresult_de_json(self):
"""Test ChosenInlineResult.de_json() method"""
print('Testing ChosenInlineResult.de_json()')
result = telegram.ChosenInlineResult.de_json(self.json_dict)
self.assertEqual(result.result_id, self.result_id)
@ -62,17 +59,11 @@ class ChosenInlineResultTest(BaseTest, unittest.TestCase):
self.assertEqual(result.query, self.query)
def test_choseninlineresult_to_json(self):
"""Test ChosenInlineResult.to_json() method"""
print('Testing ChosenInlineResult.to_json()')
result = telegram.ChosenInlineResult.de_json(self.json_dict)
self.assertTrue(self.is_json(result.to_json()))
def test_choseninlineresult_to_dict(self):
"""Test ChosenInlineResult.to_dict() method"""
print('Testing ChosenInlineResult.to_dict()')
result = telegram.ChosenInlineResult.de_json(self.json_dict).to_dict()
self.assertTrue(self.is_dict(result))

View file

@ -53,9 +53,6 @@ class InlineQueryTest(BaseTest, unittest.TestCase):
}
def test_inlinequery_de_json(self):
"""Test InlineQuery.de_json() method"""
print('Testing InlineQuery.de_json()')
inlinequery = telegram.InlineQuery.de_json(self.json_dict)
self.assertEqual(inlinequery.id, self.id)
@ -65,17 +62,11 @@ class InlineQueryTest(BaseTest, unittest.TestCase):
self.assertEqual(inlinequery.offset, self.offset)
def test_inlinequery_to_json(self):
"""Test InlineQuery.to_json() method"""
print('Testing InlineQuery.to_json()')
inlinequery = telegram.InlineQuery.de_json(self.json_dict)
self.assertTrue(self.is_json(inlinequery.to_json()))
def test_inlinequery_to_dict(self):
"""Test InlineQuery.to_dict() method"""
print('Testing InlineQuery.to_dict()')
inlinequery = telegram.InlineQuery.de_json(self.json_dict).to_dict()
self.assertTrue(self.is_dict(inlinequery))

View file

@ -66,9 +66,6 @@ class InlineQueryResultArticleTest(BaseTest, unittest.TestCase):
}
def test_article_de_json(self):
"""Test InlineQueryResultArticle.de_json() method"""
print('Testing InlineQueryResultArticle.de_json()')
article = telegram.InlineQueryResultArticle.de_json(self.json_dict)
self.assertEqual(article.type, self.type)
@ -86,17 +83,11 @@ class InlineQueryResultArticleTest(BaseTest, unittest.TestCase):
self.assertEqual(article.thumb_width, self.thumb_width)
def test_article_to_json(self):
"""Test InlineQueryResultArticle.to_json() method"""
print('Testing InlineQueryResultArticle.to_json()')
article = telegram.InlineQueryResultArticle.de_json(self.json_dict)
self.assertTrue(self.is_json(article.to_json()))
def test_article_to_dict(self):
"""Test InlineQueryResultArticle.to_dict() method"""
print('Testing InlineQueryResultArticle.to_dict()')
article = \
telegram.InlineQueryResultArticle.de_json(self.json_dict).to_dict()
@ -137,9 +128,6 @@ class InlineQueryResultPhotoTest(BaseTest, unittest.TestCase):
}
def test_photo_de_json(self):
"""Test InlineQueryResultPhoto.de_json() method"""
print('Testing InlineQueryResultPhoto.de_json()')
photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict)
self.assertEqual(photo.type, self.type)
@ -157,17 +145,11 @@ class InlineQueryResultPhotoTest(BaseTest, unittest.TestCase):
self.disable_web_page_preview)
def test_photo_to_json(self):
"""Test InlineQueryResultPhoto.to_json() method"""
print('Testing InlineQueryResultPhoto.to_json()')
photo = telegram.InlineQueryResultPhoto.de_json(self.json_dict)
self.assertTrue(self.is_json(photo.to_json()))
def test_photo_to_dict(self):
"""Test InlineQueryResultPhoto.to_dict() method"""
print('Testing InlineQueryResultPhoto.to_dict()')
photo = \
telegram.InlineQueryResultPhoto.de_json(self.json_dict).to_dict()
@ -206,9 +188,6 @@ class InlineQueryResultGifTest(BaseTest, unittest.TestCase):
}
def test_gif_de_json(self):
"""Test InlineQueryResultGif.de_json() method"""
print('Testing InlineQueryResultGif.de_json()')
gif = telegram.InlineQueryResultGif.de_json(self.json_dict)
self.assertEqual(gif.type, self.type)
@ -225,17 +204,11 @@ class InlineQueryResultGifTest(BaseTest, unittest.TestCase):
self.disable_web_page_preview)
def test_gif_to_json(self):
"""Test InlineQueryResultGif.to_json() method"""
print('Testing InlineQueryResultGif.to_json()')
gif = telegram.InlineQueryResultGif.de_json(self.json_dict)
self.assertTrue(self.is_json(gif.to_json()))
def test_gif_to_dict(self):
"""Test InlineQueryResultGif.to_dict() method"""
print('Testing InlineQueryResultGif.to_dict()')
gif = telegram.InlineQueryResultGif.de_json(self.json_dict).to_dict()
self.assertTrue(self.is_dict(gif))
@ -273,9 +246,6 @@ class InlineQueryResultMpeg4GifTest(BaseTest, unittest.TestCase):
}
def test_mpeg4_de_json(self):
"""Test InlineQueryResultMpeg4Gif.de_json() method"""
print('Testing InlineQueryResultMpeg4Gif.de_json()')
mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict)
self.assertEqual(mpeg4.type, self.type)
@ -292,17 +262,11 @@ class InlineQueryResultMpeg4GifTest(BaseTest, unittest.TestCase):
self.disable_web_page_preview)
def test_mpeg4_to_json(self):
"""Test InlineQueryResultMpeg4Gif.to_json() method"""
print('Testing InlineQueryResultMpeg4Gif.to_json()')
mpeg4 = telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict)
self.assertTrue(self.is_json(mpeg4.to_json()))
def test_mpeg4_to_dict(self):
"""Test InlineQueryResultMpeg4Gif.to_dict() method"""
print('Testing InlineQueryResultMpeg4Gif.to_dict()')
mpeg4 = \
telegram.InlineQueryResultMpeg4Gif.de_json(self.json_dict).to_dict()
@ -347,9 +311,6 @@ class InlineQueryResultVideoTest(BaseTest, unittest.TestCase):
}
def test_video_de_json(self):
"""Test InlineQueryResultVideo.de_json() method"""
print('Testing InlineQueryResultVideo.de_json()')
video = telegram.InlineQueryResultVideo.de_json(self.json_dict)
self.assertEqual(video.type, self.type)
@ -369,17 +330,11 @@ class InlineQueryResultVideoTest(BaseTest, unittest.TestCase):
self.disable_web_page_preview)
def test_video_to_json(self):
"""Test InlineQueryResultVideo.to_json() method"""
print('Testing InlineQueryResultVideo.to_json()')
video = telegram.InlineQueryResultVideo.de_json(self.json_dict)
self.assertTrue(self.is_json(video.to_json()))
def test_video_to_dict(self):
"""Test InlineQueryResultVideo.to_dict() method"""
print('Testing InlineQueryResultVideo.to_dict()')
video = \
telegram.InlineQueryResultVideo.de_json(self.json_dict).to_dict()