classification
Title: Arguments ignored in substitution in typing.Callable
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, kj, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2021-07-31 12:24 by serhiy.storchaka, last changed 2021-08-02 07:32 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27515 merged serhiy.storchaka, 2021-07-31 17:35
PR 27537 merged miss-islington, 2021-08-02 06:18
Messages (5)
msg398636 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-31 12:24
>>> import typing
>>> T = typing.TypeVar('T')
>>> P = typing.ParamSpec('P')
>>> C = typing.Callable[P, T]
>>> C[int, str, float]
typing.Callable[[int], str]

int substitutes P as [int], str substitutes T, and the third argument float is ignored.
msg398648 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-31 16:52
If Callable is nested, it works as expected:

>>> typing.List[C][int, str, float]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/typing.py", line 309, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/typing.py", line 1028, in __getitem__
    _check_generic(self, params, len(self.__parameters__))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/serhiy/py/cpython/Lib/typing.py", line 231, in _check_generic
    raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Too many parameters for typing.List[typing.Callable[~P, ~T]]; actual 3, expected 2

collections.abc.Callable raises an error too.
msg398656 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-07-31 17:50
There is also similar bug in Generic:

>>> from typing import *
>>> T = TypeVar("T")
>>> P = ParamSpec("P")
>>> class X(Generic[T, P]):
...     f: Callable[P, int]
...     x: T
... 
>>> P_2 = ParamSpec("P_2")
>>> X[int, P_2, str]
__main__.X[int, ~P_2]
msg398728 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-02 06:18
New changeset f92b9133ef67e77605cbd315b6b6c81036ce110e by Serhiy Storchaka in branch 'main':
bpo-44793: Fix checking the number of arguments when subscribe a generic type with ParamSpec parameter. (GH-27515)
https://github.com/python/cpython/commit/f92b9133ef67e77605cbd315b6b6c81036ce110e
msg398730 - (view) Author: miss-islington (miss-islington) Date: 2021-08-02 07:08
New changeset c8db292012dd84aab81eb3ed9146709696a3d290 by Miss Islington (bot) in branch '3.10':
bpo-44793: Fix checking the number of arguments when subscribe a generic type with ParamSpec parameter. (GH-27515)
https://github.com/python/cpython/commit/c8db292012dd84aab81eb3ed9146709696a3d290
History
Date User Action Args
2021-08-02 07:32:47serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-08-02 07:08:33miss-islingtonsetmessages: + msg398730
2021-08-02 06:18:17serhiy.storchakasetmessages: + msg398728
2021-08-02 06:18:10miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26047
2021-08-01 07:09:52serhiy.storchakalinkissue44801 dependencies
2021-07-31 17:50:58serhiy.storchakasetmessages: + msg398656
2021-07-31 17:35:59serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request26030
2021-07-31 16:52:30serhiy.storchakasetmessages: + msg398648
2021-07-31 12:24:26serhiy.storchakacreate