fix: add type annotations to event_handler decorator
Add proper generic type annotations to the event_handler decorator so that static type checkers (pyright, mypy) understand the decorated function is returned and used, eliminating false reportUnusedFunction warnings. Changes: - Import TypeVar and Callable from typing - Define F TypeVar bound to Callable[..., Any] - Add Callable[[F], F] return type to event_handler method - Add F type annotations to inner decorator function
This commit is contained in:
@@ -16,12 +16,15 @@ import inspect
|
||||
import traceback
|
||||
from abc import ABC
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, TypeVar
|
||||
|
||||
from loguru import logger
|
||||
|
||||
from pipecat.utils.utils import obj_count, obj_id
|
||||
|
||||
# TypeVar for preserving function signatures in decorators
|
||||
F = TypeVar("F", bound=Callable[..., Any])
|
||||
|
||||
|
||||
@dataclass
|
||||
class EventHandler:
|
||||
@@ -99,7 +102,7 @@ class BaseObject(ABC):
|
||||
logger.debug(f"{self}: waiting on event handlers to finish {list(event_names)}...")
|
||||
await asyncio.wait(tasks)
|
||||
|
||||
def event_handler(self, event_name: str):
|
||||
def event_handler(self, event_name: str) -> Callable[[F], F]:
|
||||
"""Decorator for registering event handlers.
|
||||
|
||||
Args:
|
||||
@@ -109,7 +112,7 @@ class BaseObject(ABC):
|
||||
The decorator function that registers the handler.
|
||||
"""
|
||||
|
||||
def decorator(handler):
|
||||
def decorator(handler: F) -> F:
|
||||
self.add_event_handler(event_name, handler)
|
||||
return handler
|
||||
|
||||
|
||||
Reference in New Issue
Block a user