[New-bugs-announce] [issue46643] typing.Annotated cannot wrap typing.ParamSpec args/kwargs

Gregory Beauregard report at bugs.python.org
Fri Feb 4 17:19:25 EST 2022


New submission from Gregory Beauregard <greg at greg.red>:

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

----------
components: Library (Lib)
messages: 412546
nosy: AlexWaygood, GBeauregard, Jelle Zijlstra, gvanrossum, kj
priority: normal
severity: normal
status: open
title: typing.Annotated cannot wrap typing.ParamSpec args/kwargs
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46643>
_______________________________________


More information about the New-bugs-announce mailing list