Skip to content

Add pyupgrade to pre-commit #2301

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 4 commits into from
Jan 17, 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
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion examples/conversationbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/conversationbot2.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/deeplinking.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/echobot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/errorhandlerbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/inlinebot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/inlinekeyboard.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/inlinekeyboard2.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/nestedconversationbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/passportbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/paymentbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/persistentconversationbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/pollbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion examples/rawapibot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=W0603
"""Simple Bot to reply to Telegram messages.

Expand Down
1 change: 0 additions & 1 deletion examples/timerbot.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion telegram/bot.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions telegram/ext/picklepersistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def load_singlefile(self) -> None:
# 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)
Expand All @@ -120,7 +120,7 @@ def load_file(filename: str) -> Any:
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
Expand Down
6 changes: 3 additions & 3 deletions telegram/utils/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions tests/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ def test_link(self, chat):

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,
Expand Down
1 change: 0 additions & 1 deletion tests/test_commandhandler.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions tests/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@

@pytest.fixture(scope='function')
def dp2(bot):
for dp in create_dp(bot):
yield dp
yield from create_dp(bot)


class TestDispatcher:
Expand Down Expand Up @@ -690,7 +689,7 @@ def test_non_context_deprecation(self, dp):
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
Expand Down Expand Up @@ -750,7 +749,7 @@ def logger(message):
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
Expand Down
5 changes: 2 additions & 3 deletions tests/test_file.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
3 changes: 1 addition & 2 deletions tests/test_inputfile.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -68,7 +67,7 @@ def test_mimetypes(self, caplog):

# 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')
Expand Down
1 change: 0 additions & 1 deletion tests/test_messageid.py
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
2 changes: 1 addition & 1 deletion tests/test_parsemode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_passport.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def test_all_types(self, passport_data, bot, all_passport_data):

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!')
Expand Down
7 changes: 3 additions & 4 deletions tests/test_photo.py
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -31,7 +30,7 @@

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

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

Expand Down Expand Up @@ -350,7 +349,7 @@ def test_send_file_unicode_filename(self, bot, chat_id):
"""
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]
Expand Down
1 change: 0 additions & 1 deletion tests/test_poll.py
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
1 change: 0 additions & 1 deletion tests/test_sticker.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 9 additions & 9 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,16 +112,16 @@ def test_de_json_without_username_and_last_name(self, json_dict, bot):
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}'
Expand Down Expand Up @@ -505,7 +505,7 @@ def make_assertion(*_, **kwargs):
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(
Expand All @@ -514,7 +514,7 @@ def test_mention_html(self, user):
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(
Expand All @@ -526,7 +526,7 @@ def test_mention_markdown_v2(self, user):
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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy