Skip to content

functools.singledispatch interacts poorly with methods #76561

Closed

Description

@ethanhs
mannequin
BPO 32380
Nosy @ncoghlan, @methane, @ambv, @ilevkivskyi, @ethanhs
PRs
  • bpo-32380 Fix singledispatch interaction with methods #4987
  • bpo-32380: Create functools.singledispatchmethod #6306
  • bpo-32380: add "versionadded: 3.8" to singledispatchmethod #12580
  • 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 2018-05-28.22:47:25.724>
    created_at = <Date 2017-12-19.22:23:08.394>
    labels = ['3.8', 'type-bug', 'library']
    title = 'functools.singledispatch interacts poorly with methods'
    updated_at = <Date 2019-03-27.09:15:21.725>
    user = 'https://github.com/ethanhs'

    bugs.python.org fields:

    activity = <Date 2019-03-27.09:15:21.725>
    actor = 'methane'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-05-28.22:47:25.724>
    closer = 'ethan smith'
    components = ['Library (Lib)']
    creation = <Date 2017-12-19.22:23:08.394>
    creator = 'ethan smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32380
    keywords = ['patch']
    message_count = 7.0
    messages = ['308687', '308944', '314644', '314817', '317765', '317941', '338945']
    nosy_count = 5.0
    nosy_names = ['ncoghlan', 'methane', 'lukasz.langa', 'levkivskyi', 'ethan smith']
    pr_nums = ['4987', '6306', '12580']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32380'
    versions = ['Python 3.8']

    Activity

    1. ethanhs commented on Dec 19, 2017

      ethanhsmannequin
      MannequinAuthor

      Consider the following:

      from functools import singledispatch
      
      class Dispatch:
          @singledispatch
          def foo(self, a):
              return a
      
          @foo.register(int)
          def _(self, a):
              return "int"
      
          @foo.register(str)
          def _(self, a):
              return "str"
      
      cls = Dispatch()
      cls.foo(3)  # 3
      cls.foo('hm')  # 'hm'

      I find this quite unintuitive. Essentially, since singledispatch dispatches based solely on a functions first argument, it is useless on methods unless one wraps it and modifies how it uses the internal wrapper function. I believe this should be relatively easy to fix with adding a check of inspect.ismethod and then modifying the number of the checked argument where appropriate.

      I'm happy to write a patch if this change is seen as a good idea.

    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Dec 19, 2017
    3. ilevkivskyi commented on Dec 22, 2017

      @ilevkivskyi
      Member

      I have also noticed this problem and I like the idea. It appeared some time ago on python-ideas, but no one has written a patch.

    4. ncoghlan commented on Mar 29, 2018

      @ncoghlan
      Contributor

      Added Łukasz to the nosy list, as I'd like his +1 before accepting this change (I do still think it's a good idea, for the same reasons we added partialmethod).

    5. ambv commented on Apr 2, 2018

      @ambv
      Contributor

      +1

    6. ambv commented on May 26, 2018

      @ambv
      Contributor

      New changeset c651275 by Łukasz Langa (Ethan Smith) in branch 'master':
      bpo-32380: Create functools.singledispatchmethod (bpo-6306)
      c651275

    7. ethanhs commented on May 28, 2018

      ethanhsmannequin
      MannequinAuthor

      This was fixed, so I think it can be closed.

    8. methane commented on Mar 27, 2019

      @methane
      Member

      New changeset bc284f0 by Inada Naoki in branch 'master':
      bpo-32380: add "versionadded: 3.8" to singledispatchmethod (GH-12580)
      bc284f0

    9. 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.8 (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