Improve Code Quality & Test Suite (#2843)

This commit is contained in:
Harshil 2022-01-09 14:47:16 +04:00 committed by Hinrich Mahler
parent 9354db7c19
commit f4147fb583
3 changed files with 25 additions and 28 deletions

View file

@ -2533,20 +2533,20 @@ class Message(TelegramObject):
elif entity.type == MessageEntity.URL and urled: elif entity.type == MessageEntity.URL and urled:
insert = f'<a href="{text}">{text}</a>' insert = f'<a href="{text}">{text}</a>'
elif entity.type == MessageEntity.BOLD: elif entity.type == MessageEntity.BOLD:
insert = '<b>' + text + '</b>' insert = f'<b>{text}</b>'
elif entity.type == MessageEntity.ITALIC: elif entity.type == MessageEntity.ITALIC:
insert = '<i>' + text + '</i>' insert = f'<i>{text}</i>'
elif entity.type == MessageEntity.CODE: elif entity.type == MessageEntity.CODE:
insert = '<code>' + text + '</code>' insert = f'<code>{text}</code>'
elif entity.type == MessageEntity.PRE: elif entity.type == MessageEntity.PRE:
if entity.language: if entity.language:
insert = f'<pre><code class="{entity.language}">{text}</code></pre>' insert = f'<pre><code class="{entity.language}">{text}</code></pre>'
else: else:
insert = '<pre>' + text + '</pre>' insert = f'<pre>{text}</pre>'
elif entity.type == MessageEntity.UNDERLINE: elif entity.type == MessageEntity.UNDERLINE:
insert = '<u>' + text + '</u>' insert = f'<u>{text}</u>'
elif entity.type == MessageEntity.STRIKETHROUGH: elif entity.type == MessageEntity.STRIKETHROUGH:
insert = '<s>' + text + '</s>' insert = f'<s>{text}</s>'
elif entity.type == MessageEntity.SPOILER: elif entity.type == MessageEntity.SPOILER:
insert = f'<span class="tg-spoiler">{text}</span>' insert = f'<span class="tg-spoiler">{text}</span>'
else: else:
@ -2697,7 +2697,7 @@ class Message(TelegramObject):
if nested_entities: if nested_entities:
if version < 2: if version < 2:
raise ValueError( raise ValueError(
'Nested entities are not supported for Markdown ' 'version 1' 'Nested entities are not supported for Markdown version 1'
) )
text = Message._parse_markdown( text = Message._parse_markdown(
@ -2726,43 +2726,38 @@ class Message(TelegramObject):
link = text link = text
insert = f'[{link}]({orig_text})' insert = f'[{link}]({orig_text})'
elif entity.type == MessageEntity.BOLD: elif entity.type == MessageEntity.BOLD:
insert = '*' + text + '*' insert = f'*{text}*'
elif entity.type == MessageEntity.ITALIC: elif entity.type == MessageEntity.ITALIC:
insert = '_' + text + '_' insert = f'_{text}_'
elif entity.type == MessageEntity.CODE: elif entity.type == MessageEntity.CODE:
# Monospace needs special escaping. Also can't have entities nested within # Monospace needs special escaping. Also can't have entities nested within
insert = ( insert = f'`{escape_markdown(orig_text, version, MessageEntity.CODE)}`'
'`'
+ escape_markdown(
orig_text, version=version, entity_type=MessageEntity.CODE
)
+ '`'
)
elif entity.type == MessageEntity.PRE: elif entity.type == MessageEntity.PRE:
# Monospace needs special escaping. Also can't have entities nested within # Monospace needs special escaping. Also can't have entities nested within
code = escape_markdown( code = escape_markdown(
orig_text, version=version, entity_type=MessageEntity.PRE orig_text, version=version, entity_type=MessageEntity.PRE
) )
if entity.language: if entity.language:
prefix = '```' + entity.language + '\n' prefix = f'```{entity.language}\n'
else: else:
if code.startswith('\\'): if code.startswith('\\'):
prefix = '```' prefix = '```'
else: else:
prefix = '```\n' prefix = '```\n'
insert = prefix + code + '```' insert = f'{prefix}{code}```'
elif entity.type == MessageEntity.UNDERLINE: elif entity.type == MessageEntity.UNDERLINE:
if version == 1: if version == 1:
raise ValueError( raise ValueError(
'Underline entities are not supported for Markdown ' 'version 1' 'Underline entities are not supported for Markdown version 1'
) )
insert = '__' + text + '__' insert = f'__{text}__'
elif entity.type == MessageEntity.STRIKETHROUGH: elif entity.type == MessageEntity.STRIKETHROUGH:
if version == 1: if version == 1:
raise ValueError( raise ValueError(
'Strikethrough entities are not supported for Markdown ' 'version 1' 'Strikethrough entities are not supported for Markdown version 1'
) )
insert = '~' + text + '~' insert = f'~{text}~'
elif entity.type == MessageEntity.SPOILER: elif entity.type == MessageEntity.SPOILER:
if version == 1: if version == 1:
raise ValueError( raise ValueError(

View file

@ -18,12 +18,14 @@
# along with this program. If not, see [http://www.gnu.org/licenses/]. # along with this program. If not, see [http://www.gnu.org/licenses/].
# pylint: disable=no-self-use # pylint: disable=no-self-use
"""This module contains the auxiliary class ContextTypes.""" """This module contains the auxiliary class ContextTypes."""
from typing import Type, Generic, overload, Dict # pylint: disable=unused-import from typing import Type, Generic, overload, Dict, TYPE_CHECKING # pylint: disable=unused-import
from telegram.ext._callbackcontext import CallbackContext from telegram.ext._callbackcontext import CallbackContext
from telegram.ext._extbot import ExtBot # pylint: disable=unused-import
from telegram.ext._utils.types import CCT, UD, CD, BD from telegram.ext._utils.types import CCT, UD, CD, BD
if TYPE_CHECKING:
from telegram.ext._extbot import ExtBot # pylint: disable=unused-import
class ContextTypes(Generic[CCT, UD, CD, BD]): class ContextTypes(Generic[CCT, UD, CD, BD]):
""" """

View file

@ -615,13 +615,13 @@ class TestUpdater:
# (bots) running simultaneously while testing in github actions. # (bots) running simultaneously while testing in github actions.
records = caplog.records.copy() # To avoid iterating and removing at same time records = caplog.records.copy() # To avoid iterating and removing at same time
for idx, log in enumerate(records): for idx, log in enumerate(records):
print(log) print(idx, log)
msg = log.getMessage() msg = log.getMessage()
if msg.startswith('Error while getting Updates: Conflict'): if msg.startswith('Error while getting Updates: Conflict'):
caplog.records.pop(idx) # For stability caplog.records.remove(log) # For stability
if msg.startswith('No error handlers are registered'): elif msg.startswith('No error handlers are registered'):
caplog.records.pop(idx) caplog.records.remove(log)
assert len(caplog.records) == 2, caplog.records assert len(caplog.records) == 2, caplog.records