deprecation module
deprecatedWhen a function or a method is deprecated, you can use the deprecated decorator. It will print a message to warn the user that the function is deprecated. The decorator takes two optional arguments:
We have a class Person defined in a file person.py. The get_surname method is deprecated, we must use the get_lastname method instead. For that, we use the deprecated decorator on the get_surname method. from logilab.common.deprecation import deprecated class Person(object): def __init__(self, firstname, lastname): self._firstname = firstname self._lastname = lastname def get_firstname(self): return self._firstname def get_lastname(self): return self._lastname @deprecated('[1.2] use get_lastname instead') def get_surname(self): return self.get_lastname() def create_user(firstname, lastname): return Person(firstname, lastname) if __name__ == '__main__': person = create_user('Paul', 'Smith') surname = person.get_surname() When running person.py we have the message below:
class_movedNow we moved the class Person in a new_person.py file. We notice in the person.py file that the class has been moved: from logilab.common.deprecation import class_moved import new_person Person = class_moved(new_person.Person) if __name__ == '__main__': person = Person('Paul', 'Smith') When we run the person.py file, we have the following message:
The class_moved function takes one mandatory argument and two optional:
class_renamedThe class_renamed function automatically creates a class which fires a DeprecationWarning when instantiated. The function takes two mandatory arguments and one optional:
We now rename the Person class into User class in the new_person.py file. Here is the new person.py file: from logilab.common.deprecation import class_renamed from new_person import User Person = class_renamed('Person', User) if __name__ == '__main__': person = Person('Paul', 'Smith') When running person.py, we have the following message:
movedThe moved function is used to tell that a callable has been moved to a new module. It returns a callable wrapper, so that when the wrapper is called, a warning is printed telling where the object can be found. Then the import is done (and not before) and the actual object is called. NoteThe usage is somewhat limited on classes since it will fail if the wrapper is used in a class ancestors list: use the class_moved function instead (which has no lazy import feature though). The moved function takes two mandatory parameters:
We will use in person.py, the create_user function which is now defined in the new_person.py file: from logilab.common.deprecation import moved create_user = moved('new_person', 'create_user') if __name__ == '__main__': person = create_user('Paul', 'Smith') When running person.py, we have the following message:
|