tantale / deprecated

Python @deprecated decorator to deprecate old python classes, functions or methods.
MIT License
298 stars 32 forks source link

deprecate abstract method #72

Open bertsky opened 3 weeks ago

bertsky commented 3 weeks ago

This is a feature request (I think): I would like deprecated to be able to yield warnings on a method call, even when overriding the implementation in a subclass. In particular, I would like to be able to deprecate abstract methods.

Expected Behavior

Running…

from deprecated import deprecated

class Parent:
    @deprecated(reason='try not to laugh')
    def fun(self, arg):
        raise NotImplementedError()

class Child(Parent):
    def fun(self, arg):
        print(arg)
        print('yaye!')

some = Child()
some.fun('whoops')

…I would like to see…

DeprecationWarning: Call to deprecated method fun. (try not to laugh)
  some.fun('whoops')
whoops
yaye!

Actual Behavior

However, since the method is overriden, I can see no warning:

whoops
yaye!

Environment

bertsky commented 3 weeks ago

To elaborate: the use-case is quite simple: I have a library with an abstract method, which gets deprecated at some point. Inheriting code (not under my control) should see the deprecation warning when their implemented method gets called.

As a workaround, of course I can place the following in the superclass' constructor:

class Parent:
    def __init__(self, ...)
        ...
        setattr(self, 'fun', deprecated(reason='try not to laugh')(getattr(self, 'fun')))

But of course it would be much nicer if deprecated would offer something along those lines itself.