o
    DDiK                     @   s  U 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 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 d d
lmZm Z m!Z! edddZ"dZ#ed e$d< G dd de%Z&G dd de%Z'G dd de Z(G dd deZ)G dd deZ*G dd deZ+de,de-fddZ.d ee, de-fd!d"Z/de-de-fd#d$Z0d%e,de,fd&d'Z1ed(d)G d*d deZ2G d+d, d,e2eZ3G d-d. d.e3eZ4d/S )0    )ABCabstractmethod)deepcopy)	dataclassfieldfieldsreplace)	AnycastClassVarDictListMappingSequenceTypeVarUnion)FlattenSentinel)CodegenState)RemovalSentinel)is_value_of_type)CSTNodeT)CSTTransformer
CSTVisitorCSTVisitorT_CSTNodeSelfTCSTNode)bound _EMPTY_SEQUENCEc                   @      e Zd ZdS )CSTValidationErrorN__name__
__module____qualname__r   r   r   K/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/_nodes/base.pyr           r    c                   @   r   )CSTCodegenErrorNr!   r   r   r   r%   r'      r&   r'   c                   @   s(   e Zd Zd	ddZdddefddZdS )
_ChildrenCollectionVisitorreturnNc                 C   s
   g | _ d S N)childrenselfr   r   r%   __init__   s   
z#_ChildrenCollectionVisitor.__init__noder   c                 C   s   | j | dS )NF)r+   appendr-   r/   r   r   r%   on_visit"   s   z#_ChildrenCollectionVisitor.on_visitr)   N)r"   r#   r$   r.   boolr2   r   r   r   r%   r(      s    
r(   c                   @   sZ   e Zd Zdddedef ddfddZdddefd	d
Zdddddedef fddZdS )_ChildReplacementTransformerold_noder   new_noder)   Nc                 C      || _ || _d S r*   r6   r7   )r-   r6   r7   r   r   r%   r.   (   s   
z%_ChildReplacementTransformer.__init__r/   c                 C   
   || j uS r*   r6   r1   r   r   r%   r2   .      
z%_ChildReplacementTransformer.on_visitoriginal_nodeupdated_nodec                 C   s   || j u r| jS |S r*   r9   r-   r=   r>   r   r   r%   on_leave3   s   
z%_ChildReplacementTransformer.on_leave)	r"   r#   r$   r   r   r.   r4   r2   r@   r   r   r   r%   r5   '   s     


r5   c                   @   sF   e Zd Zdddeeef ddfddZdddefd	d
ZdddZ	dS )_ChildWithChangesTransformerr6   r   changesr)   Nc                 C   r8   r*   )r6   rB   )r-   r6   rB   r   r   r%   r.   <   s   
z%_ChildWithChangesTransformer.__init__r/   c                 C   r:   r*   r;   r1   r   r   r%   r2   @   r<   z%_ChildWithChangesTransformer.on_visitr=   r>   c                 C   s    || j u r|jdi | jS |S )Nr   )r6   with_changesrB   r?   r   r   r%   r@   E   s   
z%_ChildWithChangesTransformer.on_leave)r=   r   r>   r   r)   r   )
r"   r#   r$   r   strr	   r.   r4   r2   r@   r   r   r   r%   rA   ;   s    rA   c                   @   r   )_NOOPVisitorNr!   r   r   r   r%   rE   K   r&   rE   valuer)   c                 C   s$   t | tst | trt| S t| S r*   )
isinstancerD   r   _pretty_repr_sequencereprrF   r   r   r%   _pretty_reprO   s   rK   seqc                 C   s.   t | dkrdS ddgdd | D dS )Nr   z[]
[c                 S   s   g | ]}t t| d qS ),)_indentrI   ).0elr   r   r%   
<listcomp>Z   s    z)_pretty_repr_sequence.<locals>.<listcomp>])lenjoin)rL   r   r   r%   rH   V   s   rH   c                 C   s   d dd | dD S )NrM   c                 s   s    | ]}d | V  qdS )z    Nr   )rQ   liner   r   r%   	<genexpr>^   s    z_indent.<locals>.<genexpr>)rV   splitrJ   r   r   r%   rP   ]   s   rP   valc                 C   s&   z|   W S  ty   t|  Y S w r*   )
deep_cloneAttributeErrorr   )rZ   r   r   r%   _clonea   s
   
