
24.12.21 00:09, Guido van Rossum пише:
Without decorator too (that was Lukasz’ idea). Why bother with the decorator (*if* we were to go there)?
It is errorprone. Some library provide function foo() which returns an instance of private type _Foo and people start using it as a type hint. A new version converts foo() into a class. It is usually a safe backward compatible change, except that now all type hints became wrong. "a: foo" now means an instance of foo, not a callable which returns an instance of _Foo. There are also issues with subclassing.
foo = Callable[[int], str] class A(foo): ... ... def bar(x: int, /) -> str: pass ... class B(bar): ... ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: function() argument 'code' must be code, not str
T = TypeVar('T') foo = Callable[[T], list[T]] list[foo][int]
There are also issues with subscripting: list[typing.Callable[[int], list[int]]]
def bar(x: T, /) -> list[T]: pass ... list[bar][int] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: There are no type variables left in list[__main__.bar]