o
    DDit                     @   s:  U d dl mZmZ d dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZ d dlZd dlmZmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z* zd d	l+m,Z, W n e-y   G d
d dZ,Y nw h dd e*D dd e*D Z.ee/ e0d< de1de/de2fddZ3deddfddZ4G dd de5Z6dede	eej7  fddZ8de1de2fddZ9de1de	ee1  fd d!Z:d"eeej7  deee1  fd#d$Z;d%eeej7  d&ed'ef d(e/d)e2ddf
d*d+Z<d%eeej7  d&ed'ef d(e/d,e=ddf
d-d.Z>d/eeeed'ef  f d(e/d,e=d0e2ddf
d1d2Z?de1deee
ej7 f fd3d4Z@d(e/d5eej7gdf ddfd6d7ZAde1deeeeej7gdf  f fd8d9ZBde1deeeed'ef  f fd:d;ZCd<eee
ej7 f d=ej7d>ejDdeee
ej7 f fd?d@ZEd<eee
ej7 f d=ej7deee
ej7 f fdAdBZFdCeee
ej7 f de1de2fdDdEZGdCeee
ej7 f de1de2fdFdGZHdCeee
ej7 f de1de2fdHdIZIdJeeeeej7gdf  f dCeee
ej7 f d=ej7d>ejDddf
dKdLZJG dMdN dNeZKG dOdP dPeZLdS )Q    )ismethod	signature)AnyCallablecastDictget_type_hintsListOptionalSequenceSetTupleTypeUnionN)CSTTransformer
CSTVisitor)CSTNodeT)CONSTRUCTED_LEAVE_MATCHER_ATTRCONSTRUCTED_VISIT_MATCHER_ATTRVISIT_NEGATIVE_MATCHER_ATTRVISIT_POSITIVE_MATCHER_ATTR)AllOfAtLeastNAtMostNBaseMatcherNodeextract
extractallfindallmatchesMatchIfTrueMatchMetadataMatchMetadataIfTrueOneOfreplace)TYPED_FUNCTION_RETURN_MAPPING)	UnionTypec                   @   s   e Zd ZdS )r%   N)__name__
__module____qualname__ r)   r)   R/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/matchers/_visitors.pyr%   5   s    r%   c                 C      h | ]}d |j  qS )visit_r&   .0clsr)   r)   r*   	<setcomp>:       r1   c                 C   r+   )leave_r-   r.   r)   r)   r*   r1   ;   r2   CONCRETE_METHODSobj	attr_namereturnc                 C   s   t tt| |dtS )z6Check if obj.attr is a property without evaluating it.N)
isinstancegetattrtypeproperty)r5   r6   r)   r)   r*   is_property?   s   r<   kwargsMatchDecoratorMismatchc                 C   s   t di | S )Nr)   )r>   )r=   r)   r)   r*   _match_decorator_unpicklerE   s   r?   c                       sL   e Zd Zdededdf fddZdeed eedf f fd	d
Z  Z	S )r>   funcmessager7   Nc                    s(   t  d| d|  || _|| _d S )NzInvalid function signature for z: )super__init__r@   rA   )selfr@   rA   	__class__r)   r*   rC   J   s   
zMatchDecoratorMismatch.__init__).r>   .c                 C   s   t | j| jdffS )N)r@   rA   )r?   r@   rA   rD   r)   r)   r*   
__reduce__O   s   z!MatchDecoratorMismatch.__reduce__)
r&   r'   r(   strrC   r   r   objectrH   __classcell__r)   r)   rE   r*   r>   I   s
    matcherc                 C   s.   t | ttfrdd | jD S tt| jjgS )Nc                 S   s   g | ]	}t t|jjqS r)   )r9   cstrF   r&   r/   mr)   r)   r*   
