codekingpro/portable-devtools
114k
1# Copyright 2023-present, the HuggingFace Inc. team.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""Contains utilities to flag a feature as "experimental" in Huggingface Hub."""15 16import warnings17from collections.abc import Callable18from functools import wraps19 20from .. import constants21 22 23def experimental(fn: Callable) -> Callable:24 """Decorator to flag a feature as experimental.25 26 An experimental feature triggers a warning when used as it might be subject to breaking changes without prior notice27 in the future.28 29 Warnings can be disabled by setting `HF_HUB_DISABLE_EXPERIMENTAL_WARNING=1` as environment variable.30 31 Args:32 fn (`Callable`):33 The function to flag as experimental.34 35 Returns:36 `Callable`: The decorated function.37 38 Example:39 40 ```python41 >>> from huggingface_hub.utils import experimental42 43 >>> @experimental44 ... def my_function():45 ... print("Hello world!")46 47 >>> my_function()48 UserWarning: 'my_function' is experimental and might be subject to breaking changes in the future without prior49 notice. You can disable this warning by setting `HF_HUB_DISABLE_EXPERIMENTAL_WARNING=1` as environment variable.50 Hello world!51 ```52 """53 # For classes, put the "experimental" around the "__new__" method => __new__ will be removed in warning message54 name = fn.__qualname__[: -len(".__new__")] if fn.__qualname__.endswith(".__new__") else fn.__qualname__55 56 @wraps(fn)57 def _inner_fn(*args, **kwargs):58 if not constants.HF_HUB_DISABLE_EXPERIMENTAL_WARNING:59 warnings.warn(60 f"'{name}' is experimental and might be subject to breaking changes in the future without prior notice."61 " You can disable this warning by setting `HF_HUB_DISABLE_EXPERIMENTAL_WARNING=1` as environment"62 " variable.",63 UserWarning,64 )65 return fn(*args, **kwargs)66 67 return _inner_fn68 