@@ -2152,3 +2152,125 @@ async def wrapper(*args, **kwargs):
21522152 return wrapper
21532153
21542154 return _decorate if func is None else _decorate (func )
2155+
2156+
2157+ # ============================================================================
2158+ # Backward Compatibility Decorators
2159+ # ============================================================================
2160+
2161+
2162+ def exempt_from_compat_check (func : Callable ) -> Callable :
2163+ """
2164+ Mark a function as exempt from backward compatibility checks.
2165+
2166+ Use this decorator for:
2167+ - Internal APIs not intended for external use
2168+ - Experimental features that may change
2169+ - Functions explicitly documented as unstable
2170+
2171+ Args:
2172+ func: The function to mark as exempt
2173+
2174+ Returns:
2175+ The original function with an exemption marker
2176+
2177+ Example:
2178+ @exempt_from_compat_check
2179+ def experimental_api():
2180+ '''This API may change without notice'''
2181+ pass
2182+ """
2183+ func ._exempt_from_compat_check = True
2184+ return func
2185+
2186+
2187+ def deprecated (
2188+ version : str ,
2189+ removal_version : Optional [str ] = None ,
2190+ alternative : Optional [str ] = None ,
2191+ reason : Optional [str ] = None
2192+ ) -> Callable :
2193+ """
2194+ Mark a function as deprecated.
2195+
2196+ This decorator:
2197+ 1. Adds deprecation metadata to the function
2198+ 2. Issues a DeprecationWarning when the function is called
2199+ 3. Allows the compatibility checker to track deprecation lifecycle
2200+
2201+ Args:
2202+ version: Version where deprecation starts (e.g., "1.0.0")
2203+ removal_version: Version where function will be removed (e.g., "2.0.0")
2204+ alternative: Name of the recommended replacement function
2205+ reason: Optional explanation for the deprecation
2206+
2207+ Returns:
2208+ Decorator function
2209+
2210+ Example:
2211+ @deprecated(
2212+ version="1.0.0",
2213+ removal_version="2.0.0",
2214+ alternative="new_train_model",
2215+ reason="Improved performance and cleaner API"
2216+ )
2217+ def old_train_model(config):
2218+ pass
2219+ """
2220+ def decorator (func : Callable ) -> Callable :
2221+ # Add metadata
2222+ func ._deprecated = True
2223+ func ._deprecated_version = version
2224+ func ._removal_version = removal_version
2225+ func ._alternative = alternative
2226+ func ._deprecation_reason = reason
2227+
2228+ @functools .wraps (func )
2229+ def wrapper (* args , ** kwargs ):
2230+ # Build warning message
2231+ msg_parts = [f"{ func .__name__ } is deprecated since version { version } ." ]
2232+
2233+ if alternative :
2234+ msg_parts .append (f"Use { alternative } instead." )
2235+
2236+ if removal_version :
2237+ msg_parts .append (f"Will be removed in version { removal_version } ." )
2238+
2239+ if reason :
2240+ msg_parts .append (f"Reason: { reason } " )
2241+
2242+ warnings .warn (
2243+ " " .join (msg_parts ),
2244+ DeprecationWarning ,
2245+ stacklevel = 2
2246+ )
2247+
2248+ return func (* args , ** kwargs )
2249+
2250+ return wrapper
2251+
2252+ return decorator
2253+
2254+
2255+ def internal_api (func : Callable ) -> Callable :
2256+ """
2257+ Mark a function as internal API (not for external use).
2258+
2259+ This is semantically similar to exempt_from_compat_check but
2260+ more explicitly communicates that the function is internal.
2261+
2262+ Args:
2263+ func: The function to mark as internal
2264+
2265+ Returns:
2266+ The original function with an internal API marker
2267+
2268+ Example:
2269+ @internal_api
2270+ def _internal_helper():
2271+ '''For internal use only'''
2272+ pass
2273+ """
2274+ func ._internal_api = True
2275+ func ._exempt_from_compat_check = True # Also exempt from checks
2276+ return func
0 commit comments