o
    i5                     @  s  U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZ d dlmZ erKd d	lmZ d d
lmZ ejdkrWd dlmZ nzd dlmZ W n eym   dFddZY nw d dlmZ erd dlmZ d dlmZ d dlmZ edZ edZ!dddZ"de#d< dddGd"d#Z$dFd$d%Z%dHd&d'Z&dId*d+Z'dJd/d0Z(	dKdLd4d5Z)dMd7d8Z*dNd9d:Z+dOd>d?Z,dPdCdDZ-g dEZ.dS )Q    )annotationsN)defaultdict)Sequence)wraps)Path)TYPE_CHECKINGAnyTypeVarget_args)DeprecationType)Callable)IdentityFunction)      
deprecatedmessagestrreturnr   c                 C  s   t | S N)_deprecate_functionr    r   P/home/app/Keep/.python/lib/python3.10/site-packages/polars/_utils/deprecation.pyr      s   r   )issue_warning)Mapping)	ParamSpec)	AmbiguousPTZearliestZlatest)TFzMapping[bool, Ambiguous]USE_EARLIEST_TO_AMBIGUOUS versionr#   Nonec                C  s&   |r|    d| d} t| t dS )a  
    Issue a deprecation warning.

    Parameters
    ----------
    message
        The message associated with the warning.
    version
        The version in which deprecation occurred
        (if the version number was not already included in `message`).
    z
(Deprecated in version )N)stripr   DeprecationWarning)r   r#   r   r   r   issue_deprecation_warning/   s   r(   c                      d fdd}|S )z+Decorator to mark a function as deprecated.functionCallable[P, T]r   c                   s.   t  d	 fdd}t |_|_|S )
NargsP.argskwargsP.kwargsr   r   c                    s   t   | i |S r   r(   r,   r.   )r*   r   r   r   wrapperD   s   z6_deprecate_function.<locals>.decorate.<locals>.wrapperr,   r-   r.   r/   r   r   )r   inspect	signature__signature__Z__deprecated__r*   r2   r   r*   r   decorateC   s
   z%_deprecate_function.<locals>.decorateNr*   r+   r   r+   r   )r   r9   r   r   r   r   @   s   
r   c                  C  s   ddd} | S )zJDecorator to mark `streaming` argument as deprecated due to being renamed.r*   r+   r   c                   s&   t  d	 fdd}t |_|S )
Nr,   r-   r.   r/   r   r   c                    sF   d|v rt d |d rd|d< nd|vrd|d< |d=  | i |S )NZ	streamingzIthe `streaming` parameter was deprecated in 1.25.0; use `engine` instead.Zenginez	in-memoryr0   r1   r8   r   r   r2   T   s   
z@deprecate_streaming_parameter.<locals>.decorate.<locals>.wrapperr3   r   r4   r5   r6   r7   r   r8   r   r9   S   s   z/deprecate_streaming_parameter.<locals>.decorateNr:   r   )r9   r   r   r   deprecate_streaming_parameterP   s   
r<   old_namenew_namec                     d fdd}|S )a  
    Decorator to mark a function parameter as deprecated due to being renamed.

    Use as follows:

        @deprecate_renamed_parameter("old_name", new_name="new_name")
        def myfunc(new_name): ...

    Ensure that you also update the function docstring with a note about the
    deprecation, specifically adding a `.. versionchanged:: 0.0.0` directive
    that states which parameter was renamed to which new name and in which
    version the rename happened.
    r*   r+   r   c                   s,   t  d	 fdd}t |_|S )
