[issue41905] add update_abstractmethods function to update an ABC's abstract methods

Raymond Hettinger report at bugs.python.org
Sat Oct 3 20:16:09 EDT 2020


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

Guido, can the method update be made automatic?  When instantiation fails, recheck to see the missing abstract methods had been defined?

    >>> from abc import *
    >>> class P(ABC):
            @abstractmethod
            def m(self):
                    pass
            
    >>> class C(P):
            pass

    >>> C.m = lambda self: None
    >>> C()
    Traceback (most recent call last):
      File "<pyshell#11>", line 1, in <module>
        C()
    TypeError: Can't instantiate abstract class C with abstract method m

The latter message doesn't make sense that it should occur at all.

Roughly, the existing instantiation logic is:

     if cls.__abstractmethods__:
         raise TypeError(f"TypeError: Can't instantiate abstract class
                         {cls.__name} with abstract method {methname}")

Could that be changed to something like this:

     if cls.__abstractmethods__:
         for methname is cls.__abstractmethods__.copy():
             if getattr(cls, methname).__isabstractmethod__:
                 raise TypeError(f"TypeError: Can't instantiate abstract class
                                  {cls.__name} with abstract method {methname}")
             cls.__abstractmethods__.remove(methname)

I haven't thought this through.  Was just thinking that it would nice to have automatic updates rather than transferring the responsibility outside of the core ABC logic.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41905>
_______________________________________


More information about the Python-bugs-list mailing list