Skip to main content

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

Release files for philiprehberger-deprecate 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-deprecate 0.3.0
File Size Uploaded
philiprehberger_deprecate-0.3.0.tar.gz 193.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-deprecate 0.3.0
File Interpreter ABI Platform
philiprehberger_deprecate-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 199.0 kB

Release files / philiprehberger_deprecate-0.3.0.tar.gz

Download URL philiprehberger_deprecate-0.3.0.tar.gz
Size 193.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d4b3306c2b66ab3e26e5e3fe83b628d49e76eb3160e2ce38cddf72867419a4d0
BLAKE2b-256 checksum
How to use checksums
280e7b4876c6ec4ed4b14f5b45556d3e7ac77ae17a06345d47b01162e92eefa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_deprecate-0.3.0-py3-none-any.whl

Download URL philiprehberger_deprecate-0.3.0-py3-none-any.whl
Size 6.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e3889432b53a418cf43b46317a33a574721d6831517feb44de68fb00084c65b
BLAKE2b-256 checksum
How to use checksums
b6162abb80948596090da42eaad6efe9227d4c35ac7c335a6cea713310087263
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page