<listcomp>Z   s    z/_get_possible_match_classes.<locals>.<listcomp>)r8   r"   r   optionsr9   rM   rF   r&   )rL   r)   r)   r*   _get_possible_match_classesX   s   rR   
annotationc                 C   s   t | tpt| dd tu S )N
__origin__)r8   r%   r9   r   rS   r)   r)   r*   _annotation_is_union_   s   
rV   c                 C   s$   t | r
t| dg S ttt | gS )N__args__)rV   r9   r   r   rJ   rU   r)   r)   r*   _get_possible_annotated_classesf   s   rX   classesc                 C   s&   t  }| D ]}|tt|  q|S N)setupdaterX   r$   )rY   retvalr0   r)   r)   r*   (_get_valid_leave_annotations_for_classesm   s   
r^   possible_match_classesmeth.decorator_nameexpected_nonec          	      C   s   t |}|r |dtd td urt|jd| dd d S d|vr&d S t|d }t| }|D ]}|D ]	}t||r? nq6t|jd| dd|j d q2d S )Nr7   @z( should only decorate functions that do znot return.z" decorated function cannot return z	the type .)	r   getr:   r>   r(   rX   r^   
issubclassr&   )	r_   r`   ra   rb   
type_hintspossible_annotated_classespossible_returnsretrS   r)   r)   r*   _verify_return_annotation~   s>   


rk   expected_param_countc          
   	   C   s   t |}t|j|kr$t|jd| d| d |dkr d d dd t| D }|D ])}t|}| D ] }|D ]	}	t||	rF nq=t|jd| d	|j	 d
d d q9q1d S )Nrc   z& should decorate functions which take z
 parameter   s c                 S   s   g | ]
\}}|d kr|qS )r7   r)   )r/   kvr)   r)   r*   rP      s    z1_verify_parameter_annotations.<locals>.<listcomp>z can be called with  z4but the decorated function parameter annotations do znot include this type.)
r   len
parametersr>   r(   r   itemsrX   rf   r&   )
r_   r`   ra   rl   meth_signatureparamsparamrh   matchrS   r)   r)   r*   _verify_parameter_annotations   s@   	


rz   decoratormapexpected_none_returnc          	      C   s~   |   D ]8\}}t|}tdd |D }|D ]$}t|s#t|jd|r+t|jdt||||d t||||d qqd S )Nc                 s   s     | ]}t |tttfV  qd S rZ   )r8   r   r   r   rN   r)   r)   r*   	<genexpr>   s
    
