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_attribute,
    deprecated_class,
    deprecated_module,
    deprecated_param,
    silenced,
)

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.

Deprecating attributes

class Config:
    old_value = deprecated_attribute(
        "old_value",
        since="1.0",
        removed_in="2.0",
        replacement="new_value",
    )

    def __init__(self) -> None:
        self.new_value = 42

cfg = Config()
cfg.old_value  # emits DeprecationWarning and returns 42 (from new_value)

Silencing during tests

from philiprehberger_deprecate import silenced

with silenced():
    old_function()  # no DeprecationWarning emitted inside the block

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
deprecated_attribute(name, since=None, removed_in=None, replacement=None) Descriptor that emits DeprecationWarning on attribute access; transparently returns the replacement attribute's value when set
silenced() Context manager that suppresses DeprecationWarning from this package (useful in tests)

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.3.0.tar.gz (193.0 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.3.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_deprecate-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d4b3306c2b66ab3e26e5e3fe83b628d49e76eb3160e2ce38cddf72867419a4d0
MD5 34e13a372e091248db98bd4cded85637
BLAKE2b-256 280e7b4876c6ec4ed4b14f5b45556d3e7ac77ae17a06345d47b01162e92eefa8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_deprecate-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e3889432b53a418cf43b46317a33a574721d6831517feb44de68fb00084c65b
MD5 0f7f80047dbdb1a302163f0773bf5e6c
BLAKE2b-256 b6162abb80948596090da42eaad6efe9227d4c35ac7c335a6cea713310087263

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