Merge pull request 'ruff-4-perf-fixes' (#4935) from ruff-4-perf-fixes into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4935
This commit is contained in:
Mic92
2025-08-25 13:12:14 +00:00
11 changed files with 79 additions and 87 deletions

View File

@@ -14,7 +14,6 @@ class Backup:
def list_provider(machine: Machine, host: Remote, provider: str) -> list[Backup]:
results = []
backup_metadata = machine.select("config.clan.core.backups")
list_command = backup_metadata["providers"][provider]["list"]
proc = host.run(
@@ -35,8 +34,11 @@ def list_provider(machine: Machine, host: Remote, provider: str) -> list[Backup]
msg = f"Failed to parse json output from provider {provider}:\n{proc.stdout}"
raise ClanError(msg) from e
for archive in parsed_json:
results.append(Backup(name=archive["name"], job_name=archive.get("job_name")))
results: list[Backup] = []
results.extend(
Backup(name=archive["name"], job_name=archive.get("job_name"))
for archive in parsed_json
)
return results

View File

@@ -444,8 +444,9 @@ class FlakeCacheEntry:
if not isinstance(selector.value, list):
msg = f"Expected list for SET selector value, got {type(selector.value)}"
raise ClanError(msg)
for subselector in selector.value:
fetched_indices.append(subselector.value)
fetched_indices.extend(
subselector.value for subselector in selector.value
)
# if it's just a str, that is the index
elif selector.type == SelectorType.STR:
if not isinstance(selector.value, str):
@@ -635,9 +636,9 @@ class FlakeCacheEntry:
keys_to_select: list[str] = []
# if we want to select all keys, we take all existing sub elements
if selector.type == SelectorType.ALL:
for key in self.value:
if self.value[key].exists:
keys_to_select.append(key)
keys_to_select.extend(
key for key in self.value if self.value[key].exists
)
# if we want to select a set of keys, we take the keys from the selector
if selector.type == SelectorType.SET:
@@ -657,9 +658,9 @@ class FlakeCacheEntry:
# if we are a list, return a list
if self.is_list:
result_list: list[Any] = []
for index in keys_to_select:
result_list.append(self.value[index].select(selectors[1:]))
result_list: list[Any] = [
self.value[index].select(selectors[1:]) for index in keys_to_select
]
return result_list
# otherwise return a dict
@@ -681,12 +682,10 @@ class FlakeCacheEntry:
if selector.type == SelectorType.ALL:
str_selector = "*"
elif selector.type == SelectorType.SET:
subselectors: list[str] = []
if not isinstance(selector.value, list):
msg = f"Expected list for SET selector value in error handling, got {type(selector.value)}"
raise ClanError(msg)
for subselector in selector.value:
subselectors.append(subselector.value)
subselectors = [subselector.value for subselector in selector.value]
str_selector = "{" + ",".join(subselectors) + "}"
else:
if not isinstance(selector.value, str):
@@ -967,9 +966,9 @@ class Flake:
nix_options = self.nix_options[:] if self.nix_options is not None else []
str_selectors: list[str] = []
for selector in selectors:
str_selectors.append(selectors_as_json(parse_selector(selector)))
str_selectors = [
selectors_as_json(parse_selector(selector)) for selector in selectors
]
config = nix_config()
@@ -1079,10 +1078,9 @@ class Flake:
if self.flake_cache_path is None:
msg = "Flake cache path cannot be None"
raise ClanError(msg)
not_fetched_selectors = []
for selector in selectors:
if not self._cache.is_cached(selector):
not_fetched_selectors.append(selector)
not_fetched_selectors = [
selector for selector in selectors if not self._cache.is_cached(selector)
]
if not_fetched_selectors:
self.get_from_nix(not_fetched_selectors)

View File

@@ -133,12 +133,7 @@ def list_difference(all_items: list, filter_items: list) -> list:
"""
# Unmerge the lists
res = []
for value in all_items:
if value not in filter_items:
res.append(value)
return res
return [value for value in all_items if value not in filter_items]
def find_duplicates(string_list: list[str]) -> list[str]: