Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arguments ignored in substitution in typing.Callable #88956

Closed
serhiy-storchaka opened this issue Jul 31, 2021 · 5 comments
Closed

Arguments ignored in substitution in typing.Callable #88956

serhiy-storchaka opened this issue Jul 31, 2021 · 5 comments
Labels
3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

BPO 44793
Nosy @gvanrossum, @serhiy-storchaka, @miss-islington, @Fidget-Spinner
PRs
  • bpo-44793: Fix checking the number of arguments when subscribe a gene… #27515
  • [3.10] bpo-44793: Fix checking the number of arguments when subscribe a generic type with ParamSpec parameter. (GH-27515) #27537
  • 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:

    assignee = None
    closed_at = <Date 2021-08-02.07:32:47.001>
    created_at = <Date 2021-07-31.12:24:26.513>
    labels = ['type-bug', 'library', '3.10', '3.11']
    title = 'Arguments ignored in substitution in typing.Callable'
    updated_at = <Date 2021-08-02.07:32:47.000>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-08-02.07:32:47.000>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-02.07:32:47.001>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2021-07-31.12:24:26.513>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44793
    keywords = ['patch']
    message_count = 5.0
    messages = ['398636', '398648', '398656', '398728', '398730']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'miss-islington', 'kj']
    pr_nums = ['27515', '27537']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44793'
    versions = ['Python 3.10', 'Python 3.11']

    @serhiy-storchaka
    Copy link
    Member Author

    >>> 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.

    @serhiy-storchaka serhiy-storchaka added 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jul 31, 2021
    @serhiy-storchaka
    Copy link
    Member Author

    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.

    @serhiy-storchaka
    Copy link
    Member Author

    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]

    @serhiy-storchaka
    Copy link
    Member Author

    New changeset f92b913 by Serhiy Storchaka in branch 'main':
    bpo-44793: Fix checking the number of arguments when subscribe a generic type with ParamSpec parameter. (GH-27515)
    f92b913

    @miss-islington
    Copy link
    Contributor

    New changeset c8db292 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)
    c8db292

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants