Skip to content

functools.singledispatchmethod does not define __class_getitem__ #89847

Closed

Description

@AlexWaygood
BPO 45684
Nosy @rhettinger, @ambv, @AlexWaygood
PRs
  • bpo-45684: Add __class_getitem__ method to singledispatchmethod #29355
  • 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-12-12.10:05:25.386>
    created_at = <Date 2021-11-01.18:23:01.442>
    labels = ['type-bug', '3.9', '3.10', '3.11', 'invalid', 'library']
    title = '`functools.singledispatchmethod` does not define `__class_getitem__`'
    updated_at = <Date 2021-12-12.10:05:25.385>
    user = 'https://github.com/AlexWaygood'

    bugs.python.org fields:

    activity = <Date 2021-12-12.10:05:25.385>
    actor = 'AlexWaygood'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-12-12.10:05:25.386>
    closer = 'AlexWaygood'
    components = ['Library (Lib)']
    creation = <Date 2021-11-01.18:23:01.442>
    creator = 'AlexWaygood'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45684
    keywords = ['patch']
    message_count = 4.0
    messages = ['405462', '405819', '405850', '408373']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'lukasz.langa', 'AlexWaygood']
    pr_nums = ['29355']
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45684'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. AlexWaygood commented on Nov 1, 2021

      @AlexWaygood
      MemberAuthor

      functools.singledispatchmethod is marked as being a generic class in typeshed, but does not define __class_getitem__, so cannot be parameterized at runtime.

      cpython source code:

      class singledispatchmethod:

      typeshed stub: https://github.com/python/typeshed/blob/f4143c40e85db42dc98549e09329e196668395ee/stdlib/functools.pyi#L94

    2. added
      3.11only security fixes
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Nov 1, 2021
    3. AlexWaygood commented on Nov 5, 2021

      @AlexWaygood
      MemberAuthor

      To clarify why this is an issue:

      It's probably quite improbable that anybody would ever need to use singledispatchmethod in a type annotation. But, if they do, they'll find themselves in an impossible situation if they're using mypy with the --strict setting. If you're using --strict (as I always do), mypy will complain if you leave unparameterised any class that is marked as a generic in typeshed. But if you try to parameterise singledispatchmethod, and you're not using from __future__ import annotations, your code will fail at runtime.

      It would be better if the cpython source were changed to add __class_getitem__ to singledispatchmethod, rather than making the class no longer generic in typeshed. This is because being able to parameterise classes gives mypy much more information to work with, giving it the ability to provide far more fine-grained analysis of your code.

      Here is a minimal reproducible example of the error mypy will give: https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict

    4. AlexWaygood commented on Nov 6, 2021

      @AlexWaygood
      MemberAuthor
    5. AlexWaygood commented on Dec 12, 2021

      @AlexWaygood
      MemberAuthor

      Closing this, as I don't think the use case I presented is strong enough, and there are ultimately more important things to worry about.

    6. 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.10 (EOL)end of life3.11only security fixes3.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