-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[flake8-return] Only add return None at end of function (RET503) #11074
[flake8-return] Only add return None at end of function (RET503) #11074
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RET503 | 256 | 122 | 134 | 0 | 0 |
RUF100 | 2 | 2 | 0 | 0 | 0 |
if checker.settings.preview.is_enabled() { | ||
return result; | ||
} |
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.
Early return if in preview mode because we only need to know if the function has at least one implicit return to add a single return None
at the end of the function.
The stable version can add multiple return so it cannot return early.
The code should be simplified once the preview version become stable.
A comment should be added that when the preview version becomes stable the code can be simplified with early return everywhere. |
Will it be included in Ruff 0.5.0? |
Just seen this -- unfortunately I just set the release workflow in motion, so unfortunately not. Sorry :-( |
No worries. |
Will it be included in Ruff 0.6.0? |
ec68233
to
dda1efa
Compare
CodSpeed Performance ReportMerging #11074 will not alter performanceComparing Summary
|
I don't think this requires a minor release, considering that the change is gated behind preview mode. |
} | ||
} | ||
} | ||
|
||
/// RET503 | ||
fn implicit_return(checker: &mut Checker, function_def: &ast::StmtFunctionDef, stmt: &Stmt) { | ||
if has_implicit_return(checker, stmt) && checker.settings.preview.is_enabled() { |
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.
I don't have a great solution but I do find it a bit surprising that has_implicit_return
adds diagnostics in the non preview mode.
I'm somewhat inclined to duplicate the logic between preview and non-preview mode. That should also make it much easier when stabilizing the new behavior (and it's not that much code that needs copying). Unless you see a way of how we can avoid the side-effects of has_implicit_return
in stable (e.g. could it return a Vec
with all the statements for which diagnostics should be added?)
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.
I don't mind going ahead with the PR as is but I'm interested to hear what you think of duplicating the logic OR maybe you have another idea to make this a bit clearer
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.
I used the Vec
solution to remove the side effects.
The code is nicer.
Thank you for suggesting it.
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.
Thanks. Will merge now and sorry for the long wait
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.
You are welcome
452fbd9
to
5d1fc6c
Compare
Summary
In
RET503
documentation, it is said that it only addsreturn None
at the end of functions.The current behavior can add
return None
in multiple place in the function, but not at the end of the function.This behavior does not match the documentation and goes against other
flake8-return
rules likeRET505
and caused people to open #5765.The new behavior only adds
return None
at the end of functions like in the documentation and results in more readable code.The new behavior is only available in preview mode.
The range of
RET503
is changed to be the entire function, as it will be more clear it concerns the entire function.Before
After
Test Plan
I added a preview snapshot for
RET503
.I compared the diff between the
RET503
stable and preview snapshot.I checked the ecosystem reports and fixed one regression.