Skip to content

collections.abc.Callable and type variables #84674

Description

@serhiy-storchaka
BPO 40494
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi, @Fidget-Spinner

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 2020-12-25.22:10:21.763>
created_at = <Date 2020-05-04.08:07:12.472>
labels = ['type-bug', 'library', '3.9']
title = 'collections.abc.Callable and type variables'
updated_at = <Date 2020-12-25.22:10:21.762>
user = 'https://github.com/serhiy-storchaka'

bugs.python.org fields:

activity = <Date 2020-12-25.22:10:21.762>
actor = 'gvanrossum'
assignee = 'none'
closed = True
closed_date = <Date 2020-12-25.22:10:21.763>
closer = 'gvanrossum'
components = ['Library (Lib)']
creation = <Date 2020-05-04.08:07:12.472>
creator = 'serhiy.storchaka'
dependencies = []
files = []
hgrepos = []
issue_num = 40494
keywords = []
message_count = 8.0
messages = ['368015', '368020', '368055', '368058', '368314', '381070', '383744', '383772']
nosy_count = 4.0
nosy_names = ['gvanrossum', 'serhiy.storchaka', 'levkivskyi', 'kj']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue40494'
versions = ['Python 3.9']

Activity

  1. serhiy-storchaka commented on May 4, 2020

    @serhiy-storchaka
    MemberAuthor

    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]
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    type-bugAn unexpected behavior, bug, or error
    on May 4, 2020
  3. ilevkivskyi commented on May 4, 2020

    @ilevkivskyi
    Member

    Here I think the behavior of typing.Callable is correct.

  4. gvanrossum commented on May 4, 2020

    @gvanrossum
    Member

    Yeah, the fix will require a variant of types.GenericAlias that substitute's type variables in lists.

  5. serhiy-storchaka commented on May 4, 2020

    @serhiy-storchaka
    MemberAuthor

    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.

  6. gvanrossum commented on May 6, 2020

    @gvanrossum
    Member

    Hm, I am indeed torn. ISTM a subclass just for Callable is slightly better.

  7. gvanrossum commented on Nov 16, 2020

    @gvanrossum
    Member

    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.

  8. Fidget-Spinner commented on Dec 25, 2020

    @Fidget-Spinner
    Member

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

  9. gvanrossum commented on Dec 25, 2020

    @gvanrossum
    Member

    Indeed. Thanks!

  10. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.9 (EOL)end of lifestdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions