Issue 32380: functools.singledispatch interacts poorly with methods (original) (raw)

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: @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: Alyssa 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.
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) https://github.com/python/cpython/commit/bc284f0c7a9a7a9a4bf12c680823023a6770ce06
History
Date User Action Args
2022-04-11 14:58:55 admin set github: 76561
2019-03-27 09:15:21 methane set nosy: + methanemessages: +
2019-03-27 08:36:17 methane set pull_requests: + <pull%5Frequest12524>
2018-05-28 22:47:25 ethan smith set status: open -> closedresolution: fixedmessages: + stage: patch review -> resolved
2018-05-26 20:38:36 lukasz.langa set messages: +
2018-04-02 17:40:29 lukasz.langa set messages: +
2018-03-29 12:17:13 ncoghlan set nosy: + ncoghlan, lukasz.langamessages: +
2018-03-29 10:44:13 ethan smith set versions: + Python 3.8, - Python 3.6
2018-03-29 10:38:40 ethan smith set pull_requests: + <pull%5Frequest6024>
2017-12-23 07:12:16 ethan smith set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest4873>
2017-12-22 23:53:47 levkivskyi set nosy: + levkivskyimessages: +
2017-12-19 22:23:08 ethan smith create