Skip to content

Sourcery refactored main branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main
Open

Sourcery refactored main branch#1
sourcery-ai[bot] wants to merge 1 commit intomainfrom
sourcery/main

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot commented Jul 12, 2023

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from New-dev0 July 12, 2023 11:03
Copy link
Copy Markdown
Author

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to GitHub API limits, only the first 60 comments can be shown.

async def buttons_handler(ctx: BotContext[CommandEvent]):
m = await ctx.bot.prepare_response_message(ctx.event.message)
m.message = f"Please select an option:"
m.message = "Please select an option:"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function buttons_handler refactored with the following changes:

async def buttons_handler(ctx: BotContext[CommandEvent]):
m = await ctx.bot.prepare_response_message(ctx.event.message)
m.message = f"Please select an option:"
m.message = "Please select an option:"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function buttons_handler refactored with the following changes:

async def buttons_handler(ctx: BotContext[CommandEvent]):
m = await ctx.bot.prepare_response_message(ctx.event.message)
m.message = f"Please select an option:"
m.message = "Please select an option:"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function buttons_handler refactored with the following changes:

async def buttons_handler(ctx: BotContext[CommandEvent]):
m = await ctx.bot.prepare_response_message(ctx.event.message)
m.message = f"Please select an option:"
m.message = "Please select an option:"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function buttons_handler refactored with the following changes:

@app.on_community_update()
async def community_update_handler(ctx: BotContext[CommunityUpdatedEvent]):
print(ctx.event.community_id + " was updated")
print(f"{ctx.event.community_id} was updated")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function community_update_handler refactored with the following changes:

Comment on lines -56 to +68
results = []
for i in range(len(data[1])):
results.append(
InlineQueryResultArticle(
id=str(i),
title=data[1][i],
description=data[1][i],
input_message=InputMessageContent(data[2][i]),
article_url=data[3][i],
thumb_url="https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png",
thumb_width=48,
thumb_height=48))
results = [
InlineQueryResultArticle(
id=str(i),
title=data[1][i],
description=data[1][i],
input_message=InputMessageContent(data[2][i]),
article_url=data[3][i],
thumb_url="https://upload.wikimedia.org/wikipedia/commons/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png",
thumb_width=48,
thumb_height=48,
)
for i in range(len(data[1]))
]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function on_inline_query refactored with the following changes:

async def buttons_handler(ctx: BotContext[CommandEvent]):
m = await ctx.bot.prepare_response_message(ctx.event.message)
m.message = f"Please select an option:"
m.message = "Please select an option:"
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function buttons_handler refactored with the following changes:

@app.on_community_update()
async def community_update_handler(ctx: BotContext[CommunityUpdatedEvent]):
print(ctx.event.community_id + " was updated")
print(f"{ctx.event.community_id} was updated")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function community_update_handler refactored with the following changes:

else:
res = in_s
return res
return in_s[len(lstr):] if in_s.startswith(lstr) else in_s
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _lstrip_str refactored with the following changes:

