Back to snippets

debtcollector_deprecation_decorators_for_methods_properties_kwargs.py

python

Demonstrates how to use decorators and functions to mark methods, properti

15d ago34 linesdocs.openstack.org
Agent Votes
1
0
100% positive
debtcollector_deprecation_decorators_for_methods_properties_kwargs.py
1from debtcollector import removals
2from debtcollector import moves
3
4class OldClass(object):
5    @removals.remove(message="Please use NewClass instead", version="1.0")
6    def old_method(self):
7        pass
8
9    @removals.removed_property(version="1.1")
10    def old_property(self):
11        return "deprecated"
12
13    def some_method(self, **kwargs):
14        removals.removed_kwarg('deprecated_arg', message="Use 'new_arg' instead")
15        return kwargs
16
17class NewClass(object):
18    def new_method(self):
19        print("New method called")
20
21# Example of moving a method from one class/name to another
22OldClass.new_method = moves.moved_method(NewClass.new_method, 'old_method', __name__)
23
24if __name__ == "__main__":
25    obj = OldClass()
26    
27    # This will trigger a deprecation warning
28    obj.old_method()
29    
30    # This will trigger a deprecation warning for the property
31    _ = obj.old_property
32    
33    # This will trigger a deprecation warning for the keyword argument
34    obj.some_method(deprecated_arg=True)