-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[flake8-return] Fix false-positive for variables used inside nested functions in RET504
#17656
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
Conversation
… functions in `RET504`
|
| 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 |
flake8_return] Fix false-positive for variables used inside nested functions in RET504flake8-return] Fix false-positive for variables used inside nested functions in RET504
MichaReiser
left a comment
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'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.
Alright! |
|
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?? |
| 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; | ||
| }; |
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.
Is there a better way of getting the binding for the assigned_id?
It looks like there is a duplicate diagnostic. If you view the whole file it's more obvious than in the diff: Lines 84 to 122 in 16c055b
These are both emitted on line 269. I'm guessing that's the cause of the numerous new ecosystem hits too. |
Wow, didn't see that until you mentioned. |
|
@ntBre I fixed the duplicated diagnostics issue, but the ecosystem changes still looks weird. |
I think you can safely ignore the changes with non- 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! |
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! |
|
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. |
|
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 aboveor something else with https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git |
This command worked like a charm. Here's the new PR. Thanks! |
…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? -->
Summary
Fixes #14052.
Test Plan
Snapshot tests.