Legacy API Wrapper

1.5 · active · verified Sat Apr 11

legacy-api-wrap (version 1.5) provides a decorator for Python functions to seamlessly bridge old and new API interfaces. It allows developers to deprecate old argument names while still supporting them, typically by emitting warnings when old names are used. The library is actively maintained with feature-driven releases, ensuring compatibility and developer-friendly transitions.

Warnings

Install

Imports

Quickstart

This example demonstrates defining a function with a new API signature and wrapping it with `legacy_api_wrap` to allow calls using old argument names. It also shows how the decorator emits `FutureWarning`s when old arguments are used.

from legacy_api_wrap import legacy_api_wrap
import warnings

# Define the new function with updated argument names
@legacy_api_wrap(old_arg_name='new_arg_name', another_old='yet_another')
def my_new_function(new_arg_name: str, yet_another: int = 10):
    """This function uses the new API signature."""
    print(f"New function called! new_arg_name='{new_arg_name}', yet_another={yet_another}")

print("Calling with old API arguments (expecting a FutureWarning):")
with warnings.catch_warnings(record=True) as w:
    warnings.simplefilter("always") # Ensure warnings are always captured
    my_new_function(old_arg_name="Hello Legacy", another_old=5)
    assert len(w) > 0
    assert issubclass(w[-1].category, FutureWarning)
    print(f"Warning caught: {w[-1].message}")

print("\nCalling with new API arguments:")
my_new_function(new_arg_name="Hello Modern", yet_another=20)

view raw JSON →