Sourcery refactored master branch#1
Conversation
| for install in reqs: | ||
| if install.startswith("#"): | ||
| continue | ||
| requirements_list.append(install.strip()) | ||
|
|
||
| requirements_list.extend( | ||
| install.strip() for install in reqs if not install.startswith("#") | ||
| ) |
There was a problem hiding this comment.
Function get_requirements refactored with the following changes:
- Replace a for append loop with list extend (
for-append-to-extend) - Lift code into else after jump in control flow (
reintroduce-else) - Remove redundant continue statement (
remove-redundant-continue)
| else: | ||
| name = name[:-4] | ||
| requirements["ext"].append(dependency) | ||
| name = name[:-4] | ||
| requirements["ext"].append(dependency) |
There was a problem hiding this comment.
Function get_optional_requirements refactored with the following changes:
- Remove unnecessary else after guard condition (
remove-unnecessary-else)
| kwargs = dict( | ||
| return dict( |
There was a problem hiding this comment.
Function get_setup_kwargs refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| lines_with_attrs = [] | ||
| for idx, line in enumerate(docstring_lines): | ||
| if line.strip() == "Attributes:": | ||
| lines_with_attrs = docstring_lines[idx + 1 :] | ||
| break | ||
|
|
||
| lines_with_attrs = next( | ||
| ( | ||
| docstring_lines[idx + 1 :] | ||
| for idx, line in enumerate(docstring_lines) | ||
| if line.strip() == "Attributes:" | ||
| ), | ||
| [], | ||
| ) |
There was a problem hiding this comment.
Function AdmonitionInserter._create_available_in refactored with the following changes:
- Use the built-in function
nextinstead of a for-loop (use-next)
| for idx, value in list(enumerate(lines)): | ||
| if ( | ||
| value.startswith(".. seealso:") | ||
| # The docstring contains heading "Examples:", but Sphinx will have it converted | ||
| # to ".. admonition: Examples": | ||
| or value.startswith(".. admonition:: Examples") | ||
| or value.startswith(".. version") | ||
| # The space after ":param" is important because docstring can contain ":paramref:" | ||
| # in its plain text in the beginning of a line (e.g. ExtBot): | ||
| or value.startswith(":param ") | ||
| # some classes (like "Credentials") have no params, so insert before attrs: | ||
| or value.startswith(".. attribute::") | ||
| ): | ||
| return idx | ||
| return len(lines) - 1 | ||
| return next( | ||
| ( | ||
| idx | ||
| for idx, value in list(enumerate(lines)) | ||
| if ( | ||
| value.startswith(".. seealso:") | ||
| # The docstring contains heading "Examples:", but Sphinx will have it converted | ||
| # to ".. admonition: Examples": | ||
| or value.startswith(".. admonition:: Examples") | ||
| or value.startswith(".. version") | ||
| # The space after ":param" is important because docstring can contain ":paramref:" | ||
| # in its plain text in the beginning of a line (e.g. ExtBot): | ||
| or value.startswith(":param ") | ||
| # some classes (like "Credentials") have no params, so insert before attrs: | ||
| or value.startswith(".. attribute::") | ||
| ) | ||
| ), | ||
| len(lines) - 1, | ||
| ) |
There was a problem hiding this comment.
Function AdmonitionInserter._find_insert_pos_for_admonition refactored with the following changes:
- Use the built-in function
nextinstead of a for-loop (use-next)
| callable_output = results(current_offset_int) | ||
| if not callable_output: | ||
| effective_results: Sequence[InlineQueryResult] = [] | ||
| else: | ||
| if callable_output := results(current_offset_int): | ||
| effective_results = callable_output | ||
| # the callback *might* return more results on the next call, so we increment | ||
| # the page count | ||
| next_offset = str(current_offset_int + 1) | ||
|
|
||
| else: | ||
| effective_results: Sequence[InlineQueryResult] = [] |
There was a problem hiding this comment.
Function Bot._effective_inline_results refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Swap if/else branches (
swap-if-else-branches)
| if self.full_name is not None: | ||
| return self.full_name | ||
| return None | ||
| return self.full_name if self.full_name is not None else None |
There was a problem hiding this comment.
Function Chat.effective_name 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 self.username: | ||
| return f"https://t.me/{self.username}" | ||
| return None | ||
| return f"https://t.me/{self.username}" if self.username else None |
There was a problem hiding this comment.
Function Chat.link 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 value is not None: | ||
| if recursive and hasattr(value, "to_dict"): | ||
| data[key] = value.to_dict(recursive=True) | ||
| else: | ||
| data[key] = value | ||
| elif not recursive: | ||
| if value is not None and recursive and hasattr(value, "to_dict"): | ||
| data[key] = value.to_dict(recursive=True) | ||
| elif value is not None or not recursive: | ||
| data[key] = value | ||
|
|
There was a problem hiding this comment.
Function TelegramObject._get_attrs refactored with the following changes:
- Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Remove redundant conditional (
remove-redundant-if)
| if self.username: | ||
| return f"@{self.username}" | ||
| return self.full_name | ||
| return f"@{self.username}" if self.username else self.full_name |
There was a problem hiding this comment.
Function User.name 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 self.username: | ||
| return f"https://t.me/{self.username}" | ||
| return None | ||
| return f"https://t.me/{self.username}" if self.username else None |
There was a problem hiding this comment.
Function User.link 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)
| for message_type in MessageType: | ||
| if message[message_type]: | ||
| return message_type | ||
|
|
||
| return None | ||
| return next( | ||
| ( | ||
| message_type | ||
| for message_type in MessageType | ||
| if message[message_type] | ||
| ), | ||
| None, | ||
| ) |
There was a problem hiding this comment.
Function effective_message_type refactored with the following changes:
- Use the built-in function
nextinstead of a for-loop (use-next)
| self.file_id: str = str(file_id) | ||
| self.file_unique_id: str = str(file_unique_id) | ||
| self.file_id: str = file_id | ||
| self.file_unique_id: str = file_unique_id |
There was a problem hiding this comment.
Function _BaseMedium.__init__ refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast)
| super().__init__(api_kwargs=api_kwargs) | ||
| # Required | ||
| self.phone_number: str = str(phone_number) | ||
| self.phone_number: str = phone_number |
There was a problem hiding this comment.
Function Contact.__init__ refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast)
| self.file_id: str = str(file_id) | ||
| self.file_unique_id: str = str(file_unique_id) | ||
| self.file_id: str = file_id | ||
| self.file_unique_id: str = file_unique_id |
There was a problem hiding this comment.
Function File.__init__ refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast)
| name = prefix + "request" | ||
| name = f"{prefix}request" |
There was a problem hiding this comment.
Function ApplicationBuilder._request_check refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| if not self.pattern: | ||
| return True | ||
| callback_data = update.callback_query.data | ||
| if callback_data is None: | ||
| return False | ||
| if isinstance(self.pattern, type): | ||
| return isinstance(callback_data, self.pattern) | ||
| if callable(self.pattern): | ||
| return self.pattern(callback_data) | ||
| if not isinstance(callback_data, str): | ||
| return False | ||
| if match := re.match(self.pattern, callback_data): | ||
| return match |
There was a problem hiding this comment.
Function CallbackQueryHandler.check_update refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else)
| if isinstance(chat_id, int): | ||
| return frozenset({chat_id}) | ||
| return frozenset(chat_id) | ||
| return frozenset({chat_id}) if isinstance(chat_id, int) else frozenset(chat_id) |
There was a problem hiding this comment.
Function ChatJoinRequestHandler._parse_chat_id 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 self._chat_ids and not self._usernames: | ||
| if self._chat_ids or self._usernames: | ||
| return ( | ||
| True | ||
| if update.chat_join_request.chat.id in self._chat_ids | ||
| else update.chat_join_request.from_user.username | ||
| in self._usernames | ||
| ) | ||
| else: | ||
| return True | ||
| if update.chat_join_request.chat.id in self._chat_ids: | ||
| return True | ||
| if update.chat_join_request.from_user.username in self._usernames: | ||
| return True | ||
| return False |
There was a problem hiding this comment.
Function ChatJoinRequestHandler.check_update refactored with the following changes:
- Lift code into else after jump in control flow [×3] (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches) - Split conditional into multiple branches (
split-or-ifs) - Merge duplicate blocks in conditional (
merge-duplicate-blocks) - Replace if statement with if expression [×2] (
assign-if-exp) - Simplify boolean if expression (
boolean-if-exp-identity) - Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast) - Remove redundant conditional (
remove-redundant-if)
| if ( | ||
| (self.has_args is None) | ||
| or (self.has_args is True and args) | ||
| or (self.has_args is False and not args) | ||
| or (isinstance(self.has_args, int) and len(args) == self.has_args) | ||
| ): | ||
| return True | ||
| return False | ||
| return bool( | ||
| ( | ||
| (self.has_args is None) | ||
| or (self.has_args is True and args) | ||
| or (self.has_args is False and not args) | ||
| or (isinstance(self.has_args, int) and len(args) == self.has_args) | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Function CommandHandler._check_correct_args 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 ( | ||
| command_parts[0].lower() in self.commands | ||
| and command_parts[1].lower() == message.get_bot().username.lower() | ||
| if ( | ||
| command_parts[0].lower() not in self.commands | ||
| or command_parts[1].lower() | ||
| != message.get_bot().username.lower() | ||
| ): | ||
| return None | ||
|
|
||
| if not self._check_correct_args(args): | ||
| return None | ||
|
|
||
| filter_result = self.filters.check_update(update) | ||
| if filter_result: | ||
| if filter_result := self.filters.check_update(update): |
There was a problem hiding this comment.
Function CommandHandler.check_update refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Simplify logical expression using De Morgan identities (
de-morgan)
|
|
||
| exc = self.task.exception() | ||
| if exc: | ||
| if exc := self.task.exception(): |
There was a problem hiding this comment.
Function PendingState.resolve refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Lift repeated conditional into its own if statement (
lift-duplicated-conditional)
|
|
||
| all_handlers: List[BaseHandler[Update, CCT]] = [] | ||
| all_handlers.extend(entry_points) | ||
| all_handlers: List[BaseHandler[Update, CCT]] = list(entry_points) |
There was a problem hiding this comment.
Function ConversationHandler.__init__ refactored with the following changes:
- Merge extend into list declaration (
merge-list-extend)
| chat = update.effective_chat | ||
| user = update.effective_user | ||
|
|
||
| key: List[Union[int, str]] = [] | ||
|
|
||
| if self.per_chat: | ||
| chat = update.effective_chat | ||
| if chat is None: | ||
| raise RuntimeError("Can't build key for update without effective chat!") | ||
| key.append(chat.id) | ||
| else: | ||
| key.append(chat.id) | ||
|
|
||
| if self.per_user: | ||
| user = update.effective_user | ||
|
|
There was a problem hiding this comment.
Function ConversationHandler._get_key refactored with the following changes:
- Move assignments closer to their usage [×2] (
move-assign) - Lift code into else after jump in control flow (
reintroduce-else)
| f"{repr(handler.callback.__name__) if handler is not None else 'BaseHandler'} " | ||
| f"returned state {new_state} which is unknown to the " | ||
| f"ConversationHandler{' ' + self.name if self.name is not None else ''}.", | ||
| f"{repr(handler.callback.__name__) if handler is not None else 'BaseHandler'} returned state {new_state} which is unknown to the ConversationHandler{f' {self.name}' if self.name is not None else ''}.", |
There was a problem hiding this comment.
Function ConversationHandler._update_state refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation)
| if isinstance(chat_id, int): | ||
| return {chat_id} | ||
| return set(chat_id) | ||
| return {chat_id} if isinstance(chat_id, int) else set(chat_id) |
There was a problem hiding this comment.
Function _ChatUserBaseFilter._parse_chat_id 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)
| chat_or_user = self._get_chat_or_user(message) | ||
| if chat_or_user: | ||
| if chat_or_user := self._get_chat_or_user(message): |
There was a problem hiding this comment.
Function _ChatUserBaseFilter.filter refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| first = message.entities[0] | ||
|
|
||
| if self.only_start: | ||
| return bool(first.type == MessageEntity.BOT_COMMAND and first.offset == 0) | ||
| return bool(any(e.type == MessageEntity.BOT_COMMAND for e in message.entities)) | ||
| first = message.entities[0] | ||
|
|
||
| return first.type == MessageEntity.BOT_COMMAND and first.offset == 0 | ||
| return any((e.type == MessageEntity.BOT_COMMAND for e in message.entities)) |
There was a problem hiding this comment.
Function Command.filter refactored with the following changes:
- Move assignments closer to their usage (
move-assign) - Remove unnecessary casts to int, str, float or bool [×2] (
remove-unnecessary-cast)
| if self.values and emoji: # for filters.Dice.Dice(4) SLOT_MACHINE -> SlotMachine | ||
| if self.values: # for filters.Dice.Dice(4) SLOT_MACHINE -> SlotMachine |
There was a problem hiding this comment.
Function _Dice.__init__ refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| if not isinstance(mention, str): | ||
| return mention | ||
| return mention.lstrip("@") | ||
| return mention if not isinstance(mention, str) else mention.lstrip("@") |
There was a problem hiding this comment.
Function Mention._fix_mention_username 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)
Branch
masterrefactored 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
masterbranch, then run:Help us improve this pull request!