Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

functools.singledispatch interacts poorly with methods #76561

Closed
ethanhs mannequin opened this issue Dec 19, 2017 · 7 comments
Closed

functools.singledispatch interacts poorly with methods #76561

ethanhs mannequin opened this issue Dec 19, 2017 · 7 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ethanhs
Copy link
Mannequin

ethanhs mannequin commented Dec 19, 2017

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']

    @ethanhs
    Copy link
    Mannequin Author

    ethanhs mannequin commented Dec 19, 2017

    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.

    @ethanhs ethanhs mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 19, 2017
    @ilevkivskyi
    Copy link
    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.

    @ethanhs ethanhs mannequin added the 3.8 only security fixes label Mar 29, 2018
    @ncoghlan
    Copy link
    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).

    @ambv
    Copy link
    Contributor

    ambv commented Apr 2, 2018

    +1

    @ambv
    Copy link
    Contributor

    ambv commented May 26, 2018

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

    @ethanhs
    Copy link
    Mannequin Author

    ethanhs mannequin commented May 28, 2018

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

    @ethanhs ethanhs mannequin closed this as completed May 28, 2018
    @methane
    Copy link
    Member

    methane commented Mar 27, 2019

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

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants