Skip to content

Commit

Permalink
Update code style for new ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Jul 23, 2023
1 parent 87587a4 commit 949a042
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 36 deletions.
2 changes: 1 addition & 1 deletion bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def on_command_error(self, context: commands.Context, exception: DiscordEx
else:
await super().on_command_error(context, exception)

async def log_to_dev_log(self, title: str, details: str = None, *, icon: str = None) -> None:
async def log_to_dev_log(self, title: str, details: str | None = None, *, icon: str | None = None) -> None:
"""Send an embed message to the dev-log channel."""
devlog = self.get_channel(constants.Channels.devlog)

Expand Down
2 changes: 1 addition & 1 deletion bot/exts/core/internal_eval/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def visit_Expr(self, node: ast.Expr) -> ast.Expr | ast.Assign: # noqa: N802
log.trace("Found a trailing last expression in the evaluation code")

log.trace("Creating assignment statement with trailing expression as the right-hand side")
right_hand_side = list(ast.iter_child_nodes(node))[0]
right_hand_side = next(iter(ast.iter_child_nodes(node)))

assignment = ast.Assign(
targets=[ast.Name(id="_value_last_expression", ctx=ast.Store())],
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/events/hacktoberfest/hacktoberstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, bot: Bot):

@in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER)
@commands.group(name="hacktoberstats", aliases=("hackstats",), invoke_without_command=True)
async def hacktoberstats_group(self, ctx: commands.Context, github_username: str = None) -> None:
async def hacktoberstats_group(self, ctx: commands.Context, github_username: str | None = None) -> None:
"""
Display an embed for a user's Hacktoberfest contributions.
Expand All @@ -70,7 +70,7 @@ async def hacktoberstats_group(self, ctx: commands.Context, github_username: str

@in_month(Month.SEPTEMBER, Month.OCTOBER, Month.NOVEMBER)
@hacktoberstats_group.command(name="link")
async def link_user(self, ctx: commands.Context, github_username: str = None) -> None:
async def link_user(self, ctx: commands.Context, github_username: str | None = None) -> None:
"""
Link the invoking user's Github github_username to their Discord ID.
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/events/trivianight/_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def __init__(self, data: list[QuestionData]) -> None:
def __iter__(self) -> Iterable[Question]:
return iter(self._questions)

def next_question(self, number: str = None) -> Question:
def next_question(self, number: str | None = None) -> Question:
"""
Consume one random question from the trivia night game.
Expand All @@ -145,7 +145,7 @@ def next_question(self, number: str = None) -> Question:

if number is not None:
try:
question = [q for q in self._all_questions if q.number == int(number)][0]
question = next(q for q in self._all_questions if q.number == int(number))
except IndexError:
raise ValueError(f"Question number {number} does not exist.")
elif len(self._questions) == 0:
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/events/trivianight/_scoreboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, bot: Bot):
self._points = {}
self._speed = {}

def assign_points(self, user_id: int, *, points: int = None, speed: float = None) -> None:
def assign_points(self, user_id: int, *, points: int | None = None, speed: float | None = None) -> None:
"""
Assign points or deduct points to/from a certain user.
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/events/trivianight/trivianight.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async def load(self, ctx: commands.Context, *, to_load: str | None) -> None:

@trivianight.command(aliases=("next",))
@commands.has_any_role(*TRIVIA_NIGHT_ROLES)
async def question(self, ctx: commands.Context, question_number: str = None) -> None:
async def question(self, ctx: commands.Context, question_number: str | None = None) -> None:
"""
Gets a random question from the unanswered question list and lets the user(s) choose the answer.
Expand Down
16 changes: 7 additions & 9 deletions bot/exts/fun/snakes/_snakes_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,8 @@ async def _fetch(self, url: str, params: dict | None = None) -> dict:
if params is None:
params = {}

async with async_timeout.timeout(10):
async with self.bot.http_session.get(url, params=params) as response:
return await response.json()
async with async_timeout.timeout(10), self.bot.http_session.get(url, params=params) as response:
return await response.json()

def _get_random_long_message(self, messages: list[str], retries: int = 10) -> str:
"""
Expand Down Expand Up @@ -846,7 +845,7 @@ async def quiz_command(self, ctx: Context) -> None:
await self._validate_answer(ctx, quiz, answer, options)

