Skip to content

Commit 988c0b3

Browse files
authored
Use dict.get() to simplify GithubDependentsInfo.__init__()
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.
1 parent 69ce4e6 commit 988c0b3

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

github_dependents_info/gh_dependents_info.py

+13-19
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,21 @@
1515
class GithubDependentsInfo:
1616
def __init__(self, repo, **options) -> None:
1717
self.repo = repo
18-
self.outputrepo = self.repo if "outputrepo" not in options else options["outputrepo"]
19-
if self.outputrepo is None or self.outputrepo == "" or len(self.outputrepo) < 4:
18+
self.outputrepo = options.get("outputrepo", self.repo)
19+
if len(self.outputrepo or "") < 4:
2020
self.outputrepo = self.repo
2121
self.url_init = f"https://github.com/{self.repo}/network/dependents"
22-
self.url_starts_with = f"/{self.repo}/network/dependents" + "?package_id="
23-
self.sort_key = "name" if "sort_key" not in options else options["sort_key"]
24-
self.min_stars = None if "min_stars" not in options else options["min_stars"]
25-
self.json_output = True if "json_output" in options and options["json_output"] is True else False
26-
self.merge_packages = True if "merge_packages" in options and options["merge_packages"] is True else False
27-
self.doc_url = options["doc_url"] if "doc_url" in options else None
28-
self.markdown_file = options["markdown_file"] if "markdown_file" in options else None
29-
self.badge_color = options["badge_color"] if "badge_color" in options else "informational"
30-
self.debug = True if "debug" in options and options["debug"] is True else False
31-
self.overwrite_progress = (
32-
True if "overwrite_progress" in options and options["overwrite_progress"] is True else False
33-
)
34-
self.csv_directory = (
35-
Path(options["csv_directory"])
36-
if ("csv_directory" in options and options["csv_directory"] is not None)
37-
else None
38-
)
22+
self.url_starts_with = f"/{self.repo}/network/dependents?package_id="
23+
self.sort_key = options.get("sort_key", "name")
24+
self.min_stars = options.get("min_stars")
25+
self.json_output = bool(options.get("json_output"))
26+
self.merge_packages = bool(options.get("merge_packages"))
27+
self.doc_url = options.get("doc_url")
28+
self.markdown_file = options.get("markdown_file")
29+
self.badge_color = options.get("badge_color", "informational")
30+
self.debug = bool(options.get("debug"))
31+
self.overwrite_progress = (bool(options.get("overwrite_progress")))
32+
self.csv_directory = Path(options.get("csv_directory"))
3933
self.total_sum = 0
4034
self.total_public_sum = 0
4135
self.total_private_sum = 0

0 commit comments

Comments
 (0)