Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

typing: Remove FuncT.

Zixuan James Li30536caa68f0
diff --git a/zerver/openapi/python_examples.py b/zerver/openapi/python_examples.py
index de52ca96882..90ab39f53cb 100644
--- a/zerver/openapi/python_examples.py
+++ b/zerver/openapi/python_examples.py
@@ -17,8 +17,9 @@ import os
import sys
from email.headerregistry import Address
from functools import wraps
-from typing import Any, Callable, Dict, List, Set, TypeVar, cast
+from typing import Any, Callable, Dict, List, Set, TypeVar
+from typing_extensions import ParamSpec
from zulip import Client
from zerver.models import get_realm, get_user
@@ -30,10 +31,13 @@ TEST_FUNCTIONS: Dict[str, Callable[..., object]] = {}
REGISTERED_TEST_FUNCTIONS: Set[str] = set()
CALLED_TEST_FUNCTIONS: Set[str] = set()
-FuncT = TypeVar("FuncT", bound=Callable[..., object])
+ParamT = ParamSpec("ParamT")
+ReturnT = TypeVar("ReturnT")
-def openapi_test_function(endpoint: str) -> Callable[[FuncT], FuncT]:
+def openapi_test_function(
+ endpoint: str,
+) -> Callable[[Callable[ParamT, ReturnT]], Callable[ParamT, ReturnT]]:
"""This decorator is used to register an OpenAPI test function with
its endpoint. Example usage:
@@ -41,16 +45,16 @@ def openapi_test_function(endpoint: str) -> Callable[[FuncT], FuncT]:
def ...
"""
- def wrapper(test_func: FuncT) -> FuncT:
+ def wrapper(test_func: Callable[ParamT, ReturnT]) -> Callable[ParamT, ReturnT]:
@wraps(test_func)
- def _record_calls_wrapper(*args: object, **kwargs: object) -> object:
+ def _record_calls_wrapper(*args: ParamT.args, **kwargs: ParamT.kwargs) -> ReturnT:
CALLED_TEST_FUNCTIONS.add(test_func.__name__)
return test_func(*args, **kwargs)
REGISTERED_TEST_FUNCTIONS.add(test_func.__name__)
TEST_FUNCTIONS[endpoint] = _record_calls_wrapper
- return cast(FuncT, _record_calls_wrapper) # https://github.com/python/mypy/issues/1927
+ return _record_calls_wrapper
return wrapper
diff --git a/zerver/tests/test_service_bot_system.py b/zerver/tests/test_service_bot_system.py
index fc7557d9e1c..04021eae2bd 100644
--- a/zerver/tests/test_service_bot_system.py
+++ b/zerver/tests/test_service_bot_system.py
@@ -1,10 +1,11 @@
from functools import wraps
-from typing import Any, Callable, Dict, Optional, TypeVar, cast
+from typing import Any, Callable, Dict, Optional
from unittest import mock
import orjson
from django.conf import settings
from django.test import override_settings
+from typing_extensions import Concatenate, ParamSpec
from zerver.actions.create_user import do_create_user
from zerver.actions.message_send import get_service_bot_events
@@ -413,20 +414,22 @@ class TestServiceBotConfigHandler(ZulipTestCase):
self.bot_handler.send_message(message={"type": "private", "to": []})
-FuncT = TypeVar("FuncT", bound=Callable[..., None])
+ParamT = ParamSpec("ParamT")
-def for_all_bot_types(test_func: FuncT) -> FuncT:
+def for_all_bot_types(
+ test_func: Callable[Concatenate["TestServiceBotEventTriggers", ParamT], None]
+) -> Callable[Concatenate["TestServiceBotEventTriggers", ParamT], None]:
@wraps(test_func)
- def _wrapped(*args: object, **kwargs: object) -> None:
- assert len(args) > 0
- self = cast(TestServiceBotEventTriggers, args[0])
+ def _wrapped(
+ self: "TestServiceBotEventTriggers", /, *args: ParamT.args, **kwargs: ParamT.kwargs
+ ) -> None:
for bot_type in BOT_TYPE_TO_QUEUE_NAME:
self.bot_profile.bot_type = bot_type
self.bot_profile.save()
- test_func(*args, **kwargs)
+ test_func(self, *args, **kwargs)
- return cast(FuncT, _wrapped) # https://github.com/python/mypy/issues/1927
+ return _wrapped
class TestServiceBotEventTriggers(ZulipTestCase):