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

Use dict.get() to simplify GithubDependentsInfo.__init__() #660

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

cclauss
Copy link

@cclauss cclauss commented Nov 11, 2024

A subset of the proposed changes in

% 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.

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.
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.

1 participant