Skip to content

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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

cclauss
Copy link

@cclauss cclauss commented Nov 11, 2024

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

7	SIM401	[*] if-else-block-instead-of-dict-get
4	SIM210	[*] if-expr-with-true-false
3	SIM108	[*] if-else-block-instead-of-if-exp
2	PD002 	[*] pandas-use-of-inplace-argument
1	RSE102	[*] unnecessary-paren-on-raise-exception
1	RET506	[*] superfluous-else-raise
1	PD011 	[ ] pandas-use-of-dot-values
1	RUF010	[*] explicit-f-string-type-conversion
1	RUF100	[*] unused-noqa
[*] fixable with `ruff check --fix`

% ruff check --select=PD,RET,RSE,RUF,SIM --fix --unsafe-fixes

Found 23 errors (22 fixed, 1 remaining).

% 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 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, this rule will
also suggest replacing if-else expressions with dict.get calls.

Example

if "bar" in foo:
    value = foo["bar"]
else:
    value = 0

Use instead:

value = foo.get("bar", 0)

If preview mode is enabled:

value = foo["bar"] if "bar" in foo else 0

Use instead:

value = foo.get("bar", 0)

References

Related Issue

Type of Change

  • 📚 Examples / docs / tutorials / dependencies update
  • 🔧 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

  • I've read the CODE_OF_CONDUCT.md document.
  • I've read the 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.

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.
@nvuillam
Copy link
Owner

nvuillam commented Nov 11, 2024

@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants