Skip to content

Conversation

@LaBatata101
Copy link
Contributor

Summary

Fixes #14052.

Test Plan

Snapshot tests.

@github-actions
Copy link
Contributor

github-actions bot commented Apr 27, 2025

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+6 -8 violations, +0 -0 fixes in 3 projects; 1 project error; 51 projects unchanged)

apache/airflow (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

- performance/src/performance_dags/performance_dag/performance_dag_utils.py:464:12: RET504 Unnecessary assignment to `safe_dag_prefix` before `return` statement

apache/superset (+4 -4 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

+ scripts/erd/erd.py:195:5: D413 [*] Missing blank line after last section ("Parameters")
- scripts/erd/erd.py:195:5: D413 [*] Missing blank line after last section ("Parameters")
+ tests/integration_tests/base_tests.py:553:9: PLR0913 Too many arguments in function definition (12 > 5)
- tests/integration_tests/base_tests.py:553:9: PLR0913 Too many arguments in function definition (12 > 5)
+ tests/integration_tests/reports/utils.py:194:5: ANN201 Missing return type annotation for public function `create_dashboard_report`
- tests/integration_tests/reports/utils.py:194:5: ANN201 Missing return type annotation for public function `create_dashboard_report`
+ tests/unit_tests/fixtures/bash_mock.py:23:9: ANN205 Missing return type annotation for staticmethod `tag_latest_release`
- tests/unit_tests/fixtures/bash_mock.py:23:9: ANN205 Missing return type annotation for staticmethod `tag_latest_release`

zulip/zulip (+2 -3 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select ALL

- zerver/lib/generate_test_data.py:16:12: RET504 Unnecessary assignment to `config` before `return` statement
- zerver/lib/query_helpers.py:15:5: D404 First word of the docstring should not be "This"
+ zerver/lib/query_helpers.py:15:5: D404 First word of the docstring should not be "This"
+ zerver/lib/test_helpers.py:784:5: D400 First line should end with a period
- zerver/lib/test_helpers.py:784:5: D400 First line should end with a period

openai/openai-cookbook (error)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --no-preview --select A,E703,F704,B015,B018,D100

Failed to clone openai/openai-cookbook: error: 6635 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output

Changes by rule (7 rules affected)

code total + violation - violation + fix - fix
D413 2 1 1 0 0
PLR0913 2 1 1 0 0
ANN201 2 1 1 0 0
ANN205 2 1 1 0 0
D404 2 1 1 0 0
D400 2 1 1 0 0
RET504 2 0 2 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+1 -3 violations, +0 -0 fixes in 3 projects; 52 projects unchanged)

apache/airflow (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- performance/src/performance_dags/performance_dag/performance_dag_utils.py:464:12: RET504 Unnecessary assignment to `safe_dag_prefix` before `return` statement

apache/superset (+1 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- superset/sql_lab.py:680:5: DOC201 `return` is not documented in docstring
+ superset/sql_lab.py:680:5: DOC201 `return` is not documented in docstring

zulip/zulip (+0 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

- zerver/lib/generate_test_data.py:16:12: RET504 Unnecessary assignment to `config` before `return` statement

Changes by rule (2 rules affected)

code total + violation - violation + fix - fix
DOC201 2 1 1 0 0
RET504 2 0 2 0 0

@ntBre ntBre added the bug Something isn't working label Apr 28, 2025
@LaBatata101 LaBatata101 changed the title [flake8_return] Fix false-positive for variables used inside nested functions in RET504 [flake8-return] Fix false-positive for variables used inside nested functions in RET504 May 1, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

I'm not sure this is the right fix because it only works for directly nested functions but not functions e.g. nested inside a with statement.

Recursively visiting nested functions is also somewhat expensive because it requires traversing the entire body of the function (deep!) and we even do this multiple times if functions are nested, even if there are no violations.

I'd prefer if we could make use of the information already present in the SemanticModel (binding, scopes). However, this may require making this a deferred rule that runs after the entire semantic model is fully built.

@LaBatata101
Copy link
Contributor Author

I'd prefer if we could make use of the information already present in the SemanticModel (binding, scopes). However, this may require making this a deferred rule that runs after the entire semantic model is fully built.

Alright!

@LaBatata101
Copy link
Contributor Author

I've put the rule to execute in bindings.rs not sure if there's a better place to do it.

I didn't get it why the snapshot changed, but it seems correct??

Comment on lines +617 to +625
let Some(assigned_binding) = checker
.semantic()
.bindings
.iter()
.filter(|binding| binding.kind.is_assignment())
.find(|binding| binding.name(checker.source()) == assigned_id.as_str())
else {
continue;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a better way of getting the binding for the assigned_id?

@LaBatata101 LaBatata101 requested a review from MichaReiser May 3, 2025 22:02
@ntBre
Copy link
Contributor

ntBre commented May 5, 2025

I've put the rule to execute in bindings.rs not sure if there's a better place to do it.

I didn't get it why the snapshot changed, but it seems correct??

It looks like there is a duplicate diagnostic. If you view the whole file it's more obvious than in the diff:

RET504.py:269:12: RET504 [*] Unnecessary assignment to `val` before `return` statement
|
267 | return val
268 | val = 1
269 | return val # RET504
| ^^^ RET504
|
= help: Remove unnecessary assignment
Unsafe fix
265 265 | def str_to_bool(val):
266 266 | if isinstance(val, bool):
267 267 | return val
268 |- val = 1
269 |- return val # RET504
268 |+ return 1
270 269 |
271 270 |
272 271 | def str_to_bool(val):
RET504.py:269:12: RET504 [*] Unnecessary assignment to `val` before `return` statement
|
267 | return val
268 | val = 1
269 | return val # RET504
| ^^^ RET504
|
= help: Remove unnecessary assignment
Unsafe fix
265 265 | def str_to_bool(val):
266 266 | if isinstance(val, bool):
267 267 | return val
268 |- val = 1
269 |- return val # RET504
268 |+ return 1
270 269 |
271 270 |
272 271 | def str_to_bool(val):

These are both emitted on line 269. I'm guessing that's the cause of the numerous new ecosystem hits too.

@LaBatata101
Copy link
Contributor Author

I'm guessing that's the cause of the numerous new ecosystem hits too.

Wow, didn't see that until you mentioned. github-actions doesn't notify me when it edits the ecosystem comment.

@LaBatata101
Copy link
Contributor Author

@ntBre I fixed the duplicated diagnostics issue, but the ecosystem changes still looks weird.

@ntBre
Copy link
Contributor

ntBre commented May 7, 2025

@ntBre I fixed the duplicated diagnostics issue, but the ecosystem changes still looks weird.

I think you can safely ignore the changes with non-RET504 codes because I don't think any of your changes should affect other rules. The two ecosystem hits for RET504 itself look correct to me. Is there anything else that looks weird?

I'll let Micha re-review this, but we might want to add a test for his suggestion about nested scopes to show off your binding-based implementation. Otherwise this looks reasonable to me based on a quick look. Happy to review in more detail if y'all want!

@LaBatata101
Copy link
Contributor Author

LaBatata101 commented May 7, 2025

I think you can safely ignore the changes with non-RET504 codes because I don't think any of your changes should affect other rules. The two ecosystem hits for RET504 itself look correct to me. Is there anything else that looks weird?

Nope, not really.

I have this other open PR #17692 (which is not related to this one) that I think you guys may have missed, could you take a look?

Anyway, thanks for the help!

@MichaReiser MichaReiser requested a review from ntBre May 26, 2025 09:37
@LaBatata101 LaBatata101 closed this Jun 2, 2025
@LaBatata101 LaBatata101 deleted the fix-RET504 branch June 2, 2025 14:05
@ntBre
Copy link
Contributor

ntBre commented Jun 2, 2025

Sorry for the delay reviewing this. Are you planning to reopen, or was there something wrong with the PR? Just checking before I clear it from my notifications.

@LaBatata101
Copy link
Contributor Author

Sorry for the delay reviewing this. Are you planning to reopen, or was there something wrong with the PR? Just checking before I clear it from my notifications.

I was fixing another issue for RET-504 and accidentally made a branch with the same name as this one. Git told me the branch already existed, so I just deleted it. I didn’t realize this PR was still open though 😅. That's why it got closed. Not sure how to fix this.

@ntBre
Copy link
Contributor

ntBre commented Jun 2, 2025

Ahh, I see. I was going to say I'd just review the other PR and then we can try to resurrect this branch afterwards, but I wonder if it might resurrect the second one instead. It might be easiest to open a new PR if you can recover this branch locally. I think you can probably do

git branch new-branch-name 31f62a1eca7a4b9e17f75231510dd1e5db54c1a4  # looks like the most recent hash above

or something else with git reflog, if you need it. I usually have to double check the git commands I use less frequently 😆

https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git

@LaBatata101
Copy link
Contributor Author

LaBatata101 commented Jun 2, 2025

git branch new-branch-name 31f62a1eca7a4b9e17f75231510dd1e5db54c1a4

This command worked like a charm. Here's the new PR.

Thanks!

ntBre pushed a commit that referenced this pull request Jul 10, 2025
…d functions in `RET504` (#18433)

<!--
Thank you for contributing to Ruff/ty! To help us out with reviewing,
please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title? (Please prefix
with `[ty]` for ty pull
  requests.)
- Does this pull request include references to any relevant issues?
-->

## Summary

<!-- What's the purpose of the change? What does it do, and why? -->
This PR is the same as #17656.

I accidentally deleted the branch of that PR, so I'm creating a new one.

Fixes #14052

## Test Plan

Add regression tests
<!-- How was it tested? -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RET504 false-positive when nested functions reference the variable

3 participants