Skip to content

Commit b208d31

Browse files
Merge pull request #2780 from VWS-Python/ty
Get the code ready for ty
2 parents f004c33 + a9da08c commit b208d31

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/mock_vws/_requests_mock_server/mock_web_query_api.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import email.utils
88
from collections.abc import Callable, Iterable, Mapping
99
from http import HTTPMethod, HTTPStatus
10+
from typing import ParamSpec, Protocol, runtime_checkable
1011

1112
from beartype import beartype
1213
from requests.models import PreparedRequest
@@ -25,13 +26,29 @@
2526
_ROUTES: set[Route] = set()
2627

2728
_ResponseType = tuple[int, Mapping[str, str], str]
29+
_P = ParamSpec("_P")
30+
31+
32+
@runtime_checkable
33+
class _RouteMethod(Protocol[_P]):
34+
"""
35+
Callable used for routing which also exposes ``__name__``.
36+
"""
37+
38+
__name__: str
39+
40+
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _ResponseType:
41+
"""
42+
Return a mock response.
43+
"""
44+
... # pylint: disable=unnecessary-ellipsis
2845

2946

3047
@beartype
3148
def route(
3249
path_pattern: str,
3350
http_methods: Iterable[str],
34-
) -> Callable[[Callable[..., _ResponseType]], Callable[..., _ResponseType]]:
51+
) -> Callable[[_RouteMethod[_P]], _RouteMethod[_P]]:
3552
"""Register a decorated method so that it can be recognized as a route.
3653
3754
Args:
@@ -44,8 +61,8 @@ def route(
4461
"""
4562

4663
def decorator(
47-
method: Callable[..., _ResponseType],
48-
) -> Callable[..., _ResponseType]:
64+
method: _RouteMethod[_P],
65+
) -> _RouteMethod[_P]:
4966
"""Register a decorated method so that it can be recognized as a route.
5067
5168
Returns:

src/mock_vws/_requests_mock_server/mock_web_services_api.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import uuid
1313
from collections.abc import Callable, Iterable, Mapping
1414
from http import HTTPMethod, HTTPStatus
15-
from typing import Any
15+
from typing import Any, ParamSpec, Protocol, runtime_checkable
1616
from zoneinfo import ZoneInfo
1717

1818
from beartype import BeartypeConf, beartype
@@ -39,13 +39,29 @@
3939
_ROUTES: set[Route] = set()
4040

4141
_ResponseType = tuple[int, Mapping[str, str], str]
42+
_P = ParamSpec("_P")
43+
44+
45+
@runtime_checkable
46+
class _RouteMethod(Protocol[_P]):
47+
"""
48+
Callable used for routing which also exposes ``__name__``.
49+
"""
50+
51+
__name__: str
52+
53+
def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _ResponseType:
54+
"""
55+
Return a mock response.
56+
"""
57+
... # pylint: disable=unnecessary-ellipsis
4258

4359

4460
@beartype
4561
def route(
4662
path_pattern: str,
4763
http_methods: Iterable[HTTPMethod],
48-
) -> Callable[[Callable[..., _ResponseType]], Callable[..., _ResponseType]]:
64+
) -> Callable[[_RouteMethod[_P]], _RouteMethod[_P]]:
4965
"""Register a decorated method so that it can be recognized as a route.
5066
5167
Args:
@@ -59,8 +75,8 @@ def route(
5975

6076
@beartype
6177
def decorator(
62-
method: Callable[..., _ResponseType],
63-
) -> Callable[..., _ResponseType]:
78+
method: _RouteMethod[_P],
79+
) -> _RouteMethod[_P]:
6480
"""Register a decorated method so that it can be recognized as a route.
6581
6682
Returns:

0 commit comments

Comments
 (0)