Skip to main content

Python decorator and command to automate deprecating components

Project description

Auto deprecator Documentation Status

Deprecation toolkit in Python

How does it work?

The library provides the full cycle to deprecate a function in the following ways

For example, a function called old_hello_world should be deprecated in the version 2.0.0, while the current version of the library is 1.0.0.

Add a decorator deprecate above the function like the below can manage the mentioned deprecation cycle.

from auto_deprecator import deprecate

@deprecate(expiry='2.0.0', current='1.9.0')
def old_hello_world():
    return print("Hello world!")

def hello_world():
    return print("Hello world again!")

You can also suggest the replacing function / method. For details, please refer to the section Provide hints to users.

Warning Stage

Alert the users the deprecation time

When the user calls the methods or initializes the objects which will be deprecated in the next version or on an expected date, the user should receive the warning of the future deprecation but get the return in success. The default warning handler is to throw a DeprecationWarning and the handle method can be customized in the section Customize the deprecation handling

>>> old_hello_world()
Hello world!
DeprecationWarning: The function "old_hello_world" will be deprecated on version 2.0.0
Test as if deprecated

Before the component is deprecated, unit / integration testing should be run to ensure the deprecation does not break the existing flow. Pass in the environment variables in the testing to simulate that the version is deployed.

(bash) hello-world-app
Hello world!
DeprecationWarning: The function "old_hello_world" will be deprecated in version 2.0.0
(bash) DEPRECATED_VERSION=2.0.0 hello-world-app
Traceback (most recent call last):
RuntimeError: The function "old_hello_world" is deprecated in version 2.0.0

Expired Stage

If the current version has reached the function expiry version, calling the deprecated function will trigger the exception by default.

from auto_deprecator import deprecate

__version__ = '2.0.0'

@deprecate(expiry='2.0.0', current=__version__)
def old_hello_world():
    return print("Hello world!")

For example, the above function is called by the downstream process after-hello-world. The owner of the process is not aware that the function should be deprecated and replaced by another function, and the process is crashed by the default exception. To work around the exception in the production, before a proper fix is provided, the environment variable DEPRECATED_VERSION can be injected in the downstream process.

DEPRECATED_VERSION=1.9 after-hello-world

Cleaning Stage

Automatic deprecation before release

Deprecating the functions is no longer a manual work. Every time before release, run the command auto-deprecate to remove the functions deprecated in the coming version.

$ auto-deprecate --version 2.0.0

The command removes the function old_hello_world from the source codes as the expiry version is 2.0.0. Also, if the source file does not require to import the auto-deprecate anymore (as all the functions have already been deprecated), the import line will be removed as well.

$ git difftool -y -x sdiff
from auto_deprecator import deprecate                         <
@deprecate(expiry='2.0.0', current='1.9.0')                   <
def old_hello_world():                                        <
    return print("Hello world!")                              <
def hello_world():                                              def hello_world():
    return print("Hello world again!")                        /     return print("Hello world again!")

The function with a comment line to state the expiry version is another way to inform the script auto-deprecate to remove the part of the code when it is expired. For example,

def old_hello_world():
    # auto-deprecate: expiry=2.0.0
    print('hello world')

For the details of the comment hints, please refer to the section Auto deprecation hints in comments.


The library can be easily installed with pip

pip install auto-deprecator

Alternative Installation

If the auto-deprecator is included and the functions are well deprecated (following the whole cycle mentioned above), your software does not need auto-deprecator anymore. For developers who are not comfortable to include a library not always in use as a dependency, they can just clone the source code into your project instead.

For example, your Python project contains a module called “utils” to maintain all the utility functions.

└── test_py_project
    └── utils

With the bash command “curl”,

curl -o $DEST

the source code of auto-deprecator can be cloned into the target directory, i.e. “test_py_project/utils” in the example

curl -o test_py_project/utils/


Provide hints to users

Provide the parameter “relocate”, the warning / error message will inform the user about the relocated method.

@deprecate(expiry='2.1.0', current='2.0.0', relocate='new_compute_method')
def compute_method():
    return 'hello world'
>>> old_hello_world()
Hello world!
DeprecationWarning: The function "old_hello_world" will be deprecated on version 2.0.0..
                    Please use method / function "new_compute_method".

Import current version from module name

Instead of importing the version (__version__) in the module,

from your_package import __version__

@deprecate(expiry='2.1.0', current=__version__)
def compute_method():
    return 'hello world'

specifying the module name, which includes the version attribute, can help maintain the source code in a clean manner.

@deprecate(expiry='2.1.0', version_module='your_package')
def compute_method():
    return 'hello world'

Especially if the function is removed by the action auto-deprecate, the unused import will not be left in the module.

Customize the deprecation handling

By default, the deprecate decorator raise DeprecationWarning for the future expiry and RuntimeError on the expiration. The behavior can be modified so as to fit in the infrastructure / production environment.

For example, the DeprecationWarning can be replaced by a simple print out by injecting a callable function into the parameter warn_handler.

@deprecate(expiry='2.1.0', current='2.0.0', warn_handler=print)
def compute_method():
    return 'hello world'

Same for injecting a callable function into the parameter error_handler, the behavior is replaced if the function is deprecated.

Auto deprecation hints in comments

The auto deprecation script handles not only the expiry parts wrapped by the decorator, but also those stated with comments. The comment line in the format # auto-deprecate: expiry=<version> in the scope of the function or class is treated same as the decorator hints @deprecate(expiry="version", ...).

For example, the below function will be removed


def old_hello_world():
    # auto-deprecate: expiry=2.0.0
    print('hello world')

when the script is called with current version greater than 2.0.0

$ auto-deprecate --version 2.1.0


2020.4.0 (2020-04-23)

  • Support deprecation hints in comments

  • Simplify the project architecture

2020.3.0 (2020-04-11)

  • Support automatic deprecation in the directory

  • Support customizing the deprecation handler

2020.2.0 (2020-02-11)

  • Introduce parameter version_module in the deprecate decorator, to import the version dynamically

  • Removed magic version import

2010.1.0, 2010.1.1, 2010.1.2 (2020-01-21)

  • Support alerting the users the deprecate version

  • Support testing with environment variables

  • Support automatically deprecate the expiry source code

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

auto_deprecator-2020.5.0.tar.gz (89.8 kB view hashes)

Uploaded Source

Built Distribution

auto_deprecator-2020.5.0-py2.py3-none-any.whl (9.6 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page