mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	typing: Remove FuncT.
We can express the type of these decorators with Concatenate and ParamSpec now for tighter type annotations. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							21fd62427d
						
					
				
				
					commit
					30536caa68
				
			@@ -17,8 +17,9 @@ import os
 | 
				
			|||||||
import sys
 | 
					import sys
 | 
				
			||||||
from email.headerregistry import Address
 | 
					from email.headerregistry import Address
 | 
				
			||||||
from functools import wraps
 | 
					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 zulip import Client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from zerver.models import get_realm, get_user
 | 
					from zerver.models import get_realm, get_user
 | 
				
			||||||
@@ -30,10 +31,13 @@ TEST_FUNCTIONS: Dict[str, Callable[..., object]] = {}
 | 
				
			|||||||
REGISTERED_TEST_FUNCTIONS: Set[str] = set()
 | 
					REGISTERED_TEST_FUNCTIONS: Set[str] = set()
 | 
				
			||||||
CALLED_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
 | 
					    """This decorator is used to register an OpenAPI test function with
 | 
				
			||||||
    its endpoint. Example usage:
 | 
					    its endpoint. Example usage:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -41,16 +45,16 @@ def openapi_test_function(endpoint: str) -> Callable[[FuncT], FuncT]:
 | 
				
			|||||||
    def ...
 | 
					    def ...
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def wrapper(test_func: FuncT) -> FuncT:
 | 
					    def wrapper(test_func: Callable[ParamT, ReturnT]) -> Callable[ParamT, ReturnT]:
 | 
				
			||||||
        @wraps(test_func)
 | 
					        @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__)
 | 
					            CALLED_TEST_FUNCTIONS.add(test_func.__name__)
 | 
				
			||||||
            return test_func(*args, **kwargs)
 | 
					            return test_func(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        REGISTERED_TEST_FUNCTIONS.add(test_func.__name__)
 | 
					        REGISTERED_TEST_FUNCTIONS.add(test_func.__name__)
 | 
				
			||||||
        TEST_FUNCTIONS[endpoint] = _record_calls_wrapper
 | 
					        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
 | 
					    return wrapper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,10 +1,11 @@
 | 
				
			|||||||
from functools import wraps
 | 
					from functools import wraps
 | 
				
			||||||
from typing import Any, Callable, Dict, Optional, TypeVar, cast
 | 
					from typing import Any, Callable, Dict, Optional
 | 
				
			||||||
from unittest import mock
 | 
					from unittest import mock
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import orjson
 | 
					import orjson
 | 
				
			||||||
from django.conf import settings
 | 
					from django.conf import settings
 | 
				
			||||||
from django.test import override_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.create_user import do_create_user
 | 
				
			||||||
from zerver.actions.message_send import get_service_bot_events
 | 
					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": []})
 | 
					            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)
 | 
					    @wraps(test_func)
 | 
				
			||||||
    def _wrapped(*args: object, **kwargs: object) -> None:
 | 
					    def _wrapped(
 | 
				
			||||||
        assert len(args) > 0
 | 
					        self: "TestServiceBotEventTriggers", /, *args: ParamT.args, **kwargs: ParamT.kwargs
 | 
				
			||||||
        self = cast(TestServiceBotEventTriggers, args[0])
 | 
					    ) -> None:
 | 
				
			||||||
        for bot_type in BOT_TYPE_TO_QUEUE_NAME:
 | 
					        for bot_type in BOT_TYPE_TO_QUEUE_NAME:
 | 
				
			||||||
            self.bot_profile.bot_type = bot_type
 | 
					            self.bot_profile.bot_type = bot_type
 | 
				
			||||||
            self.bot_profile.save()
 | 
					            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):
 | 
					class TestServiceBotEventTriggers(ZulipTestCase):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user