-
Notifications
You must be signed in to change notification settings - Fork 140
refactor(lint): add flake8-return rules #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
e60e403
to
8dc64aa
Compare
3a17a35
to
5f2fbac
Compare
5f2fbac
to
7b70e74
Compare
7b70e74
to
f00238b
Compare
some of these were inadvertently fixed in the RET505 commit
f00238b
to
73e5905
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.
Seems alright overall, though I have to admit I'm not a fan of rewriting elif: return ...
chains to if:
when they all contain return
s (RET505
). Some examples:
- https://github.com/DisnakeDev/disnake/pull/948/files#diff-f01ce824ca379b31cd2020f1a9e1c746848a67a012afb7808cfad5f6d1bee062L495-R499
- https://github.com/DisnakeDev/disnake/pull/948/files#diff-3576d5b144cbf52422d024d3aa0d6353a3d44548b6c691226c97df1024d9f440R947
- https://github.com/DisnakeDev/disnake/pull/948/files#diff-9193cf57ab5b9cff2bcd207093171c901346772ab36b595b8f76481ca79a8134L491-R499
They compile down to the same bytecode and all that, but make the code more difficult to read ("does the previous if
always return or can it fall through?") and maintain (the links above and afonasev/flake8-return#128 outline this quite well).
Rewriting if + elif + else
is mostly fine, but once you're dealing with multiple elif
s, rewriting them all to if
s makes things worse and feels dangerous considering future changes, imho.
@@ -192,7 +192,7 @@ async def __timeout_task_impl(self) -> None: | |||
while True: | |||
# Guard just in case someone changes the value of the timeout at runtime | |||
if self.timeout is None: | |||
return | |||
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.
This doesn't need an explicit None
, all the return x()
in this method are shortcuts for x(); return
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.
okay, so i'll change the other two to
x()
return
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.
sounds good to me.
# the payload will always be the proper channel payload | ||
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore | ||
|
||
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.
nit: the spacing here seems weird, whitespace above the if
would make more sense, i.e.
**kwargs,
)
if payload is not None:
# the payload will always be the proper channel payload
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore
return None
value = try_enum(ChannelType, channel_type) | ||
if value is ChannelType.text: | ||
return TextChannel, value | ||
elif value is ChannelType.voice: | ||
if value is ChannelType.voice: | ||
return VoiceChannel, value | ||
elif value is ChannelType.category: | ||
if value is ChannelType.category: | ||
return CategoryChannel, value | ||
elif value is ChannelType.news: | ||
if value is ChannelType.news: | ||
return TextChannel, value | ||
elif value is ChannelType.stage_voice: | ||
if value is ChannelType.stage_voice: | ||
return StageChannel, value | ||
elif value is ChannelType.forum: | ||
if value is ChannelType.forum: | ||
return ForumChannel, value | ||
else: | ||
return None, value | ||
return None, value |
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.
These should be lookup tables - out of scope here though
@@ -154,18 +154,16 @@ async def is_owner(self, user: Union[disnake.User, disnake.Member]) -> bool: | |||
""" | |||
if self.owner_id: | |||
return user.id == self.owner_id | |||
elif self.owner_ids: | |||
if self.owner_ids: | |||
return user.id in self.owner_ids |
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.
return user.id in self.owner_ids | |
return user.id in self.owner_ids | |
@@ -505,8 +504,7 @@ def _update_copy(self: CommandT, kwargs: Dict[str, Any]) -> CommandT: | |||
kw.update(self.__original_kwargs__) | |||
copy = self.__class__(self.callback, **kw) | |||
return self._ensure_assignment_on_copy(copy) | |||
else: |
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.
should be early return, similar to base_core.py
@@ -449,6 +449,7 @@ async def launch_shard(self, gateway: str, shard_id: int, *, initial: bool = Fal | |||
# keep reading the shard while others connect | |||
self.__shards[shard_id] = ret = Shard(ws, self, self.__queue.put_nowait) | |||
ret.launch() | |||
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.
consider removing the return
above instead?
@@ -111,6 +112,7 @@ async def volume(self, ctx, volume: int): | |||
|
|||
ctx.voice_client.source.volume = volume / 100 | |||
await ctx.send(f"Changed volume to {volume}%") | |||
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.
command callbacks shouldn't return anything.
@@ -40,6 +40,7 @@ async def userinfo_error(ctx: commands.Context, error: commands.CommandError): | |||
# so we handle this in this error handler: | |||
if isinstance(error, commands.UserNotFound): | |||
return await ctx.send("Couldn't find that user.") | |||
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.
same here
@onerandomusername would you mind updating and resolving conflicts? |
Summary
Enable all
RET
errors on ruff.As some of these error codes might not be desired, I seperated each of them into an individual commit (except for RET506, I fixed some of that in RET505 inadvertently).
Checklist
pdm lint
pdm run pyright