diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 01bfd7fb5..9bf9e0311 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,3 +25,10 @@ repos: hooks: - id: mypy files: ^(telegram|examples)/.*\.py$ +- repo: https://github.com/asottile/pyupgrade + rev: v2.7.4 + hooks: + - id: pyupgrade + files: ^(telegram|examples|tests)/.*\.py$ + args: + - --py36-plus diff --git a/examples/conversationbot.py b/examples/conversationbot.py index 0837b9475..d004cf18c 100644 --- a/examples/conversationbot.py +++ b/examples/conversationbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/conversationbot2.py b/examples/conversationbot2.py index 49d0deea9..0fb8e4353 100644 --- a/examples/conversationbot2.py +++ b/examples/conversationbot2.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/deeplinking.py b/examples/deeplinking.py index eb1cee13c..e0886b2e5 100644 --- a/examples/deeplinking.py +++ b/examples/deeplinking.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/echobot.py b/examples/echobot.py index c8a264584..43c40de6c 100644 --- a/examples/echobot.py +++ b/examples/echobot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/errorhandlerbot.py b/examples/errorhandlerbot.py index 3aa90d0df..fcb51bc19 100644 --- a/examples/errorhandlerbot.py +++ b/examples/errorhandlerbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/inlinebot.py b/examples/inlinebot.py index 460e4c70a..a25361435 100644 --- a/examples/inlinebot.py +++ b/examples/inlinebot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/inlinekeyboard.py b/examples/inlinekeyboard.py index 8158cc084..a4c45edd7 100644 --- a/examples/inlinekeyboard.py +++ b/examples/inlinekeyboard.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/inlinekeyboard2.py b/examples/inlinekeyboard2.py index 4d6092bec..08090a3bd 100644 --- a/examples/inlinekeyboard2.py +++ b/examples/inlinekeyboard2.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/nestedconversationbot.py b/examples/nestedconversationbot.py index 9b91576dd..b46fe96c1 100644 --- a/examples/nestedconversationbot.py +++ b/examples/nestedconversationbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/passportbot.py b/examples/passportbot.py index 33372753e..3721329af 100644 --- a/examples/passportbot.py +++ b/examples/passportbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/paymentbot.py b/examples/paymentbot.py index 7080e30a7..307681114 100644 --- a/examples/paymentbot.py +++ b/examples/paymentbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/persistentconversationbot.py b/examples/persistentconversationbot.py index 492f202bb..ea29a2a5c 100644 --- a/examples/persistentconversationbot.py +++ b/examples/persistentconversationbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116, C0103 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/pollbot.py b/examples/pollbot.py index 4f604a9a5..075571b93 100644 --- a/examples/pollbot.py +++ b/examples/pollbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/examples/rawapibot.py b/examples/rawapibot.py index b25d72766..464c0b336 100644 --- a/examples/rawapibot.py +++ b/examples/rawapibot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0603 """Simple Bot to reply to Telegram messages. diff --git a/examples/timerbot.py b/examples/timerbot.py index 499677559..ad57bfae9 100644 --- a/examples/timerbot.py +++ b/examples/timerbot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=W0613, C0116 # type: ignore[union-attr] # This program is dedicated to the public domain under the CC0 license. diff --git a/requirements-dev.txt b/requirements-dev.txt index eec7eb8ef..bf34254ae 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -4,6 +4,7 @@ black==20.8b1 flake8==3.8.4 pylint==2.6.0 mypy==0.790 +pyupgrade==2.7.4 pytest==4.2.0 # Need older attrs version for pytest 4.2.0 diff --git a/telegram/bot.py b/telegram/bot.py index 828e476b9..0fe60c1c6 100644 --- a/telegram/bot.py +++ b/telegram/bot.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # pylint: disable=E0611,E0213,E1102,C0103,E1101,R0913,R0904 # # A library that provides a Python interface to the Telegram Bot API diff --git a/telegram/ext/picklepersistence.py b/telegram/ext/picklepersistence.py index df6e7598f..625de9850 100644 --- a/telegram/ext/picklepersistence.py +++ b/telegram/ext/picklepersistence.py @@ -105,7 +105,7 @@ class PicklePersistence(BasePersistence): # For backwards compatibility with files not containing bot data self.bot_data = data.get('bot_data', {}) self.conversations = data['conversations'] - except IOError: + except OSError: self.conversations = dict() self.user_data = defaultdict(dict) self.chat_data = defaultdict(dict) @@ -120,7 +120,7 @@ class PicklePersistence(BasePersistence): try: with open(filename, "rb") as file: return pickle.load(file) - except IOError: + except OSError: return None except pickle.UnpicklingError as exc: raise TypeError(f"File {filename} does not contain valid pickle data") from exc diff --git a/telegram/utils/request.py b/telegram/utils/request.py index 6e9592139..9a846f46b 100644 --- a/telegram/utils/request.py +++ b/telegram/utils/request.py @@ -78,9 +78,9 @@ def _render_part(self: RequestField, name: str, value: str) -> str: # pylint: d Content-Disposition headers since telegram servers don't understand it. Instead just escape \\ and " and replace any \n and \r with a space. """ - value = value.replace(u'\\', u'\\\\').replace(u'"', u'\\"') - value = value.replace(u'\r', u' ').replace(u'\n', u' ') - return u'{}="{}"'.format(name, value) + value = value.replace('\\', '\\\\').replace('"', '\\"') + value = value.replace('\r', ' ').replace('\n', ' ') + return f'{name}="{value}"' RequestField._render_part = _render_part # type: ignore # pylint: disable=W0212 diff --git a/tests/conftest.py b/tests/conftest.py index f773f69d4..28414d53a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -132,8 +132,7 @@ def create_dp(bot): @pytest.fixture(scope='session') def _dp(bot): - for dp in create_dp(bot): - yield dp + yield from create_dp(bot) @pytest.fixture(scope='function') @@ -177,14 +176,14 @@ def updater(bot): @pytest.fixture(scope='function') def thumb_file(): - f = open(u'tests/data/thumb.jpg', 'rb') + f = open('tests/data/thumb.jpg', 'rb') yield f f.close() @pytest.fixture(scope='class') def class_thumb_file(): - f = open(u'tests/data/thumb.jpg', 'rb') + f = open('tests/data/thumb.jpg', 'rb') yield f f.close() diff --git a/tests/test_chat.py b/tests/test_chat.py index 024531092..e8e95748d 100644 --- a/tests/test_chat.py +++ b/tests/test_chat.py @@ -114,11 +114,11 @@ class TestChat: def test_full_name(self): chat = Chat( - id=1, type=Chat.PRIVATE, first_name=u'first\u2022name', last_name=u'last\u2022name' + id=1, type=Chat.PRIVATE, first_name='first\u2022name', last_name='last\u2022name' ) - assert chat.full_name == u'first\u2022name last\u2022name' - chat = Chat(id=1, type=Chat.PRIVATE, first_name=u'first\u2022name') - assert chat.full_name == u'first\u2022name' + assert chat.full_name == 'first\u2022name last\u2022name' + chat = Chat(id=1, type=Chat.PRIVATE, first_name='first\u2022name') + assert chat.full_name == 'first\u2022name' chat = Chat( id=1, type=Chat.PRIVATE, diff --git a/tests/test_commandhandler.py b/tests/test_commandhandler.py index 0ff37c406..a2cd81ec2 100644 --- a/tests/test_commandhandler.py +++ b/tests/test_commandhandler.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index fd639ff68..41a6bbaff 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -42,8 +42,7 @@ from collections import defaultdict @pytest.fixture(scope='function') def dp2(bot): - for dp in create_dp(bot): - yield dp + yield from create_dp(bot) class TestDispatcher: @@ -690,7 +689,7 @@ class TestDispatcher: def test_error_while_persisting(self, cdp, monkeypatch): class OwnPersistence(BasePersistence): def __init__(self): - super(OwnPersistence, self).__init__() + super().__init__() self.store_user_data = True self.store_chat_data = True self.store_bot_data = True @@ -750,7 +749,7 @@ class TestDispatcher: def test_persisting_no_user_no_chat(self, cdp): class OwnPersistence(BasePersistence): def __init__(self): - super(OwnPersistence, self).__init__() + super().__init__() self.store_user_data = True self.store_chat_data = True self.store_bot_data = True diff --git a/tests/test_file.py b/tests/test_file.py index f122d995d..2aef3f2ed 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 @@ -53,10 +52,10 @@ class TestFile: file_id = 'NOTVALIDDOESNOTMATTER' file_unique_id = 'adc3145fd2e84d95b64d68eaa22aa33e' file_path = ( - u'https://api.org/file/bot133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0/document/file_3' + 'https://api.org/file/bot133505823:AAHZFMHno3mzVLErU5b5jJvaeG--qUyLyG0/document/file_3' ) file_size = 28232 - file_content = u'Saint-Saëns'.encode('utf-8') # Intentionally contains unicode chars. + file_content = 'Saint-Saëns'.encode() # Intentionally contains unicode chars. def test_de_json(self, bot): json_dict = { diff --git a/tests/test_inputfile.py b/tests/test_inputfile.py index d8cf7dcbd..97368ae0d 100644 --- a/tests/test_inputfile.py +++ b/tests/test_inputfile.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 @@ -68,7 +67,7 @@ class TestInputFile: # Test string file with caplog.at_level(logging.DEBUG): - assert InputFile(open('tests/data/text_file.txt', 'r')).mimetype == 'text/plain' + assert InputFile(open('tests/data/text_file.txt')).mimetype == 'text/plain' assert len(caplog.records) == 1 assert caplog.records[0].getMessage().startswith('Could not parse file content') diff --git a/tests/test_messageid.py b/tests/test_messageid.py index 3f45c1cf3..770cb4519 100644 --- a/tests/test_messageid.py +++ b/tests/test_messageid.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 # Leandro Toledo de Souza diff --git a/tests/test_parsemode.py b/tests/test_parsemode.py index b35171cd6..f205f9d7c 100644 --- a/tests/test_parsemode.py +++ b/tests/test_parsemode.py @@ -28,7 +28,7 @@ class TestParseMode: 'bold italic link ' 'name.' ) - formatted_text_formatted = u'bold italic link name.' + formatted_text_formatted = 'bold italic link name.' @flaky(3, 1) @pytest.mark.timeout(10) diff --git a/tests/test_passport.py b/tests/test_passport.py index e192710be..f5099af73 100644 --- a/tests/test_passport.py +++ b/tests/test_passport.py @@ -405,7 +405,7 @@ class TestPassport: def test_bot_init_invalid_key(self, bot): with pytest.raises(TypeError): - Bot(bot.token, private_key=u'Invalid key!') + Bot(bot.token, private_key='Invalid key!') with pytest.raises(ValueError): Bot(bot.token, private_key=b'Invalid key!') diff --git a/tests/test_photo.py b/tests/test_photo.py index 72f98b984..9969dc5f1 100644 --- a/tests/test_photo.py +++ b/tests/test_photo.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 # Leandro Toledo de Souza @@ -31,7 +30,7 @@ from tests.conftest import expect_bad_request, check_shortcut_call, check_shortc @pytest.fixture(scope='function') def photo_file(): - f = open(u'tests/data/telegram.jpg', 'rb') + f = open('tests/data/telegram.jpg', 'rb') yield f f.close() @@ -58,7 +57,7 @@ def photo(_photo): class TestPhoto: width = 800 height = 800 - caption = u'PhotoTest - *Caption*' + caption = 'PhotoTest - *Caption*' photo_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram_new.jpg' file_size = 29176 @@ -350,7 +349,7 @@ class TestPhoto: """ Regression test for https://github.com/python-telegram-bot/python-telegram-bot/issues/1202 """ - with open(u'tests/data/测试.png', 'rb') as f: + with open('tests/data/测试.png', 'rb') as f: message = bot.send_photo(photo=f, chat_id=chat_id) photo = message.photo[-1] diff --git a/tests/test_poll.py b/tests/test_poll.py index 4a16cc2d9..94114818e 100644 --- a/tests/test_poll.py +++ b/tests/test_poll.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 # Leandro Toledo de Souza diff --git a/tests/test_sticker.py b/tests/test_sticker.py index 0280f167a..3d8e3abac 100644 --- a/tests/test_sticker.py +++ b/tests/test_sticker.py @@ -1,5 +1,4 @@ #!/usr/bin/env python -# -*- coding: utf-8 -*- # # A library that provides a Python interface to the Telegram Bot API # Copyright (C) 2015-2021 diff --git a/tests/test_user.py b/tests/test_user.py index e1003cfc1..ce18d2c01 100644 --- a/tests/test_user.py +++ b/tests/test_user.py @@ -57,8 +57,8 @@ def user(bot): class TestUser: id_ = 1 is_bot = True - first_name = u'first\u2022name' - last_name = u'last\u2022name' + first_name = 'first\u2022name' + last_name = 'last\u2022name' username = 'username' language_code = 'en_us' can_join_groups = True @@ -112,16 +112,16 @@ class TestUser: def test_name(self, user): assert user.name == '@username' user.username = None - assert user.name == u'first\u2022name last\u2022name' + assert user.name == 'first\u2022name last\u2022name' user.last_name = None - assert user.name == u'first\u2022name' + assert user.name == 'first\u2022name' user.username = self.username assert user.name == '@username' def test_full_name(self, user): - assert user.full_name == u'first\u2022name last\u2022name' + assert user.full_name == 'first\u2022name last\u2022name' user.last_name = None - assert user.full_name == u'first\u2022name' + assert user.full_name == 'first\u2022name' def test_link(self, user): assert user.link == f'https://t.me/{user.username}' @@ -505,7 +505,7 @@ class TestUser: assert user.copy_message(chat_id='chat_id', message_id='message_id') def test_mention_html(self, user): - expected = u'{}' + expected = '{}' assert user.mention_html() == expected.format(user.id, user.full_name) assert user.mention_html('thename\u2022') == expected.format( @@ -514,7 +514,7 @@ class TestUser: assert user.mention_html(user.username) == expected.format(user.id, user.username) def test_mention_markdown(self, user): - expected = u'[{}](tg://user?id={})' + expected = '[{}](tg://user?id={})' assert user.mention_markdown() == expected.format(user.full_name, user.id) assert user.mention_markdown('the_name*\u2022') == expected.format( @@ -526,7 +526,7 @@ class TestUser: user.first_name = 'first{name' user.last_name = 'last_name' - expected = u'[{}](tg://user?id={})' + expected = '[{}](tg://user?id={})' assert user.mention_markdown_v2() == expected.format( escape_markdown(user.full_name, version=2), user.id diff --git a/tests/test_video.py b/tests/test_video.py index 6505ba513..ec9f486a8 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -54,7 +54,7 @@ class TestVideo: thumb_height = 320 thumb_file_size = 1767 - caption = u'VideoTest - *Caption*' + caption = 'VideoTest - *Caption*' video_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.mp4' video_file_id = '5a3128a4d2a04750b5b58397f3b5e812' diff --git a/tests/test_videonote.py b/tests/test_videonote.py index 8feee4edf..e93c3e879 100644 --- a/tests/test_videonote.py +++ b/tests/test_videonote.py @@ -49,7 +49,7 @@ class TestVideoNote: thumb_height = 240 thumb_file_size = 11547 - caption = u'VideoNoteTest - Caption' + caption = 'VideoNoteTest - Caption' videonote_file_id = '5a3128a4d2a04750b5b58397f3b5e812' videonote_file_unique_id = 'adc3145fd2e84d95b64d68eaa22aa33e' diff --git a/tests/test_voice.py b/tests/test_voice.py index cf2efc3dd..6ff2bd269 100644 --- a/tests/test_voice.py +++ b/tests/test_voice.py @@ -46,7 +46,7 @@ class TestVoice: mime_type = 'audio/ogg' file_size = 9199 - caption = u'Test *voice*' + caption = 'Test *voice*' voice_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.ogg' voice_file_id = '5a3128a4d2a04750b5b58397f3b5e812' diff --git a/tests/test_webhookinfo.py b/tests/test_webhookinfo.py index 08776b2a4..81eefa21b 100644 --- a/tests/test_webhookinfo.py +++ b/tests/test_webhookinfo.py @@ -36,7 +36,7 @@ def webhook_info(): ) -class TestWebhookInfo(object): +class TestWebhookInfo: url = "http://www.google.com" has_custom_certificate = False pending_update_count = 5