[Python-ideas] __implements__ on arguments to ABCMeta.register

Eric Snow ericsnowcurrently at gmail.com
Sat May 21 02:03:17 CEST 2011

ABCMeta.register is great.  It adds the cls argument to the _abc_registry of
the ABC.  However, the class that was passed in does not get touched.  If
you then want to find out to which classes a class has been registered, you
can't find out from that class.  Whereas your can find out from an abstract
base class which classes have been registered to it.

I propose having ABCMeta.register add/update a special method __implements__
to the class that is getting registered.  This would not be done to
builtin/extension types.  It adds the ABC to the __implements__ of the
subclass that is getting registered.  Something along these lines, right
before the final return in the method:

        if not hasattr(subclass, "__implements__"):
                subclass.__implements__ = {cls}
            except TypeError:

This is a small addition, but I realize it [potentially] adds another
special method to classes, so it's not trivial.

The use case is that I want to be able to validate that a class implements
all of the abstract methods of all the classes to which it has been
registered.  I don't have a programmatic way of discovering that set without
asking every class out there.  This is an easy way to accomplish this (for
non-extension/non-builtin types).  An alternative is to subclass ABCMeta and
tack this on, but that only works for my ABCs.  Another is to use a class
decorator to do this any place I do a register (or even to do the register
too), but again, only for the places that I do the registration.

Anyway, if it's useful to me then it may be useful to others, so I wanted to
put this out there.  I expect this has come up before, particularly during
discussions about PEP 3119.  However, I wasn't able to track down anything
specifically about doing this sort of "reverse registration".  And, of
course, I may be overestimating the value of this functionality.  If this
does not seem that valuable to anyone else, then no big deal.  :)

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

More information about the Python-ideas mailing list