From 9b50116e58d275d457c67a2e4845a93bc0c16e88 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sun, 8 Nov 2020 21:42:10 +0000 Subject: [PATCH] Refactor unnecessary `else` / `elif` when `if` block has a `return` statement --- aiogram/bot/api.py | 2 +- aiogram/dispatcher/dispatcher.py | 48 +++++++++++++-------------- aiogram/dispatcher/filters/builtin.py | 5 ++- aiogram/dispatcher/filters/filters.py | 14 ++++---- aiogram/dispatcher/storage.py | 4 +-- aiogram/dispatcher/webhook.py | 7 ++-- aiogram/types/fields.py | 2 +- aiogram/types/message.py | 27 ++++++++------- aiogram/types/mixins.py | 3 +- aiogram/utils/payload.py | 4 +-- 10 files changed, 54 insertions(+), 62 deletions(-) diff --git a/aiogram/bot/api.py b/aiogram/bot/api.py index 03c7e209..d0639aa2 100644 --- a/aiogram/bot/api.py +++ b/aiogram/bot/api.py @@ -107,7 +107,7 @@ def check_result(method_name: str, content_type: str, status_code: int, body: st if HTTPStatus.OK <= status_code <= HTTPStatus.IM_USED: return result_json.get('result') - elif parameters.retry_after: + if parameters.retry_after: raise exceptions.RetryAfter(parameters.retry_after) elif parameters.migrate_to_chat_id: raise exceptions.MigrateToChat(parameters.migrate_to_chat_id) diff --git a/aiogram/dispatcher/dispatcher.py b/aiogram/dispatcher/dispatcher.py index b5e0b68f..978c4426 100644 --- a/aiogram/dispatcher/dispatcher.py +++ b/aiogram/dispatcher/dispatcher.py @@ -307,9 +307,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, @@ -1242,29 +1241,28 @@ 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 f3425386..dee2c058 100644 --- a/aiogram/dispatcher/filters/builtin.py +++ b/aiogram/dispatcher/filters/builtin.py @@ -501,8 +501,7 @@ class IsSenderContact(BoundFilter): is_sender_contact = message.contact.user_id == message.from_user.id if self.is_sender_contact: return is_sender_contact - else: - return not is_sender_contact + return not is_sender_contact class StateFilter(BoundFilter): @@ -698,7 +697,7 @@ class IsReplyFilter(BoundFilter): async def check(self, msg: Message): if msg.reply_to_message and self.is_reply: return {'reply': msg.reply_to_message} - elif not msg.reply_to_message and not self.is_reply: + if not msg.reply_to_message and not self.is_reply: return True diff --git a/aiogram/dispatcher/filters/filters.py b/aiogram/dispatcher/filters/filters.py index 4d6ff0c2..09ab2477 100644 --- a/aiogram/dispatcher/filters/filters.py +++ b/aiogram/dispatcher/filters/filters.py @@ -29,11 +29,10 @@ def get_filter_spec(dispatcher, filter_: callable): if 'dispatcher' in spec: kwargs['dispatcher'] = dispatcher if inspect.isawaitable(filter_) \ - or inspect.iscoroutinefunction(filter_) \ - or isinstance(filter_, AbstractFilter): + or inspect.iscoroutinefunction(filter_) \ + or isinstance(filter_, AbstractFilter): return FilterObj(filter=filter_, kwargs=kwargs, is_async=True) - else: - return FilterObj(filter=filter_, kwargs=kwargs, is_async=False) + return FilterObj(filter=filter_, kwargs=kwargs, is_async=False) def get_filters_spec(dispatcher, filters: typing.Iterable[callable]): @@ -54,8 +53,7 @@ async def execute_filter(filter_: FilterObj, args): """ if filter_.is_async: return await filter_.filter(*args, **filter_.kwargs) - else: - return filter_.filter(*args, **filter_.kwargs) + return filter_.filter(*args, **filter_.kwargs) async def check_filters(filters: typing.Iterable[FilterObj], args): @@ -121,7 +119,7 @@ class FilterRecord: def _check_event_handler(self, event_handler) -> bool: if self.event_handlers: return event_handler in self.event_handlers - elif self.exclude_event_handlers: + if self.exclude_event_handlers: return event_handler not in self.exclude_event_handlers return True @@ -221,7 +219,7 @@ class BoundFilter(Filter): if cls.key is not None: if cls.key in full_config: return {cls.key: full_config[cls.key]} - elif cls.required: + if cls.required: return {cls.key: cls.default} diff --git a/aiogram/dispatcher/storage.py b/aiogram/dispatcher/storage.py index 1262eed0..da9da2da 100644 --- a/aiogram/dispatcher/storage.py +++ b/aiogram/dispatcher/storage.py @@ -285,9 +285,9 @@ class FSMContext: if value is None: return - elif isinstance(value, str): + if isinstance(value, str): return value - elif isinstance(value, State): + if isinstance(value, State): return value.state return str(value) diff --git a/aiogram/dispatcher/webhook.py b/aiogram/dispatcher/webhook.py index fe1f2cbe..e498a265 100644 --- a/aiogram/dispatcher/webhook.py +++ b/aiogram/dispatcher/webhook.py @@ -187,10 +187,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/fields.py b/aiogram/types/fields.py index a4a58d37..cd0d006c 100644 --- a/aiogram/types/fields.py +++ b/aiogram/types/fields.py @@ -32,7 +32,7 @@ class BaseField(metaclass=abc.ABCMeta): def resolve_base(self, instance): if self.base_object is None or hasattr(self.base_object, 'telegram_types'): return - elif isinstance(self.base_object, str): + if isinstance(self.base_object, str): self.base_object = instance.telegram_types.get(self.base_object) def get_value(self, instance): diff --git a/aiogram/types/message.py b/aiogram/types/message.py index 900b731c..5a9e03fb 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -2786,7 +2786,7 @@ class Message(base.TelegramObject): if self.text: kwargs["disable_web_page_preview"] = disable_web_page_preview return await self.bot.send_message(text=text, **kwargs) - elif self.audio: + if self.audio: return await self.bot.send_audio( audio=self.audio.file_id, caption=text, @@ -2795,33 +2795,33 @@ class Message(base.TelegramObject): duration=self.audio.duration, **kwargs, ) - elif self.animation: + if self.animation: return await self.bot.send_animation( animation=self.animation.file_id, caption=text, **kwargs ) - elif self.document: + if self.document: return await self.bot.send_document( document=self.document.file_id, caption=text, **kwargs ) - elif self.photo: + if self.photo: return await self.bot.send_photo( photo=self.photo[-1].file_id, caption=text, **kwargs ) - elif self.sticker: + if self.sticker: kwargs.pop("parse_mode") return await self.bot.send_sticker(sticker=self.sticker.file_id, **kwargs) - elif self.video: + if self.video: return await self.bot.send_video( video=self.video.file_id, caption=text, **kwargs ) - elif self.video_note: + if self.video_note: kwargs.pop("parse_mode") return await self.bot.send_video_note( video_note=self.video_note.file_id, **kwargs ) - elif self.voice: + if self.voice: return await self.bot.send_voice(voice=self.voice.file_id, **kwargs) - elif self.contact: + if self.contact: kwargs.pop("parse_mode") return await self.bot.send_contact( phone_number=self.contact.phone_number, @@ -2830,7 +2830,7 @@ class Message(base.TelegramObject): vcard=self.contact.vcard, **kwargs, ) - elif self.venue: + if self.venue: kwargs.pop("parse_mode") return await self.bot.send_venue( latitude=self.venue.location.latitude, @@ -2841,22 +2841,21 @@ class Message(base.TelegramObject): foursquare_type=self.venue.foursquare_type, **kwargs, ) - elif self.location: + if self.location: kwargs.pop("parse_mode") return await self.bot.send_location( latitude=self.location.latitude, longitude=self.location.longitude, **kwargs, ) - elif self.poll: + if self.poll: kwargs.pop("parse_mode") return await self.bot.send_poll( question=self.poll.question, options=[option.text for option in self.poll.options], **kwargs, ) - else: - raise TypeError("This type of message can't be copied.") + raise TypeError("This type of message can't be copied.") async def copy_to( self, diff --git a/aiogram/types/mixins.py b/aiogram/types/mixins.py index 13f8412f..66390807 100644 --- a/aiogram/types/mixins.py +++ b/aiogram/types/mixins.py @@ -42,8 +42,7 @@ class Downloadable: """ if hasattr(self, 'file_path'): return self - else: - return await self.bot.get_file(self.file_id) + return await self.bot.get_file(self.file_id) async def get_url(self): """ diff --git a/aiogram/utils/payload.py b/aiogram/utils/payload.py index 9b3fbc9a..30c76acd 100644 --- a/aiogram/utils/payload.py +++ b/aiogram/utils/payload.py @@ -37,9 +37,9 @@ def _normalize(obj): """ if isinstance(obj, list): return [_normalize(item) for item in obj] - elif isinstance(obj, dict): + if isinstance(obj, dict): return {k: _normalize(v) for k, v in obj.items() if v is not None} - elif hasattr(obj, 'to_python'): + if hasattr(obj, 'to_python'): return obj.to_python() return obj