Comment on lines -97 to -101
subscription = await self.ws.subscribe(
return await self.ws.subscribe(
"/chat/queue/events",
callback=lambda event: callback(self._parse_event(event)),
)
return subscription
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ChatClient.subscribe_to_notifications refactored with the following changes:

Comment on lines -109 to +120
evt = self.build_object(MessageEvent, json_data)
# evt = MessageEvent.build_from_json(json_data)
return self.build_object(MessageEvent, json_data)
# evt = MessageEvent.build_from_json(json_data)
elif type == EventType.COMMAND.value:
evt = self.build_object(CommandEvent, json_data)
# evt = CommandEvent.build_from_json(json_data)
return self.build_object(CommandEvent, json_data)
# evt = CommandEvent.build_from_json(json_data)
elif type == EventType.CALLBACK_QUERY.value:
evt = self.build_object(CallbackQueryEvent, json_data)
# evt = CallbackQueryEvent.build_from_json(json_data)
return self.build_object(CallbackQueryEvent, json_data)
# evt = CallbackQueryEvent.build_from_json(json_data)
elif type == EventType.INLINE_QUERY.value:
evt = self.build_object(InlineQueryEvent, json_data)
return self.build_object(InlineQueryEvent, json_data)
else:
evt = self.build_object(ChatEvent, json_data)
# evt = ChatEvent.build_from_json(json_data)
return evt
return self.build_object(ChatEvent, json_data)
# evt = ChatEvent.build_from_json(json_data)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ChatClient._parse_event refactored with the following changes:

id = message.id
else:
id = message
id = message.id if isinstance(message, Message) else message
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MessageController.reply refactored with the following changes:

Comment on lines -138 to +135
if isinstance(message, Message):
id = message.id
else:
id = message

id = message.id if isinstance(message, Message) else message
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MessageController.reply_text refactored with the following changes:

Comment on lines -178 to +171
if isinstance(message, Message):
id = message.id
else:
id = message
id = message.id if isinstance(message, Message) else message
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MessageController.edit_message_text refactored with the following changes:

Comment on lines -196 to +186
if isinstance(message, Message):
id = message.id
else:
id = message
id = message.id if isinstance(message, Message) else message
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function MessageController.delete_message refactored with the following changes:

Comment on lines -57 to +72
def from_json(self, data: JSONDict) -> "Group":
def from_json(cls, data: JSONDict) -> "Group":
if data is not None:
self.id = data.get("groupId")
self.name = data.get("groupName")
self.community_id = data.get("communityId")
self.enabled_free = data.get("enabledFree")
self.enabled_public = data.get("enabledPublic")
self.default_group = data.get("defaultGroup")
self.is_public = data.get("isPublic")
self.created_by = data.get("createdBy")
self.icon = data.get("icon")
self.group_logo_url = data.get("groupLogoUrl")
self.allowed_content = data.get("allowedContent")
self.created_at = data.get("createdAt")
self.updated_at = data.get("updatedAt")
return self
cls.id = data.get("groupId")
cls.name = data.get("groupName")
cls.community_id = data.get("communityId")
cls.enabled_free = data.get("enabledFree")
cls.enabled_public = data.get("enabledPublic")
cls.default_group = data.get("defaultGroup")
cls.is_public = data.get("isPublic")
cls.created_by = data.get("createdBy")
cls.icon = data.get("icon")
cls.group_logo_url = data.get("groupLogoUrl")
cls.allowed_content = data.get("allowedContent")
cls.created_at = data.get("createdAt")
cls.updated_at = data.get("updatedAt")
return cls
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Group.from_json refactored with the following changes:

if data is None:
return None
return cls(app).from_json(data)
return None if data is None else cls(app).from_json(data)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function SwitchObject.build_from_json refactored with the following changes:

Comment on lines -30 to +56
if app.auto_update_bot:
"""Called when app start
if not app.auto_update_bot:
return
"""Called when app start
This method registers the bot commands and updates the bot info
"""
# get all app commands
commands = self.app._register_commands or []
description = self.app._bot_description or ""
# register the commands
self._info = BotInfo(description=description, id=self.id)
for command in commands:
command_name = command.command
if isinstance(command_name, str):
command_names = command_name.split(",")
else:
command_names = command_name

for c_name in command_names:
self.info.commands.append(
BotCommandInfo(
command=c_name,
description=command.description,
channel=command.channel,
)
# get all app commands
commands = self.app._register_commands or []
description = self.app._bot_description or ""
# register the commands
self._info = BotInfo(description=description, id=self.id)
for command in commands:
command_name = command.command
if isinstance(command_name, str):
command_names = command_name.split(",")
else:
command_names = command_name

for c_name in command_names:
self.info.commands.append(
BotCommandInfo(
command=c_name,
description=command.description,
channel=command.channel,
)
)

self.info = await self.update_bot_info(self.info)

pass
self.info = await self.update_bot_info(self.info)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Bot.on_app_start refactored with the following changes:

return True

return await self.other(ctx)
return True if r1 else await self.other(ctx)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function OrFilter.__call__ refactored with the following changes:

Comment on lines -83 to +80
return bool(
return (
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function self_filter refactored with the following changes:

return (
ctx.event.message is not None
and ctx.event.message.user_id == ctx.user.id
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function me_filter refactored with the following changes:

Comment on lines -112 to +115
return bool(ctx.event.message is not None and ctx.event.message.receiver_id == ctx.user.id)
return (
ctx.event.message is not None
and ctx.event.message.receiver_id == ctx.user.id
)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function incoming_filter refactored with the following changes:

Comment on lines -136 to +144
return bool(ctx.event.community_id is not None)
return ctx.event.community_id is not None
if isinstance(community_id, str):
community_id = frozenset({community_id})
else:
community_id = frozenset(community_id)
return bool(ctx.event.community_id in community_id)
return ctx.event.community_id in community_id
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function community.__call__ refactored with the following changes:

Comment on lines -153 to +161
return bool(ctx.event.channel_id is not None)
return ctx.event.channel_id is not None
if isinstance(channel_id, str):
channel_id = frozenset({channel_id})
else:
channel_id = frozenset(channel_id)
return bool(ctx.event.channel_id in channel_id)
return ctx.event.channel_id in channel_id
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function channel.__call__ refactored with the following changes:

Comment on lines -168 to +176
return bool(ctx.event.group_id is not None)
return ctx.event.group_id is not None
if isinstance(group_id, str):
group_id = frozenset({group_id})
else:
group_id = frozenset(group_id)
return bool(ctx.event.group_id in group_id)
return ctx.event.group_id in group_id
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function group.__call__ refactored with the following changes:

Comment on lines -13 to -15
if TYPE_CHECKING:
pass

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 13-15 refactored with the following changes:

Comment on lines -36 to +38
if (self.event_types is not None) and (not context.event.type in self.event_types):
if (
self.event_types is not None
and context.event.type not in self.event_types
):
return False
if self.filter:
return await self.filter(context)
return True
return await self.filter(context) if self.filter else True
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function EventHandler.should_handle refactored with the following changes:

  • Simplify logical expression using De Morgan identities (de-morgan)
  • Lift code into else after jump in control flow (reintroduce-else)
  • Replace if statement with if expression (assign-if-exp)

Comment on lines -10 to -12
if TYPE_CHECKING:
pass

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 10-12 refactored with the following changes:

Comment on lines -12 to -14
if TYPE_CHECKING:
pass

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12-14 refactored with the following changes:

Comment on lines -12 to -14
if TYPE_CHECKING:
pass

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 12-14 refactored with the following changes:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants