Skip to content

Commit b22618d

Browse files
committed
fix(utils): resolve type errors in task manager with proper Protocol and type annotations
- Add CriticalTasksProvider Protocol with @runtime_checkable decorator - Use isinstance() check with Protocol instead of getattr() for type safety - Add proper type annotations for task discovery method - Fix pyright type checking errors for task configuration handling - Improve type safety for cog-driven task registration
1 parent 4394b40 commit b22618d

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

tux/utils/task_manager.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from collections.abc import Callable, Coroutine
2525
from dataclasses import dataclass, field
2626
from enum import Enum, auto
27-
from typing import Any, ClassVar, NamedTuple, cast
27+
from typing import Any, ClassVar, NamedTuple, Protocol, cast, runtime_checkable
2828

2929
from discord.ext import tasks
3030
from loguru import logger
@@ -93,6 +93,13 @@ class CriticalTaskConfig:
9393
health_check_interval: float = 300.0 # 5 minutes
9494

9595

96+
@runtime_checkable
97+
class CriticalTasksProvider(Protocol):
98+
"""Protocol for cogs that provide critical tasks."""
99+
100+
def get_critical_tasks(self) -> list[CriticalTaskConfig]: ...
101+
102+
96103
class TaskManager:
97104
"""
98105
Enhanced task manager with health monitoring, metrics, and recovery capabilities.
@@ -205,20 +212,17 @@ def discover_and_register_cog_tasks(self) -> None:
205212
"""
206213
Discover and register critical tasks from all loaded cogs.
207214
208-
This method asks each cog if it has critical tasks to register,
209-
making the system dynamic and cog-driven instead of hardcoded.
215+
This method iterates through all loaded cogs and looks for a
216+
`get_critical_tasks` method. If found, it calls the method to
217+
get a list of CriticalTaskConfig objects and registers them.
210218
"""
211-
logger.info("Discovering critical tasks from cogs...")
212-
213219
for cog_name, cog in self.bot.cogs.items():
214-
# Check if the cog has a method to report its critical tasks
215-
get_tasks_method = getattr(cog, "get_critical_tasks", None)
216-
if get_tasks_method and callable(get_tasks_method):
220+
if isinstance(cog, CriticalTasksProvider):
217221
try:
218-
if task_configs := get_tasks_method():
219-
for config in task_configs:
220-
self.register_critical_task(config)
221-
logger.debug(f"Discovered task {config.name} from cog {cog_name}")
222+
task_configs = cog.get_critical_tasks()
223+
for config in task_configs:
224+
self.register_critical_task(config)
225+
logger.debug(f"Discovered task {config.name} from cog {cog_name}")
222226
except Exception as e:
223227
logger.warning(f"Error discovering tasks from cog {cog_name}: {e}")
224228
continue

0 commit comments

Comments
 (0)