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.

classification
Title: help() should use the class signature
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: jafo Nosy List: BreamoreBoy, jafo, ronaldoussoren, yselivanov
Priority: normal Keywords:

Created on 2013-03-14 21:26 by jafo, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg184194 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2013-03-14 21:26
David Beazley in his tutorial pointed out that you could use a metaclass to create function signatures for the common use case of:

class foo:
   def __init__(self, name, value, high, low):
      self.name = name
      self.value = value
      [...]

The signature can be used so that the classes created using an automation metaclass will show a signature of "(*args)".  inspect.signature will use this signature, but "help()" will not use the signature.

This is a stub created during the tutorial, I will flesh it out further during the sprints.
msg184498 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2013-03-18 19:28
This might be a duplicate of issue17053, but the patch provided there doesn't resolve the issue, at least as far as I know it.  Here is an example, from David Beazley's talk at PyCon 2013:

from inspect import Parameter, Signature

def make_signature(names):
   return Signature(
         Parameter(name, Parameter.POSITIONAL_OR_KEYWORD) for name in names)

class Structure:
   __signature__ = make_signature([])
   def __init__(self, *args, **kwargs):
      bound = self.__signature__.bind(*args, **kwargs)
      for name, val in bound.arguments.items():
         setattr(self, name, val)

class Stock(Structure):
   __signature__ = make_signature(['name', 'shares', 'price'])

pyth = Stock('PYTH', 100, 50)
help(pyth.__init__)

Which produces:

__init__(self, *args, **kwargs) method of __main__.Stock instance

Instead of:

__init__(self, name, shares, price) method of __main__.Stock instance
msg184540 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2013-03-18 22:29
If I read the code correctly help(pyth.__init__) won't use the __signature__ because that is an attribute of the type, not of the method itself. 

With the patch in issue17053 help should be better when __init__'s signatuer is set explicitly:


class Stock(Structure):
    __signature__ = make_signature(['name', 'shares', 'price'])
    def __init__(self, *args, **kwds):
        super(Stock, self).__init__(*args, **kwds)
    __init__.__signature__ = __signature__


Sadly enough it isn't easily possible to define a subclass of function where __signature__ is a property that returns the class attribute __signature__.
msg220504 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-06-13 21:50
issue17053 was closed in favour of issue19674 but I don't know if this issue is a duplicate of the former anyway.
msg221106 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-06-20 18:24
Since 3.4, help() uses signature.
Closing this one.
History
Date User Action Args
2022-04-11 14:57:42adminsetgithub: 61626
2014-06-20 18:24:40yselivanovsetstatus: open -> closed

nosy: + yselivanov
messages: + msg221106

resolution: rejected
2014-06-13 21:50:29BreamoreBoysetnosy: + BreamoreBoy
messages: + msg220504
2013-03-18 22:29:36ronaldoussorensetnosy: + ronaldoussoren
messages: + msg184540
2013-03-18 19:28:28jafosettype: behavior
messages: + msg184498
2013-03-14 21:26:30jafocreate