Skip to content

Sourcery refactored master branch#1

Open
sourcery-ai[bot] wants to merge 1 commit intomasterfrom
sourcery/master
Open

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

Conversation

@sourcery-ai
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot commented Dec 13, 2023

Branch master 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 master branch, then run:

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

Help us improve this pull request!

@sourcery-ai sourcery-ai Bot requested a review from Lo-pro-file December 13, 2023 19:36
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.

Sourcery timed out performing refactorings.

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

Comment thread setup.py
Comment on lines -16 to +18
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("#")
)
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 get_requirements refactored with the following changes:

Comment thread setup.py
Comment on lines -53 to +52
else:
name = name[:-4]
requirements["ext"].append(dependency)
name = name[:-4]
requirements["ext"].append(dependency)
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 get_optional_requirements refactored with the following changes:

Comment thread setup.py
Comment on lines -73 to +70
kwargs = dict(
return dict(
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 get_setup_kwargs refactored with the following changes:

Comment on lines -170 to +177
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:"
),
[],
)
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 AdmonitionInserter._create_available_in refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment on lines -361 to +381
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,
)
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 AdmonitionInserter._find_insert_pos_for_admonition refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment thread telegram/_bot.py
Comment on lines -2698 to +2701
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] = []
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._effective_inline_results refactored with the following changes:

Comment thread telegram/_chat.py
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
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 Chat.effective_name refactored with the following changes:

Comment thread telegram/_chat.py
Comment on lines -446 to +444
if self.username:
return f"https://t.me/{self.username}"
return None
return f"https://t.me/{self.username}" if self.username else None
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 Chat.link refactored with the following changes:

Comment on lines -538 to -545
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

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 TelegramObject._get_attrs refactored with the following changes:

Comment thread telegram/_user.py
if self.username:
return f"@{self.username}"
return self.full_name
return f"@{self.username}" if self.username else self.full_name
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 User.name refactored with the following changes:

Comment thread telegram/_user.py
Comment on lines -189 to +187
if self.username:
return f"https://t.me/{self.username}"
return None
return f"https://t.me/{self.username}" if self.username else None
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 User.link refactored with the following changes:

Comment thread telegram/helpers.py
Comment on lines -139 to +146
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,
)
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 effective_message_type refactored with the following changes:

  • Use the built-in function next instead of a for-loop (use-next)

Comment on lines -66 to +67
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
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 _BaseMedium.__init__ refactored with the following changes:

super().__init__(api_kwargs=api_kwargs)
# Required
self.phone_number: str = str(phone_number)
self.phone_number: str = phone_number
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 Contact.__init__ refactored with the following changes:

Comment thread telegram/_files/file.py
Comment on lines -96 to +97
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
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 File.__init__ refactored with the following changes:

Comment on lines -425 to +427
name = prefix + "request"
name = f"{prefix}request"
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 ApplicationBuilder._request_check refactored with the following changes:

Comment on lines +143 to +155
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
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 CallbackQueryHandler.check_update refactored with the following changes:

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)
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 ChatJoinRequestHandler._parse_chat_id refactored with the following changes:

Comment on lines -121 to -127
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
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 ChatJoinRequestHandler.check_update refactored with the following changes:

Comment on lines -156 to +163
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)
)
)
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 CommandHandler._check_correct_args refactored with the following changes:

Comment on lines -192 to +202
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):
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 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():
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 PendingState.resolve refactored with the following changes:


all_handlers: List[BaseHandler[Update, CCT]] = []
all_handlers.extend(entry_points)
all_handlers: List[BaseHandler[Update, CCT]] = list(entry_points)
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 ConversationHandler.__init__ refactored with the following changes:

Comment on lines -648 to +658
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

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 ConversationHandler._get_key refactored with the following changes:

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 ''}.",
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 ConversationHandler._update_state refactored with the following changes:

Comment thread telegram/ext/filters.py
Comment on lines -682 to +674
if isinstance(chat_id, int):
return {chat_id}
return set(chat_id)
return {chat_id} if isinstance(chat_id, int) else set(chat_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 _ChatUserBaseFilter._parse_chat_id refactored with the following changes:

Comment thread telegram/ext/filters.py
Comment on lines -793 to +783
chat_or_user = self._get_chat_or_user(message)
if chat_or_user:
if chat_or_user := self._get_chat_or_user(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 _ChatUserBaseFilter.filter refactored with the following changes:

Comment thread telegram/ext/filters.py
Comment on lines -971 to +964
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))
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 Command.filter refactored with the following changes:

Comment thread telegram/ext/filters.py
Comment on lines -1008 to +997
if self.values and emoji: # for filters.Dice.Dice(4) SLOT_MACHINE -> SlotMachine
if self.values: # for filters.Dice.Dice(4) SLOT_MACHINE -> SlotMachine
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 _Dice.__init__ refactored with the following changes:

Comment thread telegram/ext/filters.py
Comment on lines -1594 to +1583
if not isinstance(mention, str):
return mention
return mention.lstrip("@")
return mention if not isinstance(mention, str) else mention.lstrip("@")
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 Mention._fix_mention_username 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