@snakes_group.command(name="name", aliases=("name_gen",))
async def name_command(self, ctx: Context, *, name: str = None) -> None:
async def name_command(self, ctx: Context, *, name: str | None = None) -> None:
"""
Snakifies a username.
Expand Down Expand Up @@ -1001,9 +1000,8 @@ async def card_command(self, ctx: Context, *, name: Snake = None) -> None:
async with ctx.typing():

stream = BytesIO()
async with async_timeout.timeout(10):
async with self.bot.http_session.get(content["image_list"][0]) as response:
stream.write(await response.read())
async with async_timeout.timeout(10), self.bot.http_session.get(content["image_list"][0]) as response:
stream.write(await response.read())

stream.seek(0)

Expand Down Expand Up @@ -1033,7 +1031,7 @@ async def fact_command(self, ctx: Context) -> None:
await ctx.send(embed=embed)

@snakes_group.command(name="snakify")
async def snakify_command(self, ctx: Context, *, message: str = None) -> None:
async def snakify_command(self, ctx: Context, *, message: str | None = None) -> None:
"""
How would I talk if I were a snake?
Expand Down Expand Up @@ -1068,7 +1066,7 @@ async def snakify_command(self, ctx: Context, *, message: str = None) -> None:
await ctx.send(embed=embed)

@snakes_group.command(name="video", aliases=("get_video",))
async def video_command(self, ctx: Context, *, search: str = None) -> None:
async def video_command(self, ctx: Context, *, search: str | None = None) -> None:
"""
Gets a YouTube video about snakes.
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/holidays/halloween/monstersurvey.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def monster_group(self, ctx: Context) -> None:
@monster_group.command(
name="vote"
)
async def monster_vote(self, ctx: Context, name: str = None) -> None:
async def monster_vote(self, ctx: Context, name: str | None = None) -> None:
"""
Cast a vote for a particular monster.
Expand Down Expand Up @@ -141,7 +141,7 @@ async def monster_vote(self, ctx: Context, name: str = None) -> None:
@monster_group.command(
name="show"
)
async def monster_show(self, ctx: Context, name: str = None) -> None:
async def monster_show(self, ctx: Context, name: str | None = None) -> None:
"""Shows the named monster. If one is not named, it sends the default voting embed instead."""
if name is None:
await ctx.invoke(self.monster_leaderboard)
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/holidays/pride/pride_anthem.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_video(self, genre: str | None = None) -> dict:
log.info("No videos for that genre.")

@commands.command(name="prideanthem", aliases=("anthem", "pridesong"))
async def prideanthem(self, ctx: commands.Context, genre: str = None) -> None:
async def prideanthem(self, ctx: commands.Context, genre: str | None = None) -> None:
"""
Sends a message with a video of a random pride anthem.
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/holidays/pride/pride_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def send_select_fact(self, target: discord.abc.Messageable, day_num: int)
return

@commands.command(name="pridefact", aliases=("pridefacts",))
async def pridefact(self, ctx: commands.Context, option: int | str = None) -> None:
async def pridefact(self, ctx: commands.Context, option: int | str | None = None) -> None:
"""
Sends a message with a pride fact of the day.
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/holidays/pride/pride_leader.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def invalid_embed_generate(self, pride_leader: str) -> discord.Embed:

def embed_builder(self, pride_leader: dict) -> discord.Embed:
"""Generate an Embed with information about a pride leader."""
name = [name for name, info in PRIDE_RESOURCE.items() if info == pride_leader][0]
name = next(name for name, info in PRIDE_RESOURCE.items() if info == pride_leader)

