From 39e094736f8c1060337e7aaef0eb3a4ecf91ff18 Mon Sep 17 00:00:00 2001 From: ehhuang Date: Sat, 22 Mar 2025 08:17:23 -0700 Subject: [PATCH] chore: make mypy happy with webmethod (#1758) # What does this PR do? Gets rid of errors like the below, which is on all webmethod decorated functions llama_stack/apis/agents/agents.py:398: error: Value of type variable "T" of function cannot be "Callable[[Agents, AgentConfig], Coroutine[Any, Any, AgentCreateResponse]]" [type-var] ## Test Plan Run mypy and observes mypy errors gone --- llama_stack/schema_utils.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/llama_stack/schema_utils.py b/llama_stack/schema_utils.py index d84b1e95f..8fd55add0 100644 --- a/llama_stack/schema_utils.py +++ b/llama_stack/schema_utils.py @@ -5,7 +5,7 @@ # the root directory of this source tree. from dataclasses import dataclass -from typing import Any, Callable, List, Optional, Protocol, TypeVar +from typing import Any, Callable, List, Optional, TypeVar from .strong_typing.schema import json_schema_type, register_schema # noqa: F401 @@ -22,11 +22,7 @@ class WebMethod: descriptive_name: Optional[str] = None -class HasWebMethod(Protocol): - __webmethod__: WebMethod - - -T = TypeVar("T", bound=HasWebMethod) # Bound T to classes that match this protocol +T = TypeVar("T", bound=Callable[..., Any]) def webmethod( @@ -47,8 +43,8 @@ def webmethod( :param response_examples: Sample responses that the operation might produce. Pass a list of objects, not JSON. """ - def wrap(cls: T) -> T: - cls.__webmethod__ = WebMethod( + def wrap(func: T) -> T: + func.__webmethod__ = WebMethod( # type: ignore route=route, method=method, public=public or False, @@ -57,6 +53,6 @@ def webmethod( raw_bytes_request_body=raw_bytes_request_body, descriptive_name=descriptive_name, ) - return cls + return func return wrap