From 2ab9f1890b3bf32fb638303a5a8b4c28ced1a79a Mon Sep 17 00:00:00 2001 From: Thomas MK Date: Fri, 3 Mar 2023 14:36:41 +0100 Subject: [PATCH 1/4] Add `__name__` to `_Wrapped` --- stdlib/functools.pyi | 3 +++ test_cases/stdlib/check_functools.py | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/stdlib/functools.pyi b/stdlib/functools.pyi index 5db4c65bdb8d..20b612123c2a 100644 --- a/stdlib/functools.pyi +++ b/stdlib/functools.pyi @@ -72,6 +72,9 @@ WRAPPER_UPDATES: tuple[Literal["__dict__"]] class _Wrapped(Generic[_PWrapped, _RWrapped, _PWrapper, _RWapper]): __wrapped__: Callable[_PWrapped, _RWrapped] def __call__(self, *args: _PWrapper.args, **kwargs: _PWrapper.kwargs) -> _RWapper: ... + # as with ``Callable``, we'll assume that these attributes exist + __name__: str + __qualname__: str class _Wrapper(Generic[_PWrapped, _RWrapped]): def __call__(self, f: Callable[_PWrapper, _RWapper]) -> _Wrapped[_PWrapped, _RWrapped, _PWrapper, _RWapper]: ... diff --git a/test_cases/stdlib/check_functools.py b/test_cases/stdlib/check_functools.py index 327e60faa07d..68d6d9d0399b 100644 --- a/test_cases/stdlib/check_functools.py +++ b/test_cases/stdlib/check_functools.py @@ -1,10 +1,28 @@ from __future__ import annotations import sys +from functools import wraps +from typing import Callable, ParamSpec, TypeVar +from typing_extensions import ParamSpec, assert_type + +P = ParamSpec("P") +T_co = TypeVar("T_co", covariant=True) + + +def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]: + func_name = func.__name__ + + @wraps(func) + def wrapper(*args: P.args, **kwargs: P.kwargs): + print(args) + return func(*args, **kwargs) + + wrapper.__name__ = func_name + return wrapper + if sys.version_info >= (3, 8): from functools import cached_property - from typing_extensions import assert_type class A: def __init__(self, x: int): From a95d07188eada8240d47642639f80d05ca5cde0f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 3 Mar 2023 13:43:05 +0000 Subject: [PATCH 2/4] Remove duplicated import --- test_cases/stdlib/check_functools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_cases/stdlib/check_functools.py b/test_cases/stdlib/check_functools.py index 68d6d9d0399b..b1457749a8b0 100644 --- a/test_cases/stdlib/check_functools.py +++ b/test_cases/stdlib/check_functools.py @@ -2,7 +2,7 @@ import sys from functools import wraps -from typing import Callable, ParamSpec, TypeVar +from typing import Callable, TypeVar from typing_extensions import ParamSpec, assert_type P = ParamSpec("P") From a241668486330c11a19db39b6844f2b649f3c843 Mon Sep 17 00:00:00 2001 From: Thomas MK Date: Fri, 3 Mar 2023 14:53:57 +0100 Subject: [PATCH 3/4] Add more tests and add return type --- test_cases/stdlib/check_functools.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test_cases/stdlib/check_functools.py b/test_cases/stdlib/check_functools.py index b1457749a8b0..dadee7d10f81 100644 --- a/test_cases/stdlib/check_functools.py +++ b/test_cases/stdlib/check_functools.py @@ -10,14 +10,18 @@ def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]: - func_name = func.__name__ @wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs): + def wrapper(*args: P.args, **kwargs: P.kwargs) -> T_co: print(args) return func(*args, **kwargs) - wrapper.__name__ = func_name + # verify that the wrapped function has all these attributes + wrapper.__annotations__ = func.__annotations__ + wrapper.__doc__ = func.__doc__ + wrapper.__module__ = func.__module__ + wrapper.__name__ = func.__name__ + wrapper.__qualname__ = func.__qualname__ return wrapper From f35d647cb1d904c19c1ad2ce306ce2e0981ccb78 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 3 Mar 2023 13:55:19 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks --- test_cases/stdlib/check_functools.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test_cases/stdlib/check_functools.py b/test_cases/stdlib/check_functools.py index dadee7d10f81..4ec828832731 100644 --- a/test_cases/stdlib/check_functools.py +++ b/test_cases/stdlib/check_functools.py @@ -10,7 +10,6 @@ def my_decorator(func: Callable[P, T_co]) -> Callable[P, T_co]: - @wraps(func) def wrapper(*args: P.args, **kwargs: P.kwargs) -> T_co: print(args)