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

[ruff] if k in d: del d[k] (RUF051) #14553

Merged
merged 7 commits into from
Dec 11, 2024
Merged

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #7537.

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Nov 23, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+16 -0 violations, +0 -0 fixes in 6 projects; 49 projects unchanged)

DisnakeDev/disnake (+3 -0 violations, +0 -0 fixes)

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

+ disnake/ext/commands/cog.py:177:21: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ disnake/ext/commands/cog.py:179:21: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ disnake/ext/commands/cog.py:181:21: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

RasaHQ/rasa (+2 -0 violations, +0 -0 fixes)

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

+ rasa/core/migrate.py:134:9: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ rasa/nlu/classifiers/diet_classifier.py:781:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

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

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

+ docs/build_docs.py:604:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ docs/build_docs.py:606:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ providers/src/airflow/providers/google/cloud/operators/dataproc.py:666:17: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ providers/src/airflow/providers/google/cloud/operators/dataproc.py:680:21: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

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

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

+ superset/dashboards/schemas.py:185:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

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

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

+ corporate/tests/test_stripe.py:662:17: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ zerver/lib/markdown/__init__.py:2456:9: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ zerver/lib/test_runner.py:103:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ zerver/tornado/event_queue.py:542:13: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`
+ zerver/tornado/handlers.py:42:9: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

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

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

+ astropy/modeling/core.py:189:17: RUF051 [*] Use `pop` instead of `key in dict` followed by `delete dict[key]`

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF051 16 16 0 0 0

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Nov 25, 2024
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.

Thanks.

This rule would fit well into the flake8-simplify group. Would you be interested in creating an upstream issue to see if they're interested in such a rule?

@MichaReiser MichaReiser added the needs-decision Awaiting a decision from a maintainer label Nov 25, 2024
@InSyncWithFoo
Copy link
Contributor Author

Created upstream issue here. The repo has been inactive for nearly a year, so I won't hold my breath.

@MichaReiser
Copy link
Member

Thanks for opening the upstream issue.

We discussed this internally. Let's give flake8-simplify about a week to get back to the issue. If we don't hear back in a week, then let's implement this rule as part of ruf (as you did).

@InSyncWithFoo feel free to ping me after a week

@InSyncWithFoo InSyncWithFoo changed the title [ruff] if k in d: del d[k] (RUF041) [ruff] if k in d: del d[k] (RUF051) Dec 4, 2024
@InSyncWithFoo
Copy link
Contributor Author

@MichaReiser It has been a week. What do you say?

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.

Thanks. I left a few comments related to the code.

The main question to me is if we want to add this rule to Ruff today. I see that @charliermarsh mark the rule as accepted.

To me, this seems to be mainly a stylistic rule and I personally would prefer if x in k: del k[x] in many places. But maybe that's just because I'm unused to a dict type to not have a remove method. I'd be on board merging this rule if we consider using pop is a more idiomatic writting of this pattern. @AlexWaygood what's your take on this?

@AlexWaygood
Copy link
Member

To me, this seems to be mainly a stylistic rule and I personally would prefer if x in k: del k[x] in many places. But maybe that's just because I'm unused to a dict type to not have a remove method. I'd be on board merging this rule if we consider using pop is a more idiomatic writting of this pattern. @AlexWaygood what's your take on this?

I agree that using .pop(..., None) is the idiomatic way to do this in Python. The rule makes sense to me. While I agree that it reads a little bit awkwardly if you're new to the language, it's much more efficient than if x in k: del k[x], and it's more concise. I feel like pointing out better idioms in a certain language that may not exist in other languages is one of the most helpful things linters can do for beginners in that language. So I support the rule.

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.

Perfect, thanks @AlexWaygood for the extra context (and educating me :))

I'm requesting changes for the code review feedback that I submitted earlier today

@MichaReiser MichaReiser removed the needs-decision Awaiting a decision from a maintainer label Dec 11, 2024
@MichaReiser MichaReiser enabled auto-merge (squash) December 11, 2024 11:08
@MichaReiser MichaReiser merged commit 6f8d8fa into astral-sh:main Dec 11, 2024
20 checks passed
@InSyncWithFoo InSyncWithFoo deleted the RUF041 branch December 11, 2024 12:54
TheBits pushed a commit to TheBits/ruff that referenced this pull request Dec 11, 2024
## Summary

Resolves astral-sh#7537.

## Test Plan

`cargo nextest run` and `cargo insta test`.

---------

Co-authored-by: Micha Reiser <[email protected]>
MichaReiser pushed a commit that referenced this pull request Dec 11, 2024
(Accidentally introduced in #14553).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
preview Related to preview mode features rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[rule] replace if key in dict: del dict[key] with dict.pop(key, None)
4 participants