Sourcery refactored main branch - #1
Conversation
| if e.errno == errno.ENOENT: | ||
| pass |
There was a problem hiding this comment.
Function do_clear refactored with the following changes:
- Remove redundant conditional (
remove-redundant-if)
| with open(r) as f: | ||
| contents = f.read() | ||
| contents = Path(r).read_text() |
There was a problem hiding this comment.
Function import_requirements refactored with the following changes:
- Simplify basic file reads with
pathlib(path-read) - Swap positions of nested conditionals [×5] (
swap-nested-ifs) - Hoist nested repeated code outside conditional statements [×5] (
hoist-similar-statement-from-if) - Replace if statement with if expression (
assign-if-exp) - Invert any/all to simplify comparisons (
invert-any-all) - Swap if/else branches (
swap-if-else-branches)
| if os.name != "nt": | ||
| if "LANG" not in os.environ: | ||
| click.echo( | ||
| "{}: the environment variable {} is not set!" | ||
| "\nWe recommend setting this in {} (or equivalent) for " | ||
| "proper expected behavior.".format( | ||
| click.style("Warning", fg="red", bold=True), | ||
| click.style("LANG", bold=True), | ||
| click.style("~/.profile", fg="green"), | ||
| ), | ||
| err=True, | ||
| ) | ||
| if os.name != "nt" and "LANG" not in os.environ: | ||
| click.echo( | ||
| "{}: the environment variable {} is not set!" | ||
| "\nWe recommend setting this in {} (or equivalent) for " | ||
| "proper expected behavior.".format( | ||
| click.style("Warning", fg="red", bold=True), | ||
| click.style("LANG", bold=True), | ||
| click.style("~/.profile", fg="green"), | ||
| ), | ||
| err=True, | ||
| ) |
There was a problem hiding this comment.
Function ensure_environment refactored with the following changes:
- Merge nested if conditions (
merge-nested-ifs)
| project._which("python") | ||
| if not (project.s.USING_DEFAULT_PYTHON or system) | ||
| else None | ||
| None | ||
| if (project.s.USING_DEFAULT_PYTHON or system) | ||
| else project._which("python") |
There was a problem hiding this comment.
Function ensure_pipfile refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression) - Use named expression to simplify assignment and conditional (
use-named-expression)
This removes the following comments ( why? ):
# Write changes out to disk.
| python_entry = find_python(finder, line) | ||
| return python_entry | ||
| return find_python(finder, line) |
There was a problem hiding this comment.
Function find_a_system_python refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if not allow_global: | ||
| src_dir = os.getenv( | ||
| "PIP_SRC", os.getenv("PIP_SRC_DIR", project.virtualenv_src_location) | ||
| src_dir = ( | ||
| os.getenv("PIP_SRC", os.getenv("PIP_SRC_DIR")) | ||
| if allow_global | ||
| else os.getenv( | ||
| "PIP_SRC", | ||
| os.getenv("PIP_SRC_DIR", project.virtualenv_src_location), | ||
| ) | ||
| else: | ||
| src_dir = os.getenv("PIP_SRC", os.getenv("PIP_SRC_DIR")) | ||
| ) |
There was a problem hiding this comment.
Function pip_install_deps refactored with the following changes:
- Replace if statement with if expression [×2] (
assign-if-exp) - Add single value to dictionary directly rather than using update() (
simplify-dictionary-update) - Swap if/else branches of if expression to remove negation (
swap-if-expression)
| result = find_python(finder, command) | ||
| if result: | ||
| if result := find_python(finder, command): | ||
| return result | ||
| result = finder.which(command) | ||
| if result: | ||
| return result.path.as_posix() | ||
| return "" | ||
| return result.path.as_posix() if (result := finder.which(command)) else "" |
There was a problem hiding this comment.
Function fallback_which refactored with the following changes:
- Use named expression to simplify assignment and conditional [×2] (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| super().__init__() | ||
| self._modules = {"pkg_resources": pkg_resources, "pipenv": pipenv} | ||
| self.base_working_set = base_working_set if base_working_set else BASE_WORKING_SET | ||
| self.base_working_set = base_working_set or BASE_WORKING_SET |
There was a problem hiding this comment.
Function Environment.__init__ refactored with the following changes:
- Simplify if expression by using or [×2] (
or-if-exp-identity)
| dist = next( | ||
| iter(dist for dist in self.base_working_set if dist.project_name == name), | ||
| if dist := next( | ||
| iter( | ||
| dist | ||
| for dist in self.base_working_set | ||
| if dist.project_name == name | ||
| ), | ||
| None, | ||
| ) | ||
| if dist: | ||
| ): |
There was a problem hiding this comment.
Function Environment.safe_import refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression)
| deps = set() | ||
| deps.add(dist) | ||
| deps = {dist} |
There was a problem hiding this comment.
Function Environment.resolve_dist refactored with the following changes:
- Merge add into set declaration (
merge-set-add)
| py_version = sysconfig.get_python_version() | ||
| return py_version | ||
| return sysconfig.get_python_version() |
There was a problem hiding this comment.
Function Environment.python_version refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| include_dirs = self.get_include_path() | ||
| if include_dirs: | ||
| include_path = include_dirs.get( | ||
| if include_dirs := self.get_include_path(): | ||
| if include_path := include_dirs.get( | ||
| "include", include_dirs.get("platinclude") | ||
| ) | ||
| if not include_path: | ||
| ): | ||
| include_dir = Path(include_path) | ||
| else: | ||
| return {} | ||
| include_dir = Path(include_path) |
There was a problem hiding this comment.
Function Environment.python_info refactored with the following changes:
- Use named expression to simplify assignment and conditional [×2] (
use-named-expression) - Lift code into else after jump in control flow (
reintroduce-else) - Swap if/else branches (
swap-if-else-branches)
| lib_dirs = purelib | ||
| else: | ||
| lib_dirs = purelib + os.pathsep + platlib | ||
| lib_dirs = purelib if purelib == platlib else purelib + os.pathsep + platlib |
There was a problem hiding this comment.
Function Environment.base_paths refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp)
| for key in ("purelib", "platlib", "stdlib", "platstdlib"): | ||
| pylib_lines.append( | ||
| f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})" | ||
| ) | ||
| pylib_lines.extend( | ||
| f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})" | ||
| for key in ("purelib", "platlib", "stdlib", "platstdlib") | ||
| ) | ||
| if python_inc: | ||
| for key in ("include", "platinclude"): | ||
| pyinc_lines.append( | ||
| f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})" | ||
| ) | ||
| pyinc_lines.extend( | ||
| f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})" | ||
| for key in ("include", "platinclude") | ||
| ) |
There was a problem hiding this comment.
Function Environment.build_command refactored with the following changes:
- Replace a for append loop with list extend [×2] (
for-append-to-extend) - Replace assignment with augmented assignment (
aug-assign)
| sys_prefix = Path(c.stdout.strip()).as_posix() | ||
| return sys_prefix | ||
| return Path(c.stdout.strip()).as_posix() |
There was a problem hiding this comment.
Function Environment.sys_prefix refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if getattr(sys, "real_prefix", None) is not None: | ||
| # virtualenv venvs | ||
| result = True | ||
| else: | ||
| # PEP 405 venvs | ||
| result = sys.prefix != getattr(sys, "base_prefix", sys.prefix) | ||
| return result | ||
| return ( | ||
| True | ||
| if getattr(sys, "real_prefix", None) is not None | ||
| else sys.prefix != getattr(sys, "base_prefix", sys.prefix) | ||
| ) |
There was a problem hiding this comment.
Function is_using_venv refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Inline variable that is immediately returned (
inline-immediately-returned-variable)
This removes the following comments ( why? ):
# virtualenv venvs
# PEP 405 venvs
| return virtual_env and not (pipenv_active or ignore_virtualenvs) | ||
| return virtual_env and not pipenv_active and not ignore_virtualenvs |
There was a problem hiding this comment.
Function is_in_virtualenv refactored with the following changes:
- Simplify logical expression using De Morgan identities (
de-morgan)
| PIPENV_SPINNER_FAIL_TEXT = "✘ {0}" if not PIPENV_HIDE_EMOJIS else "{0}" | ||
| PIPENV_SPINNER_OK_TEXT = "✔ {0}" if not PIPENV_HIDE_EMOJIS else "{0}" | ||
| PIPENV_SPINNER_FAIL_TEXT = "{0}" if PIPENV_HIDE_EMOJIS else "✘ {0}" | ||
| PIPENV_SPINNER_OK_TEXT = "{0}" if PIPENV_HIDE_EMOJIS else "✔ {0}" |
There was a problem hiding this comment.
Lines 431-432 refactored with the following changes:
- Swap if/else branches of if expression to remove negation [×2] (
swap-if-expression)
| line = f" {line}" | ||
| else: | ||
| line = f" {line}" | ||
| line = f" {line}" if line.startswith("File") else f" {line}" |
There was a problem hiding this comment.
Function handle_exception refactored with the following changes:
- Replace if statement with if expression (
assign-if-exp) - Swap if/else branches of if expression to remove negation (
swap-if-expression)
| click.echo( | ||
| "{} {}".format("OUTPUT: ", self.out), | ||
| file=file, | ||
| err=True, | ||
| ) | ||
| click.echo(f"OUTPUT: {self.out}", file=file, err=True) | ||
| if self.err: | ||
| click.echo( | ||
| "{} {}".format("STDERR: ", self.err), | ||
| file=file, | ||
| err=True, | ||
| ) | ||
| click.echo(f"STDERR: {self.err}", file=file, err=True) |
There was a problem hiding this comment.
Function PipenvCmdError.show refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting) - Simplify unnecessary nesting, casting and constant values in f-strings [×2] (
simplify-fstring-formatting)
| color = None | ||
| if self.ctx is not None: | ||
| color = self.ctx.color | ||
| color = self.ctx.color if self.ctx is not None else None |
There was a problem hiding this comment.
Function PipenvUsageError.show refactored with the following changes:
- Move setting of default value for variable into
elsebranch (introduce-default-else) - Replace if statement with if expression (
assign-if-exp)
| search_string = exc.match_string if exc.match_string else exc.exception_name | ||
| split_string = ( | ||
| exc.show_from_string if exc.show_from_string else exc.exception_name | ||
| ) | ||
| search_string = exc.match_string or exc.exception_name | ||
| split_string = exc.show_from_string or exc.exception_name |
There was a problem hiding this comment.
Function prettify_exc refactored with the following changes:
- Simplify if expression by using or [×2] (
or-if-exp-identity) - Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp) - Swap if/else branches of if expression to remove negation (
swap-if-expression)
| major = int(match.group(1)) | ||
| minor = int(match.group(2)) | ||
| patch = match.group(3) | ||
| major = int(match[1]) | ||
| minor = int(match[2]) | ||
| patch = match[3] |
There was a problem hiding this comment.
Function Version.parse refactored with the following changes:
- Replace m.group(x) with m[x] for re.Match objects [×3] (
use-getitem-for-re-match-groups)
| c = self._run( | ||
| return self._run( | ||
| "install", | ||
| "python", | ||
| str(version), | ||
| timeout=self.project.s.PIPENV_INSTALL_TIMEOUT, | ||
| ) | ||
| return c |
There was a problem hiding this comment.
Function Asdf.install refactored with the following changes:
- Inline variable that is immediately returned (
inline-immediately-returned-variable)
| if isinstance(f.newlines, str): | ||
| return f.newlines | ||
| return DEFAULT_NEWLINES | ||
| return f.newlines if isinstance(f.newlines, str) else DEFAULT_NEWLINES |
There was a problem hiding this comment.
Function preferred_newlines refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else) - Replace if statement with if expression (
assign-if-exp)
| lockfile_dict = {} | ||
| categories = self.get_package_categories(for_lockfile=True) | ||
| _lockfile = self._lockfile(categories=categories) | ||
| for category in categories: | ||
| lockfile_dict[category] = _lockfile.get(category, {}).copy() | ||
| lockfile_dict.update({"_meta": self.get_lockfile_meta()}) | ||
| lockfile_dict = { | ||
| category: _lockfile.get(category, {}).copy() | ||
| for category in categories | ||
| } | ||
| lockfile_dict["_meta"] = self.get_lockfile_meta() |
There was a problem hiding this comment.
Function Project.get_or_create_lockfile refactored with the following changes:
- Add single value to dictionary directly rather than using update() (
simplify-dictionary-update) - Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Move assignment closer to its usage within a block (
move-assign-in-block) - Convert for loop into dictionary comprehension (
dict-comprehension)
| if self.lockfile_exists and hasattr(self.lockfile_content, "keys"): | ||
| meta_ = self.lockfile_content.get("_meta", {}) | ||
| sources_ = meta_.get("sources") | ||
| if sources_: | ||
| return sources_ | ||
|
|
||
| else: | ||
| if not self.lockfile_exists or not hasattr(self.lockfile_content, "keys"): | ||
| return self.pipfile_sources() | ||
| meta_ = self.lockfile_content.get("_meta", {}) | ||
| if sources_ := meta_.get("sources"): | ||
| return sources_ |
There was a problem hiding this comment.
Function Project.sources refactored with the following changes:
- Swap if/else branches (
swap-if-else-branches) - Remove unnecessary else after guard condition (
remove-unnecessary-else) - Use named expression to simplify assignment and conditional (
use-named-expression)
| for name in section.keys(): | ||
| if pep423_name(name) == package_name: | ||
| return name | ||
| return None | ||
| return next( | ||
| (name for name in section.keys() if pep423_name(name) == package_name), | ||
| None, | ||
| ) |
There was a problem hiding this comment.
Function Project.get_package_name_in_pipfile refactored with the following changes:
- Use the built-in function
nextinstead of a for-loop (use-next)
| # Read and append Pipfile. | ||
| name = self.get_package_name_in_pipfile(package_name, category=category) | ||
| p = self.parsed_pipfile | ||
| if name: | ||
| if name := self.get_package_name_in_pipfile( | ||
| package_name, category=category | ||
| ): | ||
| p = self.parsed_pipfile |
There was a problem hiding this comment.
Function Project.remove_package_from_pipfile refactored with the following changes:
- Use named expression to simplify assignment and conditional (
use-named-expression) - Move assignments closer to their usage (
move-assign)
This removes the following comments ( why? ):
# Read and append Pipfile.
| packages = set([pep423_name(pkg) for pkg in packages]) | ||
| packages = {pep423_name(pkg) for pkg in packages} | ||
| for category in self.get_package_categories(): | ||
| pipfile_section = parsed.get(category, {}) | ||
| pipfile_packages = set( | ||
| [pep423_name(pkg_name) for pkg_name in pipfile_section.keys()] | ||
| ) | ||
| pipfile_packages = { | ||
| pep423_name(pkg_name) for pkg_name in pipfile_section.keys() | ||
| } |
There was a problem hiding this comment.
Function Project.remove_packages_from_pipfile refactored with the following changes:
- Replace list(), dict() or set() with comprehension [×2] (
collection-builtin-to-comprehension) - Replace unneeded comprehension with generator [×2] (
comprehension-to-generator)
Sourcery Code Quality Report❌ Merging this PR will decrease code quality in the affected files by 0.05%.
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
mainrefactored 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
mainbranch, then run:Help us improve this pull request!