Created
May 16, 2017 16:02
-
-
Save gvanrossum/4232a4cdad00bd92a7a64cf3e2795820 to your computer and use it in GitHub Desktop.
Variadic decorator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
variadic.py:17: error: Overloaded function signatures 1 and 2 overlap with incompatible return types | |
variadic.py:17: error: Overloaded function signatures 1 and 3 overlap with incompatible return types | |
variadic.py:38: error: Revealed type is 'Any' | |
variadic.py:39: error: Revealed type is 'Any' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import * | |
T = TypeVar('T') | |
T1 = TypeVar('T1') | |
T2 = TypeVar('T2') | |
T3 = TypeVar('T3') | |
R = TypeVar('R') | |
class Promise(Generic[T, R]): | |
def __init__(self, func: Callable[..., R], args: Tuple[Any, ...]) -> None: | |
self.func = func | |
self.args = args | |
def call(self) -> R: | |
return self.func(*self.args) | |
@overload | |
def promised(func: Callable[[T1], R]) -> Promise[Tuple[T1], R]: | |
pass | |
@overload | |
def promised(Func: Callable[[T1, T2], R]) -> Promise[Tuple[T1, T2], R]: | |
pass | |
@overload | |
def promised(func: Callable[[T1, T2, T3], R]) -> Promise[Tuple[T1, T2, T3], R]: | |
pass | |
def promised(func): | |
def wrapper(*args): | |
return Promise(func, args) | |
return wrapper | |
@promised | |
def foo(a: int, b: str) -> float: | |
return a + float(b) | |
@promised | |
def bar(x: Tuple[int, int]) -> str: | |
return str(x[0] + x[1]) | |
reveal_type(foo(0, '')) | |
reveal_type(bar((0, 0))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment