Performance: ruff rules C4 and PERF

This commit is contained in:
Christian Clauss
2023-08-22 13:48:41 +02:00
committed by Oz Tiram
parent dc5430144d
commit e23890e105
28 changed files with 57 additions and 77 deletions
+2 -4
View File
@@ -171,8 +171,7 @@ def categories_option(f):
def callback(ctx, param, value):
state = ctx.ensure_object(State)
if value:
for opt in re.split(r", *| ", value):
state.installstate.categories.append(opt)
state.installstate.categories += re.split(r", *| ", value)
return value
return option(
@@ -235,8 +234,7 @@ def extra_pip_args(f):
def callback(ctx, param, value):
state = ctx.ensure_object(State)
if value:
for opt in value.split(" "):
state.installstate.extra_pip_args.append(opt)
state.installstate.extra_pip_args += value.split(" ")
return value
return option(
+10 -9
View File
@@ -346,16 +346,17 @@ class Environment:
"value = u'{0}'.format(json.dumps(paths)); print(value)"
)
sysconfig_line = "sysconfig.get_path('{0}')"
if python_lib:
for key in ("purelib", "platlib", "stdlib", "platstdlib"):
pylib_lines.append(
f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})"
)
pylib_lines += [
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 += [
f"u'{key}': u'{{0}}'.format({sysconfig_line.format(key)})"
for key in ("include", "platinclude")
]
lines = pylib_lines + pyinc_lines
if scripts:
lines.append(
@@ -810,7 +811,7 @@ class Environment:
raise OSError(f"No such file: {activate_this!s}")
with open(activate_this) as f:
code = compile(f.read(), activate_this, "exec")
exec(code, dict(__file__=activate_this))
exec(code, {"__file__": activate_this})
@contextlib.contextmanager
def activated(self):
+4 -4
View File
@@ -1095,12 +1095,12 @@ class Project:
def remove_packages_from_pipfile(self, packages):
parsed = self.parsed_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()
}
to_remove = packages & pipfile_packages
for pkg in to_remove:
pkg_name = self.get_package_name_in_pipfile(pkg, category=category)
+1 -3
View File
@@ -96,9 +96,7 @@ def do_graph(project, bare=False, json=False, json_tree=False, reverse=False):
except jsonlib.JSONDecodeError:
raise exceptions.JSONParseError(c.stdout, c.stderr)
else:
for d in parsed:
if d["package"]["key"] not in BAD_PACKAGES:
data.append(d)
data += [d for d in parsed if d["package"]["key"] not in BAD_PACKAGES]
click.echo(simplejson.dumps(data, indent=4))
sys.exit(0)
elif json_tree:
+2 -2
View File
@@ -152,7 +152,7 @@ def do_install(
sys.exit(1)
# Allow more than one package to be provided.
package_args = [p for p in packages] + [f"-e {pkg}" for pkg in editable_packages]
package_args = list(packages) + [f"-e {pkg}" for pkg in editable_packages]
# Install all dependencies, if none was provided.
# This basically ensures that we have a pipfile and lockfile, then it locks and
# installs from the lockfile
@@ -550,7 +550,7 @@ def batch_install(
allow_global=allow_global,
extra_pip_args=extra_pip_args,
)
except StopIteration:
except StopIteration: # noqa: PERF203
console.print(
f"Unable to find {index_name} in sources, please check dependencies: {dependencies}",
style="bold red",
+2 -2
View File
@@ -101,8 +101,8 @@ def do_lock(
def overwrite_with_default(default, dev):
dev_keys = set(list(dev.keys()))
prod_keys = set(list(default.keys()))
dev_keys = set(dev.keys())
prod_keys = set(default.keys())
for pkg in dev_keys & prod_keys:
dev[pkg] = default[pkg]
return dev
+1 -1
View File
@@ -42,7 +42,7 @@ def do_outdated(project, pypi_mirror=None, pre=False, clear=False):
for package in lockfile.get(category, []):
try:
updated_packages[package] = lockfile[category][package]["version"]
except KeyError:
except KeyError: # noqa: PERF203
pass
outdated = []
skipped = []
+1 -1
View File
@@ -86,7 +86,7 @@ def do_uninstall(
# Remove known "bad packages" from the list.
bad_pkgs = get_canonical_names(BAD_PACKAGES)
ignored_packages = bad_pkgs & set(list(package_map.keys()))
ignored_packages = bad_pkgs & set(package_map.keys())
for ignored_pkg in get_canonical_names(ignored_packages):
if project.s.is_verbose():
click.echo(f"Ignoring {ignored_pkg}.", err=True)
+2 -2
View File
@@ -123,7 +123,7 @@ def upgrade(
if index_url:
index_name = add_index_to_pipfile(project, index_url)
package_args = [p for p in packages] + [f"-e {pkg}" for pkg in editable_packages]
package_args = list(packages) + [f"-e {pkg}" for pkg in editable_packages]
requested_install_reqs = defaultdict(dict)
requested_packages = defaultdict(dict)
@@ -184,7 +184,7 @@ def upgrade(
pypi_mirror=pypi_mirror,
)
# Mutate the existing lockfile with the upgrade data for the categories
for package_name, _ in upgrade_lock_data.items():
for package_name in upgrade_lock_data.keys():
correct_package_lock = full_lock_resolution.get(package_name)
if correct_package_lock:
lockfile[category][package_name] = correct_package_lock
+2 -2
View File
@@ -1054,7 +1054,7 @@ def get_constraints_from_deps(deps):
else:
c = canonicalize_name(dep_name)
else:
if not any([k in dep_version for k in ["path", "file", "uri"]]):
if not any(k in dep_version for k in ["path", "file", "uri"]):
if dep_version.get("skip_resolver") is True:
continue
version = dep_version.get("version", None)
@@ -1097,7 +1097,7 @@ def prepare_constraint_file(
constraints_file.write(f"{requirementstxt_sources}\n")
if constraints:
constraints_file.write("\n".join([c for c in constraints]))
constraints_file.write("\n".join(list(constraints)))
constraints_file.close()
return constraints_file.name
+2 -2
View File
@@ -184,11 +184,11 @@ def temp_path():
'/home/user/.pyenv/versions/3.7.0/lib/python3.7/site-packages'
]
"""
path = [p for p in sys.path]
path = list(sys.path)
try:
yield
finally:
sys.path = [p for p in path]
sys.path = list(path)
TRACKED_TEMPORARY_DIRECTORIES = []
+1 -1
View File
@@ -298,7 +298,7 @@ def set_write_bit(fn: str) -> None:
for path in [fn, os.path.dirname(fn)]:
try:
os.chflags(path, 0)
except AttributeError:
except AttributeError: # noqa: PERF203
pass
return None
for root, dirs, files in os.walk(fn, topdown=False):
+1 -3
View File
@@ -138,6 +138,4 @@ class PackageIndexHTMLParser(HTMLParser):
# If tag is an anchor
if tag == "a":
# find href attribute
for attr in attrs:
if attr[0] == "href":
self.urls.append(attr[1])
self.urls += [attr[1] for attr in attrs if attr[0] == "href"]
+1 -1
View File
@@ -259,7 +259,7 @@ class Lockfile(BaseModel):
@property
def extended_keys(self):
return [k for k in itertools.product(self.section_keys, ["", "vcs", "editable"])]
return list(itertools.product(self.section_keys, ["", "vcs", "editable"]))
def get(self, k):
return self.__getitem__(k)
+6 -11
View File
@@ -62,7 +62,7 @@ class PipenvMarkers(BaseModel):
@classmethod
def from_pipfile(cls, name, pipfile):
attr_fields = [field_name for field_name in cls.__fields__]
attr_fields = list(cls.__fields__)
found_keys = [k for k in pipfile.keys() if k in attr_fields]
marker_strings = [f"{k} {pipfile[k]}" for k in found_keys]
if pipfile.get("markers"):
@@ -163,8 +163,7 @@ def _get_specs(specset):
if op in ("in", "not in"):
versions = version.split(",")
op = "==" if op == "in" else "!="
for ver in versions:
result.append((op, _tuplize_version(ver.strip())))
result += [(op, _tuplize_version(ver.strip())) for ver in versions]
else:
result.append((spec.operator, _tuplize_version(spec.version)))
return sorted(result, key=operator.itemgetter(1))
@@ -617,11 +616,7 @@ def merge_markers(m1, m2):
# type: (Marker, Marker) -> Optional[Marker]
if not all((m1, m2)):
return next(iter(v for v in (m1, m2) if v), None)
m1 = _ensure_marker(m1)
m2 = _ensure_marker(m2)
_markers = [] # type: List[Marker]
for marker in (m1, m2):
_markers.append(str(marker))
_markers = [str(_ensure_marker(marker)) for marker in (m1, m2)]
marker_str = " and ".join([normalize_marker_str(m) for m in _markers if m])
return _ensure_marker(normalize_marker_str(marker_str))
@@ -654,9 +649,9 @@ def marker_from_specifier(spec) -> Marker:
spec = "=={}".format(spec.lstrip("="))
if not spec:
return None
marker_segments = []
for marker_segment in cleanup_pyspecs(spec):
marker_segments.append(format_pyversion(marker_segment))
marker_segments = [
format_pyversion(marker_segment) for marker_segment in cleanup_pyspecs(spec)
]
marker_str = " and ".join(marker_segments).replace('"', "'")
return Marker(marker_str)
+1 -2
View File
@@ -139,8 +139,7 @@ def get_pip_args(
for key in arg_map.keys():
if key in locals() and locals().get(key):
arg_set.extend(arg_map.get(key))
for extra_pip_arg in extra_pip_args:
arg_set.append(extra_pip_arg)
arg_set += extra_pip_args or []
return list(dict.fromkeys(arg_set))
+3 -6
View File
@@ -301,12 +301,9 @@ class Pipfile(BaseModel):
@property
def extended_keys(self):
return [
k
for k in itertools.product(
("packages", "dev-packages"), ("", "vcs", "editable")
)
]
return list(
itertools.product(("packages", "dev-packages"), ("", "vcs", "editable"))
)
def get_deps(self, dev=False, only=True):
deps = {} # type: Dict[Text, Dict[Text, Union[List[Text], Text]]]
+1 -1
View File
@@ -371,7 +371,7 @@ def get_path(root, path, default=_UNSET):
for seg in path:
try:
cur = cur[seg]
except (KeyError, IndexError) as exc:
except (KeyError, IndexError) as exc: # noqa: PERF203
raise PathAccessError(exc, seg, path)
except TypeError:
# either string index in a list, or a parent that
+1 -4
View File
@@ -377,10 +377,7 @@ class Resolver:
)
for c in self.parsed_default_constraints
]
default_constraints = []
for c in possible_default_constraints:
default_constraints.append(c)
return set(default_constraints)
return set(possible_default_constraints)
@property
def possible_constraints(self):
+2 -2
View File
@@ -310,11 +310,11 @@ def is_python_command(line):
@contextmanager
def temp_path():
"""Allow the ability to set os.environ temporarily"""
path = [p for p in sys.path]
path = list(sys.path)
try:
yield
finally:
sys.path = [p for p in path]
sys.path = list(path)
def is_readonly_path(fn):
+1 -6
View File
@@ -17,13 +17,8 @@ TOML_DICT_NAMES = [o.__class__.__name__ for o in TOML_DICT_OBJECTS]
def cleanup_toml(tml):
toml = tml.split("\n")
new_toml = []
# Remove all empty lines from TOML.
for line in toml:
if line.strip():
new_toml.append(line)
toml = "\n".join(new_toml)
toml = "\n".join(line for line in tml.split("\n") if line.strip())
new_toml = []
# Add newlines between TOML sections.
for i, line in enumerate(toml.split("\n")):
+1 -1
View File
@@ -425,7 +425,7 @@ def _inline_activate_virtualenv(project):
raise exceptions.VirtualenvActivationException()
with open(activate_this) as f:
code = compile(f.read(), activate_this, "exec")
exec(code, dict(__file__=activate_this))
exec(code, {"__file__": activate_this})
# Catch all errors, just in case.
except Exception:
click.echo(
+3 -1
View File
@@ -95,13 +95,15 @@ exclude = [
select = [
"ASYNC",
"B",
"C9",
"C4",
"C90",
"E",
"F",
"FLY",
"G",
"I",
"ISC",
"PERF",
"PIE",
"PL",
"TID",
+2 -2
View File
@@ -265,7 +265,7 @@ def rename_if_needed(ctx, vendor_dir, item):
def _ensure_package_in_requirements(ctx, requirements_file, package):
requirement = None
log("using requirements file: %s" % requirements_file)
req_file_lines = [line for line in requirements_file.read_text().splitlines()]
req_file_lines = list(requirements_file.read_text().splitlines())
if package:
match = [r for r in req_file_lines if r.strip().lower().startswith(package)]
matched_req = None
@@ -720,7 +720,7 @@ def unpin_and_copy_requirements(ctx, requirement_file, name="requirements.txt"):
ctx.run(f"pipenv install -r {target.as_posix()}", env=env, hide=True)
result = ctx.run("pipenv lock -r", env=env, hide=True).stdout.strip()
ctx.run("pipenv --rm", env=env, hide=True)
result = list(sorted(line.strip() for line in result.splitlines()[1:]))
result = sorted(line.strip() for line in result.splitlines()[1:])
new_requirements = requirement_file.parent.joinpath(name)
requirement_file.rename(requirement_file.parent.joinpath(f"{name}.bak"))
new_requirements.write_text("\n".join(result))
+1 -1
View File
@@ -38,7 +38,7 @@ def check_internet():
for url in ("http://httpbin.org/ip", "http://clients3.google.com/generate_204"):
try:
try_internet(url)
except KeyboardInterrupt:
except KeyboardInterrupt: # noqa: PERF203
warnings.warn(
f"Skipped connecting to internet: {url}", RuntimeWarning, stacklevel=1
)
+1 -1
View File
@@ -187,7 +187,7 @@ def test_local_tar_gz_file(pipenv_instance_private_pypi, testsroot):
# This tests for a bug when installing a zipfile
c = p.pipenv(f"install {requests_path}")
assert c.returncode == 0
key = [k for k in p.pipfile["packages"].keys()][0]
key = list(p.pipfile["packages"].keys())[0]
dep = p.pipfile["packages"][key]
assert "file" in dep or "path" in dep
+1 -1
View File
@@ -628,7 +628,7 @@ six = "*"
c = p.pipenv("lock --categories prereq")
assert c.returncode == 0
assert p.lockfile["prereq"]["six"]["index"] == "test"
assert p.lockfile["default"] == dict()
assert p.lockfile["default"] == {}
c = p.pipenv("lock --categories packages")
assert c.returncode == 0
assert p.lockfile["prereq"]["six"]["index"] == "test"
+1 -1
View File
@@ -125,7 +125,7 @@ def test_convert_deps_to_pip_one_way(deps, expected):
@pytest.mark.utils
def test_convert_deps_to_pip_one_way():
deps = {"uvicorn": dict()}
deps = {"uvicorn": {}}
expected = "uvicorn"
assert dependencies.convert_deps_to_pip(deps) == [expected.lower()]