-
Notifications
You must be signed in to change notification settings - Fork 0
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
Sourcery refactored main branch #1
base: main
Are you sure you want to change the base?
Conversation
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.
Due to GitHub API limits, only the first 60 comments can be shown.
"Warning: build in %s is using versioneer.py from %s" | ||
% (os.path.dirname(me), versioneer_py) | ||
f"Warning: build in {os.path.dirname(me)} is using versioneer.py from {versioneer_py}" | ||
) | ||
|
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.
Function get_root
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
print("unable to run %s" % dispcmd) | ||
print(f"unable to run {dispcmd}") | ||
print(e) | ||
return None, None | ||
else: | ||
if verbose: | ||
print("unable to find command, tried %s" % (commands,)) | ||
print(f"unable to find command, tried {commands}") | ||
return None, None | ||
stdout = p.communicate()[0].strip() | ||
if sys.version_info[0] >= 3: | ||
stdout = stdout.decode() | ||
if p.returncode != 0: | ||
if verbose: | ||
print("unable to run %s (error)" % dispcmd) | ||
print("stdout was %s" % stdout) | ||
print(f"unable to run {dispcmd} (error)") | ||
print(f"stdout was {stdout}") |
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.
Function run_command
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
f = open(versionfile_abs, "r") | ||
for line in f.readlines(): | ||
if line.strip().startswith("git_refnames ="): | ||
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) | ||
if mo: | ||
keywords["date"] = mo.group(1) | ||
f.close() | ||
with open(versionfile_abs, "r") as f: | ||
for line in f.readlines(): | ||
if line.strip().startswith("git_refnames ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["refnames"] = mo[1] | ||
if line.strip().startswith("git_full ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["full"] = mo[1] | ||
if line.strip().startswith("git_date ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["date"] = mo[1] |
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.
Function git_get_keywords
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
refs = set([r.strip() for r in refnames.strip("()").split(",")]) | ||
refs = {r.strip() for r in refnames.strip("()").split(",")} | ||
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of | ||
# just "foo-1.0". If we see a "tag: " prefix, prefer those. | ||
TAG = "tag: " | ||
tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)]) | ||
tags = {r[len(TAG) :] for r in refs if r.startswith(TAG)} |
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.
Function git_versions_from_keywords
refactored with the following changes:
- Replace list(), dict() or set() with comprehension (
collection-builtin-to-comprehension
) - Replace unneeded comprehension with generator (
comprehension-to-generator
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
GITS = ["git"] | ||
if sys.platform == "win32": | ||
GITS = ["git.cmd", "git.exe"] | ||
|
||
GITS = ["git.cmd", "git.exe"] if sys.platform == "win32" else ["git"] | ||
out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) | ||
if rc != 0: | ||
if verbose: | ||
print("Directory %s not under git control" % root) | ||
print(f"Directory {root} not under git control") |
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.
Function git_pieces_from_vcs
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
) - Move setting of default value for variable into
else
branch (introduce-default-else
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
) - Merge dictionary assignment with declaration (
merge-dict-assign
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
This removes the following comments ( why? ):
# maybe improved later
print("UPDATING %s" % target_versionfile) | ||
print(f"UPDATING {target_versionfile}") |
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.
Function get_cmdclass.cmd_build_exe.run
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
print("UPDATING %s" % target_versionfile) | ||
print(f"UPDATING {target_versionfile}") |
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.
Function get_cmdclass.cmd_py2exe.run
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
print("UPDATING %s" % target_versionfile) | ||
print(f"UPDATING {target_versionfile}") |
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.
Function get_cmdclass.cmd_sdist.make_release_tree
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
print(" creating %s" % cfg.versionfile_source) | ||
print(f" creating {cfg.versionfile_source}") |
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.
Function do_setup
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
value_to_append = "{}={}".format(values, os.environ[values]) | ||
value_to_append = f"{values}={os.environ[values]}" |
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.
Function MimicDockerEnvHandling.__call__
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
'Cannot mount "{}" in editable mode ' | ||
"as it is not a directory".format(args.repo), | ||
f'Cannot mount "{args.repo}" in editable mode as it is not a directory', | ||
extra=dict(phase="failed"), | ||
) | ||
sys.exit(1) | ||
|
||
if args.image_name: | ||
r2d.output_image_spec = args.image_name | ||
else: | ||
# we will pick a name after fetching the repository | ||
r2d.output_image_spec = "" | ||
sys.exit(1) | ||
|
||
r2d.output_image_spec = args.image_name or "" |
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.
Function make_r2d
refactored with the following changes:
- Simplify if expression by using or (
or-if-exp-identity
) - Replace call to format with f-string. (
use-fstring-for-formatting
) - Replace if statement with if expression (
assign-if-exp
)
This removes the following comments ( why? ):
# we will pick a name after fetching the repository
keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} | ||
return keywords | ||
return {"refnames": git_refnames, "full": git_full, "date": git_date} |
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.
Function get_keywords
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
print("unable to run %s" % dispcmd) | ||
print(f"unable to run {dispcmd}") | ||
print(e) | ||
return None, None | ||
else: | ||
if verbose: | ||
print("unable to find command, tried %s" % (commands,)) | ||
print(f"unable to find command, tried {commands}") | ||
return None, None | ||
stdout = p.communicate()[0].strip() | ||
if sys.version_info[0] >= 3: | ||
stdout = stdout.decode() | ||
if p.returncode != 0: | ||
if verbose: | ||
print("unable to run %s (error)" % dispcmd) | ||
print("stdout was %s" % stdout) | ||
print(f"unable to run {dispcmd} (error)") | ||
print(f"stdout was {stdout}") |
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.
Function run_command
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
for i in range(3): | ||
for _ in range(3): |
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.
Function versions_from_parentdir
refactored with the following changes:
- Replace unused for index with underscore (
for-index-underscore
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
) - Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
) - Remove unnecessary calls to
str()
from formatted values in f-strings (remove-str-from-fstring
)
f = open(versionfile_abs, "r") | ||
for line in f.readlines(): | ||
if line.strip().startswith("git_refnames ="): | ||
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) | ||
if mo: | ||
keywords["date"] = mo.group(1) | ||
f.close() | ||
with open(versionfile_abs, "r") as f: | ||
for line in f.readlines(): | ||
if line.strip().startswith("git_refnames ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["refnames"] = mo[1] | ||
if line.strip().startswith("git_full ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["full"] = mo[1] | ||
if line.strip().startswith("git_date ="): | ||
if mo := re.search(r'=\s*"(.*)"', line): | ||
keywords["date"] = mo[1] |
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.
Function git_get_keywords
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Replace m.group(x) with m[x] for re.Match objects (
use-getitem-for-re-match-groups
)
if tag == self.output_image_spec + ":latest": | ||
if tag == f'{self.output_image_spec}:latest': |
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.
Function Repo2Docker.find_image
refactored with the following changes:
- Use f-string instead of string concatenation (
use-fstring-for-concatenation
)
elif self.git_workdir is None: | ||
checkout_path = tempfile.mkdtemp(prefix="repo2docker") | ||
else: | ||
if self.git_workdir is None: | ||
checkout_path = tempfile.mkdtemp(prefix="repo2docker") | ||
else: | ||
checkout_path = self.git_workdir | ||
checkout_path = self.git_workdir | ||
|
||
try: | ||
self.fetch(self.repo, self.ref, checkout_path) | ||
|
||
if self.find_image(): | ||
self.log.info( | ||
"Reusing existing image ({}), not " | ||
"building.".format(self.output_image_spec) | ||
f"Reusing existing image ({self.output_image_spec}), not building." | ||
) | ||
|
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.
Function Repo2Docker.build
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Replace call to format with f-string. (
use-fstring-for-formatting
) - Merge dictionary updates via the union operator. (
dict-assign-update-to-union
)
return "Image(tags={},config={})".format(self.tags, self.config) | ||
return f"Image(tags={self.tags},config={self.config})" |
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.
Function Image.__repr__
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
for vstr in reversed(versions_list): | ||
if matcher.match(str_to_version(vstr)): | ||
return vstr | ||
return None | ||
return next( | ||
( | ||
vstr | ||
for vstr in reversed(versions_list) | ||
if matcher.match(str_to_version(vstr)) | ||
), | ||
None, | ||
) |
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.
Function find_semver_match
refactored with the following changes:
- Use the built-in function
next
instead of a for-loop (use-next
)
return tuple([int(n) for n in vstr.split(".")]) | ||
return tuple(int(n) for n in vstr.split(".")) |
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.
Function str_to_version
refactored with the following changes:
- Replace unneeded comprehension with generator (
comprehension-to-generator
)
comparison_symbol = constraint_str[0 : first_digit.start()].strip() | ||
comparison_symbol = constraint_str[:first_digit.start()].strip() | ||
|
||
# Default to "^" search if no matching mode specified (up to next major version) | ||
if (first_digit.start() == 0) or (comparison_symbol == "^"): | ||
if major(constraint) == 0: | ||
if major(constraint) != 0: | ||
return VersionRange(constraint, (major(constraint) + 1,), True) | ||
|
||
# Also, julia treats pre-1.0 releases specially, as if the first | ||
# non-zero number is actually a major number: | ||
# https://docs.julialang.org/en/latest/stdlib/Pkg/#Caret-specifiers-1 | ||
# So we need to handle it separately by bumping the first non-zero | ||
# enumber. | ||
for i, n in enumerate(constraint): | ||
if ( | ||
for i, n in enumerate(constraint): | ||
if ( | ||
n != 0 or i == len(constraint) - 1 | ||
): # (using the last existing number handles situations like "^0.0" or "^0") | ||
upper = constraint[0:i] + (n + 1,) | ||
break | ||
return VersionRange(constraint, upper, True) | ||
else: | ||
return VersionRange(constraint, (major(constraint) + 1,), True) | ||
|
||
upper = constraint[:i] + (n + 1,) | ||
break | ||
return VersionRange(constraint, upper, True) |
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.
Function create_semver_matcher
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] (
remove-redundant-slice-index
) - Swap if/else branches (
swap-if-else-branches
) - Remove unnecessary else after guard condition (
remove-unnecessary-else
)
raise ValueError( | ||
'Port specification "{}" has ' "an invalid port.".format(mapping) | ||
) | ||
raise ValueError(f'Port specification "{mapping}" has an invalid port.') | ||
if p > 65535: | ||
raise ValueError( | ||
'Port specification "{}" specifies ' | ||
"a port above 65535.".format(mapping) | ||
f'Port specification "{mapping}" specifies a port above 65535.' | ||
) | ||
|
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.
Function validate_and_generate_port_mapping.check_port
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
raise ValueError( | ||
'Port specification "{}" has ' | ||
"an invalid protocol.".format(mapping) | ||
) | ||
raise ValueError(f'Port specification "{mapping}" has an invalid protocol.') |
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.
Function validate_and_generate_port_mapping.check_port_string
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
return int(float(num) * self.UNIT_SUFFIXES[suffix]) | ||
return int(num * self.UNIT_SUFFIXES[suffix]) |
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.
Function ByteSpecification.validate
refactored with the following changes:
- Remove unnecessary casts to int, str, float or bool (
remove-unnecessary-cast
)
if ignore is not None: | ||
ignored_names = ignore(src, names) | ||
else: | ||
ignored_names = set() | ||
|
||
ignored_names = ignore(src, names) if ignore is not None else set() |
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.
Function copytree
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
url = "https://mran.microsoft.com/snapshot/{}".format(try_date.isoformat()) | ||
url = f"https://mran.microsoft.com/snapshot/{try_date.isoformat()}" | ||
r = requests.head(url) | ||
if r.ok: | ||
return url | ||
raise ValueError( | ||
"No snapshot found for {} or {} days prior in mran.microsoft.com".format( | ||
snapshot_date.strftime("%Y-%m-%d"), max_days_prior | ||
) | ||
f'No snapshot found for {snapshot_date.strftime("%Y-%m-%d")} or {max_days_prior} days prior in mran.microsoft.com' |
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.
Function RBuildPack.get_mran_snapshot_url
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
if V(self.r_version) >= V("4"): | ||
vs = "40" | ||
else: | ||
vs = "35" | ||
|
||
vs = "40" if V(self.r_version) >= V("4") else "35" |
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.
Function RBuildPack.get_build_scripts
refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp
)
# Delete /tmp/downloaded_packages only if install.R fails, as the second | ||
# invocation of install.R might be able to reuse them | ||
"Rscript %s && touch /tmp/.preassembled || true && rm -rf /tmp/downloaded_packages" | ||
% installR_path, | ||
f"Rscript {installR_path} && touch /tmp/.preassembled || true && rm -rf /tmp/downloaded_packages", | ||
) | ||
] | ||
|
||
|
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.
Function RBuildPack.get_preassemble_scripts
refactored with the following changes:
- Replace interpolated string formatting with f-string (
replace-interpolation-with-fstring
)
This removes the following comments ( why? ):
# invocation of install.R might be able to reuse them
# Delete /tmp/downloaded_packages only if install.R fails, as the second
# only run install.R if the pre-assembly failed | ||
# Delete any downloaded packages in /tmp, as they aren't reused by R | ||
"""if [ ! -f /tmp/.preassembled ]; then Rscript {}; rm -rf /tmp/downloaded_packages; fi""".format( | ||
installR_path | ||
), | ||
f"""if [ ! -f /tmp/.preassembled ]; then Rscript {installR_path}; rm -rf /tmp/downloaded_packages; fi""", | ||
) | ||
] | ||
|
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.
Function RBuildPack.get_assemble_scripts
refactored with the following changes:
- Replace call to format with f-string. (
use-fstring-for-formatting
)
This removes the following comments ( why? ):
# only run install.R if the pre-assembly failed
# Delete any downloaded packages in /tmp, as they aren't reused by R
env = super().get_env() + [("CONDA_DEFAULT_ENV", "${KERNEL_PYTHON_PREFIX}")] | ||
return env | ||
return super().get_env() + [("CONDA_DEFAULT_ENV", "${KERNEL_PYTHON_PREFIX}")] |
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.
Function CondaBuildPack.get_env
refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable
)
Sourcery Code Quality Report✅ Merging this PR will increase code quality in the affected files by 0.40%.
Here are some functions in these files that still need a tune-up:
Legend and ExplanationThe emojis denote the absolute quality of the code:
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request. Please see our documentation here for details on how these metrics are calculated. We are actively working on this report - lots more documentation and extra metrics to come! Help us improve this quality report! |
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!