Nr,   r-   r.   r/   r   r   c                    s    t | j  | i |S r   )_rename_keyword_argument__qualname__r1   )r*   r>   r=   r#   r   r   r2   {   s   z>deprecate_renamed_parameter.<locals>.decorate.<locals>.wrapperr3   r;   r7   r>   r=   r#   r8   r   r9   z   s   z-deprecate_renamed_parameter.<locals>.decorateNr:   r   )r=   r>   r#   r9   r   rB   r   deprecate_renamed_parameteri   s   rC   r.   dict[str, object]	func_namec                 C  s   | |v rM||v r*|rd| nd}d|d| d|d| d| d|d	}t ||r1d
| nd}td|  d| d| d| d	 || ||< dS dS )z(Rename a keyword argument of a function.zwas deprecated in version zis deprecated`z` received both `z` and `z` as arguments; `z` z, use `z	` insteadz in version r!   zthe argument `z` for `z$` is deprecated. It was renamed to `.N)	TypeErrorr(   pop)r=   r>   r.   rE   r#   Zis_deprecatedmsgZ
in_versionr   r   r   r@      s.   r@   allowed_argslist[str] | None
str | Nonec                  r?   )a  
    Decorator for deprecating the use of non-keyword arguments in a function.

    Use as follows:

        @deprecate_nonkeyword_arguments(allowed_args=["self", "val"], version="1.0.0")
        def myfunc(self, val: int = 0, other: int: = 0): ...

    Ensure that you also update the function docstring with a note about the
    deprecation, specifically adding a `.. versionchanged:: 0.0.0` directive
    that states that we now expect keyword args and in which version this
    update happened.

    Parameters
    ----------
    allowed_args
        The names of some first arguments of the decorated function that are allowed to
        be given as positional arguments. Should include "self" when decorating class
        methods. If set to None (default), equal to all arguments that do not have a
        default value.
    message
        Optionally overwrite the default warning message.
    version
        The Polars version number in which the warning is first issued.
    r*   r+   r   c                   s   t }d ur n
dd |j D   fdd|j D }|jdd d |j|d}t d u rHdj d	}|jt	 d
nt
dfdd}||_|S )Nc                 S  s0   g | ]}|j |j|jfv r|j|ju r|jqS r   )kindPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORDdefaultemptyname.0pr   r   r   
<listcomp>   s    zDdeprecate_nonkeyword_arguments.<locals>.decorate.<locals>.<listcomp>c                   s:   g | ]}|j |j|jfv r|j vr|j|jd n|qS )rN   )rN   rO   rP   rS   replaceKEYWORD_ONLYrT   )
allow_argsr   r   rW      s    
c                 S  s   | j S r   rX   )rV   r   r   r   <lambda>   s    zBdeprecate_nonkeyword_arguments.<locals>.decorate.<locals>.<lambda>)key)
parameterszall arguments of zo{except_args} will be keyword-only in the next breaking release. Use keyword arguments to silence this warning.)Zexcept_argsr,   r-   r.   r/   r   r   c                    s&   t | krtd  | i |S )Nr"   )lenr(   r1   )r*   rJ   num_allowed_argsr#   r   r   r2      s   zAdeprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapperr3   )r4   r5   r^   valuessortrY   r_   rA   format_format_argument_listr   r6   )r*   Zold_sig
new_paramsZnew_sigZ
msg_formatr2   rK   r   r#   )r[   r*   rJ   r`   r   r9      s*   

	z0deprecate_nonkeyword_arguments.<locals>.decorateNr:   r   )rK   r   r#   r9   r   rf   r   deprecate_nonkeyword_arguments   s   -rg   	list[str]c                 C  sh   d| v r	|  d | sdS t| dkrd| d S | d }ddd	 | d
d D }d| d|S )zaFormat allowed arguments list for use in the warning message of `deprecate_nonkeyword_arguments`.selfr!      z except for r   z, c                 S  s   g | ]}|qS r   r   )rU   xr   r   r   rW      s    z)_format_argument_list.<locals>.<listcomp>Nz and )remover_   join)rK   lastr,   r   r   r   rd      s   
rd   c                   r)   )a  
    Decorator to mark a function argument as deprecated due to being made multi-positional.

    Use as follows:

        @deprecate_parameter_as_multi_positional("columns")
        def myfunc(*columns): ...

    Ensure that you also update the function docstring with a note about the
    deprecation, specifically adding a `.. versionchanged:: 0.0.0` directive
    that states that we now expect positional args and in which version this
    update happened.
    r*   r+   r   c                   s(   t  d	 fdd}t |_|S )
Nr,   r-   r.   r/   r   r   c                    s   z| }W n ty    | i | Y S w td d t|tr*t|tr.|f}n	t|ts7t|}| | }  | i |S )Nz	passing `zP` as a keyword argument is deprecated. Pass it as a positional argument instead.)rI   KeyErrorr(   
isinstancer   r   tuple)r,   r.   	arg_value)r*   r=   r   r   r2     s   

zJdeprecate_parameter_as_multi_positional.<locals>.decorate.<locals>.wrapperr3   r;   r7   r=   r8   r   r9     s   z9deprecate_parameter_as_multi_positional.<locals>.decorateNr:   r   )r=   r9   r   rt   r   'deprecate_parameter_as_multi_positional   s   ru   sourcemodule_pathdefaultdict[str, list[str]]c                   s`   t | }g d fdd dfdd	ttG  fd
ddt j}| | S )N	decoratorr   r   r   c                   s:   t | tjrd| jv r| jS dS t | tjr | jS dS )NZ	deprecater!   )rq   astNameidCallfunc)ry   r   r   r   r   .  s
   