r]   T)frozenc                
       s  e Zd ZU dZeee  ed< d8ddZe	de
ddf fdd	Zd8d
dZd8ddZd8ddZeded  fddZdededeeeee f fddZededd fddZdefddZededdfddZdede
ddfddZdede
defd d!Zdedefd"d#Zd$d defd%d&Z ded'd d(e!deee!f fd)d*Z"ded'd deeef fd+d,Z#ded'd de
defd-d.Z$ded$e%defd/d0Z&de'fd1d2Z(defd3d4Z)e	d5e%de%de
fd6d7Z*  Z+S )9r   r   	__slots__r)   Nc                 C   s   |    d S r*   )	_validater,   r   r   r%   __post_init__r   s   zCSTNode.__post_init__kwargsc                    sP   t  jdi | d| jvrtj| _d| jvrtj| _d| jvr&tj| _dS dS )ae  
        HACK: Add our implementation of `__repr__`, `__hash__`, and `__eq__` to the
        class's __dict__ to prevent dataclass from generating it's own `__repr__`,
        `__hash__`, and `__eq__`.

        The alternative is to require each implementation of a node to remember to add
        `repr=False, eq=False`, which is more error-prone.
        __repr____eq____hash__Nr   )super__init_subclass____dict__r   rc   rd   re   )clsrb   	__class__r   r%   rg   w   s   



zCSTNode.__init_subclass__c                 C      dS )a  
        Override this to perform runtime validation of a newly created node.

        The function is called during `__init__`. It should check for possible mistakes
        that wouldn't be caught by a static type checker.

        If you can't use a static type checker, and want to perform a runtime validation
        of this node's types, use `validate_types` instead.
        Nr   r,   r   r   r%   r`      s   
zCSTNode._validatec                 C   sb   t | D ]*}t| |j}t||js.td|jdt| j d|j d dt| qdS )a  
        Compares the type annotations on a node's fields with those field's actual
        values at runtime. Raises a TypeError is a mismatch is found.

        Only validates the current node, not any of it's children. For a recursive
        version, see :func:`validate_types_deep`.

        If you're using a static type checker (highly recommended), this is useless.
        However, if your code doesn't use a static type checker, or if you're unable to
        statically type your code for some reason, you can use this method to help
        validate your tree.

        Some (non-typing) validation is done unconditionally during the construction of
        a node. That validation does not overlap with the work that
        :func:`validate_types_deep` does.
        zExpected an instance of z on z's 'z' field, but instead got zan instance of N)r   getattrnamer   type	TypeErrorr"   )r-   frF   r   r   r%   validate_types_shallow   s   zCSTNode.validate_types_shallowc                 C   s    |    | jD ]}|  qdS )z`
        Like :func:`validate_types_shallow`, but recursively validates the whole tree.
        N)rr   r+   validate_types_deep)r-   chr   r   r%   rs      s   

zCSTNode.validate_types_deepc                 C   s   t  }| | |jS )a  
        The immediate (not transitive) child CSTNodes of the current node. Various
        properties on the nodes, such as string values, will not be visited if they are
        not a subclass of CSTNode.

        Iterable properties of the node (e.g. an IndentedBlock's body) will be flattened
        into the children's sequence.

        The children will always be returned in the same order that they appear
        lexically in the code.
        )r(   _visit_and_replace_childrenr+   r-   visitorr   r   r%   r+      s   
zCSTNode.childrenr-   rw   c                 C   sv   | | }|rtt| |}n| }t|tr||  | }n|| |}t|ttt	fs9t
ddt|j  |S )z
        Visits the current node, its children, and all transitive children using
        the given visitor's callbacks.
        z6Expected a node of type CSTNode or a RemovalSentinel, zbut got a return value of )r2   r
   r   ru   rG   r   r@   r   r   r   	Exceptionro   r"   )r-   rw   should_visit_childrenwith_updated_childrenleave_resultr   r   r%   visit   s"   



