Skip to main content

Python Deprecation Toolkit

Project description

Python Deprecation Toolkit

Python code deprecation toolkit for deprecating your Python code using a shitlist approach.

Installation

Install using pip install py-deprecate.

Example

from py_deprecate import deprecated
from py_deprecate.exceptions import DeprecationIntroduced

def allowed_sum_caller() -> int:
    return sum(5, 10)

def forbidden_sum_caller() -> int:
    return sum(5, 10)

# Only allowed_sum_caller is allowed to use sum.
@deprecated(
    allowed_deprecations=[allowed_sum_caller],
    message="sum is no longer supported."
)
def sum(a: int, b: int) -> int:
    return a + b

assert allowed_sum_caller() == 15

# Now, try with the function that's not whitelisted
try:
    forbidden_sum_caller()
except DeprecationIntroduced as exc:
    print("Caught deprecated exception!")
#> Caught deprecated exception!

Behaviour

py_deprcate will raise an exception by default if a deprecated method or function is called where it's not supposed to be called. This can be overridden by specifying a behavior. Continuing from the example above:

from py_deprecate import deprecated, Disabled

@deprecated(
    allowed_deprecations=[allowed_sum_caller],
    message="sum is no longer supported.",
    behavior=Disabled
)
def sum(a: int, b: int) -> int:
    return a + b

assert forbidden_sum_caller() == 15

Here we've disabled all the side effects by using Disabled behavior. By default three behaviors are present: Disabled, RaiseExeption, and Log. You can also write your own behaviors like this:

from typing import Callable
from py_deprecate.behaviors.base import BaseBehavior

class CustomBehavior(BaseBehavior):
    def execute(self, message: str, func: Callable, *args, **kwargs):
        print("Custom behavior that will print this and raise Exception.")
        raise Exception

@deprecated(
    allowed_deprecations=[allowed_sum_caller],
    message="sum is no longer supported.",
    behavior=CustomBehavior
)
def sum(a: int, b: int) -> int:
    return a + b

forbidden_sum_caller()

#> Custom behavior that will print this and raise Exception.
#> Traceback (most recent call last):
#>  ...
#>  File "<stdin>", line 4, in execute
#> Exception

Tests

Run the tests using python -m unittest.

Resources

Shitlist Driven Development
This awesome Ruby gem that inspired me to create py_deprecate!

License

Python Deprecation Toolkit is licensed under the MIT License.

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

py_deprecate-0.0.4.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_deprecate-0.0.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file py_deprecate-0.0.4.tar.gz.

File metadata

  • Download URL: py_deprecate-0.0.4.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for py_deprecate-0.0.4.tar.gz
Algorithm Hash digest
SHA256 184e3db57559ed48cc79e651ce0218e36b68eef697d219a9341359208f9539fe
MD5 e7486ebc00cb72f577fbde60d6fa28b3
BLAKE2b-256 7dc94c01a6b184d935421ddd20d6e88b440d0b1241a605c3686d8d7c89b31361

See more details on using hashes here.

File details

Details for the file py_deprecate-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: py_deprecate-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.5

File hashes

Hashes for py_deprecate-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b2506aa889308eed5ac62dc35034aa0d09a50ef7be4cff0d1a0f6accade27644
MD5 34ac764fe9a6f33a1de712142fce162b
BLAKE2b-256 4709292abd8790138d8e164d57befb0f33bc68b6a46ce3daa0b8079fad466260

See more details on using hashes here.

Supported by

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