From c1570bf4f0a2db6e30f0e1dfa12063da3bb2a2b2 Mon Sep 17 00:00:00 2001 From: txltedXgod Date: Wed, 11 Nov 2026 11:16:21 +0100 Subject: [PATCH] fix(ci): modernize typing annotations, fix ruff & mypy checks, and update Python 3.10+ matrix --- .github/workflows/ci.yml | 8 ++++---- pyproject.toml | 7 +++---- src/taskpilot/actions/__init__.py | 2 +- src/taskpilot/actions/base.py | 9 +++++---- src/taskpilot/actions/builtins.py | 10 +++++----- src/taskpilot/cli.py | 4 ++-- src/taskpilot/config.py | 4 ++-- src/taskpilot/cron.py | 4 ++-- src/taskpilot/models.py | 4 ++-- src/taskpilot/runner.py | 5 ++--- src/taskpilot/scheduler.py | 14 +++++++------- 11 files changed, 35 insertions(+), 36 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c2c08a5..829d950 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: [main] + branches: [main, "fix/*"] pull_request: jobs: @@ -11,13 +11,13 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.9", "3.10", "3.11", "3.12"] + python-version: ["3.10", "3.11", "3.12"] steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v7 - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 + uses: actions/setup-python@v7 with: python-version: ${{ matrix.python-version }} cache: pip diff --git a/pyproject.toml b/pyproject.toml index 243e578..f4fb340 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ version = "0.1.0" description = "A lightweight, dependency-friendly automation & task-scheduling toolkit with cron schedules, pluggable actions, and a clean CLI." readme = "README.md" license = { text = "MIT" } -requires-python = ">=3.9" +requires-python = ">=3.10" authors = [{ name = "Seidou Takidzava" }] keywords = ["automation", "scheduler", "cron", "cli", "tasks", "jobs"] classifiers = [ @@ -17,7 +17,6 @@ classifiers = [ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", @@ -54,14 +53,14 @@ pythonpath = ["src"] [tool.ruff] line-length = 100 -target-version = "py39" +target-version = "py310" src = ["src", "tests"] [tool.ruff.lint] select = ["E", "F", "I", "UP", "B", "C4", "SIM"] [tool.mypy] -python_version = "3.9" +python_version = "3.10" warn_unused_ignores = true ignore_missing_imports = true files = ["src"] diff --git a/src/taskpilot/actions/__init__.py b/src/taskpilot/actions/__init__.py index baaa7c3..5fe34a7 100644 --- a/src/taskpilot/actions/__init__.py +++ b/src/taskpilot/actions/__init__.py @@ -7,7 +7,7 @@ from __future__ import annotations -from taskpilot.actions.base import Action, ActionContext, registry from taskpilot.actions import builtins as _builtins # noqa: F401 (registers actions) +from taskpilot.actions.base import Action, ActionContext, registry __all__ = ["Action", "ActionContext", "registry"] diff --git a/src/taskpilot/actions/base.py b/src/taskpilot/actions/base.py index e55a322..c415758 100644 --- a/src/taskpilot/actions/base.py +++ b/src/taskpilot/actions/base.py @@ -2,8 +2,9 @@ from __future__ import annotations +from collections.abc import Callable from dataclasses import dataclass, field -from typing import Any, Callable, Dict +from typing import Any @dataclass @@ -12,7 +13,7 @@ class ActionContext: task_name: str dry_run: bool = False - env: Dict[str, str] = field(default_factory=dict) + env: dict[str, str] = field(default_factory=dict) # An action is any callable taking (context, **params) and returning a str. @@ -23,7 +24,7 @@ class ActionRegistry: """Maps action names to callables.""" def __init__(self) -> None: - self._actions: Dict[str, Action] = {} + self._actions: dict[str, Action] = {} def register(self, name: str, action: Action | None = None): """Register an action. Usable directly or as a decorator.""" @@ -49,7 +50,7 @@ def get(self, name: str) -> Action: def names(self) -> list[str]: return sorted(self._actions) - def run(self, name: str, context: ActionContext, params: Dict[str, Any]) -> str: + def run(self, name: str, context: ActionContext, params: dict[str, Any]) -> str: return self.get(name)(context, **params) diff --git a/src/taskpilot/actions/builtins.py b/src/taskpilot/actions/builtins.py index 9cd0fc5..0ba75ea 100644 --- a/src/taskpilot/actions/builtins.py +++ b/src/taskpilot/actions/builtins.py @@ -13,7 +13,7 @@ import json import subprocess import urllib.request -from typing import Any, Optional +from typing import Any from taskpilot.actions.base import ActionContext, registry @@ -28,7 +28,7 @@ def log_action(context: ActionContext, message: str = "", **_: Any) -> str: def shell_action( context: ActionContext, command: str, - timeout: Optional[float] = 60.0, + timeout: float | None = 60.0, check: bool = True, **_: Any, ) -> str: @@ -57,7 +57,7 @@ def http_action( context: ActionContext, url: str, method: str = "GET", - headers: Optional[dict[str, str]] = None, + headers: dict[str, str] | None = None, body: Any = None, timeout: float = 30.0, **_: Any, @@ -66,7 +66,7 @@ def http_action( if context.dry_run: return f"[dry-run] would {method} {url}" - data: Optional[bytes] = None + data: bytes | None = None request_headers = dict(headers or {}) if body is not None: if isinstance(body, (dict, list)): @@ -87,7 +87,7 @@ def http_action( def webhook_action( context: ActionContext, url: str, - payload: Optional[dict[str, Any]] = None, + payload: dict[str, Any] | None = None, **_: Any, ) -> str: """POST a JSON *payload* to *url*. Handy for Slack/Telegram webhooks.""" diff --git a/src/taskpilot/cli.py b/src/taskpilot/cli.py index 81e8b65..81fd58a 100644 --- a/src/taskpilot/cli.py +++ b/src/taskpilot/cli.py @@ -14,7 +14,7 @@ import json import logging import sys -from typing import Optional, Sequence +from collections.abc import Sequence from taskpilot import __version__ from taskpilot.actions.base import registry @@ -61,7 +61,7 @@ def _build_parser() -> argparse.ArgumentParser: return parser -def main(argv: Optional[Sequence[str]] = None) -> int: +def main(argv: Sequence[str] | None = None) -> int: parser = _build_parser() args = parser.parse_args(argv) _configure_logging(getattr(args, "verbose", False)) diff --git a/src/taskpilot/config.py b/src/taskpilot/config.py index c1dfd37..43a3d77 100644 --- a/src/taskpilot/config.py +++ b/src/taskpilot/config.py @@ -20,14 +20,14 @@ import json from pathlib import Path -from typing import Any, Union +from typing import Any import yaml from taskpilot.cron import CronSchedule from taskpilot.models import Task -PathLike = Union[str, Path] +PathLike = str | Path class ConfigError(ValueError): diff --git a/src/taskpilot/cron.py b/src/taskpilot/cron.py index b24401e..a3dd3df 100644 --- a/src/taskpilot/cron.py +++ b/src/taskpilot/cron.py @@ -98,7 +98,7 @@ class CronSchedule: days_of_week: frozenset[int] @classmethod - def parse(cls, expression: str) -> "CronSchedule": + def parse(cls, expression: str) -> CronSchedule: raw = expression.strip() if not raw: raise CronError("Cron expression must not be empty") @@ -112,7 +112,7 @@ def parse(cls, expression: str) -> "CronSchedule": parsed = [ _parse_field(field, low, high) - for field, (low, high) in zip(fields, _FIELD_BOUNDS) + for field, (low, high) in zip(fields, _FIELD_BOUNDS, strict=True) ] return cls(expression, *parsed) diff --git a/src/taskpilot/models.py b/src/taskpilot/models.py index 3011a48..9896059 100644 --- a/src/taskpilot/models.py +++ b/src/taskpilot/models.py @@ -4,7 +4,7 @@ from dataclasses import dataclass, field from datetime import datetime -from typing import Any, Optional +from typing import Any from taskpilot.cron import CronSchedule @@ -25,7 +25,7 @@ class Task: enabled: bool = True description: str = "" - def next_run(self, after: Optional[datetime] = None) -> datetime: + def next_run(self, after: datetime | None = None) -> datetime: """Return the next scheduled run time strictly after *after*.""" return self.schedule.next_after(after or datetime.now()) diff --git a/src/taskpilot/runner.py b/src/taskpilot/runner.py index 517351b..9480cb4 100644 --- a/src/taskpilot/runner.py +++ b/src/taskpilot/runner.py @@ -4,7 +4,6 @@ import logging from datetime import datetime -from typing import Optional from taskpilot.actions.base import ActionContext, ActionRegistry from taskpilot.actions.base import registry as default_registry @@ -18,10 +17,10 @@ class TaskRunner: def __init__( self, - registry: Optional[ActionRegistry] = None, + registry: ActionRegistry | None = None, *, dry_run: bool = False, - env: Optional[dict[str, str]] = None, + env: dict[str, str] | None = None, ) -> None: self.registry = registry or default_registry self.dry_run = dry_run diff --git a/src/taskpilot/scheduler.py b/src/taskpilot/scheduler.py index f1474cb..d280000 100644 --- a/src/taskpilot/scheduler.py +++ b/src/taskpilot/scheduler.py @@ -4,8 +4,8 @@ import logging import time +from collections.abc import Callable, Iterable from datetime import datetime -from typing import Callable, Iterable, Optional from taskpilot.models import Task, TaskResult from taskpilot.runner import TaskRunner @@ -24,12 +24,12 @@ class Scheduler: def __init__( self, tasks: Iterable[Task], - runner: Optional[TaskRunner] = None, + runner: TaskRunner | None = None, ) -> None: self.tasks: list[Task] = list(tasks) self.runner = runner or TaskRunner() - def run_pending(self, moment: Optional[datetime] = None) -> list[TaskResult]: + def run_pending(self, moment: datetime | None = None) -> list[TaskResult]: """Run every enabled task whose schedule matches *moment*.""" moment = (moment or datetime.now()).replace(second=0, microsecond=0) results: list[TaskResult] = [] @@ -44,8 +44,8 @@ def run_forever( self, *, poll_seconds: float = 60.0, - max_iterations: Optional[int] = None, - on_result: Optional[Callable[[TaskResult], None]] = None, + max_iterations: int | None = None, + on_result: Callable[[TaskResult], None] | None = None, sleep: Callable[[float], None] = time.sleep, ) -> None: """Continuously poll and run due tasks. @@ -55,7 +55,7 @@ def run_forever( """ logger.info("Scheduler starting with %d task(s)", len(self.tasks)) iteration = 0 - last_minute: Optional[datetime] = None + last_minute: datetime | None = None try: while max_iterations is None or iteration < max_iterations: now = datetime.now().replace(second=0, microsecond=0) @@ -70,7 +70,7 @@ def run_forever( except KeyboardInterrupt: # pragma: no cover - interactive only logger.info("Scheduler stopped by user") - def describe(self, after: Optional[datetime] = None) -> list[dict]: + def describe(self, after: datetime | None = None) -> list[dict]: """Return a serialisable summary of upcoming runs.""" after = after or datetime.now() rows = []