Код: Выделить всё
from functools import wraps
def with_log(fun): # no type hint on `fun`
@wraps(fun)
def _decorated():
print(f"Calling {fun}")
return fun()
return _decorated
class Dog:
@with_log
def bark(self, times: int):
"""Prints woof several times"""
print(" ".join(["woof"] * times))
return True
dog = Dog()
dog.bark(3)
Код: Выделить всё
from collections.abc import Callable
from functools import wraps
from typing import ParamSpec
P = ParamSpec("P")
def with_log(fun: Callable[P, bool]):
@wraps(fun)
def _decorated(*args: P.args, **kwargs: P.kwargs):
print(f"Calling {fun}")
return fun(*args, **kwargs)
return _decorated
class Dog:
@with_log # "Literal[1]" is not assignable to "bool" ✅
def bark(self, times: int):
"""Prints woof several times"""
print(" ".join(["woof"] * times))
return 1
dog = Dog()
dog.bark(3)
Подробнее здесь: https://stackoverflow.com/questions/796 ... ated-funct