|
| 1 | +--- |
| 2 | +author: orsinium |
| 3 | +topics: |
| 4 | + - typing |
| 5 | +traces: |
| 6 | + - [module: typing, type: ParamSpec] |
| 7 | +pep: 612 |
| 8 | +python: "3.10" |
| 9 | +--- |
| 10 | + |
| 11 | +# ParamSpec |
| 12 | + |
| 13 | +Let's say, you have a typical decorator that returns a new function. Something like this: |
| 14 | + |
| 15 | +```python {no-print} |
| 16 | +def debug(f): |
| 17 | + name = f.__name__ |
| 18 | + def inner(*args, **kwargs): |
| 19 | + print(f'called {name} with {args=} and {kwargs=}') |
| 20 | + return f(*args, **kwargs) |
| 21 | + return inner |
| 22 | + |
| 23 | +@debug |
| 24 | +def concat(a: str, b: str) -> str: |
| 25 | + return a + b |
| 26 | + |
| 27 | +concat('hello ', 'world') |
| 28 | +# called concat with args=('hello ', 'world') and kwargs={} |
| 29 | +``` |
| 30 | + |
| 31 | +If you check the type of `concat` using [reveal_type](https://t.me/pythonetc/712), you'll see that its type is unknown because of the decorator: |
| 32 | + |
| 33 | +```python {continue} |
| 34 | +reveal_type(concat) |
| 35 | +# Revealed type is "Any" |
| 36 | +``` |
| 37 | + |
| 38 | +So, we need to properly annotate the decorator. But how? |
| 39 | + |
| 40 | +This is not precise enough (type errors like `x: int = concat(1, 2)` won't be detected): |
| 41 | + |
| 42 | +```python |
| 43 | +from typing import Callable |
| 44 | +def debug(f: Callable) -> Callable: ... |
| 45 | +``` |
| 46 | + |
| 47 | +This is slightly better but function arguments are still untyped: |
| 48 | + |
| 49 | +```python {continue} |
| 50 | +from typing import TypeVar |
| 51 | + |
| 52 | +T = TypeVar('T') |
| 53 | +def debug(f: Callable[..., T]) -> Callable[..., T]: ... |
| 54 | +``` |
| 55 | + |
| 56 | +This is type safe but it requres the decorated function to accept exactly 2 arguments: |
| 57 | + |
| 58 | +```python {continue} |
| 59 | +A = TypeVar('A') |
| 60 | +B = TypeVar('B') |
| 61 | +R = TypeVar('R') |
| 62 | +def debug(f: Callable[[A, B], R]) -> Callable[[A, B], R]: ... |
| 63 | +``` |
| 64 | + |
| 65 | +This is type safe and works on any function but it will report type error because `inner` is not guaranteed to have the same type as the passed callable (for example, someone might pass a class that is callable but we return a function): |
| 66 | + |
| 67 | +```python {continue} |
| 68 | +F = TypeVar('F', bound=Callable) |
| 69 | +def debug(f: F) -> F: ... |
| 70 | +``` |
| 71 | + |
| 72 | +[PEP 612](https://peps.python.org/pep-0612/) (landed in Python 3.10) introduced [typing.ParamSpec](https://docs.python.org/3/library/typing.html#typing.ParamSpec) which solves exactly this problem. You can use it to tell type checkers that the decorator returns a new function that accepts exactly the same arguments as the wrapped one: |
| 73 | + |
| 74 | +```python |
| 75 | +from typing import Callable, TypeVar, ParamSpec |
| 76 | + |
| 77 | +P = ParamSpec('P') |
| 78 | +R = TypeVar('R') |
| 79 | + |
| 80 | +def debug(f: Callable[P, R]) -> Callable[P, R]: |
| 81 | + def inner(*args: P.args, **kwargs: P.kwargs) -> R: |
| 82 | + ... |
| 83 | + return f(*args, **kwargs) |
| 84 | + return inner |
| 85 | + |
| 86 | +@debug |
| 87 | +def concat(a: str, b: str) -> str: |
| 88 | + ... |
| 89 | + |
| 90 | +reveal_type(concat) |
| 91 | +# Revealed type is "def (a: str, b: str) -> str" |
| 92 | +``` |
0 commit comments