Add pyupgrade to pre-commit Hooks (#2301)

* Add pyupgrade to pre-commit

* update test_chat
This commit is contained in:
Bibo-Joshi 2021-01-17 23:24:20 +01:00 committed by GitHub
parent b43a599e53
commit f31787a8ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 44 additions and 61 deletions

View file

@ -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

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -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.

View file

@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=W0603
"""Simple Bot to reply to Telegram messages.

View file

@ -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.

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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()

View file

@ -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,

View file

@ -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

View file

@ -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

View file

@ -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 = {

View file

@ -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')

View file

@ -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 <devs@python-telegram-bot.org>

View file

@ -28,7 +28,7 @@ class TestParseMode:
'<b>bold</b> <i>italic</i> <a href="http://google.com">link</a> '
'<a href="tg://user?id=123456789">name</a>.'
)
formatted_text_formatted = u'bold italic link name.'
formatted_text_formatted = 'bold italic link name.'
@flaky(3, 1)
@pytest.mark.timeout(10)

View file

@ -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!')

View file

@ -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 <devs@python-telegram-bot.org>
@ -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'<b>PhotoTest</b> - *Caption*'
caption = '<b>PhotoTest</b> - *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]

View file

@ -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 <devs@python-telegram-bot.org>

View file

@ -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

View file

@ -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'<a href="tg://user?id={}">{}</a>'
expected = '<a href="tg://user?id={}">{}</a>'
assert user.mention_html() == expected.format(user.id, user.full_name)
assert user.mention_html('the<b>name\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

View file

@ -54,7 +54,7 @@ class TestVideo:
thumb_height = 320
thumb_file_size = 1767
caption = u'<b>VideoTest</b> - *Caption*'
caption = '<b>VideoTest</b> - *Caption*'
video_file_url = 'https://python-telegram-bot.org/static/testfiles/telegram.mp4'
video_file_id = '5a3128a4d2a04750b5b58397f3b5e812'

View file

@ -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'

View file

@ -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'

View file

@ -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