This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2017-12-19 22:23 by ethan smith, last changed 2022-04-11 14:58 by admin. 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
PR 12580 merged methane, 2019-03-27 08:36
Messages (7)
msg308687 - (view) Author: Ethan Smith (ethan smith) * Date: 2017-12-19 22:23
Consider the following:

from functools import singledispatch

class Dispatch:
    def foo(self, a):
        return a

    def _(self, a):
        return "int"

    def _(self, a):
        return "str"

cls = Dispatch()  # 3'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
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)
msg317941 - (view) Author: Ethan Smith (ethan smith) * Date: 2018-05-28 22:47
This was fixed, so I think it can be closed.
msg338945 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-03-27 09:15
New changeset bc284f0c7a9a7a9a4bf12c680823023a6770ce06 by Inada Naoki in branch 'master':
bpo-32380: add "versionadded: 3.8" to singledispatchmethod (GH-12580)
Date User Action Args
2022-04-11 14:58:55adminsetgithub: 76561
2019-03-27 09:15:21methanesetnosy: + methane
messages: + msg338945
2019-03-27 08:36:17methanesetpull_requests: + pull_request12524
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