Author kj
Recipients BTaskaya, Zac Hatfield-Dodds, corona10, gvanrossum, kj, levkivskyi, serhiy.storchaka
Date 2020-11-02.05:38:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1604295522.64.0.913506062559.issue42195@roundup.psfhosted.org>
In-reply-to
Content
Dear Guido, from what I can see in the typing module, _CallableType already casts the args list to a tuple before creating the __CallableGenericAlias, so it should support cacheing. This is taken from the from _CallableType::

def __getitem__(self, params):
    ...  # (some checking code here)
    args, result = params
    ...  # (some checking code here)

        params = (tuple(args), result)  # args is cast to a tuple
    return self.__getitem_inner__(params)

@_tp_cache   # the cache
def __getitem_inner__(self, params):
    args, result = params
    ...  # (some checking code here)
    
    # This is the suspect code causing the flattening of args
    params = args + (result,)     
    return self.copy_with(params)

def copy_with(self, params):
    return _CallableGenericAlias(self.__origin__, params,
                                 name=self._name, inst=self._inst)

Changing the suspect code from ``params = args + (result,)`` to ``params = (args, result)`` allows typing.Callable to be consistent with collections.abc.Callable's GenericAlias, and also allows for cacheing.

With that change:

>>> from typing import Callable
>>> Callable[[int, ], str].__args__
((<class 'int'>,), <class 'str'>)  # note the args is a tuple

>>> from collections.abc import Callable
>>> Callable[[int, ], str].__args__
([<class 'int'>], <class 'str'>)   # note the args is a list

This isn't fully consistent with collections.abc.Callable's GenericAlias just yet, but it's close.
History
Date User Action Args
2020-11-02 05:38:42kjsetrecipients: + kj, gvanrossum, serhiy.storchaka, levkivskyi, corona10, Zac Hatfield-Dodds, BTaskaya
2020-11-02 05:38:42kjsetmessageid: <1604295522.64.0.913506062559.issue42195@roundup.psfhosted.org>
2020-11-02 05:38:42kjlinkissue42195 messages
2020-11-02 05:38:42kjcreate