Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ python =
deps =
coverage
pytest
pytest-mypy-plugins; python_version >= "3.9"
install_command =
python -m pip install --no-binary coverage {opts} {packages}
commands =
Expand Down
5 changes: 5 additions & 0 deletions src/wrapt/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Callable, TypeVar

F = TypeVar("F", bound=Callable)

def FunctionWrapper(wrapped: F, wrapper: Callable) -> F: ...
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this all be more precisely defined as:

from typing import Callable, TypeVar, Any, Tuple, Dict, Union

WrappedFunction = TypeVar("WrappedFunction", bound=Callable)

# Standard signature for wrappers
StandardWrapper = Callable[[WrappedFunction, Any, Tuple[Any, ...], Dict[str, Any]], Any]

# Catch-all wrapper signature
CatchAllWrapper = Callable[..., Any]

# Union type to accept either wrapper style
WrapperFunction = Union[StandardWrapper, CatchAllWrapper]

def FunctionWrapper(
    wrapped: WrappedFunction, wrapper: WrapperFunction
) -> WrappedFunction: ...

The standard wrapper would be:

    def standard_wrapper(wrapped, instance, *args, **kwargs):
        pass

It would be rare, but someone may want to instead define this as:

    def catch_all_wrapper(*args, **kwargs):
        pass

Anyway, starting to understand what I need to do. Just working out easy way of maintaining the tests. It is a pain to have to add in the out section with exact line numbers.

1 change: 1 addition & 0 deletions src/wrapt/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
partial
18 changes: 18 additions & 0 deletions tests/test_mypy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- case: function_wrapper
main: |
from wrapt import FunctionWrapper

def x(a: bool, b: str) -> int:
return 1

def wrapper(*args, **kwargs):
pass

x = FunctionWrapper(x, wrapper)
reveal_type(x)

x(1, None)
out: |
main:10: note: Revealed type is "def (a: builtins.bool, b: builtins.str) -> builtins.int"
main:12: error: Argument 1 to "x" has incompatible type "int"; expected "bool" [arg-type]
main:12: error: Argument 2 to "x" has incompatible type "None"; expected "str" [arg-type]
Loading