Dennis Sweeney <sweeney.dennis...@gmail.com> added the comment:

I don't think changing @wraps is what you want. Even if you manually set 
`wrapper.__isabstractmethod__ = False`, you won't reach `print('f is 
called!')`, since f() is overridden by the child. And if you do that, the ABC 
wouldn't have any abstract methods, since the name A.f points to the function 
defined at `def wrapper()`.

If I understand correctly, you want to modify the behavior of a method while 
also having it be abstract. These two goals are sort of in tension with one 
another, since defining f as abstract means your implementation won't be used, 
yet you want to specify part of the implementation. One solution would be to 
modify the subclass's methods to do what you want when the subclass is created.


from abc import ABC, abstractmethod
from functools import wraps

class A(ABC):
    @abstractmethod
    def f(self):
        pass

    def __init_subclass__(cls, **class_kwargs):
        old_f = cls.f
        @wraps(old_f)
        def wrapper(*args, **kwargs):
            print("f is called!")
            old_f(*args, **kwargs)
        cls.f = wrapper
        super().__init_subclass__()

class B(A):
    def f(self):
        print('f!')

class C(A):
    pass

B().f()
# f is called!
# f!

C()
# TypeError: Can't instantiate abstract class C with abstract method f

----------
nosy: +Dennis Sweeney

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43010>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to