This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: collections.abc.Callable and type variables
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, levkivskyi, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-05-04 08:07 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg368015 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-04 08:07
There is a difference between typing.Callable and collections.abc.Callable.

>>> import typing, collections.abc
>>> T = typing.TypeVar('T')
>>> C1 = typing.Callable[[T], T]
>>> C2 = collections.abc.Callable[[T], T]
>>> C1
typing.Callable[[~T], ~T]
>>> C2
collections.abc.Callable[[~T], ~T]
>>> C1[int]
typing.Callable[[int], int]
>>> C2[int]
collections.abc.Callable[[~T], int]
msg368020 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2020-05-04 08:31
Here I think the behavior of typing.Callable is correct.
msg368055 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-05-04 15:05
Yeah, the fix will require a variant of types.GenericAlias that substitute's type variables in lists.
msg368058 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-04 15:20
Of course. There is more than one way to fix it:

* Make GenericAlias substituting type variables in list. It is easier and it will fix this particular case, but there will be subtle differences in __args__.
* Add a GenericAlias subclass with overridden constructor, __repr__, __getitem__, __reduce__ (like the _GenericAlias subclass added in issue40397).

I think we should first resolve issue40397 and later decide what way be better. It is not necessary to reproduce all details of _GenericAlias if the practical behavior is the same.
msg368314 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-05-06 23:31
Hm, I am indeed torn. ISTM a subclass just for Callable is slightly better.
msg381070 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-16 05:22
From https://bugs.python.org/issue42195 it looks like we need to create a subclass just for Callable. See https://bugs.python.org/issue42102 and PR 22848.
msg383744 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2020-12-25 15:01
Now that issue42195 has been resolved by subclassing types.GenericAlias, can this be closed?

On 3.9 and 3.10:

>>> import typing, collections.abc
>>> T = typing.TypeVar('T')
>>> C2 = collections.abc.Callable[[T], T]
>>> C2[int]
collections.abc.Callable[[int], int]

It seems to be fixed :).
msg383772 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-12-25 22:10
Indeed. Thanks!
History
Date User Action Args
2022-04-11 14:59:30adminsetgithub: 84674
2020-12-25 22:10:21gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg383772

stage: resolved
2020-12-25 15:01:05kjsetnosy: + kj
messages: + msg383744
2020-11-16 05:22:34gvanrossumsetmessages: + msg381070
2020-05-06 23:31:59gvanrossumsetmessages: + msg368314
2020-05-04 15:20:04serhiy.storchakasetmessages: + msg368058
2020-05-04 15:05:28gvanrossumsetmessages: + msg368055
2020-05-04 08:31:40levkivskyisetmessages: + msg368020
2020-05-04 08:07:12serhiy.storchakacreate