Message412546
Consider the following.
```
import logging
from typing import Annotated, Callable, ParamSpec, TypeVar
T = TypeVar("T")
P = ParamSpec("P")
def add_logging(f: Callable[P, T]) -> Callable[P, T]:
"""A type-safe decorator to add logging to a function."""
def inner(*args: Annotated[P.args, "meta"], **kwargs: P.kwargs) -> T:
logging.info(f"{f.__name__} was called")
return f(*args, **kwargs)
return inner
@add_logging
def add_two(x: float, y: float) -> float:
"""Add two numbers together."""
return x + y
```
This raises an error at runtime because P.args/P.kwargs cannot pass the typing._type_check called by Annotated because they are not callable(). This prevents being able to use Annotated on these type annotations.
This can be fixed by adding __call__ methods that raise to typing.ParamSpecArgs and typing.ParamSpecKwargs to match other typeforms.
I can write this patch given agreement |
|
Date |
User |
Action |
Args |
2022-02-04 22:19:26 | GBeauregard | set | recipients:
+ GBeauregard, gvanrossum, JelleZijlstra, kj, AlexWaygood |
2022-02-04 22:19:25 | GBeauregard | set | messageid: <1644013165.99.0.0164231326825.issue46643@roundup.psfhosted.org> |
2022-02-04 22:19:25 | GBeauregard | link | issue46643 messages |
2022-02-04 22:19:25 | GBeauregard | create | |
|