Skip to content

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

onerandomusername
Copy link
Member

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

  • If code changes were made, then they have been tested
    • I have updated the documentation to reflect the changes
    • I have formatted the code properly by running pdm lint
    • I have type-checked the code by running pdm run pyright
  • This PR fixes an issue
  • This PR adds something new (e.g. new method or parameters)
  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • This PR is not a code change (e.g. documentation, README, ...)

@onerandomusername onerandomusername added s: needs review Issue/PR is awaiting reviews t: refactor/typing/lint Refactors, typing changes and/or linting changes skip news labels Feb 23, 2023
@onerandomusername onerandomusername changed the title Lint/flake8 return chore(lint): add flake8-return rules Feb 23, 2023
@onerandomusername onerandomusername changed the title chore(lint): add flake8-return rules refactor(lint): add flake8-return rules Feb 24, 2023
@onerandomusername onerandomusername marked this pull request as ready for review February 26, 2023 17:10
@onerandomusername onerandomusername force-pushed the lint/flake8-return branch 2 times, most recently from 3a17a35 to 5f2fbac Compare April 6, 2023 20:28
Copy link
Member

@shiftinv shiftinv left a 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 returns (RET505). Some examples:

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 elifs, rewriting them all to ifs 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
Copy link
Member

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

Copy link
Member Author

@onerandomusername onerandomusername Apr 28, 2023

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me.

Comment on lines 477 to +481
# the payload will always be the proper channel payload
return self.__class__(state=self._state, guild=self.guild, data=payload) # type: ignore

return None

Copy link
Member

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

Comment on lines 4122 to +4135
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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@shiftinv
Copy link
Member

@onerandomusername would you mind updating and resolving conflicts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
s: needs review Issue/PR is awaiting reviews skip news t: refactor/typing/lint Refactors, typing changes and/or linting changes
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

2 participants