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

Simplify key in dct and dct[key] to dct.get(key) #5933

Closed
janosh opened this issue Jul 20, 2023 · 11 comments · Fixed by #7895
Closed

Simplify key in dct and dct[key] to dct.get(key) #5933

janosh opened this issue Jul 20, 2023 · 11 comments · Fixed by #7895
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule

Comments

@janosh
Copy link

janosh commented Jul 20, 2023

# bad
if "key" in dct and dct["key"]:
    ...

# good
if dct.get("key"):
    ...
@agnes-sharan
Copy link

Hi could I work on this?

@charliermarsh charliermarsh added rule Implementing or modifying a lint rule needs-decision Awaiting a decision from a maintainer labels Jul 21, 2023
@charliermarsh
Copy link
Member

Hi @agnes-sharan -- thanks for asking! We need to make a decision as maintainers on whether to include this (and how it should be categorized) before opening it up for contributors. We'll label it as such and comment here when there's consensus.

@sbrugman
Copy link
Contributor

sbrugman commented Jul 21, 2023

Examples in the wild: https://grep.app/search?q=%20%22%28.%2B%29%22%20in%20%5Ba-z0-9%5D%2B%20and%20%5Ba-z0-9%5D%2B%5C%5B%22%28.%2B%29%22%5C%5D%3A&regexp=true

It would make sense to propose this rule in flake8-simplify. They already have a couple of dict-related rules, and then port it to ruff (if they are responsive).

@janosh janosh changed the title Simplify key in dict check before access to dict.get Simplify key in dct and dct[key] to dct.get(key) Jul 21, 2023
@janosh
Copy link
Author

janosh commented Jul 21, 2023

The regex can be made more generic to increase the number of hits. The site doesn't seem to support backreferences (.+) in .+ and .+\[\1\] so might be some false positives in there.

@sbrugman
Copy link
Contributor

sbrugman commented Jul 23, 2023

Refurb might be a candidate to include this rule into: https://github.com/dosisod/refurb

In the cases that dct is a common dict, this syntax is clearly more readable (just one ref to dct and key)

Alternatively, this could be a RUF category rule.

@charliermarsh charliermarsh added accepted Ready for implementation and removed needs-decision Awaiting a decision from a maintainer labels Sep 21, 2023
@charliermarsh
Copy link
Member

We can add this as a ruff rule if there's interest.

@harupy
Copy link
Contributor

harupy commented Oct 10, 2023

@charliermarsh can I work on this?

@charliermarsh
Copy link
Member

Go for it!

@harupy
Copy link
Contributor

harupy commented Oct 10, 2023

How would you name this rule?

@charliermarsh
Copy link
Member

We may need to limit this to boolean tests, since it returns None rather than False.

@charliermarsh
Copy link
Member

I'm not sure, maybe like UnnecessaryDictIn? Easy to rename during review though.

charliermarsh pushed a commit that referenced this issue Oct 13, 2023
## Summary

Close #5933

## Test Plan

`cargo test`
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Ready for implementation rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants