[New-bugs-announce] [issue30129] functools.partialmethod should look more like what it's impersonating.

Skip Montanaro report at bugs.python.org
Fri Apr 21 10:27:51 EDT 2017


New submission from Skip Montanaro:

I needed to create a partial method in Python 2.7, so I grabbed functools.partialmethod from a Python 3.5.2 install. For various reasons, one of the reasons I wanted this was to suck in some methods from a delegated class so they appeared in dir() and help() output on the primary class (the one containing the partialmethod objects). Suppose I have

class Something:
  def meth(self, arg1, arg2):
    "meth doc"
    return arg1 + arg2

then in the metaclass for another class I construct an attribute (call it "mymeth") which is a partialmethod object. When I (for example), run pydoc, that other class's attribute appears as:

    mymeth = <functools.partial object>

It would be nice if it at least included the doc string from meth, something like:

    mymeth = <functools.partial object>
        meth doc

Even better would be a proper signature:

    mymeth(self, arg1, arg2)
        meth doc

In my copy of functools.partialmethod, I inserted an extra line in __get__, right after the call to partial():

    results.__doc__ = self.func.__doc__

That helps a bit, as I can

    print("mymeth doc:", inst.mymeth.__doc__)

and see

    mymeth doc: meth doc

displayed. That's not enough for help()/pydoc though.

I suspect the heavy lifting will have to be done in pydoc.Doc.document(). inspect.isroutine() returns False for functools.partial objects. I also see _signature_get_partial() in inspect.py. That might be the source of the problem. When I create a partialmethod object in my little example, it actually looks like a functools.partial object, not a partialmethod object. It's not clear that this test:

    if isinstance(partialmethod, functools.partialmethod):

in inspect._signature_from_callable() is testing for the correct type.

Apologies that I can't easily provide a detailed example. My Python 2.x metaclass example (where I'm smashing methods from one class into another) doesn't work in Python 3.x for some reason, the whole partialmethod thing isn't available in Python 2.x (inspect doesn't know about partialmethod or partial) and it's not really a "hello world"-sized example anyway. I'll beat on things a bit more to try and craft a workable Python 3.x example.

----------
components: Library (Lib)
messages: 292050
nosy: skip.montanaro
priority: normal
severity: normal
status: open
title: functools.partialmethod should look more like what it's impersonating.
type: enhancement
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30129>
_______________________________________


More information about the New-bugs-announce mailing list