zCSTNode.visitc                 C   rl   )a  
        Intended to be overridden by subclasses to provide a low-level hook for the
        visitor API.

        Don't call this directly. Instead, use `visitor.visit_and_replace_node` or
        `visitor.visit_and_replace_module`. If you need list of children, access the
        `children` property instead.

        The general expectation is that children should be visited in the order in which
        they appear lexically.
        Nr   rv   r   r   r%   ru     s   z#CSTNode._visit_and_replace_childrenc                 C   rl   )z
        Intended to be overridden by nodes that will be iterated over inside
        Module and IndentedBlock. Returning true signifies that this node is
        essentially useless and can be dropped when doing a visit across it.
        Fr   r,   r   r   r%   _is_removable  s   zCSTNode._is_removablestatec                 C   s   d S r*   r   r-   r~   r   r   r%   _codegen_impl&  s   zCSTNode._codegen_implc                 K   s*   | |  | j|fi | ||  d S r*   )before_codegenr   after_codegen)r-   r~   rb   r   r   r%   _codegen)  s   
zCSTNode._codegenrB   c                 K   s   t | fi |S )a  
        A convenience method for performing mutation-like operations on immutable nodes.
        Creates a new object of the same type, replacing fields with values from the
        supplied keyword arguments.

        For example, to update the test of an if conditional, you could do::

            def leave_If(self, original_node: cst.If, updated_node: cst.If) -> cst.If:
                new_node = updated_node.with_changes(test=new_conditional)
                return new_node

        ``new_node`` will have the same ``body``, ``orelse``, and whitespace fields as
        ``updated_node``, but with the updated ``test`` field.

        The accepted arguments match the arguments given to ``__init__``, however there
        are no required or positional arguments.

        TODO: This API is untyped. There's probably no sane way to type it using pyre's
        current feature-set, but we should still think about ways to type this or a
        similar API in the future.
        )r   )r-   rB   r   r   r%   rC   .  s   zCSTNode.with_changesc              	   C   s   i }t | D ];}|j}|d dkrqt| |}t|ttfr%t|||< qztdd |D ||< W q tyA   t|||< Y qw t	| di |S )aY  
        Recursively clone the entire tree. The created tree is a new tree has the same
        representation but different identity.

        >>> tree = cst.parse_expression("1+2")

        >>> tree.deep_clone() == tree
        False

        >>> tree == tree
        True

        >>> tree.deep_equals(tree.deep_clone())
        True
        r   _c                 s   s    | ]}t |V  qd S r*   )r]   )rQ   vr   r   r%   rX   e  s    z%CSTNode.deep_clone.<locals>.<genexpr>Nr   )
r   rn   rm   rG   rD   bytesr]   tuplerp   ro   )r-   cloned_fieldsr   keyrZ   r   r   r%   r[   F  s   
zCSTNode.deep_cloneotherc                 C   s   ddl m} || |S )z
        Recursively inspects the entire tree under ``self`` and ``other`` to determine if
        the two trees are equal by representation instead of identity (``==``).
        r   )deep_equals)libcst._nodes.deep_equalsr   )r-   r   deep_equals_implr   r   r%   r   k  s   
zCSTNode.deep_equalsr6   r7   c                 C   *   |  t||}t|ttfrtd|S )a  
        Recursively replaces any instance of ``old_node`` with ``new_node`` by identity.
        Use this to avoid nested ``with_changes`` blocks when you are replacing one of
        a node's deep children with a new node. Note that if you have previously
        modified the tree in a way that ``old_node`` appears more than once as a deep
        child, all instances will be replaced.
        )Logic error, cannot get a *Sentinel here!)r|   r5   rG   r   r   rx   )r-   r6   r7   new_treer   r   r%   deep_replacet  s   
zCSTNode.deep_replacec                 C   s(   |  t|tj}t|trtd|S )z
        Recursively removes any instance of ``old_node`` by identity. Note that if you
        have previously modified the tree in a way that ``old_node`` appears more than
        once as a deep child, all instances will be removed.
        z/Logic error, cannot get a FlattenSentinel here!)r|   r5   r   REMOVErG   r   rx   )r-   r6   r   r   r   r%   deep_remove  s   

