Skip to content
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

[pylint] Add fix for useless-else-on-loop (PLW0120) #9590

Merged
merged 7 commits into from
Jan 21, 2024

Conversation

diceroll123
Copy link
Contributor

@diceroll123 diceroll123 commented Jan 20, 2024

Summary

adds fix for useless-else-on-loop / PLW0120.

Test Plan

cargo test

@diceroll123 diceroll123 force-pushed the add-autofix-to-PLW0120 branch from d11e4a3 to dee62b0 Compare January 20, 2024 07:16
Copy link
Contributor

github-actions bot commented Jan 20, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+13 -13 violations, +0 -0 fixes in 2 projects; 41 projects unchanged)

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

ruff check --no-cache --exit-zero --no-preview --select ALL

- airflow/configuration.py:1640:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/configuration.py:1640:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/batch_client.py:371:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/batch_client.py:371:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/batch_client.py:409:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/batch_client.py:409:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/datasync.py:318:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/datasync.py:318:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/bigquery.py:2237:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/bigquery.py:2237:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/bigquery.py:3075:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/bigquery.py:3075:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/gcs.py:357:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/gcs.py:357:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/http/hooks/http.py:411:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/http/hooks/http.py:411:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/jenkins/operators/jenkins_job_trigger.py:181:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/jenkins/operators/jenkins_job_trigger.py:181:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- scripts/in_container/run_generate_constraints.py:317:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ scripts/in_container/run_generate_constraints.py:317:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- tests/providers/elasticsearch/log/elasticmock/__init__.py:85:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ tests/providers/elasticsearch/log/elasticmock/__init__.py:85:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents

demisto/content (+2 -2 violations, +0 -0 fixes)

- Packs/Carbon_Black_Enterprise_Response/Scripts/CBLiveGetFile/CBLiveGetFile.py:142:1: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ Packs/Carbon_Black_Enterprise_Response/Scripts/CBLiveGetFile/CBLiveGetFile.py:142:1: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- Packs/ThreatQ/Integrations/ThreatQ_v2/ThreatQ_v2.py:589:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ Packs/ThreatQ/Integrations/ThreatQ_v2/ThreatQ_v2.py:589:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and dedent its contents

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLW0120 26 13 13 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+13 -13 violations, +0 -0 fixes in 2 projects; 41 projects unchanged)

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

ruff check --no-cache --exit-zero --preview --select ALL

+ airflow/configuration.py:1640:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/configuration.py:1640:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/batch_client.py:371:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/batch_client.py:371:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/batch_client.py:409:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/batch_client.py:409:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/amazon/aws/hooks/datasync.py:318:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/amazon/aws/hooks/datasync.py:318:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/bigquery.py:2237:17: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/bigquery.py:2237:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/bigquery.py:3075:17: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/bigquery.py:3075:17: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/google/cloud/hooks/gcs.py:357:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/google/cloud/hooks/gcs.py:357:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/http/hooks/http.py:411:13: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/http/hooks/http.py:411:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ airflow/providers/jenkins/operators/jenkins_job_trigger.py:181:9: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- airflow/providers/jenkins/operators/jenkins_job_trigger.py:181:9: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ scripts/in_container/run_generate_constraints.py:317:13: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- scripts/in_container/run_generate_constraints.py:317:13: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ tests/providers/elasticsearch/log/elasticmock/__init__.py:85:5: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- tests/providers/elasticsearch/log/elasticmock/__init__.py:85:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it

demisto/content (+2 -2 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

+ Packs/Carbon_Black_Enterprise_Response/Scripts/CBLiveGetFile/CBLiveGetFile.py:142:1: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- Packs/Carbon_Black_Enterprise_Response/Scripts/CBLiveGetFile/CBLiveGetFile.py:142:1: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it
+ Packs/ThreatQ/Integrations/ThreatQ_v2/ThreatQ_v2.py:589:5: PLW0120 [*] `else` clause on loop without a `break` statement; remove the `else` and dedent its contents
- Packs/ThreatQ/Integrations/ThreatQ_v2/ThreatQ_v2.py:589:5: PLW0120 `else` clause on loop without a `break` statement; remove the `else` and de-indent all the code inside it

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLW0120 26 13 13 0 0

@diceroll123 diceroll123 marked this pull request as ready for review January 20, 2024 08:15
@diceroll123 diceroll123 force-pushed the add-autofix-to-PLW0120 branch from bf09089 to bfba477 Compare January 21, 2024 00:12
@charliermarsh charliermarsh self-requested a review January 21, 2024 03:57
@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label Jan 21, 2024
@charliermarsh charliermarsh changed the title [pylint] - add fix for useless-else-on-loop / PLW0120 [pylint] Add fix for useless-else-on-loop (PLW0120) Jan 21, 2024
@charliermarsh
Copy link
Member

Thanks @diceroll123, these are great! I know I'm behind on these reviews but I will get to them over time.

@diceroll123
Copy link
Contributor Author

Thanks, and no rush! 😄 It's the weekend, take it easy.

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Looks great!

pub(crate) fn useless_else_on_loop(
checker: &mut Checker,
/// Generate a [`Fix`] to remove the `else` clause from the given statement.
fn remove_else(
Copy link
Member

Choose a reason for hiding this comment

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

I moved the handling out to its own function so that it can return Result and make all the error cases explicit (rather than using unwrap, which panics on failure).

indented,
locator.line_start(else_range.start()),
locator.full_line_end(end.end()),
)))
Copy link
Member

Choose a reason for hiding this comment

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

I simplified this a bit, but we now drop comments on the same line as the else:. I think this is ok.

Copy link
Member

Choose a reason for hiding this comment

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

@diceroll123 - Did you feel strongly about preserving these? I can see why it might be useful, but the comments do get moved, right? Like:

else:  # some comment
  x = 1
  y = 2

Was getting moved to:

x = 1
y = 2  # some comment

Which struct me as potentially-wrong.

For example, in this case:

def test_retain_comment():
    """Retain the comment within the `else` block"""
    for j in range(10):
        pass
    else:  # [useless-else-on-loop]
        print("fat chance")
        for j in range(10):
            break

The comment was moved to after the break.

Copy link
Member

Choose a reason for hiding this comment

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

(Merging to keep things moving but happy to revisit.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No strong feelings either way! It's much simpler without preserving comments on the else line.

@charliermarsh charliermarsh added the preview Related to preview mode features label Jan 21, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) January 21, 2024 15:55
@charliermarsh charliermarsh merged commit 9e5f3f1 into astral-sh:main Jan 21, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations preview Related to preview mode features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants