Skip to main content

Decorator and utilities for deprecating functions, parameters, and classes with zero boilerplate

Project description

philiprehberger-deprecate

Tests PyPI version Last updated

Decorator and utilities for deprecating functions, parameters, and classes with zero boilerplate.

Installation

pip install philiprehberger-deprecate

Usage

from philiprehberger_deprecate import (
    deprecated,
    deprecated_class,
    deprecated_module,
    deprecated_param,
)

Deprecate a function

@deprecated(remove_in="2.0.0", alternative="new_fetch")
def old_fetch(url: str) -> str:
    ...

Calling old_fetch() emits:

DeprecationWarning: Function 'old_fetch' is deprecated. Will be removed in 2.0.0. Use 'new_fetch' instead.

Deprecate a parameter

@deprecated_param("color", renamed_to="colour", remove_in="2.0.0")
def draw(shape: str, *, colour: str = "red") -> None:
    ...

draw(shape="circle", color="blue")  # warns and forwards color -> colour

Deprecate a class

@deprecated_class(remove_in="3.0.0", alternative="NewClient")
class OldClient:
    def __init__(self, host: str) -> None:
        self.host = host

Instantiating OldClient() emits a DeprecationWarning.

Deprecate a module

# in mypkg/legacy/__init__.py
from philiprehberger_deprecate import deprecated_module

deprecated_module(__name__, remove_in="2.0.0", alternative="mypkg.modern")

Importing mypkg.legacy emits:

DeprecationWarning: Module 'mypkg.legacy' is deprecated. Will be removed in 2.0.0. Use 'mypkg.modern' instead.

API

Function Description
deprecated(remove_in, *, alternative, message) Function decorator that emits DeprecationWarning on each call
deprecated_param(param, *, renamed_to, remove_in) Function decorator that warns when a deprecated parameter is passed and optionally maps it to its replacement
deprecated_class(remove_in, *, alternative, message) Class decorator that emits DeprecationWarning on instantiation
deprecated_module(name, *, remove_in, alternative, message) Emits DeprecationWarning once when a deprecated module is imported

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_deprecate-0.2.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_deprecate-0.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_deprecate-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_deprecate-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2d24257043f9f9750dab571fd9e2895bd9fe7922a2e79f41397051a4c1529f4b
MD5 fa9865b0cb2e54a1a588cba9946660ee
BLAKE2b-256 f4d355ec11317e0dbc527c3fc52d155b72e419afa37f6e783b3a29356f28d613

See more details on using hashes here.

File details

Details for the file philiprehberger_deprecate-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_deprecate-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c80f92ed2deca664f8f8e4847c5d717b8056790cedd52cd6ade285a79ea64b24
MD5 bb82e04d571b68f540f419a14cc6b2a7
BLAKE2b-256 eb84ee17efb8681cadc00aa888fc18be7b08a5483c81748476f2bc2bb069f264

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