I like the Callable decorator idea very much.
It supports all Python function flavors out of the box, isn't it?

Also, what is about allowing to make callable types from existing functions
(and even methods maybe) with type hints?
def f(a: int, /, b: float) -> str:
    return str(a*b)

F = Callable(f)

Could it work?

I'm ok with making an explicit Callable type alias first for every usage.
But if I can create it from an existing function augmented with type hints
without copy-pasting the signature -- it can make my life significantly
easier.

What do you think?

On Fri, Dec 24, 2021 at 11:57 AM Serhiy Storchaka <storch...@gmail.com>
wrote:

> 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
>
> There are also issues with subscripting:
>
> >>> T = TypeVar('T')
> >>> foo = Callable[[T], list[T]]
> >>> list[foo][int]
> 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]
>
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-le...@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/ALW5PMZ3MRIG3BGTX5DVIKFO4JS45MBK/
> Code of Conduct: http://python.org/psf/codeofconduct/
>


-- 
Thanks,
Andrew Svetlov
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/MWWWIIYOWH3TCZGAW7J6OHDCO6IDTJWZ/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to