z._find_deprecated_functions.<locals>.deprecatedrE   c                   s   d  g| S )NrG   )rn   )rE   )rw   object_pathr   r   qualified_name5  s   z2_find_deprecated_functions.<locals>.qualified_namec                      s0   e Zd Zd
fddZd
 fddZeZd	S )z3_find_deprecated_functions.<locals>.FunctionVisitornoder   r   r$   c                   s"     |j | |    d S r   )appendrS   generic_visitrI   )ri   r   )r   r   r   visit_ClassDef;  s   
zB_find_deprecated_functions.<locals>.FunctionVisitor.visit_ClassDefc                   sN   t  fdd|jD r  ddd}| |j | | d S )Nc                 3  s    | ]	}|  V  qd S r   r   )rU   d)decorator_namer   r   r   	<genexpr>A  s    zX_find_deprecated_functions.<locals>.FunctionVisitor.visit_FunctionDef.<locals>.<genexpr>Z
deprecate_r   r*   )anydecorator_listremoveprefixrY   r   rS   r   )ri   r   r]   )r   r   results)r   r   visit_FunctionDef@  s   
zE_find_deprecated_functions.<locals>.FunctionVisitor.visit_FunctionDefN)r   r   r   r$   )__name__
__module__rA   r   r   visit_AsyncFunctionDefr   )r   r   r   r   r   r   FunctionVisitor:  s    r   )ry   r   r   r   )rE   r   r   r   )rz   parser   listNodeVisitorvisit)rv   rw   treer   r   )r   rw   r   r   r   r   _find_deprecated_functions(  s   
r   typesr   dict[str, list[str]]c               	     s  t tt}D ]}||vr"d|dtt|dd  }t|qttjd j	j
}tt |dD ]K}||}d|jd}|jd	d
d,}t| |d D ]\}	}
|	|vrid|d}t| |	 |
 qWW d   n1 s{w   Y  q5 fddt D S )a  
    Return a dict identifying functions/methods that are deprecated in some way.

    Parameters
    ----------
    *types
        The types of deprecations to identify.
        If empty, all types are returned; recognised values are:
            - "function"
            - "renamed_parameter"
            - "streaming_parameter"
            - "nonkeyword_arguments"
            - "parameter_as_multi_positional"

    Examples
    --------
    >>> from polars._utils.deprecation import identify_deprecations
    >>> identify_deprecations("streaming_parameter")  # doctest: +IGNORE_RESULT
    {'streaming_parameter': [
        'functions.lazy.collect_all',
        'functions.lazy.collect_all_async',
        'lazyframe.frame.LazyFrame.collect',
        'lazyframe.frame.LazyFrame.collect_async',
        'lazyframe.frame.LazyFrame.explain',
        'lazyframe.frame.LazyFrame.show_graph',
    ]}
    zunrecognised deprecation type z.
Expected one (or more) of rj   rk   Zpolarsz*.pyrG   z.pyrzutf-8)encoding)rv   rw   z.
Nc                   s&   i | ]}r
|v r|t  | qS r   )sorted)rU   depr   r   r   r   
<dictcomp>  s    z)identify_deprecations.<locals>.<dictcomp>)setr
   r   reprr   
ValueErrorr   sysmodules__file__parentr   r   rglobrelative_torn   partsremovesuffixopenr   readitemsextend)r   Zvalid_typestprJ   package_pathZpy_filerel_pathrw   srcZdeprecation_typeZ
func_namesr   r   r   identify_deprecationsN  s>   

r   )rg   ru   rC   r<   r   r   )r   r   r   r   )r   r   r#   r   r   r$   )r   r   )r=   r   r>   r   r#   r   r   r   )r=   r   r>   r   r.   rD   rE   r   r#   r   r   r$   )NN)rK   rL   r   rM   r#   r   r   r   )rK   rh   r   r   )r=   r   r   r   )rv   r   rw   r   r   rx   )r   r   r   r   )/
__future__r   rz   r4   r   collectionsr   collections.abcr   	functoolsr   pathlibr   typingr   r   r	   r
   Zpolars._typingr   r   Zpolars._utils.variousr   version_infowarningsr   Ztyping_extensionsImportErrorr   r   r   r   r   r   r    __annotations__r(   r   r<   rC   r@   rg   rd   ru   r   r   __all__r   r   r   r   <module>   sT    





M

*
&?