zCSTNode.deep_removec                 K   r   )a  
        A convenience method for applying :attr:`with_changes` to a child node. Use
        this to avoid chains of :attr:`with_changes` or combinations of
        :attr:`deep_replace` and :attr:`with_changes`.

        The accepted arguments match the arguments given to the child node's
        ``__init__``.

        TODO: This API is untyped. There's probably no sane way to type it using pyre's
        current feature-set, but we should still think about ways to type this or a
        similar API in the future.
        r   )r|   rA   rG   r   r   rx   )r-   r6   rB   r   r   r   r%   with_deep_changes  s   zCSTNode.with_deep_changesc                 C   s   | |u S )a  
        CSTNodes are only treated as equal by identity. This matches the behavior of
        CPython's AST nodes.

        If you actually want to compare the value instead of the identity of the current
        node with another, use `node.deep_equals`. Because `deep_equals` must traverse
        the entire tree, it can have an unexpectedly large time complexity.

        We're not exposing value equality as the default behavior because of
        `deep_equals`'s large time complexity.
        r   )r-   r   r   r   r%   rd     s   zCSTNode.__eq__c                 C   s   t | S r*   )idr,   r   r   r%   re     s   zCSTNode.__hash__c                 C   s   t t| dkrt| j dS t| j dg}t| D ]}|j}|d dkr<t| |}|t| dt| d q|d d	|S )	Nr   z()(r   =rO   )rM   )
rU   r   ro   r"   rn   rm   r0   rP   rK   rV   )r-   linesrq   r   rF   r   r   r%   rc     s   


zCSTNode.__repr__argsc                    s   t  fdddS )z
        A helper that allows us to easily use CSTNodes in dataclass constructor
        defaults without accidentally aliasing nodes by identity across multiple
        instances.
        c                      s    i S r*   r   r   r   ri   rb   r   r%   <lambda>  s    zCSTNode.field.<locals>.<lambda>)default_factory)r   )ri   r   rb   r   r   r%   r     s   
zCSTNode.fieldr3   ),r"   r#   r$   r_   r   r   rD   __annotations__ra   classmethodr	   rg   r`   rr   rs   propertyr+   r   r   r   r   r   r|   r   ru   r4   r}   r   r   r   rC   r[   r   r   r   r   r   objectrd   intre   rc   r   __classcell__r   r   rj   r%   r   n   sp   
 




=%	




 c                   @   s<   e Zd ZdZedee fddZdede	defddZ
d	S )
BaseLeafr   r)   c                 C   s   t S r*   )r   r,   r   r   r%   r+        zBaseLeaf.childrenr-   rw   c                 C   s   | S r*   r   rv   r   r   r%   ru     r   z$BaseLeaf._visit_and_replace_childrenN)r"   r#   r$   r_   r   r   r   r+   r   r   ru   r   r   r   r%   r     s    r   c                   @   s0   e Zd ZU dZdZeed< deddfddZdS )	BaseValueTokenaT  
    Represents the subset of nodes that only contain a value. Not all tokens from the
    tokenizer will exist as BaseValueTokens. In places where the token is always a
    constant value (e.g. a COLON token), the token's value will be implicitly folded
    into the parent CSTNode, and hard-coded into the implementation of _codegen.
    r   rF   r~   r)   Nc                 C   s   | | j d S r*   )	add_tokenrF   r   r   r   r%   r     s   zBaseValueToken._codegen_impl)	r"   r#   r$   __doc__r_   rD   r   r   r   r   r   r   r%   r     s
   
 r   N)5abcr   r   copyr   dataclassesr   r   r   r   typingr	   r
   r   r   r   r   r   r   r   libcst._flatten_sentinelr   libcst._nodes.internalr   libcst._removal_sentinelr   libcst._type_enforcer   libcst._typesr   libcst._visitorsr   r   r   r   r   r   SyntaxErrorr    r'   r(   r5   rA   rE   r   rD   rK   rH   rP   r]   r   r   r   r   r   r   r%   <module>   s8   ,	  j