Skip to main content

Python decorator and command to automate deprecating components

Project description

Auto deprecator

https://img.shields.io/pypi/v/auto_deprecator.svg https://travis-ci.org/auto-deprecator/auto-deprecator.svg Documentation Status https://img.shields.io/pypi/dm/auto-deprecator.svg

Deprecation toolkit in Python

How does it work?

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

https://raw.githubusercontent.com/auto-deprecator/auto-deprecator/master/docs/cycle.png

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 hello_world.py --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.

Installation

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.

.
├── setup.py
└── test_py_project
    ├── cli.py
    ├── __init__.py
    ├── test_py_project.py
    └── utils
        └── __init__.py

With the bash command “curl”,

curl https://raw.githubusercontent.com/auto-deprecator/auto-deprecator/develop/auto_deprecator/__init__.py -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 https://raw.githubusercontent.com/auto-deprecator/auto-deprecator/develop/auto_deprecator/__init__.py -o test_py_project/utils/auto_deprecator.py

Features

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

# hello_world.py


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 hello_world.py --version 2.1.0

History

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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 2 Python 3

File details

Details for the file auto_deprecator-2020.5.0.tar.gz.

File metadata

  • Download URL: auto_deprecator-2020.5.0.tar.gz
  • Upload date:
  • Size: 89.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.5.1

File hashes

Hashes for auto_deprecator-2020.5.0.tar.gz
Algorithm Hash digest
SHA256 bc6ebf0862e04da8c071216bdf56f9733a5c690f1504ea64808f3f0588e43a8c
MD5 dedc9439c635d93c7496adccbfe35004
BLAKE2b-256 10ed53025cb0b850a67ced7104253b2d718385bea996ea436668f91a4c13134e

See more details on using hashes here.

File details

Details for the file auto_deprecator-2020.5.0-py2.py3-none-any.whl.

File metadata

  • Download URL: auto_deprecator-2020.5.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.5.1

File hashes

Hashes for auto_deprecator-2020.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5e0cab71289ad8767bdc4fd65a33bb435f55136346317d805dcbbbbd23eb3183
MD5 df378cd3a762fdbfe2b014d5c2da5287
BLAKE2b-256 a3a0b5622df34d4af71e8484c67ecb7f0ed6399e4401957b275e1934ffeb7cc9

See more details on using hashes here.

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