U
    5Af                  	   @   s   d dl Z d dlZd dlmZ d dlmZ d dlZddlm	Z	 ddlm
Z
 G dd	 d	e
Zdeeee eeeee dddZdS )    N)wraps)Optional   )__version__   )ExplicitEnumc                   @   s   e Zd ZdZdZdZdZdS )ActionnonenotifyZnotify_alwaysraiseN)__name__
__module____qualname__NONENOTIFYNOTIFY_ALWAYSRAISE r   r   B/tmp/pip-unpacked-wheel-zw5xktn0/transformers/utils/deprecation.pyr      s   r   F)old_nameversionnew_name warn_if_greater_or_equal_version!raise_if_greater_or_equal_versionraise_if_both_namesadditional_messagec           
         sX   t j|}t jt}||kr0d| n
d|  fdd}	|	S )a
  
    Function or method decorator to notify users about deprecated keyword arguments, replacing them with a new name if specified.

    This decorator allows you to:
    - Notify users when a keyword argument is deprecated.
    - Automatically replace deprecated keyword arguments with new ones.
    - Raise an error if deprecated arguments are used, depending on the specified conditions.

    By default, the decorator notifies the user about the deprecated argument while the `transformers.__version__` < specified `version`
    in the decorator. To keep notifications with any version `warn_if_greater_or_equal_version=True` can be set.

    Parameters:
        old_name (`str`):
            Name of the deprecated keyword argument.
        version (`str`):
            The version in which the keyword argument was (or will be) deprecated.
        new_name (`Optional[str]`, *optional*):
            The new name for the deprecated keyword argument. If specified, the deprecated keyword argument will be replaced with this new name.
        warn_if_greater_or_equal_version (`bool`, *optional*, defaults to `False`):
            Whether to show warning if current `transformers` version is greater or equal to the deprecated version.
        raise_if_greater_or_equal_version (`bool`, *optional*, defaults to `False`):
            Whether to raise `ValueError` if current `transformers` version is greater or equal to the deprecated version.
        raise_if_both_names (`bool`, *optional*, defaults to `False`):
            Whether to raise `ValueError` if both deprecated and new keyword arguments are set.
        additional_message (`Optional[str]`, *optional*):
            An additional message to append to the default deprecation message.

    Raises:
        ValueError:
            If raise_if_greater_or_equal_version is True and the current version is greater than or equal to the deprecated version, or if raise_if_both_names is True and both old and new keyword arguments are provided.

    Returns:
        Callable:
            A wrapped function that handles the deprecated keyword arguments according to the specified parameters.

    Example usage with renaming argument:

        ```python
        @deprecate_kwarg("reduce_labels", new_name="do_reduce_labels", version="6.0.0")
        def my_function(do_reduce_labels):
            print(do_reduce_labels)

        my_function(reduce_labels=True)  # Will show a deprecation warning and use do_reduce_labels=True
        ```

    Example usage without renaming argument:

        ```python
        @deprecate_kwarg("max_size", version="6.0.0")
        def my_function(max_size):
            print(max_size)

        my_function(max_size=1333)  # Will show a deprecation warning
        ```

    z"and removed starting from version zand will be removed in version c                    sT   t  }t|j }d|kd|kt  	
fdd}|S )Nselfclsc                     s  j }r"| d jj  d| }nr:| d j  d| }tj}d }|kr|krr^tjntj}d d d| d d|  d d|  d	}| nt|krd k	r|krtj}d
 d	 d| d d	}||< n(|krtj}d
 d	 d| d	}|d k	r> d k	r>| d  }rvr^|tjkr^tj}n
sv|tjkrvtj}|tjkrt|n"|tjtjfkrt	j
|tdd | |S )Nr   .zBoth `z` and `z` are set for `z
`. Using `=z` and ignoring deprecated `z`.`z` is deprecated z for `z`. Use `z
` instead. r   )
stacklevel)r   	__class__r   r   r   r   popr   
ValueErrorwarningswarnFutureWarning)argskwargs	func_nameZminimum_actionmessage)r   funcis_class_methodis_greater_or_equal_versionis_instance_methodr   r   r   r   version_messager   r   r   wrapped_funcr   s<    8

z6deprecate_kwarg.<locals>.wrapper.<locals>.wrapped_func)inspect	signatureset
parameterskeysr   )r-   sigZfunction_named_argsr2   r   r/   r   r   r   r   r1   r   )r-   r.   r0   r   wrapperk   s    
"4z deprecate_kwarg.<locals>.wrapper)	packagingr   parser   )
r   r   r   r   r   r   r   Zdeprecated_versioncurrent_versionr:   r   r9   r   deprecate_kwarg    s    B
>r>   )NFFFN)r3   r&   	functoolsr   typingr   Zpackaging.versionr;    r   r   r   strboolr>   r   r   r   r   <module>   s*   
     