|
1 | 1 | import inspect |
2 | | -import warnings |
3 | 2 | from collections.abc import Callable |
4 | 3 | from typing import Any, TypeVar, get_type_hints |
5 | 4 |
|
6 | | - |
7 | | -def issue_deprecation_warning(func: Callable[..., Any], request_type: type) -> None: |
8 | | - """ |
9 | | - Issue a deprecation warning for handlers that don't use the new request parameter style. |
10 | | - """ |
11 | | - func_name = getattr(func, "__name__", str(func)) |
12 | | - warnings.warn( |
13 | | - f"Handler '{func_name}' should accept a '{request_type.__name__}' parameter. " |
14 | | - "Support for handlers without this parameter will be removed in a future version.", |
15 | | - DeprecationWarning, |
16 | | - stacklevel=4, |
17 | | - ) |
18 | | - |
19 | | - |
20 | 5 | T = TypeVar("T") |
21 | 6 | R = TypeVar("R") |
22 | 7 |
|
23 | 8 |
|
24 | | -def create_call_wrapper(func: Callable[..., R], request_type: type[T]) -> tuple[Callable[[T], R], bool]: |
| 9 | +def create_call_wrapper(func: Callable[..., R], request_type: type[T]) -> Callable[[T], R]: |
25 | 10 | """ |
26 | 11 | Create a wrapper function that knows how to call func with the request object. |
27 | 12 |
|
28 | | - Returns a tuple of (wrapper_func, should_deprecate): |
29 | | - - wrapper_func: A function that takes the request and calls func appropriately |
30 | | - - should_deprecate: True if a deprecation warning should be issued |
| 13 | + Returns a wrapper function that takes the request and calls func appropriately. |
31 | 14 |
|
32 | 15 | The wrapper handles three calling patterns: |
33 | 16 | 1. Positional-only parameter typed as request_type (no default): func(req) |
34 | 17 | 2. Positional/keyword parameter typed as request_type (no default): func(**{param_name: req}) |
35 | | - 3. No request parameter or parameter with default (deprecated): func() |
| 18 | + 3. No request parameter or parameter with default: func() |
36 | 19 | """ |
37 | 20 | try: |
38 | 21 | sig = inspect.signature(func) |
39 | 22 | type_hints = get_type_hints(func) |
40 | 23 | except (ValueError, TypeError, NameError): |
41 | | - # Can't inspect signature or resolve type hints, assume no request parameter (deprecated) |
42 | | - return lambda _: func(), True |
| 24 | + return lambda _: func() |
43 | 25 |
|
44 | 26 | # Check for positional-only parameter typed as request_type |
45 | 27 | for param_name, param in sig.parameters.items(): |
46 | 28 | if param.kind == inspect.Parameter.POSITIONAL_ONLY: |
47 | 29 | param_type = type_hints.get(param_name) |
48 | 30 | if param_type == request_type: |
49 | | - # Check if it has a default - if so, treat as old style (deprecated) |
| 31 | + # Check if it has a default - if so, treat as old style |
50 | 32 | if param.default is not inspect.Parameter.empty: |
51 | | - return lambda _: func(), True |
| 33 | + return lambda _: func() |
52 | 34 | # Found positional-only parameter with correct type and no default |
53 | | - return lambda req: func(req), False |
| 35 | + return lambda req: func(req) |
54 | 36 |
|
55 | 37 | # Check for any positional/keyword parameter typed as request_type |
56 | 38 | for param_name, param in sig.parameters.items(): |
57 | 39 | if param.kind in (inspect.Parameter.POSITIONAL_OR_KEYWORD, inspect.Parameter.KEYWORD_ONLY): |
58 | 40 | param_type = type_hints.get(param_name) |
59 | 41 | if param_type == request_type: |
60 | | - # Check if it has a default - if so, treat as old style (deprecated) |
| 42 | + # Check if it has a default - if so, treat as old style |
61 | 43 | if param.default is not inspect.Parameter.empty: |
62 | | - return lambda _: func(), True |
| 44 | + return lambda _: func() |
63 | 45 |
|
64 | 46 | # Found keyword parameter with correct type and no default |
65 | 47 | # Need to capture param_name in closure properly |
66 | 48 | def make_keyword_wrapper(name: str) -> Callable[[Any], Any]: |
67 | 49 | return lambda req: func(**{name: req}) |
68 | 50 |
|
69 | | - return make_keyword_wrapper(param_name), False |
| 51 | + return make_keyword_wrapper(param_name) |
70 | 52 |
|
71 | | - # No request parameter found - use old style (deprecated) |
72 | | - return lambda _: func(), True |
| 53 | + # No request parameter found - use old style |
| 54 | + return lambda _: func() |
0 commit comments