z_check_types.<locals>.<genexpr>zoMatcher decorators should only be used on methods of MatcherDecoratableTransformer or MatcherDecoratableVisitorz]The root matcher in a matcher decorator cannot be an AtLeastN, AtMostN or MatchIfTrue matcher)rb   )rl   )ru   rR   anyr   r>   r(   rk   rz   )	r{   ra   rl   r|   rL   methodsr_   has_invalid_top_levelr`   r)   r)   r*   _check_types   s<   r   c                 C   sj   i }t | D ],}t| |s2t| |}t|tg D ]	}d|tt|< qt|tg D ]	}d|tt|< q(q|S )z
    Set of gating matchers that we need to track and evaluate. We use these
    in conjunction with the call_if_inside and call_if_not_inside decorators
    to determine whether to call a visit/leave function.
    N)dirr<   r9   r   r   r   r   )r5   visit_matchersr6   r@   rL   r)   r)   r*   _gather_matchers  s   

r   r@   c                 C   s&   |j tv rt|jd|  dd d S )Nrc   z1 should not decorate functions that are concrete zvisit or leave methods.)r&   r4   r>   r(   )ra   r@   r)   r)   r*   _assert_not_concrete%  s   

r   c                 C      i }t | D ]A}t| |rqt| |}t|sqtttjgd f |}t|tg }|r0t	d| |D ]}tt
|}g ||d|R ||< q2q|S )Nvisitr)   )r   r<   r9   r   r   r   rM   CSTNoder   r   r   re   r5   constructed_visitorsfuncnamepossible_funcr@   matchersrL   casted_matcherr)   r)   r*   _gather_constructed_visit_funcs0  *   




r   c                 C   r   )Nleaver)   )r   r<   r9   r   r   r   rM   r   r   r   r   re   r   r)   r)   r*   _gather_constructed_leave_funcsO  r   r   r   nodemetadata_resolverc                 C   sB   i }|   D ]\}}|d u rt|||dr|||< q|||< q|S Nr   )ru   r   )r   r   r   new_matchersrL   existing_noder)   r)   r*   _visit_matchersk  s   


r   c                 C   s4   i }|   D ]\}}||u rd ||< q|||< q|S rZ   )ru   )r   r   r   rL   r   r)   r)   r*   _leave_matchers~  s   

r   all_matchersc                 C   s,   t |tg }|D ]}| | d u r dS qdS NFT)r9   r   r   r5   requested_matchersrL   r)   r)   r*   _all_positive_matchers_true  s   r   c                 C   s,   t |tg }|D ]}| | d ur dS qdS r   )r9   r   r   r)   r)   r*   _all_negative_matchers_false  s   r   c                 C   s   t | |o	t| |S rZ   )r   r   )r   r5   r)   r)   r*   _should_allow_visit  s
   r   visit_funcsc                 C   s@   |   D ]\}} t|||dr| D ]}t||r|| qqd S r   )ru   r   r   )r   r   r   r   rL   
visit_funcr)   r)   r*   _visit_constructed_funcs  s   
r   c                   @   sR  e Zd ZdZd#ddZedeeee	j
 f fddZejdeeee	j
 f ddfd	dZd
e	j
defddZdededeee	jf fddZd
e	j
deddfddZde	j
deddfddZd
ee	je	je	j
f dedefddZdee	je	je	j
f deeee	j
 eef dee	j
 fddZd
ee	je	je	j
f dedeeeee	j
ee	j
 f f  fddZdee	je	je	j
f deeee	j
 eef deeeee	j
ee	j
 f f  fddZdee	je	je	j
f deeee	j
 eef d ee	je	je	j
ee	j
eeee	j
ee	j
 f f gee	je	je	j
f f f dee	je	je	j
f fd!d"Z dS )$MatcherDecoratableTransformera9  
    This class provides all of the features of a :class:`libcst.CSTTransformer`, and
    additionally supports various decorators to control when methods get called when
    traversing a tree. Use this instead of a :class:`libcst.CSTTransformer` if you
    wish to do more powerful decorator-based visiting.
    r7   Nc                 C   sL   t |  d | _t| | _t| | _t| jdddd t| jdddd d S )Nr   rm   Trl   r|   r      F)r   rC   (_MatcherDecoratableTransformer__matchersr   _extra_visit_funcsr   _extra_leave_funcsr   rG   r)   r)   r*   rC     s$   


z&MatcherDecoratableTransformer.__init__c                 C      | j d u r
t| | _ | j S rZ   )r   r   rG   r)   r)   r*   	_matchers     

z'MatcherDecoratableTransformer._matchersvaluec                 C   
   || _ d S rZ   )r   rD   r   r)   r)   r*   r        
r   c                 C   R   t | j|| | _t| j| j||  t| jt| dt|j d s#dS t	| |S Nr,   T)
r   r   r   r   r   r9   r:   r&   r   on_visitrD   r   r)   r)   r*   r        z&MatcherDecoratableTransformer.on_visitoriginal_nodeupdated_nodec                 C   s   t | jt| dt|j d rt| ||}n|}tt| j	
 D ]!\}}| ||s.q#|D ]}t | j|rCt|tjrC|||}q0q#t| j|| _|S Nr3   )r   r   r9   r:   r&   r   on_leavereversedlistr   ru   r   r8   rM   r   r   )rD   r   r   r]   rL   leave_funcs
leave_funcr)   r)   r*   r      s"   
z&MatcherDecoratableTransformer.on_leave	attributec                 C   8   t | jt| dt|j d| d rt| ||S d S Nr,   _)r   r   r9   r:   r&   r   on_visit_attributerD   r   r   r)   r)   r*   r        z0MatcherDecoratableTransformer.on_visit_attributec                 C   <   t | jt| dt|j d| d rt| || d S d S Nr3   r   )r   r   r9   r:   r&   r   on_leave_attributerD   r   r   r)   r)   r*   r   (     z0MatcherDecoratableTransformer.on_leave_attributerL   c                 C      t ||| dS ap  
        A convenience method to call :func:`~libcst.matchers.matches` without requiring
        an explicit parameter for metadata. Since our instance is an instance of
        :class:`libcst.MetadataDependent`, we work as a metadata resolver. Please see
        documentation for :func:`~libcst.matchers.matches` as it is identical to this
        function.
        r   r   rD   r   rL   r)   r)   r*   r   2     z%MatcherDecoratableTransformer.matchestreec                 C   r   ap  
        A convenience method to call :func:`~libcst.matchers.findall` without requiring
        an explicit parameter for metadata. Since our instance is an instance of
        :class:`libcst.MetadataDependent`, we work as a metadata resolver. Please see
        documentation for :func:`~libcst.matchers.findall` as it is identical to this
        function.
        r   r   rD   r   rL   r)   r)   r*   r   @     z%MatcherDecoratableTransformer.findallc                 C   r   ap  
        A convenience method to call :func:`~libcst.matchers.extract` without requiring
        an explicit parameter for metadata. Since our instance is an instance of
        :class:`libcst.MetadataDependent`, we work as a metadata resolver. Please see
        documentation for :func:`~libcst.matchers.extract` as it is identical to this
        function.
        r   r   r   r)   r)   r*   r   S  r   z%MatcherDecoratableTransformer.extractc                 C   r   av  
        A convenience method to call :func:`~libcst.matchers.extractall` without requiring
        an explicit parameter for metadata. Since our instance is an instance of
        :class:`libcst.MetadataDependent`, we work as a metadata resolver. Please see
        documentation for :func:`~libcst.matchers.extractall` as it is identical to this
        function.
        r   r   r   r)   r)   r*   r   a  r   z(MatcherDecoratableTransformer.extractallreplacementc                 C      t |||| dS ap  
        A convenience method to call :func:`~libcst.matchers.replace` without requiring
        an explicit parameter for metadata. Since our instance is an instance of
        :class:`libcst.MetadataDependent`, we work as a metadata resolver. Please see
        documentation for :func:`~libcst.matchers.replace` as it is identical to this
        function.
        r   r#   rD   r   rL   r   r)   r)   r*   r#   t     z%MatcherDecoratableTransformer.replacer7   N)!r&   r'   r(   __doc__rC   r;   r   r   r
   rM   r   r   setterboolr   r   r   RemovalSentinelr   rI   r   r   MaybeSentinelr   r   r    r!   r   r   r   r   r   r#   r)   r)   r)   r*   r     s    
#"



	

	
	 r   c                   @   sF  e Zd ZdZd"ddZedeeee	j
 f fddZejdeeee	j
 f ddfd	dZd
e	j
defddZde	j
ddfddZd
e	j
deddfddZde	j
deddfddZd
ee	je	je	j
f dedefddZdee	je	je	j
f deeee	j
 eef dee	j
 fddZd
ee	je	je	j
f dedeeeee	j
ee	j
 f f  fddZdee	je	je	j
f deeee	j
 eef deeeee	j
ee	j
 f f  fddZdee	je	je	j
f deeee	j
 eef dee	je	je	j
ee	j
eeee	j
ee	j
 f f gee	je	je	j
f f f dee	je	je	j
f fd d!ZdS )#MatcherDecoratableVisitora1  
    This class provides all of the features of a :class:`libcst.CSTVisitor`, and
    additionally supports various decorators to control when methods get called
    when traversing a tree. Use this instead of a :class:`libcst.CSTVisitor` if
    you wish to do more powerful decorator-based visiting.
    r7   Nc                 C   sL   t |  d | _t| | _t| | _t| jdddd t| jdddd d S )Nr   rm   Tr   r   )r   rC   $_MatcherDecoratableVisitor__matchersr   r   r   r   r   rG   r)   r)   r*   rC     s$   

z"MatcherDecoratableVisitor.__init__c                 C   r   rZ   )r   r   rG   r)   r)   r*   r     r   z#MatcherDecoratableVisitor._matchersr   c                 C   r   rZ   )r   r   r)   r)   r*   r     r   r   c                 C   r   r   )
r   r   r   r   r   r9   r:   r&   r   r   r   r)   r)   r*   r     r   z"MatcherDecoratableVisitor.on_visitr   c                 C   s   t | jt| dt|j d rt| | tt| j	
 D ]\}}| ||s*q|D ]}t | j|r8|| q,qt| j|| _d S r   )r   r   r9   r:   r&   r   r   r   r   r   ru   r   r   )rD   r   rL   r   r   r)   r)   r*   r     s   z"MatcherDecoratableVisitor.on_leaver   c                 C   r   r   )r   r   r9   r:   r&   r   r   r   r)   r)   r*   r     r   z,MatcherDecoratableVisitor.on_visit_attributec                 C   r   r   )r   r   r9   r:   r&   r   r   r   r)   r)   r*   r     r   z,MatcherDecoratableVisitor.on_leave_attributerL   c                 C   r   r   r   r   r)   r)   r*   r     r   z!MatcherDecoratableVisitor.matchesr   c                 C   r   r   r   r   r)   r)   r*   r     r   z!MatcherDecoratableVisitor.findallc                 C   r   r   r   r   r)   r)   r*   r     r   z!MatcherDecoratableVisitor.extractc                 C   r   r   r   r   r)   r)   r*   r   )  r   z$MatcherDecoratableVisitor.extractallr   c                 C   r   r   r   r   r)   r)   r*   r#   <  r   z!MatcherDecoratableVisitor.replacer   ) r&   r'   r(   r   rC   r;   r   r   r
   rM   r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r    r!   r   r   r   r   r   r#   r)   r)   r)   r*   r     s    
"


	

	
	 r   )Minspectr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   r   libcstrM   r   r   libcst._typesr   libcst.matchers._decoratorsr   r   r   r   libcst.matchers._matcher_baser   r   r   r   r   r   r   r   r   r    r!   r"   r#   libcst.matchers._return_typesr$   typesr%   ImportErrorr4   rI   __annotations__rJ   r   r<   r?   	Exceptionr>   r   rR   rV   rX   r^   rk   intrz   r   r   r   r   r   MetadataDependentr   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   s  8<



2

-
 4








 Z