diff --git a/src/wrapt/__init__.pyi b/src/wrapt/__init__.pyi index 2e33e00e..2a241338 100644 --- a/src/wrapt/__init__.pyi +++ b/src/wrapt/__init__.pyi @@ -16,7 +16,6 @@ if sys.version_info >= (3, 10): P = ParamSpec("P") R = TypeVar("R", covariant=True) - T = TypeVar("T", bound=Any) class Boolean(Protocol): @@ -105,8 +104,23 @@ if sys.version_info >= (3, 10): _self_parent: Any _self_owner: Any + _P = ParamSpec("_P") + _R = TypeVar("_R") + _T = TypeVar("_T") + class BoundFunctionWrapper(_FunctionWrapperBase[P, R]): def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ... + @overload + def __get__( + self, instance: None, owner: type[Any] | None = None + ) -> "BoundFunctionWrapper[P, R]": ... + @overload + def __get__( + self: "BoundFunctionWrapper[Concatenate[_T, _P], _R]", + instance: _T, + owner: type[Any] | None = None, + ) -> "BoundFunctionWrapper[_P, _R]": ... + @overload def __get__( self, instance: Any, owner: type[Any] | None = None ) -> "BoundFunctionWrapper[P, R]": ... @@ -119,6 +133,17 @@ if sys.version_info >= (3, 10): enabled: bool | Boolean | Callable[[], bool] | None = None, ) -> None: ... def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ... + @overload + def __get__( + self, instance: None, owner: type[Any] | None = None + ) -> "FunctionWrapper[P, R]": ... + @overload + def __get__( + self: "FunctionWrapper[Concatenate[_T, _P], _R]", + instance: _T, + owner: type[Any] | None = None, + ) -> BoundFunctionWrapper[_P, _R]: ... + @overload def __get__( self, instance: Any, owner: type[Any] | None = None ) -> BoundFunctionWrapper[P, R]: ... @@ -140,14 +165,8 @@ if sys.version_info >= (3, 10): class FunctionDecorator(Generic[P, R]): def __call__( self, - callable: ( - Callable[P, R] - | Callable[Concatenate[type[T], P], R] - | Callable[Concatenate[Any, P], R] - | Callable[[type[T]], R] - | Descriptor - ), - ) -> FunctionWrapper[P, R]: ... + callable: Callable[_P, _R], + ) -> FunctionWrapper[_P, _R]: ... class PartialFunctionDecorator: @overload diff --git a/tests/mypy/mypy_decorator_t1.out b/tests/mypy/mypy_decorator_t1.out index 1372f875..c48a35ff 100644 --- a/tests/mypy/mypy_decorator_t1.out +++ b/tests/mypy/mypy_decorator_t1.out @@ -1,30 +1,20 @@ tests/mypy/mypy_decorator_t1.py:30: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment] -tests/mypy/mypy_decorator_t1.py:33: error: Too few arguments for "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_decorator_t1.py:33: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] tests/mypy/mypy_decorator_t1.py:38: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "int" [arg-type] tests/mypy/mypy_decorator_t1.py:41: error: Too many arguments for "__call__" of "FunctionWrapper" [call-arg] -tests/mypy/mypy_decorator_t1.py:61: error: Too few arguments for "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_decorator_t1.py:61: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] tests/mypy/mypy_decorator_t1.py:66: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "int" [arg-type] tests/mypy/mypy_decorator_t1.py:69: error: Too many arguments for "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_decorator_t1.py:86: error: Incompatible return value type (got "FunctionWrapper[[int], int]", expected "Callable[[int], str]") [return-value] +tests/mypy/mypy_decorator_t1.py:86: note: "FunctionWrapper[[int], int].__call__" has type "Callable[[int], int]" tests/mypy/mypy_decorator_t1.py:98: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment] tests/mypy/mypy_decorator_t1.py:101: error: Too few arguments for "function3" [call-arg] tests/mypy/mypy_decorator_t1.py:106: error: Argument 1 to "function3" has incompatible type "str"; expected "int" [arg-type] tests/mypy/mypy_decorator_t1.py:109: error: Too many arguments for "function3" [call-arg] -tests/mypy/mypy_decorator_t1.py:134: error: Too few arguments for "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_decorator_t1.py:128: error: Incompatible types in assignment (expression has type "int", variable has type "str") [assignment] +tests/mypy/mypy_decorator_t1.py:134: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] tests/mypy/mypy_decorator_t1.py:139: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "int" [arg-type] tests/mypy/mypy_decorator_t1.py:142: error: Too many arguments for "__call__" of "FunctionWrapper" [call-arg] -tests/mypy/mypy_decorator_t1.py:217: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:217: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:219: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:220: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:220: error: Argument "b" to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:228: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:228: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:230: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:231: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:231: error: Argument "b" to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:239: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:239: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:241: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:242: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_decorator_t1.py:242: error: Argument "b" to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -Found 29 errors in 1 file (checked 1 source file) +tests/mypy/mypy_decorator_t1.py:164: error: Incompatible return value type (got "FunctionWrapper[[int], int]", expected "Callable[[int, int], str]") [return-value] +tests/mypy/mypy_decorator_t1.py:164: note: "FunctionWrapper[[int], int].__call__" has type "Callable[[int], int]" +Found 17 errors in 1 file (checked 1 source file) diff --git a/tests/mypy/mypy_function_wrapper_fn_t1.out b/tests/mypy/mypy_function_wrapper_fn_t1.out index 6f401f2b..8539cd2e 100644 --- a/tests/mypy/mypy_function_wrapper_fn_t1.out +++ b/tests/mypy/mypy_function_wrapper_fn_t1.out @@ -1,17 +1 @@ -tests/mypy/mypy_function_wrapper_fn_t1.py:63: error: Need type annotation for "wrapped_function" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:65: error: Need type annotation for "wrapped_method" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:66: error: Need type annotation for "wrapped_classmethod" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:67: error: Need type annotation for "wrapped_staticmethod" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:69: error: Need type annotation for "wrapped_method_instance" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:70: error: Need type annotation for "wrapped_classmethod_instance" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:71: error: Need type annotation for "wrapped_staticmethod_instance" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:73: error: Need type annotation for "wrapped_callable_class" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:73: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "type[ExampleClass1]"; expected "Callable[[VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never], VarArg(Never), KwArg(Never)], Never] | Callable[[Any, VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never]], Never] | Descriptor" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:74: error: Need type annotation for "wrapped_callable_object" [var-annotated] -tests/mypy/mypy_function_wrapper_fn_t1.py:74: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "ExampleClass1"; expected "Callable[[VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never], VarArg(Never), KwArg(Never)], Never] | Callable[[Any, VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never]], Never] | Descriptor" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:88: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "type[ExampleClass2]"; expected "Callable[[VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never], VarArg(Never), KwArg(Never)], Never] | Callable[[Any, VarArg(Never), KwArg(Never)], Never] | Callable[[type[Never]], Never] | Descriptor" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:116: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:117: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:118: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t1.py:120: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -Found 16 errors in 1 file (checked 1 source file) +Success: no issues found in 1 source file diff --git a/tests/mypy/mypy_function_wrapper_fn_t2.out b/tests/mypy/mypy_function_wrapper_fn_t2.out index 60827a9a..7ad98012 100644 --- a/tests/mypy/mypy_function_wrapper_fn_t2.out +++ b/tests/mypy/mypy_function_wrapper_fn_t2.out @@ -1,3 +1,2 @@ tests/mypy/mypy_function_wrapper_fn_t2.py:20: error: Argument 1 to "function_wrapper" has incompatible type "Callable[[str, Any, tuple[Any, ...], dict[str, Any]], Any]"; expected "Callable[[Callable[[VarArg(Never), KwArg(Never)], Any], Any, tuple[Any, ...], dict[str, Any]], Any]" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t2.py:27: error: Need type annotation for "wrapped_function" [var-annotated] -Found 2 errors in 1 file (checked 1 source file) +Found 1 error in 1 file (checked 1 source file) diff --git a/tests/mypy/mypy_function_wrapper_fn_t4.out b/tests/mypy/mypy_function_wrapper_fn_t4.out index d5567309..a6fbfb16 100644 --- a/tests/mypy/mypy_function_wrapper_fn_t4.out +++ b/tests/mypy/mypy_function_wrapper_fn_t4.out @@ -1,3 +1,4 @@ -tests/mypy/mypy_function_wrapper_fn_t4.py:26: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "str"; expected "Callable[[VarArg(Any), KwArg(Any)], Any] | Callable[[type[Never], VarArg(Any), KwArg(Any)], Any] | Callable[[Any, VarArg(Any), KwArg(Any)], Any] | Callable[[type[Never]], Any] | Descriptor" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t4.py:27: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "None"; expected "Callable[[VarArg(Any), KwArg(Any)], Any] | Callable[[type[Never], VarArg(Any), KwArg(Any)], Any] | Callable[[Any, VarArg(Any), KwArg(Any)], Any] | Callable[[type[Never]], Any] | Descriptor" [arg-type] -Found 2 errors in 1 file (checked 1 source file) +tests/mypy/mypy_function_wrapper_fn_t4.py:26: error: Need type annotation for "wrapped_function" [var-annotated] +tests/mypy/mypy_function_wrapper_fn_t4.py:26: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "str"; expected "Callable[[VarArg(Never), KwArg(Never)], Never]" [arg-type] +tests/mypy/mypy_function_wrapper_fn_t4.py:27: error: Argument 1 to "__call__" of "FunctionDecorator" has incompatible type "None"; expected "Callable[[VarArg(Never), KwArg(Never)], Any]" [arg-type] +Found 3 errors in 1 file (checked 1 source file) diff --git a/tests/mypy/mypy_function_wrapper_fn_t7.out b/tests/mypy/mypy_function_wrapper_fn_t7.out index d20eef5b..967f514a 100644 --- a/tests/mypy/mypy_function_wrapper_fn_t7.out +++ b/tests/mypy/mypy_function_wrapper_fn_t7.out @@ -1,23 +1,18 @@ -tests/mypy/mypy_function_wrapper_fn_t7.py:32: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:32: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:33: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:34: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:34: error: Argument "y" to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:76: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:76: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:77: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:77: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:78: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:78: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:80: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:80: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:88: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:88: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:89: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:89: error: Argument 2 to "__call__" of "BoundFunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:185: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:185: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:186: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:187: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "Never" [arg-type] -tests/mypy/mypy_function_wrapper_fn_t7.py:187: error: Argument "y" to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "Never" [arg-type] -Found 22 errors in 1 file (checked 1 source file) +tests/mypy/mypy_function_wrapper_fn_t7.py:30: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:68: error: Missing positional arguments "value", "name" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:71: error: Missing positional arguments "value", "name" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:74: error: Missing positional argument "name" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:78: error: Too many arguments for "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:78: error: Argument 1 to "__call__" of "BoundFunctionWrapper" has incompatible type "int"; expected "str" [arg-type] +tests/mypy/mypy_function_wrapper_fn_t7.py:83: error: Missing positional arguments "cls", "value", "name" in call to "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:86: error: Missing positional arguments "value", "name" in call to "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:88: error: Missing positional argument "name" in call to "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:88: error: Argument 1 to "__call__" of "FunctionWrapper" has incompatible type "int"; expected "type[ExampleClass1]" [arg-type] +tests/mypy/mypy_function_wrapper_fn_t7.py:88: error: Argument 2 to "__call__" of "FunctionWrapper" has incompatible type "str"; expected "int" [arg-type] +tests/mypy/mypy_function_wrapper_fn_t7.py:110: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:147: error: Too many arguments for "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:152: error: Missing positional argument "value" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:153: error: Missing positional argument "value" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:156: error: Missing positional argument "value" in call to "__call__" of "BoundFunctionWrapper" [call-arg] +tests/mypy/mypy_function_wrapper_fn_t7.py:183: error: Missing positional argument "x" in call to "__call__" of "FunctionWrapper" [call-arg] +Found 17 errors in 1 file (checked 1 source file)