pyDeprecate: Deprecation Tooling

0.7.0 · active · verified Sun Apr 12

pyDeprecate is a lightweight Python library (version 0.7.0) for managing function and class deprecations with zero dependencies. It provides automatic call forwarding to replacement functions, argument mapping between old and new APIs, and configurable warning controls to prevent log spam. It is actively maintained and designed to help library maintainers evolve APIs while maintaining backward compatibility.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `@deprecated` decorator with and without a `target` function. When a `target` is provided, calls are forwarded to the new function, and the `void` helper silences IDE warnings. When `target=None`, the deprecated function's own body is executed after a warning is issued. Warnings are initially suppressed for clean output but re-enabled to show the deprecation messages.

import warnings
from deprecate import deprecated, void

# Suppress DeprecationWarning for cleaner output in example, will re-enable later
warnings.simplefilter('ignore', DeprecationWarning)

# NEW/FUTURE API — renamed to be more explicit about what it computes
def compute_sum(a: int = 0, b: int = 3) -> int:
    """Computes the sum of two numbers."""
    return a + b

# OLD API — 'addition' was the original name before the rename
@deprecated(target=compute_sum, deprecated_in="1.0", remove_in="2.0", template_mgs="{name} is deprecated, use {target_path} instead.")
def addition(a: int, b: int = 5) -> int:
    """Adds two numbers (deprecated)."""
    # The body of 'addition' is never executed because 'target' is specified.
    # The 'void' helper is used here to silence IDE warnings about 'a' and 'b' being unused.
    return void(a, b)

# Deprecating a function with no direct target (its own body runs)
@deprecated(target=None, deprecated_in="0.5", remove_in="0.8", template_mgs="Method {name} is deprecated and will be removed.")
def old_method(value: str) -> str:
    """An old method that will be removed."""
    return f"Processing: {value}"

# Example usage:
print("--- Using deprecated function with target ---")
print(f"Result of addition(1, 2): {addition(1, 2)}")
print(f"Result of addition(10): {addition(10)}")

print("\n--- Using deprecated function with no target ---")
print(f"Result of old_method('test'): {old_method('test')}")

# To demonstrate the warning, re-enable DeprecationWarnings
print("\n--- Enabling warnings to show deprecation output ---")
warnings.simplefilter('default', DeprecationWarning)
print(f"Result of addition(3, 4): {addition(3, 4)}")
print(f"Result of old_method('another test'): {old_method('another test')}")

view raw JSON →