Typing stubs for the decorator library

5.2.0.20260408 · active · verified Sat Apr 11

types-decorator provides high-quality static type annotations (stubs) for the popular `decorator` Python library. It enables type checkers like MyPy and Pyright to analyze code using `decorator` for type correctness, enhancing maintainability and catching potential errors early. Maintained by the `typeshed` project, these stubs are automatically released to PyPI regularly, often daily, to reflect updates in the upstream library. The current version is 5.2.0.20260408.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `decorator` library (for which `types-decorator` provides stubs) to create a custom decorator that warns if a function takes too long to execute. It showcases both a simple decorator application and a decorator factory with arguments, while automatically preserving the decorated function's signature and metadata.

import time
import logging
from decorator import decorator

logging.basicConfig(level=logging.INFO)

@decorator
def warn_slow(func, timelimit=60, *args, **kw):
    """A decorator factory that logs if a function call exceeds a timelimit."""
    t0 = time.time()
    result = func(*args, **kw)
    dt = time.time() - t0
    if dt > timelimit:
        logging.warning('%s took %.2f seconds (exceeded %d s)', func.__name__, dt, timelimit)
    else:
        logging.info('%s took %.2f seconds', func.__name__, dt)
    return result

@warn_slow
def process_data(duration: float):
    """Simulates a data processing operation."""
    time.sleep(duration)
    return f"Processed data in {duration} seconds"

@warn_slow(timelimit=1) # Override default timelimit
def analyze_report(duration: float):
    """Simulates a report analysis with a custom timelimit."""
    time.sleep(duration)
    return f"Analyzed report in {duration} seconds (custom timelimit)"

# Example usage:
print(process_data(0.5))
print(process_data(2.0))
print(analyze_report(0.8))
print(analyze_report(1.5))

view raw JSON →