style: modernise typing and clean up lint findings

Repository-wide mechanical cleanup so `ruff check src tests` has a chance
of passing; no behavioural changes.

- Add `from __future__ import annotations` where PEP 604 unions are used.
  This has to come first: pyproject declares requires-python >= 3.9, where
  `X | None` is not evaluable at runtime unless annotations are stringified.
- Replace typing.List/Dict/Tuple/Set with the builtin generics and
  Optional[X] with X | None, then drop the imports that became unused.
  The four actions/*/__init__.py files needed this by hand because ruff
  leaves unused imports in __init__.py alone (possible re-exports).
- Strip shebangs from 72 importable modules. None of them are executable
  or invoked directly; the entry points are console_scripts and runpy.
- Flatten nested `with` blocks, collapse needless-bool returns, and apply
  the remaining mechanical ruff fixes (PIE810, FLY002, PERF102, FURB192,
  RUF059, I001).
- Pass check=False explicitly to the four subprocess.run() calls that
  inspect returncode themselves. That is the existing default.

Two rewrites are visible to mocks, so their tests move with them:
subprocess.run(stdout=PIPE, stderr=PIPE) became capture_output=True, and
open(path, "r", ...) lost the redundant mode.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kevin Veen-Birkenbach
2026-07-27 16:47:13 +02:00
parent 14b95d5639
commit b4e0594901
187 changed files with 591 additions and 856 deletions

View File

@@ -54,9 +54,11 @@ class TestOpenBranch(unittest.TestCase):
push_upstream.assert_called_once_with("origin", "auto-branch", cwd=".")
def test_open_branch_rejects_empty_name(self) -> None:
with patch("builtins.input", return_value=""):
with self.assertRaises(RuntimeError):
open_branch(None)
with (
patch("builtins.input", return_value=""),
self.assertRaises(RuntimeError),
):
open_branch(None)
if __name__ == "__main__":

View File

@@ -87,9 +87,11 @@ class TestDownloadCodeScanning(unittest.TestCase):
)
def test_errors_when_gh_missing(self) -> None:
with mock.patch("pkgmgr.actions.code_scanning.shutil.which", return_value=None):
with self.assertRaises(CodeScanningError):
download_code_scanning(output_dir="/tmp/should-not-be-created")
with (
mock.patch("pkgmgr.actions.code_scanning.shutil.which", return_value=None),
self.assertRaises(CodeScanningError),
):
download_code_scanning(output_dir="/tmp/should-not-be-created")
if __name__ == "__main__": # pragma: no cover

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Optional
from typing import Any
@dataclass
@@ -18,7 +18,7 @@ class FakeRunner:
- Generic runner.run(ctx, cmd, allow_failure=...)
"""
def __init__(self, mapping: Optional[dict[str, Any]] = None, default: Any = None):
def __init__(self, mapping: dict[str, Any] | None = None, default: Any = None):
self.mapping = mapping or {}
self.default = default if default is not None else FakeRunResult(0, "", "")
self.calls: list[tuple[Any, str, bool]] = []

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
"""
Unit tests for NixFlakeInstaller using unittest (no pytest).
@@ -19,7 +17,6 @@ import subprocess
import tempfile
import unittest
from contextlib import redirect_stdout
from typing import List
from unittest.mock import patch
from pkgmgr.actions.install.installers.nix import NixFlakeInstaller
@@ -73,8 +70,8 @@ class TestNixFlakeInstaller(unittest.TestCase):
which_patch.return_value = "/usr/bin/nix"
@staticmethod
def _install_cmds_from_calls(call_args_list) -> List[str]:
cmds: List[str] = []
def _install_cmds_from_calls(call_args_list) -> list[str]:
cmds: list[str] = []
for c in call_args_list:
if not c.args:
continue

View File

@@ -50,7 +50,6 @@ class TestMakefileInstaller(unittest.TestCase):
# Ensure we checked the Makefile and then called make install.
mock_file.assert_called_once_with(
os.path.join(self.ctx.repo_dir, "Makefile"),
"r",
encoding="utf-8",
errors="ignore",
)
@@ -77,7 +76,6 @@ class TestMakefileInstaller(unittest.TestCase):
# We should have read the Makefile, but not called run_command().
mock_file.assert_called_once_with(
os.path.join(self.ctx.repo_dir, "Makefile"),
"r",
encoding="utf-8",
errors="ignore",
)

View File

@@ -1,13 +1,11 @@
#!/usr/bin/env python3
import os
import unittest
from typing import Any, Dict, List
from typing import Any
from unittest.mock import MagicMock, patch
from pkgmgr.actions.install import install_repos
Repository = Dict[str, Any]
Repository = dict[str, Any]
class TestInstallReposOrchestration(unittest.TestCase):
@@ -27,7 +25,7 @@ class TestInstallReposOrchestration(unittest.TestCase):
"alias": "repo-two",
"verified": {"gpg_keys": ["FAKEKEY"]},
}
self.all_repos: List[Repository] = [self.repo1, self.repo2]
self.all_repos: list[Repository] = [self.repo1, self.repo2]
@patch("pkgmgr.actions.install.InstallationPipeline")
@patch("pkgmgr.actions.install.clone_repos")

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
import os
import unittest

View File

@@ -1,4 +1,4 @@
#!/usr/bin/env python3
from __future__ import annotations
import unittest
from unittest.mock import MagicMock, patch

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import unittest

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import io

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import os
@@ -61,13 +59,7 @@ class TestMirrorIO(unittest.TestCase):
p = os.path.join(tmpdir, "MIRRORS")
with open(p, "w", encoding="utf-8") as fh:
fh.write(
"\n".join(
[
"https://github.com/alice/repo1",
"https://github.com/alice/repo2",
"ssh://git@git.veen.world:2201/alice/repo3.git",
]
)
"https://github.com/alice/repo1\nhttps://github.com/alice/repo2\nssh://git@git.veen.world:2201/alice/repo3.git"
+ "\n"
)
@@ -99,7 +91,7 @@ class TestMirrorIO(unittest.TestCase):
p = os.path.join(tmpdir, "MIRRORS")
self.assertTrue(os.path.exists(p))
with open(p, "r", encoding="utf-8") as fh:
with open(p, encoding="utf-8") as fh:
content = fh.read()
self.assertEqual(

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import io

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import unittest

View File

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
from __future__ import annotations
import unittest

View File

@@ -37,7 +37,7 @@ class TestUpdatePyprojectVersion(unittest.TestCase):
update_pyproject_version(path, "1.2.3", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn('version = "1.2.3"', content)
@@ -62,7 +62,7 @@ class TestUpdatePyprojectVersion(unittest.TestCase):
update_pyproject_version(path, "1.2.3", preview=True)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -129,7 +129,7 @@ class TestUpdateFlakeVersion(unittest.TestCase):
update_flake_version(path, "1.2.3", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn('version = "1.2.3";', content)
@@ -144,7 +144,7 @@ class TestUpdateFlakeVersion(unittest.TestCase):
update_flake_version(path, "1.2.3", preview=True)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -170,7 +170,7 @@ class TestUpdatePkgbuildVersion(unittest.TestCase):
update_pkgbuild_version(path, "1.2.3", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("pkgver=1.2.3", content)
@@ -196,7 +196,7 @@ class TestUpdatePkgbuildVersion(unittest.TestCase):
update_pkgbuild_version(path, "1.2.3", preview=True)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -222,7 +222,7 @@ class TestUpdateSpecVersion(unittest.TestCase):
update_spec_version(path, "1.2.3", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("Version: 1.2.3", content)
@@ -249,7 +249,7 @@ class TestUpdateSpecVersion(unittest.TestCase):
update_spec_version(path, "1.2.3", preview=True)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -264,7 +264,7 @@ class TestUpdateChangelog(unittest.TestCase):
update_changelog(path, "1.2.3", message="First release", preview=False)
self.assertTrue(os.path.exists(path))
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
# New file must lead with an H1 so markdownlint MD041 is happy.
@@ -280,7 +280,7 @@ class TestUpdateChangelog(unittest.TestCase):
update_changelog(path, "1.0.0", message="Second release", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
# H1 still on top, new entry above the existing one.
@@ -303,7 +303,7 @@ class TestUpdateChangelog(unittest.TestCase):
update_changelog(path, "1.0.0", message=None, preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
# An H1 is added so MD041 is satisfied even for legacy files.
@@ -315,7 +315,7 @@ class TestUpdateChangelog(unittest.TestCase):
path = os.path.join(tmpdir, "CHANGELOG.md")
update_changelog(path, "1.2.3", message="* Provided bullet", preview=False)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("\n\n* Provided bullet\n", content)
@@ -331,7 +331,7 @@ class TestUpdateChangelog(unittest.TestCase):
preview=False,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("**Summary**", content)
@@ -348,7 +348,7 @@ class TestUpdateChangelog(unittest.TestCase):
update_changelog(path, "1.0.0", message="Preview only", preview=True)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -374,7 +374,7 @@ class TestUpdateDebianChangelog(unittest.TestCase):
preview=False,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("package-manager (1.2.3-1) unstable; urgency=medium", content)
@@ -402,7 +402,7 @@ class TestUpdateDebianChangelog(unittest.TestCase):
preview=True,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -428,7 +428,7 @@ class TestUpdateDebianChangelog(unittest.TestCase):
preview=False,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn(" * First bullet", content)
@@ -484,7 +484,7 @@ class TestUpdateSpecChangelog(unittest.TestCase):
preview=False,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("%changelog", content)
@@ -526,7 +526,7 @@ class TestUpdateSpecChangelog(unittest.TestCase):
preview=True,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertEqual(content, original)
@@ -567,7 +567,7 @@ class TestUpdateSpecChangelog(unittest.TestCase):
preview=False,
)
with open(path, "r", encoding="utf-8") as f:
with open(path, encoding="utf-8") as f:
content = f.read()
self.assertIn("- * First bullet", content)

View File

@@ -11,7 +11,6 @@ import os
import tempfile
import unittest
from pathlib import Path
from typing import Optional
from pkgmgr.actions.release.package_name import resolve_package_name
from pkgmgr.core.repository.paths import RepoPaths
@@ -20,9 +19,9 @@ from pkgmgr.core.repository.paths import RepoPaths
def _paths(
repo_dir: str,
*,
debian_control: Optional[str] = None,
arch_pkgbuild: Optional[str] = None,
rpm_spec: Optional[str] = None,
debian_control: str | None = None,
arch_pkgbuild: str | None = None,
rpm_spec: str | None = None,
) -> RepoPaths:
return RepoPaths(
repo_dir=repo_dir,

View File

@@ -36,9 +36,7 @@ class TestDeinstallRepos(unittest.TestCase):
def exists_side_effect(path):
if path == "/home/u/.local/bin/demo":
return True
if path == "/repos/github.com/alice/demo/Makefile":
return True
return False
return path == "/repos/github.com/alice/demo/Makefile"
mock_exists.side_effect = exists_side_effect