| 1 |
""" Pep-0484 type hinted decorators """ |
| 2 |
|
| 3 |
from typing import Callable |
| 4 |
|
| 5 |
|
| 6 |
def decorator(func): |
| 7 |
def wrapper(*a, **k): |
| 8 |
return str(func(*a, **k)) |
| 9 |
return wrapper |
| 10 |
|
| 11 |
|
| 12 |
def typed_decorator(func: Callable[..., int]) -> Callable[..., str]: |
| 13 |
... |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
@decorator |
| 18 |
def plain_func() -> int: |
| 19 |
return 4 |
| 20 |
|
| 21 |
|
| 22 |
plain_func() |
| 23 |
|
| 24 |
|
| 25 |
@typed_decorator |
| 26 |
def typed_func() -> int: |
| 27 |
return 4 |
| 28 |
|
| 29 |
|
| 30 |
typed_func() |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class X: |
| 36 |
@decorator |
| 37 |
def plain_method(self) -> int: |
| 38 |
return 4 |
| 39 |
|
| 40 |
@typed_decorator |
| 41 |
def typed_method(self) -> int: |
| 42 |
return 4 |
| 43 |
|
| 44 |
inst = X() |
| 45 |
|
| 46 |
|
| 47 |
inst.plain_method() |
| 48 |
|
| 49 |
|
| 50 |
inst.typed_method() |