embed = discord.Embed(
title=name,
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/holidays/valentines/be_my_valentine.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def lovefest_role(self, ctx: commands.Context) -> None:
@commands.cooldown(1, 1800, commands.BucketType.user)
@commands.group(name="bemyvalentine", invoke_without_command=True)
async def send_valentine(
self, ctx: commands.Context, user: discord.Member, *, valentine_type: str = None
self, ctx: commands.Context, user: discord.Member, *, valentine_type: str | None = None
) -> None:
"""
Send a valentine to a specified user with the lovefest role.
Expand Down Expand Up @@ -83,7 +83,7 @@ async def send_valentine(
@commands.cooldown(1, 1800, commands.BucketType.user)
@send_valentine.command(name="secret")
async def anonymous(
self, ctx: commands.Context, user: discord.Member, *, valentine_type: str = None
self, ctx: commands.Context, user: discord.Member, *, valentine_type: str | None = None
) -> None:
"""
Send an anonymous Valentine via DM to to a specified user with the lovefest role.
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/holidays/valentines/myvalenstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def levenshtein(self, source: str, goal: str) -> int:
return pre_row[-1]

@commands.command()
async def myvalenstate(self, ctx: commands.Context, *, name: str = None) -> None:
async def myvalenstate(self, ctx: commands.Context, *, name: str | None = None) -> None:
"""Find the vacation spot(s) with the most matching characters to the invoking user."""
eq_chars = collections.defaultdict(int)
if name is None:
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/utilities/challenges.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def create_view(dropdown: InformationDropdown, link: str) -> ui.View:

@commands.command(aliases=["kata"])
@commands.cooldown(1, 5, commands.BucketType.user)
async def challenge(self, ctx: commands.Context, language: str = "python", *, query: str = None) -> None:
async def challenge(self, ctx: commands.Context, language: str = "python", *, query: str | None = None) -> None:
"""
The challenge command pulls a random kata (challenge) from codewars.com.
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/utilities/colour.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def _rgb_to_name(self, rgb: tuple[int, int, int]) -> str | None:
choices=self.colour_mapping.values(),
score_cutoff=80
)
colour_name = [name for name, hex_code in self.colour_mapping.items() if hex_code == match][0]
colour_name = next(name for name, hex_code in self.colour_mapping.items() if hex_code == match)
except TypeError:
colour_name = None
return colour_name
Expand Down
2 changes: 1 addition & 1 deletion bot/exts/utilities/emoji.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def emoji_group(self, ctx: commands.Context, emoji: Emoji | None) -> None:
await self.bot.invoke_help_command(ctx)

@emoji_group.command(name="count", aliases=("c",))
async def count_command(self, ctx: commands.Context, *, category_query: str = None) -> None:
async def count_command(self, ctx: commands.Context, *, category_query: str | None = None) -> None:
"""Returns embed with emoji category and info given by the user."""
emoji_dict = defaultdict(list)

Expand Down
2 changes: 1 addition & 1 deletion bot/exts/utilities/reddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async def revoke_access_token(self) -> None:
else:
log.warning(f"Unable to revoke access token: status {response.status}.")

async def fetch_posts(self, route: str, *, amount: int = 25, params: dict = None) -> list[dict]:
async def fetch_posts(self, route: str, *, amount: int = 25, params: dict | None = None) -> list[dict]:
"""A helper method to fetch a certain amount of Reddit posts at a given route."""
# Reddit's JSON responses only provide 25 posts at most.
if not 25 >= amount > 0:
Expand Down
4 changes: 2 additions & 2 deletions bot/exts/utilities/wolfram.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ async def send_embed(
ctx: Context,
message_txt: str,
colour: int = Colours.soft_red,
footer: str = None,
img_url: str = None,
footer: str | None = None,
img_url: str | None = None,
f: discord.File = None
) -> None:
"""Generate & send a response embed with Wolfram as the author."""
Expand Down
4 changes: 2 additions & 2 deletions bot/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def check(message: discord.Message) -> bool:
done, pending = await asyncio.wait(futures, return_when=asyncio.FIRST_COMPLETED, loop=ctx.bot.loop)

# :yert:
result = list(done)[0].result()
result = next(iter(done)).result()

# Pagination was canceled - result is None
if result is None:
Expand All @@ -87,7 +87,7 @@ def check(message: discord.Message) -> bool:
# Pagination was not initiated, only one page
if result.author == ctx.bot.user:
# Continue the wait_for
result = await list(pending)[0]
result = await next(iter(pending))

# Love that duplicate code
for coro in pending:
Expand Down
6 changes: 3 additions & 3 deletions bot/utils/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ async def paginate(
cls, lines: Iterable[str], ctx: Context,
embed: Embed, prefix: str = "", suffix: str = "",
max_lines: int | None = None, max_size: int = 500, empty: bool = True,
restrict_to_user: User = None, timeout: int = 300, footer_text: str = None,
url: str = None, exception_on_empty_embed: bool = False
restrict_to_user: User = None, timeout: int = 300, footer_text: str | None = None,
url: str | None = None, exception_on_empty_embed: bool = False
) -> None:
"""
Use a paginator and set of reactions to provide pagination over a set of lines.
Expand Down Expand Up @@ -302,7 +302,7 @@ def add_line(self, line: str = "", *, empty: bool = False) -> None:
self._current_page.append(line)
self.close_page()

def add_image(self, image: str = None) -> None:
def add_image(self, image: str | None = None) -> None:
"""Adds an image to a page given the url."""
self.images.append(image)

Expand Down

0 comments on commit 949a042

Please sign in to comment.