[Python-ideas] Class introspection by pydoc vs. attributes on meta classes

Ronald Oussoren ronaldoussoren at mac.com
Sat Jul 25 13:47:06 CEST 2015


Hi,

Pydoc (and AFAIK loads of other introspection tools as well) currently ignores attributes on metaclasses. I wonder if it would be better to teach pydoc (and possibly inspect as well) about those, and thought it would be better to ask here before starting on a patch.

That is, given the following definitions:

class Meta (type):
    def hidden_class(cls):
        print('H', cls.__name__)


class MyObject (metaclass=Meta):
    @classmethod
    def public_class(cls):
        print('P', cls.__name__)

Pydoc will show “public_class” as a class method of “MyObject” when you use “help(MyObject)”, but it will not show that there is a method named “hidden_class” that can be called on “MyObject” (but not on instances of that class).  That is a problem when “hidden_class” is part of the public interface of the class itself.

The issue was found in the context of PyObjC (obviously…):  Objective-C classes can and do have instance and class methods of the same name. Those cannot both be a descriptor in the Python proxy class, and that’s why PyObjC uses an metaclass to expose Objective-C class methods to Python code. This works very well, except for some problems with introspection such as the pydoc issue I refer to above.

Ronald


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150725/49ec8da1/attachment.html>


More information about the Python-ideas mailing list