-
-
Notifications
You must be signed in to change notification settings - Fork 10
Make some automated optimizations using ruff #659
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
Open
cclauss
wants to merge
4
commits into
nvuillam:main
Choose a base branch
from
cclauss:ruff-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cclauss
added a commit
to cclauss/github-dependents-info
that referenced
this pull request
Nov 11, 2024
A subset of the proposed changes in * nvuillam#659 % [`ruff rule SIM401`](https://docs.astral.sh/ruff/rules/if-else-block-instead-of-dict-get/) # if-else-block-instead-of-dict-get (SIM401) Derived from the **flake8-simplify** linter. Fix is sometimes available. ## What it does Checks for `if` statements that can be replaced with `dict.get` calls. ## Why is this bad? `dict.get()` calls can be used to replace `if` statements that assign a value to a variable in both branches, falling back to a default value if the key is not found. When possible, using `dict.get` is more concise and more idiomatic. Under [preview mode](https://docs.astral.sh/ruff/preview), this rule will also suggest replacing `if`-`else` _expressions_ with `dict.get` calls. ## Example ```python if "bar" in foo: value = foo["bar"] else: value = 0 ``` Use instead: ```python value = foo.get("bar", 0) ``` If preview mode is enabled: ```python value = foo["bar"] if "bar" in foo else 0 ``` Use instead: ```python value = foo.get("bar", 0) ``` ## References - [Python documentation: Mapping Types](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict) ## Related Issue <!-- If your PR refers to a related issue, link it here. --> ## Type of Change <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [ ] 📚 Examples / docs / tutorials / dependencies update - [x] 🔧 Bug fix (non-breaking change which fixes an issue) - [ ] 🥂 Improvement (non-breaking change which improves an existing feature) - [ ] 🚀 New feature (non-breaking change which adds functionality) - [ ] 💥 Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🔐 Security fix ## Checklist <!-- Mark with an `x` all the checkboxes that apply (like `[x]`) --> - [x] I've read the [`CODE_OF_CONDUCT.md`](https://github.com/nvuillam/github-dependents-info/blob/master/CODE_OF_CONDUCT.md) document. - [x] I've read the [`CONTRIBUTING.md`](https://github.com/nvuillam/github-dependents-info/blob/master/CONTRIBUTING.md) guide. - [ ] I've updated the code style using `make codestyle`. - [ ] I've written tests for all new methods and classes that I created. - [ ] I've written the docstring in Google format for all the methods and classes that I used.
11 tasks
@cclauss thanks for the PR :) Please can you check the CI issues ? (don't bother about trivy ones, I should handle them in another PR) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Make some automated optimizations via the command:
ruff check --select=PD,RET,RSE,RUF,SIM --fix --unsafe-fixes
%
ruff check --select=PD,RET,RSE,RUF,SIM --statistics
%
ruff check --select=PD,RET,RSE,RUF,SIM --fix --unsafe-fixes
%
ruff rule SIM401
if-else-block-instead-of-dict-get (SIM401)
Derived from the flake8-simplify linter.
Fix is sometimes available.
What it does
Checks for
if
statements that can be replaced withdict.get
calls.Why is this bad?
dict.get()
calls can be used to replaceif
statements that assign avalue to a variable in both branches, falling back to a default value if
the key is not found. When possible, using
dict.get
is more concise andmore idiomatic.
Under preview mode, this rule will
also suggest replacing
if
-else
expressions withdict.get
calls.Example
Use instead:
If preview mode is enabled:
Use instead:
References
Related Issue
Type of Change
Checklist
CODE_OF_CONDUCT.md
document.CONTRIBUTING.md
guide.make codestyle
.