Merge pull request #8 from muhammedfurkan/deepsource-fix-81da3f6f

Refactor unnecessary `else` / `elif` when `if` block has a `return` statement
This commit is contained in:
M.Furkan 2020-11-09 00:42:27 +03:00 committed by GitHub
commit 6bff264f11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 54 additions and 62 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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):
"""

View file

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