Suppose you wish to define a proper type-forwarding decorator in python, which
supports both the common default call pattern; and the argument override call
pattern:
deffoo( fn = None, *, a = "xyz", ): defdecorator(fn): setattr(fn, "__foo__", a) return fn
if fn isNone: return decorator
else: return decorator(fn)
@foo defsome_method(x: int) -> float: ...
@foo defsome_other_method(x: int) -> float: ...
We can ask mypy the type of the resultant decorated method:
1 2
$ mypy -c 'import simple; reveal_type(simple.some_method)' <string>:1: note: Revealed type is "Any"
But establishing appropriate types, such that the types of the decorated method
are well-formed, is a challenge which requires use of TypeVar and the
@overload mechanic, and a fair amount of boilerplate: