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-BirkenbachandClaude Opus 5 committed 2026-07-27 16:47:13 +02:00
1 parent 14b95d5639
commit b4e0594901
187 files changed
+591 -856

No files matched your search

@@ -1,5 +1,3 @@
#!/usr/bin/env python3
"""
Unit tests for pkgmgr.cli.commands.release.
@@ -16,7 +14,6 @@ from __future__ import annotations
import argparse
import unittest
from types import SimpleNamespace
from typing import List
from unittest.mock import call, patch
@@ -25,7 +22,7 @@ class TestReleaseCommand(unittest.TestCase):
Tests for the `pkgmgr release` CLI wiring.
"""
def _make_ctx(self, all_repos: List[dict]) -> SimpleNamespace:
def _make_ctx(self, all_repos: list[dict]) -> SimpleNamespace:
"""
Create a minimal CLIContext-like object for tests.
@@ -39,7 +36,7 @@ class TestReleaseCommand(unittest.TestCase):
user_config_path="/tmp/config.yaml",
)
def _parse_release_args(self, argv: List[str]) -> argparse.Namespace:
def _parse_release_args(self, argv: list[str]) -> argparse.Namespace:
"""
Build a real top-level parser and parse the given argv list
to obtain the Namespace for the `release` command.
+7 -9
View File
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
"""
Unit tests for pkgmgr.cli.commands.repos
@@ -26,17 +24,17 @@ import io
import unittest
from contextlib import redirect_stdout
from types import SimpleNamespace
from typing import Any, Dict, List
from typing import Any
from unittest.mock import patch
from pkgmgr.cli.commands.repos import handle_repos_command
from pkgmgr.cli.context import CLIContext
Repository = Dict[str, Any]
Repository = dict[str, Any]
class TestReposCommand(unittest.TestCase):
def _make_ctx(self, repositories: List[Repository]) -> CLIContext:
def _make_ctx(self, repositories: list[Repository]) -> CLIContext:
"""
Helper to build a minimal CLIContext for tests.
"""
@@ -57,7 +55,7 @@ class TestReposCommand(unittest.TestCase):
When repository["directory"] is present, handle_repos_command("path")
should print this value directly without calling get_repo_dir().
"""
repos: List[Repository] = [
repos: list[Repository] = [
{
"provider": "github.com",
"account": "kevinveenbirkenbach",
@@ -93,7 +91,7 @@ class TestReposCommand(unittest.TestCase):
should call get_repo_dir(ctx.repositories_base_dir, repo) and print
the returned value.
"""
repos: List[Repository] = [
repos: list[Repository] = [
{
"provider": "github.com",
"account": "kevinveenbirkenbach",
@@ -155,7 +153,7 @@ class TestReposCommand(unittest.TestCase):
'shell' should resolve the repository directory and pass it as cwd
to run_command(), along with the full shell command string.
"""
repos: List[Repository] = [
repos: list[Repository] = [
{
"provider": "github.com",
"account": "kevinveenbirkenbach",
@@ -196,7 +194,7 @@ class TestReposCommand(unittest.TestCase):
"""
'shell' without -c/--command should print an error and exit with code 2.
"""
repos: List[Repository] = []
repos: list[Repository] = []
ctx = self._make_ctx(repos)
args = SimpleNamespace(
+3 -3
View File
@@ -2,12 +2,12 @@ from __future__ import annotations
import unittest
from types import SimpleNamespace
from typing import Any, Dict, List
from typing import Any
from unittest.mock import call, patch
from pkgmgr.cli.commands.tools import handle_tools_command
Repository = Dict[str, Any]
Repository = dict[str, Any]
class _Args:
@@ -29,7 +29,7 @@ class TestHandleToolsCommand(unittest.TestCase):
"""
def setUp(self) -> None:
self.repos: List[Repository] = [
self.repos: list[Repository] = [
{"alias": "repo1", "directory": "/tmp/repo1"},
{"alias": "repo2", "directory": "/tmp/repo2"},
]
+3 -5
View File
@@ -1,5 +1,3 @@
#!/usr/bin/env python3
"""
Unit tests for the pkgmgr CLI (version command).
@@ -23,13 +21,13 @@ import tempfile
import textwrap
import unittest
from contextlib import redirect_stdout
from typing import Any, Dict, List
from typing import Any
from unittest import mock
from pkgmgr import cli
def _fake_config() -> Dict[str, Any]:
def _fake_config() -> dict[str, Any]:
"""
Provide a minimal configuration dict sufficient for cli.main()
to start without touching real config files.
@@ -132,7 +130,7 @@ class TestCliVersion(unittest.TestCase):
def _run_cli_version_and_capture(
self,
extra_args: List[str] | None = None,
extra_args: list[str] | None = None,
) -> str:
"""
Run 'pkgmgr version [extra_args]' via cli.main() and return captured stdout.
+1 -1
View File
@@ -45,7 +45,7 @@ class TestCliBranch(unittest.TestCase):
handle_branch(args, ctx)
mock_open_branch.assert_called_once()
call_args, call_kwargs = mock_open_branch.call_args
_call_args, call_kwargs = mock_open_branch.call_args
self.assertEqual(call_kwargs.get("name"), "feature/cli-test")
self.assertEqual(call_kwargs.get("base_branch"), "develop")
self.assertEqual(call_kwargs.get("cwd"), ".")
+13 -11
View File
@@ -5,10 +5,10 @@ import os
import tempfile
import unittest
from types import SimpleNamespace
from typing import Any, Dict, List
from typing import Any
from unittest.mock import patch
Repository = Dict[str, Any]
Repository = dict[str, Any]
class TestOpenVSCodeWorkspace(unittest.TestCase):
@@ -27,13 +27,15 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
from pkgmgr.cli.tools.vscode import open_vscode_workspace
ctx = SimpleNamespace(config_merged={}, all_repositories=[])
selected: List[Repository] = [
selected: list[Repository] = [
{"provider": "github.com", "account": "x", "repository": "y"}
]
with patch("pkgmgr.cli.tools.vscode.shutil.which", return_value=None):
with self.assertRaises(RuntimeError) as cm:
open_vscode_workspace(ctx, selected)
with (
patch("pkgmgr.cli.tools.vscode.shutil.which", return_value=None),
self.assertRaises(RuntimeError) as cm,
):
open_vscode_workspace(ctx, selected)
self.assertIn("VS Code CLI ('code') not found", str(cm.exception))
@@ -44,7 +46,7 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
config_merged={"directories": {"workspaces": "~/Workspaces"}},
all_repositories=[],
)
selected: List[Repository] = [
selected: list[Repository] = [
{"provider": "github.com", "account": "x", "repository": "y"}
]
@@ -74,7 +76,7 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
all_repositories=[],
repositories_base_dir=os.path.join(tmp, "Repos"),
)
selected: List[Repository] = [
selected: list[Repository] = [
{
"provider": "github.com",
"account": "kevin",
@@ -101,7 +103,7 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
workspace_file = os.path.join(workspaces_dir, "dotlinker.code-workspace")
self.assertTrue(os.path.exists(workspace_file))
with open(workspace_file, "r", encoding="utf-8") as f:
with open(workspace_file, encoding="utf-8") as f:
data = json.load(f)
self.assertEqual(data["folders"], [{"path": repo_path}])
@@ -125,7 +127,7 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
config_merged={"directories": {"workspaces": workspaces_dir}},
all_repositories=[],
)
selected: List[Repository] = [
selected: list[Repository] = [
{
"provider": "github.com",
"account": "kevin",
@@ -149,7 +151,7 @@ class TestOpenVSCodeWorkspace(unittest.TestCase):
):
open_vscode_workspace(ctx, selected)
with open(workspace_file, "r", encoding="utf-8") as f:
with open(workspace_file, encoding="utf-8") as f:
data = json.load(f)
self.assertEqual(data, original)