|
| 1 | +""" |
| 2 | +Deprecation utilities for Marvin. |
| 3 | +
|
| 4 | +Based on Prefect's deprecation patterns but simplified for Marvin's needs. |
| 5 | +""" |
| 6 | + |
| 7 | +import functools |
| 8 | +import warnings |
| 9 | +from datetime import datetime, timedelta |
| 10 | +from typing import Any, Callable, Optional, TypeVar |
| 11 | + |
| 12 | +from typing_extensions import ParamSpec |
| 13 | + |
| 14 | +P = ParamSpec("P") |
| 15 | +R = TypeVar("R") |
| 16 | +T = TypeVar("T") |
| 17 | + |
| 18 | + |
| 19 | +class MarvinDeprecationWarning(DeprecationWarning): |
| 20 | + """A Marvin-specific deprecation warning.""" |
| 21 | + |
| 22 | + |
| 23 | +def generate_deprecation_message( |
| 24 | + name: str, |
| 25 | + start_date: Optional[datetime] = None, |
| 26 | + end_date: Optional[datetime] = None, |
| 27 | + help: str = "", |
| 28 | +) -> str: |
| 29 | + """Generate a deprecation warning message.""" |
| 30 | + if start_date is None and end_date is None: |
| 31 | + # Default to 1 month from now |
| 32 | + end_date = datetime.now() + timedelta(days=30) |
| 33 | + elif start_date and not end_date: |
| 34 | + # Calculate end date as 1 month after start |
| 35 | + end_date = start_date + timedelta(days=30) |
| 36 | + |
| 37 | + end_date_str = end_date.strftime("%b %Y") if end_date else "unknown" |
| 38 | + |
| 39 | + message = ( |
| 40 | + f"{name} is deprecated and will be removed after {end_date_str}. {help}" |
| 41 | + ).strip() |
| 42 | + |
| 43 | + return message |
| 44 | + |
| 45 | + |
| 46 | +def deprecated_class( |
| 47 | + *, |
| 48 | + start_date: Optional[datetime] = None, |
| 49 | + end_date: Optional[datetime] = None, |
| 50 | + stacklevel: int = 2, |
| 51 | + help: str = "", |
| 52 | +) -> Callable[[type[T]], type[T]]: |
| 53 | + """ |
| 54 | + Decorator to mark a class as deprecated. |
| 55 | +
|
| 56 | + Example: |
| 57 | + @deprecated_class( |
| 58 | + start_date=datetime(2025, 1, 1), |
| 59 | + help="Use Agent directly instead." |
| 60 | + ) |
| 61 | + class OldClass: |
| 62 | + pass |
| 63 | + """ |
| 64 | + |
| 65 | + def decorator(cls: type[T]) -> type[T]: |
| 66 | + message = generate_deprecation_message( |
| 67 | + name=f"{cls.__module__}.{cls.__name__}", |
| 68 | + start_date=start_date, |
| 69 | + end_date=end_date, |
| 70 | + help=help, |
| 71 | + ) |
| 72 | + |
| 73 | + original_init = cls.__init__ |
| 74 | + |
| 75 | + @functools.wraps(original_init) |
| 76 | + def new_init(self: T, *args: Any, **kwargs: Any) -> None: |
| 77 | + warnings.warn(message, MarvinDeprecationWarning, stacklevel=stacklevel) |
| 78 | + original_init(self, *args, **kwargs) |
| 79 | + |
| 80 | + cls.__init__ = new_init |
| 81 | + return cls |
| 82 | + |
| 83 | + return decorator |
| 84 | + |
| 85 | + |
| 86 | +def deprecated_callable( |
| 87 | + *, |
| 88 | + start_date: Optional[datetime] = None, |
| 89 | + end_date: Optional[datetime] = None, |
| 90 | + stacklevel: int = 2, |
| 91 | + help: str = "", |
| 92 | +) -> Callable[[Callable[P, R]], Callable[P, R]]: |
| 93 | + """ |
| 94 | + Decorator to mark a function as deprecated. |
| 95 | +
|
| 96 | + Example: |
| 97 | + @deprecated_callable( |
| 98 | + start_date=datetime(2025, 1, 1), |
| 99 | + help="Use new_function() instead." |
| 100 | + ) |
| 101 | + def old_function(): |
| 102 | + pass |
| 103 | + """ |
| 104 | + |
| 105 | + def decorator(fn: Callable[P, R]) -> Callable[P, R]: |
| 106 | + message = generate_deprecation_message( |
| 107 | + name=f"{fn.__module__}.{fn.__name__}", |
| 108 | + start_date=start_date, |
| 109 | + end_date=end_date, |
| 110 | + help=help, |
| 111 | + ) |
| 112 | + |
| 113 | + @functools.wraps(fn) |
| 114 | + def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: |
| 115 | + warnings.warn(message, MarvinDeprecationWarning, stacklevel=stacklevel) |
| 116 | + return fn(*args, **kwargs) |
| 117 | + |
| 118 | + return wrapper |
| 119 | + |
| 120 | + return decorator |
0 commit comments