Skip to main content

Deprecator provides a decorator to easily deprecate old code.

Project description

Deprecator

A framework for managing deprecation warnings with version-aware categorization.

Overview

The deprecator package provides a structured approach to handling deprecation warnings in Python packages, automatically categorizing warnings based on version comparisons and providing flexible management of deprecation policies across different packages and frameworks.

Requirements

  • Python 3.10 or higher

Quick Start

  1. Install deprecator with CLI support:

    pip install 'deprecator[cli]'
    
  2. Initialize deprecator in your project:

    deprecator init
    

    This creates a _deprecations.py module and configures your pyproject.toml.

  3. Define your deprecations:

    # In _deprecations.py
    from deprecator import for_package
    
    deprecator = for_package(__package__)
    
    OLD_API_DEPRECATION = deprecator.define(
        "old_api is deprecated, use new_api instead",
        warn_in="2.0.0",
        gone_in="3.0.0"
    )
    
  4. Use deprecations in your code:

    from ._deprecations import OLD_API_DEPRECATION
    
    @OLD_API_DEPRECATION.apply
    def old_api():
        pass
    

Architecture

Core Components

  • Deprecator (deprecator/_deprecator.py): The main deprecation management class that:

    • Tracks package version and creates version-specific warning categories
    • Provides define() method to create deprecation warnings based on gone_in/warn_in versions
    • Automatically categorizes warnings as pending, active, or expired based on current version
    • Tracks all defined deprecations internally
  • DeprecatorRegistry (deprecator/_registry.py): Manages multiple deprecator instances:

    • Caches deprecators by package name to avoid duplication
    • Provides for_package() method to get or create deprecators
    • Framework-specific registries allow different libraries to manage their own deprecation scopes
    • Each registry has a framework attribute identifying the framework/library it serves
  • Warning Categories (deprecator/_deprecator.py): Version-aware warning types:

    • PerPackagePendingDeprecationWarning: For future deprecations (current_version < warn_in)
    • PerPackageDeprecationWarning: For active deprecations (warn_in <= current_version < gone_in)
    • PerPackageExpiredDeprecationWarning: For expired deprecations (gone_in <= current_version)

Public API

  • deprecate(): Legacy decorator function for simple deprecation warnings
  • for_package(package_name: PackageName | str): Get a deprecator bound to a specific package
  • registry_for_package(package_name: PackageName | str): Get a registry bound to a specific framework

Version Logic

The deprecator automatically determines warning categories based on version comparison:

  • If current version >= gone_in version → ExpiredDeprecationWarning (should cause errors)
  • If current version >= warn_in version → DeprecationWarning (active warnings)
  • Otherwise → PendingDeprecationWarning (future warnings, typically for tests)

Usage

from deprecator import for_package

# Get a deprecator for your package (accepts str or PackageName)
deprecator = for_package("mypackage")

# Define a deprecation (using UPPER_CASE for constants)
OLD_FUNCTION_DEPRECATION = deprecator.define(
    "old_function is deprecated, use new_function instead",
    warn_in="2.0.0",
    gone_in="3.0.0"
)

# Method 1: Use as a decorator
@OLD_FUNCTION_DEPRECATION.apply
def old_function():
    # The decorator automatically emits the warning
    # ... existing functionality
    pass

# Method 2: Emit warning manually
def another_old_function():
    # Standard warning (uses stacklevel=2 by default)
    OLD_FUNCTION_DEPRECATION.warn()
    # ... existing functionality

# Method 3: Custom stacklevel for wrapper functions
def wrapper_function():
    # Custom stacklevel for wrapper functions
    OLD_FUNCTION_DEPRECATION.warn(stacklevel=3)

# Method 4: Explicit file/line for tools and linters
def linter_integration():
    # Explicit file/line for tools and linters
    OLD_FUNCTION_DEPRECATION.warn_explicit("myfile.py", 42)

Warning Methods

Each deprecation warning instance provides three methods for using deprecations:

  • apply: Use as a decorator to automatically emit warnings when the decorated function/class is used
  • warn(stacklevel=2): Emit warning using the standard warnings system
  • warn_explicit(filename, lineno, module=None): Emit warning with explicit location

The apply decorator uses typing_extensions.deprecated under the hood, while warn() and warn_explicit() mirror the stdlib warnings.warn() and warnings.warn_explicit() functions.

Cookbook

For practical examples and common patterns, see the Cookbook.

Testing Structure

Tests are organized by functionality:

  • test_deprecator.py: Core deprecator functionality
  • test_registry.py: Registry management and caching
  • test_exposed_api.py: Public API surface
  • test_legacy_deprecator.py: Legacy decorator compatibility

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

deprecator-26.1.12.0.tar.gz (114.1 kB view details)

Uploaded Source

Built Distribution

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

deprecator-26.1.12.0-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file deprecator-26.1.12.0.tar.gz.

File metadata

  • Download URL: deprecator-26.1.12.0.tar.gz
  • Upload date:
  • Size: 114.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for deprecator-26.1.12.0.tar.gz
Algorithm Hash digest
SHA256 95fd8df5605af9ea746a9985ebfcd16b794e09ed79ab703d1a298964998044b2
MD5 bf135823f38c0635723e98d247660500
BLAKE2b-256 621609c20f0fd0bad01adc7fae31149a4cfe29cfcdee1f29efea6924577137ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for deprecator-26.1.12.0.tar.gz:

Publisher: ci.yml on RonnyPfannschmidt/deprecator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file deprecator-26.1.12.0-py3-none-any.whl.

File metadata

  • Download URL: deprecator-26.1.12.0-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for deprecator-26.1.12.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd73d36e69919e2416fce4af9b8561c2e8c4eaeb449e69623aa8d4840b3a108a
MD5 06c5bf9c3ef6f6fe0936305a17f55f04
BLAKE2b-256 52c1db499f5fb1cac4dab18fbcc2b2f8376c5c17f81089a40fc7a4275bdf35e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for deprecator-26.1.12.0-py3-none-any.whl:

Publisher: ci.yml on RonnyPfannschmidt/deprecator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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