-
-
Notifications
You must be signed in to change notification settings - Fork 7
Sourcery refactored main branch #3
base: main
Are you sure you want to change the base?
Conversation
8df69b5
to
f8d57b6
Compare
There was a problem hiding this 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.
sw = spamwatch.Client(spamwatch_api) | ||
return sw | ||
return spamwatch.Client(spamwatch_api) | ||
except: | ||
sw = None | ||
log.warning("Can't connect to SpamWatch!") | ||
return sw | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function KyosukeINIT.init_sw
refactored with the following changes:
- Inline variable that is immediately returned [×2] (
inline-immediately-returned-variable
) - Move assignment closer to its usage within a block (
move-assign-in-block
)
apps = [] | ||
apps.append(pbot) | ||
apps = [pbot] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 273-274
refactored with the following changes:
- Merge append into list declaration (
merge-list-append
)
imported_module = importlib.import_module("kyosuke.modules." + module_name) | ||
imported_module = importlib.import_module(f"kyosuke.modules.{module_name}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 87-87
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
url="t.me/{}?startgroup=true".format( | ||
context.bot.username | ||
), | ||
), | ||
url=f"t.me/{context.bot.username}?startgroup=true", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function start
refactored with the following changes:
- Replace call to format with f-string [×3] (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Replace f-string with no interpolated values with string (
remove-redundant-fstring
) - Replace m.group(x) with m[x] for re.Match objects [×3] (
use-getitem-for-re-match-groups
)
module = mod_match.group(1) | ||
module = mod_match[1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function help_button
refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects [×3] (
use-getitem-for-re-match-groups
) - Replace call to format with f-string (
use-fstring-for-formatting
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Replace f-string with no interpolated values with string (
remove-redundant-fstring
)
args["pattern"] = "(?i)" + pattern | ||
args["pattern"] = f"(?i){pattern}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function inlinequery
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
args["pattern"] = "(?i)" + pattern | ||
args["pattern"] = f"(?i){pattern}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function bot
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
) - Swap if/else to remove empty if body [×2] (
remove-pass-body
) - Remove redundant pass statement (
remove-redundant-pass
)
unsafe_pattern = r"^[^/!#@\$A-Za-z]" | ||
group_only = args.get("group_only", False) | ||
disable_errors = args.get("disable_errors", False) | ||
insecure = args.get("insecure", False) | ||
if pattern is not None and not pattern.startswith("(?i)"): | ||
args["pattern"] = "(?i)" + pattern | ||
args["pattern"] = f"(?i){pattern}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function kyosuke
refactored with the following changes:
- Move assignments closer to their usage (
move-assign
) - Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
if not obj: | ||
return False | ||
string = codecs.encode(pickle.dumps(obj), "base64").decode() | ||
return string | ||
return codecs.encode(pickle.dumps(obj), "base64").decode() if obj else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function obj_to_str
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Lift code into else after jump in control flow (
reintroduce-else
) - Swap if/else branches (
swap-if-else-branches
) - Replace if statement with if expression (
assign-if-exp
)
obj = pickle.loads(codecs.decode(string.encode(), "base64")) | ||
return obj | ||
return pickle.loads(codecs.decode(string.encode(), "base64")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function str_to_obj
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not _notes: | ||
return {} | ||
return _notes["notes"] | ||
return {} if not _notes else _notes["notes"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function _get_notes
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
_notes = [] | ||
for note in await _get_notes(chat_id): | ||
_notes.append(note) | ||
return _notes | ||
return list(await _get_notes(chat_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_note_names
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if name in _notes: | ||
return _notes[name] | ||
return False | ||
return _notes[name] if name in _notes else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_note
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if not _filters: | ||
return {} | ||
return _filters["filters"] | ||
return {} if not _filters else _filters["filters"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function _get_filters
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
_filters = [] | ||
for _filter in await _get_filters(chat_id): | ||
_filters.append(_filter) | ||
return _filters | ||
return list(await _get_filters(chat_id)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_filters_names
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not chats: | ||
return [] | ||
chats_list = [] | ||
for chat in await chats.to_list(length=1000000000): | ||
chats_list.append(chat) | ||
return chats_list | ||
return [] if not chats else list(await chats.to_list(length=1000000000)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_served_chats
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not user: | ||
return False | ||
return True | ||
return bool(user) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_served_user
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
)
if not users: | ||
return [] | ||
users_list = [] | ||
for user in await users.to_list(length=1000000000): | ||
users_list.append(user) | ||
return users_list | ||
return [] if not users else list(await users.to_list(length=1000000000)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_served_users
refactored with the following changes:
- Convert for loop into list comprehension (
list-comprehension
) - Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Replace identity comprehension with call to collection constructor (
identity-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not user: | ||
return False | ||
return True | ||
return bool(user) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_gbanned_user
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
)
if not lovers: | ||
return {} | ||
return lovers["couple"] | ||
return {} if not lovers else lovers["couple"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function _get_lovers
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if date in lovers: | ||
return lovers[date] | ||
return False | ||
return lovers[date] if date in lovers else False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_couple
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if not chat: | ||
return True | ||
return False | ||
return not chat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_captcha_on
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if not chat: | ||
return True | ||
return False | ||
return not chat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_antiservice_on
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if not user: | ||
return False | ||
return True | ||
return bool(user) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function is_pmpermit_approved
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
) - Simplify boolean if expression (
boolean-if-exp-identity
)
if not text: | ||
return "" | ||
return text["text"] | ||
return "" if not text else text["text"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function get_welcome
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
kyosuke/modules/admin.py
Outdated
@rencmd(command="promote", pass_args=True) | ||
@rencmd(command="promote", pass_args=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function promote
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
kyosuke/modules/admin.py
Outdated
log_message = ( | ||
f"<b>{html.escape(chat.title)}:</b>\n" | ||
f"#LOWPROMOTED\n" | ||
f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" | ||
f"<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" | ||
) | ||
|
||
return log_message | ||
return f"<b>{html.escape(chat.title)}:</b>\n#LOWPROMOTED\n<b>Admin:</b> {mention_html(user.id, user.first_name)}\n<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function lowpromote
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
keyboard = InlineKeyboardMarkup([[ | ||
InlineKeyboardButton( | ||
"Demote", callback_data="demote_({})".format(user_member.user.id)) | ||
]]) | ||
keyboard = InlineKeyboardMarkup( | ||
[ | ||
[ | ||
InlineKeyboardButton( | ||
"Demote", callback_data=f"demote_({user_member.user.id})" | ||
) | ||
] | ||
] | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function fullpromote
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
if not user_member.status == "administrator": | ||
if user_member.status != "administrator": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function demote
refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
)
kyosuke/modules/admin.py
Outdated
is_group = chat.type != "private" and chat.type != "channel" | ||
is_group = chat.type not in ["private", "channel"] | ||
prev_message = update.effective_message.reply_to_message | ||
|
||
if prev_message is None: | ||
msg.reply_text("Reply a message to pin it!") | ||
return | ||
|
||
is_silent = True | ||
if len(args) >= 1: | ||
if prev_message and is_group: | ||
is_silent = ( | ||
args[0].lower() != "notify" | ||
or args[0].lower() == "loud" | ||
or args[0].lower() == "violent" | ||
( | ||
args[0].lower() != "notify" | ||
or args[0].lower() == "loud" | ||
or args[0].lower() == "violent" | ||
) | ||
if len(args) >= 1 | ||
else True | ||
) | ||
|
||
if prev_message and is_group: | ||
try: | ||
bot.pinChatMessage( | ||
chat.id, prev_message.message_id, disable_notification=is_silent | ||
) | ||
msg.reply_text( | ||
f"I have pinned a message.", | ||
"I have pinned a message.", | ||
reply_markup=InlineKeyboardMarkup( | ||
[ | ||
[ | ||
InlineKeyboardButton( | ||
"👀 Go to message", url=f"{message_link}") | ||
"👀 Go to message", url=f"{message_link}" | ||
) | ||
] | ||
] | ||
), | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function pin
refactored with the following changes:
- Replace multiple comparisons of same variable with
in
operator (merge-comparisons
) - Move setting of default value for variable into
else
branch (introduce-default-else
) - Replace f-string with no interpolated values with string (
remove-redundant-fstring
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Replace if statement with if expression (
assign-if-exp
) - Move assignments closer to their usage (
move-assign
)
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:Help us improve this pull request!