classification
Title: functools.singledispatch interacts poorly with methods
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Ethan Smith, levkivskyi, lukasz.langa, ncoghlan
Priority: normal Keywords: patch

Created on 2017-12-19 22:23 by Ethan Smith, last changed 2018-05-28 22:47 by Ethan Smith. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 4987 closed Ethan Smith, 2017-12-23 07:12
PR 6306 merged Ethan Smith, 2018-03-29 10:38
Messages (6)
msg308687 - (view) Author: Ethan Smith (Ethan Smith) * Date: 2017-12-19 22:23
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.
msg308944 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-12-22 23:53
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.
msg314644 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-03-29 12:17
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).
msg314817 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2018-04-02 17:40
+1
msg317765 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2018-05-26 20:38
New changeset c651275afe8515b2cf70b8152e19ce39df88f0dd by Łukasz Langa (Ethan Smith) in branch 'master':
bpo-32380: Create functools.singledispatchmethod (#6306)
https://github.com/python/cpython/commit/c651275afe8515b2cf70b8152e19ce39df88f0dd
msg317941 - (view) Author: Ethan Smith (Ethan Smith) * Date: 2018-05-28 22:47
This was fixed, so I think it can be closed.
History
Date User Action Args
2018-05-28 22:47:25Ethan Smithsetstatus: open -> closed
resolution: fixed
messages: + msg317941

stage: patch review -> resolved
2018-05-26 20:38:36lukasz.langasetmessages: + msg317765
2018-04-02 17:40:29lukasz.langasetmessages: + msg314817
2018-03-29 12:17:13ncoghlansetnosy: + ncoghlan, lukasz.langa
messages: + msg314644
2018-03-29 10:44:13Ethan Smithsetversions: + Python 3.8, - Python 3.6
2018-03-29 10:38:40Ethan Smithsetpull_requests: + pull_request6024
2017-12-23 07:12:16Ethan Smithsetkeywords: + patch
stage: patch review
pull_requests: + pull_request4873
2017-12-22 23:53:47levkivskyisetnosy: + levkivskyi
messages: + msg308944
2017-12-19 22:23:08Ethan Smithcreate