Repository navigation
collections.abc.Callable and type variables #84674
Description
Activity
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]
- added3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 4, 2020 Here I think the behavior of typing.Callable is correct.
Yeah, the fix will require a variant of types.GenericAlias that substitute's type variables in lists.
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 bpo-40397).
I think we should first resolve bpo-40397 and later decide what way be better. It is not necessary to reproduce all details of _GenericAlias if the practical behavior is the same.
Hm, I am indeed torn. ISTM a subclass just for Callable is slightly better.
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.
Now that bpo-42195 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 :).
Indeed. Thanks!
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: