From 70aa80abf160ccad8f317b4f22fd4e1380d80cfa Mon Sep 17 00:00:00 2001 From: Almaz Date: Sun, 18 Jul 2021 14:20:07 +0300 Subject: [PATCH] Simplify some conditions (#632) --- aiogram/dispatcher/dispatcher.py | 46 ++++++++++++--------------- aiogram/dispatcher/filters/builtin.py | 7 ++-- aiogram/dispatcher/webhook.py | 7 ++-- aiogram/types/inline_keyboard.py | 2 +- aiogram/types/input_file.py | 11 ++----- aiogram/types/message_entity.py | 8 ++--- aiogram/types/reply_keyboard.py | 2 +- aiogram/utils/callback_data.py | 5 ++- aiogram/utils/helper.py | 5 +-- 9 files changed, 38 insertions(+), 55 deletions(-) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index 8231c4f7..5e696f19 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -330,9 +330,8 @@ class Dispatcher(DataMixin, ContextInstanceMixin): def _loop_create_task(self, coro): if self._main_loop is None: return asyncio.create_task(coro) - else: - _ensure_loop(self._main_loop) - return self._main_loop.create_task(coro) + _ensure_loop(self._main_loop) + return self._main_loop.create_task(coro) async def start_polling(self, timeout=20, @@ -1394,29 +1393,26 @@ class Dispatcher(DataMixin, ContextInstanceMixin): no_error=True) if is_not_throttled: return await func(*args, **kwargs) - else: - kwargs.update( - { - 'rate': rate, - 'key': key, - 'user_id': user_id, - 'chat_id': chat_id - } - ) # update kwargs with parameters which were given to throttled + kwargs.update( + { + 'rate': rate, + 'key': key, + 'user_id': user_id, + 'chat_id': chat_id, + } + ) # update kwargs with parameters which were given to throttled - if on_throttled: - if asyncio.iscoroutinefunction(on_throttled): - await on_throttled(*args, **kwargs) - else: - kwargs.update( - { - 'loop': asyncio.get_running_loop() - } - ) - partial_func = functools.partial(on_throttled, *args, **kwargs) - asyncio.get_running_loop().run_in_executor(None, - partial_func - ) + if on_throttled: + if asyncio.iscoroutinefunction(on_throttled): + await on_throttled(*args, **kwargs) + else: + kwargs.update({'loop': asyncio.get_running_loop()}) + partial_func = functools.partial( + on_throttled, *args, **kwargs + ) + asyncio.get_running_loop().run_in_executor( + None, partial_func + ) return wrapped return decorator diff --git a/aiogram/dispatcher/filters/builtin.py b/aiogram/dispatcher/filters/builtin.py index 457de182..7a21ca3f 100644 --- a/aiogram/dispatcher/filters/builtin.py +++ b/aiogram/dispatcher/filters/builtin.py @@ -279,9 +279,10 @@ class Text(Filter): elif check == 0: raise ValueError(f"No one mode is specified!") - equals, contains, endswith, startswith = map(lambda e: [e] if isinstance(e, str) or isinstance(e, LazyProxy) - else e, - (equals, contains, endswith, startswith)) + equals, contains, endswith, startswith = map( + lambda e: [e] if isinstance(e, (str, LazyProxy)) else e, + (equals, contains, endswith, startswith), + ) self.equals = equals self.contains = contains self.endswith = endswith diff --git a/aiogram/dispatcher/webhook.py b/aiogram/dispatcher/webhook.py index bc21e22c..c76ffae2 100644 --- a/aiogram/dispatcher/webhook.py +++ b/aiogram/dispatcher/webhook.py @@ -188,10 +188,9 @@ class WebhookRequestHandler(web.View): if fut.done(): return fut.result() - else: - # context.set_value(WEBHOOK_CONNECTION, False) - fut.remove_done_callback(cb) - fut.add_done_callback(self.respond_via_request) + # context.set_value(WEBHOOK_CONNECTION, False) + fut.remove_done_callback(cb) + fut.add_done_callback(self.respond_via_request) finally: timeout_handle.cancel() diff --git a/aiogram/types/inline_keyboard.py b/aiogram/types/inline_keyboard.py index 97ad35da..49a947be 100644 --- a/aiogram/types/inline_keyboard.py +++ b/aiogram/types/inline_keyboard.py @@ -50,7 +50,7 @@ class InlineKeyboardMarkup(base.TelegramObject): if index % self.row_width == 0: self.inline_keyboard.append(row) row = [] - if len(row) > 0: + if row: self.inline_keyboard.append(row) return self diff --git a/aiogram/types/input_file.py b/aiogram/types/input_file.py index 3a78c499..c974025a 100644 --- a/aiogram/types/input_file.py +++ b/aiogram/types/input_file.py @@ -41,13 +41,9 @@ class InputFile(base.TelegramObject): self._path = path_or_bytesio if filename is None: filename = os.path.split(path_or_bytesio)[-1] - elif isinstance(path_or_bytesio, io.IOBase): + elif isinstance(path_or_bytesio, (io.IOBase, _WebPipe)): self._path = None self._file = path_or_bytesio - elif isinstance(path_or_bytesio, _WebPipe): - self._path = None - self._file = path_or_bytesio - elif isinstance(path_or_bytesio, Path): self._file = path_or_bytesio.open("rb") self._path = path_or_bytesio.resolve() @@ -174,10 +170,7 @@ class _WebPipe: def name(self): if not self._name: *_, part = self.url.rpartition('/') - if part: - self._name = part - else: - self._name = secrets.token_urlsafe(24) + self._name = part or secrets.token_urlsafe(24) return self._name async def open(self): diff --git a/aiogram/types/message_entity.py b/aiogram/types/message_entity.py index 58705265..9ee98c11 100644 --- a/aiogram/types/message_entity.py +++ b/aiogram/types/message_entity.py @@ -50,11 +50,9 @@ class MessageEntity(base.TelegramObject): if sys.maxunicode == 0xFFFF: return text[self.offset : self.offset + self.length] - if not isinstance(text, bytes): - entity_text = text.encode("utf-16-le") - else: - entity_text = text - + entity_text = ( + text.encode("utf-16-le") if not isinstance(text, bytes) else text + ) entity_text = entity_text[self.offset * 2 : (self.offset + self.length) * 2] return entity_text.decode("utf-16-le") diff --git a/aiogram/types/reply_keyboard.py b/aiogram/types/reply_keyboard.py index e648e036..7bfdb8e6 100644 --- a/aiogram/types/reply_keyboard.py +++ b/aiogram/types/reply_keyboard.py @@ -67,7 +67,7 @@ class ReplyKeyboardMarkup(base.TelegramObject): if index % self.row_width == 0: self.keyboard.append(row) row = [] - if len(row) > 0: + if row: self.keyboard.append(row) return self diff --git a/aiogram/utils/callback_data.py b/aiogram/utils/callback_data.py index e24ad7b1..d44fa5b9 100644 --- a/aiogram/utils/callback_data.py +++ b/aiogram/utils/callback_data.py @@ -130,7 +130,6 @@ class CallbackDataFilter(Filter): if isinstance(value, (list, tuple, set, frozenset)): if data.get(key) not in value: return False - else: - if data.get(key) != value: - return False + elif data.get(key) != value: + return False return {'callback_data': data} diff --git a/aiogram/utils/helper.py b/aiogram/utils/helper.py index 735afe5d..2189d1dc 100644 --- a/aiogram/utils/helper.py +++ b/aiogram/utils/helper.py @@ -103,10 +103,7 @@ class HelperMode(Helper): if symbol == '_' and pos > 0: need_upper = True else: - if need_upper: - result += symbol.upper() - else: - result += symbol.lower() + result += symbol.upper() if need_upper else symbol.lower() need_upper = False if first_upper: result = result[0].upper() + result[1:]