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
see #7
see #7
Conversation
updates: - [github.com/psf/black: 22.6.0 → 22.10.0](psf/black@22.6.0...22.10.0)
[pre-commit.ci] pre-commit autoupdate
updates: - [github.com/pre-commit/pre-commit-hooks: v4.3.0 → v4.4.0](pre-commit/pre-commit-hooks@v4.3.0...v4.4.0) - [github.com/psf/black: 22.10.0 → 23.1.0](psf/black@22.10.0...23.1.0)
for more information, see https://pre-commit.ci
[pre-commit.ci] pre-commit autoupdate
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
improved function based on a larger redcap csv (bridge2ai)
for more information, see https://pre-commit.ci
update conversion tools and their tests
for more information, see https://pre-commit.ci
remove 3.7, add 3.11 & 3.12
Hello @Remi-Gau! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've reviewed this pull request using the Sourcery rules engine. If you would also like our AI-powered code review then let us know.
if choices and not isinstance(choices, str): | ||
if isinstance(choices, list): | ||
item_choices = [ | ||
f"{ch.get('schema:value', ch.get('value', ''))}, {ch.get('schema:name', ch.get('name', ''))}" | ||
for ch in choices | ||
] | ||
row_data["choices"] = " | ".join(item_choices) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Merge nested if conditions (merge-nested-ifs
)
if choices and not isinstance(choices, str): | |
if isinstance(choices, list): | |
item_choices = [ | |
f"{ch.get('schema:value', ch.get('value', ''))}, {ch.get('schema:name', ch.get('name', ''))}" | |
for ch in choices | |
] | |
row_data["choices"] = " | ".join(item_choices) | |
if choices and not isinstance(choices, str) and isinstance(choices, list): | |
item_choices = [ | |
f"{ch.get('schema:value', ch.get('value', ''))}, {ch.get('schema:name', ch.get('name', ''))}" | |
for ch in choices | |
] | |
row_data["choices"] = " | ".join(item_choices) | |
Explanation
Too much nesting can make code difficult to understand, and this is especiallytrue in Python, where there are no brackets to help out with the delineation of
different nesting levels.
Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if
conditions can be combined using
and
is an easy win.
if line.strip().startswith(versionfile_source): | ||
if "export-subst" in line.strip().split()[1:]: | ||
present = True | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): Merge nested if conditions (merge-nested-ifs
)
if line.strip().startswith(versionfile_source): | |
if "export-subst" in line.strip().split()[1:]: | |
present = True | |
break | |
if line.strip().startswith(versionfile_source) and "export-subst" in line.strip().split()[1:]: | |
present = True | |
break | |
Explanation
Too much nesting can make code difficult to understand, and this is especiallytrue in Python, where there are no brackets to help out with the delineation of
different nesting levels.
Reading deeply nested code is confusing, since you have to keep track of which
conditions relate to which levels. We therefore strive to reduce nesting where
possible, and the situation where two if
conditions can be combined using
and
is an easy win.
mo = re.search(r'=\s*"(.*)"', line) | ||
if mo: | ||
keywords["refnames"] = mo.group(1) | ||
if line.strip().startswith("git_full ="): | ||
mo = re.search(r'=\s*"(.*)"', line) | ||
if mo: | ||
keywords["full"] = mo.group(1) | ||
if line.strip().startswith("git_date ="): | ||
mo = re.search(r'=\s*"(.*)"', line) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Use named expression to simplify assignment and conditional [×3] (
use-named-expression
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
@@ -230,7 +268,9 @@ | |||
|
|||
|
|||
@register_vcs_handler("git", "pieces_from_vcs") | |||
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): | |||
def git_pieces_from_vcs( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Move setting of default value for variable into
else
branch (introduce-default-else
) - Replace interpolated string formatting with f-string [×4] (
replace-interpolation-with-fstring
) - Merge dictionary assignment with declaration [×3] (
merge-dict-assign
) - Inline variable that is only used once (
inline-variable
) - Replace if statement with if expression (
assign-if-exp
) - Replace m.group(x) with m[x] for re.Match objects [×3] (
use-getitem-for-re-match-groups
) - Low code quality found in git_pieces_from_vcs - 20% (
low-code-quality
)
Explanation
The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.
How can you solve this?
It might be worth refactoring this function to make it shorter and more readable.
- Reduce the function length by extracting pieces of functionality out into
their own functions. This is the most important thing you can do - ideally a
function should be less than 10 lines. - Reduce nesting, perhaps by introducing guard clauses to return early.
- Ensure that variables are tightly scoped, so that code using related concepts
sits together within the function rather than being scattered.
if pieces["dirty"]: | ||
rendered += ".dirty" | ||
else: | ||
# exception #1 | ||
rendered = "0" | ||
if pieces["branch"] != "master": | ||
rendered += ".dev0" | ||
rendered += "+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) | ||
if pieces["dirty"]: | ||
rendered += ".dirty" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Hoist conditional out of nested conditional (
hoist-if-from-if
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
@@ -1231,12 +1538,12 @@ | |||
""" | |||
|
|||
|
|||
def versions_from_file(filename): | |||
def versions_from_file(filename: str) -> Dict[str, Any]: | |||
"""Try to determine the version from _version.py if present.""" | |||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Simplify basic file reads with
pathlib
(path-read
) - Explicitly raise from a previous error (
raise-from-previous-error
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
if pieces["dirty"]: | ||
rendered += ".dirty" | ||
else: | ||
# exception #1 | ||
rendered = "0" | ||
if pieces["branch"] != "master": | ||
rendered += ".dev0" | ||
rendered += "+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) | ||
if pieces["dirty"]: | ||
rendered += ".dirty" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Hoist conditional out of nested conditional (
hoist-if-from-if
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
""" | ||
if pieces["closest-tag"]: | ||
rendered = pieces["closest-tag"] | ||
if pieces["distance"] or pieces["dirty"]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Split conditional into multiple branches [×2] (
split-or-ifs
) - Merge duplicate blocks in conditional [×2] (
merge-duplicate-blocks
) - Replace interpolated string formatting with f-string [×2] (
replace-interpolation-with-fstring
) - Hoist conditional out of nested conditional (
hoist-if-from-if
) - Hoist repeated code outside conditional statement (
hoist-statement-from-if
) - Remove redundant conditional [×2] (
remove-redundant-if
)
@@ -1412,10 +1794,14 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Simplify conditional into switch-like form [×2] (
switch
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
@@ -1438,7 +1824,7 @@ | |||
"""The project root directory is unknown or missing key files.""" | |||
|
|||
|
|||
def get_versions(verbose=False): | |||
def get_versions(verbose: bool = False) -> Dict[str, Any]: | |||
"""Get the project version from whatever source is available. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (code-quality): We've found these issues:
- Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace interpolated string formatting with f-string [×5] (
replace-interpolation-with-fstring
)
No description provided.