o
    DDi4                    @   sz  d dl Zd dlZd dlZd dlmZ d dlmZmZ 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mZmZmZ d dlZd dlmZ d dlm Z m!Z!m"Z" d dl#m$Z$ G dd	 d	eZ%ed
ddZ&edddZ'edddZ(edZ)edZ*eded dZ+eded dZ,e- Z.G dd deZ/G dd dZ0de%fddZ1G dd dee+ e0Z2G dd dee& e0Z3G dd  d ee& e0Z4G d!d" d"ee& Z5G d#d$ d$ee& Z6G d%d& d&ee' Z7d'ee8ee8 f de7e8 fd(d)Z9G d*d+ d+Z:G d,d- d-e:Z;G d.d/ d/e:Z<G d0d1 d1Z=G d2d3 d3ee& e=Z>e%j?fd4ee&e%f de>ee&e%f  fd5d6Z@G d7d8 d8ee& e=ZAe%j?fd4ee&e%f deAee&e%f  fd9d:ZBd;e)de)fd<d=ZCd4e)d>e8de)fd?d@ZDd4ee0e=e7ejE e:e%f deFfdAdBZGeddCG dDdE dEZHdFeee!ejEf  dGeee0e=e7ejE e:e%f  dHeejIejEge-f deHfdIdJZJeee!ejEe8eFf  ZKeee0e%e8eFf  ZLdKeeKeeK f d4eeLeeL f dHeejIejEge-f deee8eejEeejE f f  fdLdMZMdKejEdNee:e4e: e3e: e5e: e6e: f dHeejIejEge-f deee8eejEeejE f f  fdOdPZNdKejEd4ee0e7ejE e:e5ee0e7ejE e:f  e6ee0e7ejE e:f  f dHeejIejEge-f deee8eejEeejE f f  fdQdRZOdKee!ejEf d4ee0e7ejE e:e5ee0e7ejE e:f  e6ee0e7ejE e:f  f dHeejIejEge-f deee8eejEeejE f f  fdSdTZPdeejIejEge-f fdUdVZQdWejRdeejIejEge-f fdXdYZSdZejTdeejIejEge-f fd[d\ZUdd]dKee!e"ejEf d4e0d^eeejRejTf  deee8eejEeejE f f  fd_d`ZVdd]dKee!e"ejEf d4e0d^eeejRejTf  deFfdadbZWG dcdd ddejXZYdd]deee!e"ejEejTf d4ee0e7ejE e:e5ee0e7ejE e:f  f d^eeejRejTf  deeejE eee8eejEeejE f f  f fdfdgZZdd]deee!e"ejEejTf d4ee0e7ejE e:f d^eeejRejTf  deejE fdhdiZ[dd]deee!e"ejEejTf d4ee0e7ejE e:f d^eeejRejTf  deee8eejEeejE f f  fdjdkZ\G dldm dmej]Z^dd]deee!e"ejEejTf d4ee0e7ejE e:f dnee!e"ejEeejEee8eejEeejE f f gee!e"ejEf f f d^eeejRejTf  dee!e"ejEf f
dodpZ_dS )q    N)ABCMeta)	dataclassfields)autoEnum)CallablecastDictGenericIteratorListMappingNoReturnOptionalPatternSequenceTupleTypeTypeVarUnion)FlattenSentinelMaybeSentinelRemovalSentinel)	LazyValuec                   @   s$   e Zd ZdZe ZdefddZdS )DoNotCareSentinelaJ  
    A sentinel that is used in matcher classes to indicate that a caller
    does not care what this value is. We recommend that you do not use this
    directly, and instead use the :func:`DoNotCare` helper. You do not
    need to use this for concrete matcher attributes since :func:`DoNotCare`
    is already the default.
    returnc                 C   s   dS )NzDoNotCare() selfr   r   V/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/matchers/_matcher_base.py__repr__/   s   zDoNotCareSentinel.__repr__N)__name__
__module____qualname____doc__r   DEFAULTstrr    r   r   r   r   r   $   s    r   	_MatcherTT)	covariant_MatchIfTrueT_BaseMatcherNodeSelfTBaseMatcherNode)bound_OtherNodeT_MetadataValueT_MatcherTypeT_OtherNodeMatcherTypeTc                   @   s&   e Zd ZdZded ddfddZdS )	AbstractBaseMatcherNodeMetaz
    Metaclass that all matcher nodes uses. Allows chaining 2 node type
    together with an bitwise-or operator to produce an :class:`TypeOf`
    matcher.
    noder+   r   zTypeOf[Type[BaseMatcherNode]]c                 C   
   t | |S N)TypeOf)r   r2   r   r   r   __or__I      
z"AbstractBaseMatcherNodeMeta.__or__N)r!   r"   r#   r$   r   r6   r   r   r   r   r1   A   s    r1   c                   @   sN   e Zd ZdZdededdfddZdededdfd	d
ZdeddfddZdS )r+   an  
    Base class that all concrete matchers subclass from. :class:`OneOf` and
    :class:`AllOf` also subclass from this in order to allow them to be used in
    any place that a concrete matcher is allowed. This means that, for example,
    you can call :func:`matches` with a concrete matcher, or a :class:`OneOf` with
    several concrete matchers as options.
    r   otherr   z0OneOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]]c                 C   r3   r4   OneOfr   r8   r   r   r   r6   W      
zBaseMatcherNode.__or__z0AllOf[Union[_BaseMatcherNodeSelfT, _OtherNodeT]]c                 C   r3   r4   AllOfr;   r   r   r   __and__\   r<   zBaseMatcherNode.__and__r*   c                 C      t tt| S r4   )r   r*   
_InverseOfr   r   r   r   
__invert__a      zBaseMatcherNode.__invert__N)	r!   r"   r#   r$   r*   r-   r6   r?   rB   r   r   r   r   r+   M   s"    	

r   c                   C   s   t jS )a8  
    Used when you want to match exactly one node, but you do not care what node it is.
    Useful inside sequences such as a :class:`libcst.matchers.Call`'s args attribte.
    You do not need to use this for concrete matcher attributes since :func:`DoNotCare`
    is already the default.

    For example, the following matcher would match against any function calls with
    three arguments, regardless of the arguments themselves and regardless of the
    function name that we were calling::

        m.Call(args=[m.DoNotCare(), m.DoNotCare(), m.DoNotCare()])
    )r   r%   r   r   r   r   	DoNotCaree   s   rD   c                   @   s   e Zd ZdZdeedf ddfddZedefdd	Z	ede
e fd
dZdededefddZdeddfddZdedefddZdddZdefddZdS )r5   a  
    Matcher that matches any one of the given types. Useful when you want to work
    with trees where a common property might belong to more than a single type.

    For example, if you want either a binary operation or a boolean operation
    where the left side has a name ``foo``::

        m.TypeOf(m.BinaryOperation, m.BooleanOperation)(left = m.Name("foo"))

    Or you could use the shorthand, like::

        (m.BinaryOperation | m.BooleanOperation)(left = m.Name("foo"))

    Also :class:`TypeOf` matchers can be used with initalizing in the default
    state of other node matchers (without passing any extra patterns)::

        m.Name | m.SimpleString

    The will be equal to::

        m.OneOf(m.Name(), m.SimpleString())
    optionszTypeOf[_MatcherTypeT]r   Nc                 G   s\   g }|D ]}t |tr|jrtd||j q|| qd| _di f| _t	|| _d S )Nz:Cannot chain an uninitalized TypeOf with an initalized oneFr   )

isinstancer5   
initalized	Exceptionextend_raw_optionsappend_initalized_call_itemstupler   rE   actual_optionsoptionr   r   r   __init__   s   

zTypeOf.__init__c                 C      | j S r4   )rL   r   r   r   r   rG      s   zTypeOf.initalizedc                 c   s0    | j D ]}| j\}}||i |}|V  qd S r4   )rJ   rM   )r   rQ   argskwargsmatcher_patternr   r   r   rE      s   

zTypeOf.optionsrT   rU   c                 O   s   d| _ ||f| _| S NT)rL   rM   )r   rT   rU   r   r   r   __call__   s   
zTypeOf.__call__r8   z4TypeOf[Union[_MatcherTypeT, _OtherNodeMatcherTypeT]]c                 C   s   t tttf  | |S r4   )r5   r   r/   r0   r;   r   r   r   r6         zTypeOf.__or__c                 C   s&   t | j|j}}td|d|)Nz.TypeError: unsupported operand type(s) for &: z and )typer!   	TypeError)r   r8   leftrightr   r   r   r?      s   zTypeOf.__and__AllOf[BaseMatcherNode]c                 C   s   t tt| j S r4   )r>   mapDoesNotMatchrE   r   r   r   r   rB      s   zTypeOf.__invert__c                 C   s*   d dd | jD }d| d| j dS )N, c                 s       | ]}t |V  qd S r4   repr).0rQ   r   r   r   	<genexpr>       z"TypeOf.__repr__.<locals>.<genexpr>zTypeOf(z, initalized = ))joinrJ   rG   )r   typesr   r   r   r       s   zTypeOf.__repr__)r   r^   )r!   r"   r#   r$   r   r/   rR   propertyboolrG   r   r+   rE   objectrX   r0   r6   r   r?   rB   r&   r    r   r   r   r   r5   u   s     

r5   c                   @   s|   e Zd ZdZdeedf ddfddZedee fdd	Z	d
e
ddfddZd
e
defddZdddZdefddZdS )r:   a  
    Matcher that matches any one of its options. Useful when you want to match
    against one of several options for a single node. You can also construct a
    :class:`OneOf` matcher by using Python's bitwise or operator with concrete
    matcher classes.

    For example, you could match against ``True``/``False`` like::

        m.OneOf(m.Name("True"), m.Name("False"))

    Or you could use the shorthand, like::

        m.Name("True") | m.Name("False")

    rE   OneOf[_MatcherT]r   Nc                 G   sT   g }|D ]}t |trtdt |ttfr||j q|| qt|| _	d S N*Cannot use AllOf and OneOf in combination!)
rF   r>   rH   r:   r5   rI   rE   rK   rN   _optionsrO   r   r   r   rR      s   
zOneOf.__init__c                 C   rS   )z
        The normalized list of options that we can choose from to satisfy a
        :class:`OneOf` matcher. If any of these matchers are true, the
        :class:`OneOf` matcher will also be considered a match.
        rq   r   r   r   r   rE         zOneOf.optionsr8   $OneOf[Union[_MatcherT, _OtherNodeT]]c                 C   r3   r4   r9   r;   r   r   r   r6      r7   zOneOf.__or__c                 C      t dro   rH   r;   r   r   r   r?         zOneOf.__and__AllOf[_MatcherT]c                 C      t dd | jD  S )Nc                 S      g | ]}t |qS r   r`   re   mr   r   r   
<listcomp>       z$OneOf.__invert__.<locals>.<listcomp>)r>   rq   r   r   r   r   rB         zOneOf.__invert__c                 C      dd dd | jD  dS )NzOneOf(ra   c                 S   rz   r   rc   re   or   r   r   r~      r   z"OneOf.__repr__.<locals>.<listcomp>rh   ri   rq   r   r   r   r   r          zOneOf.__repr__)r   rx   )r!   r"   r#   r$   r   r'   rR   rk   r   rE   r-   r6   r   r?   rB   r&   r    r   r   r   r   r:      s    	
r:   c                   @   s|   e Zd ZdZdeedf ddfddZedee fdd	Z	d
e
defddZd
e
ddfddZdddZdefddZdS )r>   a  
    Matcher that matches all of its options. Useful when you want to match
    against a concrete matcher and a :class:`MatchIfTrue` at the same time. Also
    useful when you want to match against a concrete matcher and a
    :func:`DoesNotMatch` at the same time. You can also construct a
    :class:`AllOf` matcher by using Python's bitwise and operator with concrete
    matcher classes.

    For example, you could match against ``True`` in a roundabout way like::

        m.AllOf(m.Name(), m.Name("True"))

    Or you could use the shorthand, like::

        m.Name() & m.Name("True")

    Similar to :class:`OneOf`, this can be used in place of any concrete matcher.

    Real-world cases where :class:`AllOf` is useful are hard to come by but they
    are still provided for the limited edge cases in which they make sense. In
    the example above, we are redundantly matching against any LibCST
    :class:`~libcst.Name` node as well as LibCST :class:`~libcst.Name` nodes that
    have the ``value`` of ``True``. We could drop the first option entirely and
    get the same result. Often, if you are using a :class:`AllOf`,
    you can refactor your code to be simpler.

    For example, the following matches any function call to ``foo``, and
    any function call which takes zero arguments::

        m.AllOf(m.Call(func=m.Name("foo")), m.Call(args=()))

    This could be refactored into the following equivalent concrete matcher::

        m.Call(func=m.Name("foo"), args=())

    rE   rx   r   Nc                 G   sb   g }|D ]%}t |trtdt |trtdt |tr$||j q|| qt|| _	d S )Nrp   z+Cannot use AllOf and TypeOf in combination!)
rF   r:   rH   r5   r>   rI   rE   rK   rN   rq   rO   r   r   r   rR     s   


zAllOf.__init__c                 C   rS   )z
        The normalized list of options that we can choose from to satisfy a
        :class:`AllOf` matcher. If all of these matchers are true, the
        :class:`AllOf` matcher will also be considered a match.
        rr   r   r   r   r   rE   *  rs   zAllOf.optionsr8   c                 C   ru   ro   rv   r;   r   r   r   r6   4  rw   zAllOf.__or__$AllOf[Union[_MatcherT, _OtherNodeT]]c                 C   r3   r4   r=   r;   r   r   r   r?   7  r7   zAllOf.__and__rn   c                 C   ry   )Nc                 S   rz   r   r{   r|   r   r   r   r~   <  r   z$AllOf.__invert__.<locals>.<listcomp>)r:   rq   r   r   r   r   rB   :  r   zAllOf.__invert__c                 C   r   )NzAllOf(ra   c                 S   rz   r   rc   r   r   r   r   r~   ?  r   z"AllOf.__repr__.<locals>.<listcomp>rh   r   r   r   r   r   r    >  r   zAllOf.__repr__)r   rn   )r!   r"   r#   r$   r   r'   rR   rk   r   rE   r-   r   r6   r?   rB   r&   r    r   r   r   r   r>      s    %	
r>   c                   @   s   e Zd ZdZdeddfddZedefddZd	edd
fddZ	d	eddfddZ
dedefddZdefddZdefddZdS )rA   aq  
    Matcher that inverts the match result of its child. You can also construct a
    :class:`_InverseOf` matcher by using Python's bitwise invert operator with concrete
    matcher classes or any special matcher.

    Note that you should refrain from constructing a :class:`_InverseOf` directly, and
    should instead use the :func:`DoesNotMatch` helper function.

    For example, the following matches against any identifier that isn't
    ``True``/``False``::

        m.DoesNotMatch(m.OneOf(m.Name("True"), m.Name("False")))

    Or you could use the shorthand, like:

        ~(m.Name("True") | m.Name("False"))

    matcherr   Nc                 C   
   || _ d S r4   _matcher)r   r   r   r   r   rR   V  r7   z_InverseOf.__init__c                 C   rS   )z
        The matcher that we will evaluate and invert. If this matcher is true, then
        :class:`_InverseOf` will be considered not a match, and vice-versa.
        r   r   r   r   r   r   Y     z_InverseOf.matcherr8   rt   c                 C      t ttttf  t| |S r4   r   r:   r   r'   r-   r;   r   r   r   r6   b     z_InverseOf.__or__r   c                 C   r   r4   )r   r>   r   r'   r-   r;   r   r   r   r?   g  r   z_InverseOf.__and__keyc                 C      t | j|S r4   getattrr   r   r   r   r   r   __getattr__l     z_InverseOf.__getattr__c                 C   rS   r4   r   r   r   r   r   rB   r  s   z_InverseOf.__invert__c                 C      dt | j dS )NzDoesNotMatch(rh   )rd   r   r   r   r   r   r    u     z_InverseOf.__repr__)r!   r"   r#   r$   r'   rR   rk   r   r-   r6   r?   r&   rm   r   rB   r    r   r   r   r   rA   B  s    rA   c                   @   s   e Zd ZdZdededdfddZedefdd	Zedefd
dZ	de
ddfddZde
ddfddZdedefddZdddZdefddZdS )_ExtractMatchingNodea  
    Transparent pass-through matcher that captures the node which matches its children,
    making it available to the caller of :func:`extract` or :func:`extractall`.

    Note that you should refrain from constructing a :class:`_ExtractMatchingNode`
    directly, and should instead use the :func:`SaveMatchedNode` helper function.

    For example, the following will match against any binary operation whose left
    and right operands are not integers, saving those expressions for later inspection.
    If used inside :func:`extract` or :func:`extractall`, the resulting dictionary will
    contain the keys ``left_operand`` and ``right_operand``.

        m.BinaryOperation(
            left=m.SaveMatchedNode(
                m.DoesNotMatch(m.Integer()),
                "left_operand",
            ),
            right=m.SaveMatchedNode(
                m.DoesNotMatch(m.Integer()),
                "right_operand",
            ),
        )
    r   namer   Nc                 C      || _ || _d S r4   )r   _name)r   r   r   r   r   r   rR     s   
z_ExtractMatchingNode.__init__c                 C   rS   )z
        The matcher that we will evaluate and capture matching LibCST nodes for.
        If this matcher is true, then :class:`_ExtractMatchingNode` will be considered
        a match and will save the node which matched.
        r   r   r   r   r   r     rs   z_ExtractMatchingNode.matcherc                 C   rS   )z
        The name we will call our captured LibCST node inside the resulting dictionary
        returned by :func:`extract` or :func:`extractall`.
        )r   r   r   r   r   r     r   z_ExtractMatchingNode.namer8   rt   c                 C   r   r4   r   r;   r   r   r   r6     r   z_ExtractMatchingNode.__or__r   c                 C   ru   )NzlCannot use AllOf with SavedMatchedNode children! Instead, you should use SaveMatchedNode(AllOf(options...)).rv   r;   r   r   r   r?     s   z_ExtractMatchingNode.__and__r   c                 C   r   r4   r   r   r   r   r   r     r   z _ExtractMatchingNode.__getattr__r'   c                 C   ru   )NzeCannot invert a SaveMatchedNode. Instead you should wrap SaveMatchedNode around your inversion itselfrv   r   r   r   r   rB     s   z_ExtractMatchingNode.__invert__c                 C      dt | j dt | j dS )NzSaveMatchedNode(matcher=z, name=rh   )rd   r   r   r   r   r   r   r      s   z_ExtractMatchingNode.__repr__)r   r'   )r!   r"   r#   r$   r'   r&   rR   rk   r   r   r-   r6   r?   rm   r   rB   r    r   r   r   r   r   y  s    

r   c                   @   s   e Zd ZU dZeegef ed< deegef ddfddZe	deegef fdd	Z
d
eddfddZd
eddfddZdddZdefddZdS )MatchIfTruea  
    Matcher that matches if its child callable returns ``True``. The child callable
    should take one argument which is the attribute on the LibCST node we are
    trying to match against. This is useful if you want to do complex logic to
    determine if an attribute should match or not. One example of this is the
    :func:`MatchRegex` matcher build on top of :class:`MatchIfTrue` which takes a
    regular expression and matches any string attribute where a regex match is found.

    For example, to match on any identifier spelled with the letter ``e``::

        m.Name(value=m.MatchIfTrue(lambda value: "e" in value))

    This can be used in place of any concrete matcher as long as it is not the
    root matcher. Calling :func:`matches` directly on a :class:`MatchIfTrue` is
    redundant since you can just call the child callable directly with the node
    you are passing to :func:`matches`.
    _funcfuncr   Nc                 C   r   r4   r   )r   r   r   r   r   rR     r7   zMatchIfTrue.__init__c                 C   rS   )z
        The function that we will call with a LibCST node in order to determine
        if we match. If the function returns ``True`` then we consider ourselves
        to be a match.
        r   r   r   r   r   r     rs   zMatchIfTrue.funcr8   z5OneOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]]c                 C   r3   r4   r9   r;   r   r   r   r6     r<   zMatchIfTrue.__or__z5AllOf[Union[MatchIfTrue[_MatchIfTrueT], _OtherNodeT]]c                 C   r3   r4   r=   r;   r   r   r   r?     r<   zMatchIfTrue.__and__MatchIfTrue[_MatchIfTrueT]c                    s   t  fddS )Nc                         |  S r4   r   valr   r   r   <lambda>      z(MatchIfTrue.__invert__.<locals>.<lambda>)r   r   r   r   r   rB     s   zMatchIfTrue.__invert__c                 C   r   )NzMatchIfTrue(rh   )rd   r   r   r   r   r   r       r   zMatchIfTrue.__repr__)r   r   )r!   r"   r#   r$   r   r)   rl   __annotations__rR   rk   r   r-   r6   r?   rB   r&   r    r   r   r   r   r     s$   
 	


r   regexc                    s   dt dtf fdd}t|S )a  
    Used as a convenience wrapper to :class:`MatchIfTrue` which allows for
    matching a string attribute against a regex. ``regex`` can be any regular
    expression string or a compiled ``Pattern``. This uses Python's re module
    under the hood and is compatible with syntax documented on
    `docs.python.org <https://docs.python.org/3/library/re.html>`_.

    For example, to match against any identifier that is at least one character
    long and only contains alphabetical characters::

        m.Name(value=m.MatchRegex(r'[A-Za-z]+'))

    This can be used in place of any string literal when constructing a concrete
    matcher.
    valuer   c                    s   t | trtt | S dS )NF)rF   r&   rl   re	fullmatch)r   r   r   r   _match_func  s   
zMatchRegex.<locals>._match_func)rm   rl   r   )r   r   r   r   r   
MatchRegex  s   r   c                   @      e Zd ZdZdS )_BaseMetadataMatcherz7
    Class that's only around for typing purposes.
    Nr!   r"   r#   r$   r   r   r   r   r     s    r   c                   @   s   e Zd ZdZdeeje  deddfddZe	dej
fdd	Ze	defd
dZdeddfddZdeddfddZdddZdefddZdS )MatchMetadataa  
    Matcher that looks up the metadata on the current node using the provided
    metadata provider and compares the value on the node against the value provided
    to :class:`MatchMetadata`.
    If the metadata provider is unresolved, a :class:`LookupError` exeption will be
    raised and ask you to provide a :class:`~libcst.metadata.MetadataWrapper`.
    If the metadata value does not exist for a particular node, :class:`MatchMetadata`
    will be considered not a match.

    For example, to match against any function call which has one parameter which
    is used in a load expression context::

        m.Call(
            args=[
                m.Arg(
                    m.MatchMetadata(
                        meta.ExpressionContextProvider,
                        meta.ExpressionContext.LOAD,
                    )
                )
            ]
        )

    To match against any :class:`~libcst.Name` node for the identifier ``foo``
    which is the target of an assignment::

        m.Name(
            value="foo",
            metadata=m.MatchMetadata(
                meta.ExpressionContextProvider,
                meta.ExpressionContext.STORE,
            )
        )

    This can be used in place of any concrete matcher as long as it is not the
    root matcher. Calling :func:`matches` directly on a :class:`MatchMetadata` is
    redundant since you can just check the metadata on the root node that you
    are passing to :func:`matches`.
    r   r   r   Nc                 C   r   r4   )_key_value)r   r   r   r   r   r   rR   O     
zMatchMetadata.__init__c                 C   rS   )a  
        The metadata provider that we will use to fetch values when identifying whether
        a node matches this matcher. We compare the value returned from the metadata
        provider to the value provided in ``value`` when determining a match.
        r   r   r   r   r   r   W  rs   zMatchMetadata.keyc                 C   rS   )z
        The value that we will compare against the return from the metadata provider
        for each node when determining a match.
        )r   r   r   r   r   r   `  r   zMatchMetadata.valuer8   z(OneOf[Union[MatchMetadata, _OtherNodeT]]c                 C   r3   r4   r9   r;   r   r   r   r6   i  r7   zMatchMetadata.__or__z(AllOf[Union[MatchMetadata, _OtherNodeT]]c                 C   r3   r4   r=   r;   r   r   r   r?   l  r7   zMatchMetadata.__and__c                 C   r@   r4   )r   r   rA   r   r   r   r   rB   o  s   zMatchMetadata.__invert__c                 C   r   )NzMatchMetadata(key=z, value=rh   )rd   r   r   r   r   r   r   r    t  r   zMatchMetadata.__repr__)r   r   )r!   r"   r#   r$   r   metaBaseMetadataProviderr.   rR   rk   	ProviderTr   rm   r   r-   r6   r?   rB   r&   r    r   r   r   r   r   &  s"    (

r   c                   @   s   e Zd ZdZdeeje  deege	f ddfddZ
edejfdd	Zedeege	f fd
dZdeddfddZdeddfddZdddZdefddZdS )MatchMetadataIfTruea5  
    Matcher that looks up the metadata on the current node using the provided
    metadata provider and passes it to a callable which can inspect the metadata
    further, returning ``True`` if the matcher should be considered a match.
    If the metadata provider is unresolved, a :class:`LookupError` exeption will be
    raised and ask you to provide a :class:`~libcst.metadata.MetadataWrapper`.
    If the metadata value does not exist for a particular node,
    :class:`MatchMetadataIfTrue` will be considered not a match.

    For example, to match against any arg whose qualified name might be
    ``typing.Dict``::

        m.Call(
            args=[
                m.Arg(
                    m.MatchMetadataIfTrue(
                        meta.QualifiedNameProvider,
                        lambda qualnames: any(n.name == "typing.Dict" for n in qualnames)
                    )
                )
            ]
        )

    To match against any :class:`~libcst.Name` node for the identifier ``foo``
    as long as that identifier is found at the beginning of an unindented line::

        m.Name(
            value="foo",
            metadata=m.MatchMetadataIfTrue(
                meta.PositionProvider,
                lambda position: position.start.column == 0,
            )
        )

    This can be used in place of any concrete matcher as long as it is not the
    root matcher. Calling :func:`matches` directly on a :class:`MatchMetadataIfTrue`
    is redundant since you can just check the metadata on the root node that you
    are passing to :func:`matches`.
    r   r   r   Nc                 C   r   r4   )r   r   )r   r   r   r   r   r   rR     r   zMatchMetadataIfTrue.__init__c                 C   rS   )z
        The metadata provider that we will use to fetch values when identifying whether
        a node matches this matcher. We pass the value returned from the metadata
        provider to the callable given to us in ``func``.
        r   r   r   r   r   r     rs   zMatchMetadataIfTrue.keyc                 C   rS   )z
        The function that we will call with a value retrieved from the metadata provider
        provided in ``key``. If the function returns ``True`` then we consider ourselves
        to be a match.
        r   r   r   r   r   r     rs   zMatchMetadataIfTrue.funcr8   z.OneOf[Union[MatchMetadataIfTrue, _OtherNodeT]]c                 C   r3   r4   r9   r;   r   r   r   r6     r<   zMatchMetadataIfTrue.__or__z.AllOf[Union[MatchMetadataIfTrue, _OtherNodeT]]c                 C   r3   r4   r=   r;   r   r   r   r?     r<   zMatchMetadataIfTrue.__and__c                    s   t  j fddS )Nc                    r   r4   r   r   r   r   r   r     r   z0MatchMetadataIfTrue.__invert__.<locals>.<lambda>)r   r   r   r   r   r   rB     r   zMatchMetadataIfTrue.__invert__c                 C   r   )NzMatchMetadataIfTrue(key=z, func=rh   )rd   r   r   r   r   r   r   r      r   zMatchMetadataIfTrue.__repr__)r   r   )r!   r"   r#   r$   r   r   r   r.   r   rl   rR   rk   r   r   rm   r   r-   r6   r?   rB   r&   r    r   r   r   r   r   x  s2    (
	


r   c                   @   r   )_BaseWildcardNodez
    A typing-only class for internal helpers in this module to be able to
    specify that they take a wildcard node type.
    Nr   r   r   r   r   r     s    r   c                   @      e Zd ZdZejfdeeef deddfddZ	e
defdd	Ze
deeef fd
dZdedefddZdedefddZdefddZdefddZdS )AtLeastNa  
    Matcher that matches ``n`` or more LibCST nodes in a row in a sequence.
    :class:`AtLeastN` defaults to matching against the :func:`DoNotCare` matcher,
    so if you do not specify a matcher as a child, :class:`AtLeastN`
    will match only by count. If you do specify a matcher as a child,
    :class:`AtLeastN` will instead make sure that each LibCST node matches the
    matcher supplied.

    For example, this will match all function calls with at least 3 arguments::

        m.Call(args=[m.AtLeastN(n=3)])

    This will match all function calls with 3 or more integer arguments::

        m.Call(args=[m.AtLeastN(n=3, matcher=m.Arg(m.Integer()))])

    You can combine sequence matchers with concrete matchers and special matchers
    and it will behave as you expect. For example, this will match all function
    calls that have 2 or more integer arguments in a row, followed by any arbitrary
    argument::

        m.Call(args=[m.AtLeastN(n=2, matcher=m.Arg(m.Integer())), m.DoNotCare()])

    And finally, this will match all function calls that have at least 5
    arguments, the final one being an integer::

        m.Call(args=[m.AtLeastN(n=4), m.Arg(m.Integer())])
    r   nr   Nc                C   *   |dk rt | jj d|| _|| _d S Nr   z n attribute must be positiverH   	__class__r!   _nr   r   r   r   r   r   r   rR        
zAtLeastN.__init__c                 C   rS   )aH  
        The number of nodes in a row that must match :attr:`AtLeastN.matcher` for
        this matcher to be considered a match. If there are less than ``n`` matches,
        this matcher will not be considered a match. If there are equal to or more
        than ``n`` matches, this matcher will be considered a match.
        r   r   r   r   r   r      s   z
AtLeastN.nc                 C   rS   zK
        The matcher which each node in a sequence needs to match.
        r   r   r   r   r   r   
     zAtLeastN.matcherr8   c                 C   ru   )Nz*AtLeastN cannot be used in a OneOf matcherrv   r;   r   r   r   r6     rw   zAtLeastN.__or__c                 C   ru   )Nz+AtLeastN cannot be used in an AllOf matcherrv   r;   r   r   r   r?     rw   zAtLeastN.__and__c                 C   ru   )Nz"Cannot invert an AtLeastN matcher!rv   r   r   r   r   rB     rw   zAtLeastN.__invert__c                 C   6   | j dkrdt| j dS dt| j d| j  dS )Nr   zZeroOrMore(rh   z	AtLeastN(, n=r   rd   r   r   r   r   r   r         
zAtLeastN.__repr__r!   r"   r#   r$   r   r%   r   r'   intrR   rk   r   r   rm   r   r6   r?   rB   r&   r    r   r   r   r   r     s$    

	r   r   c                 C      t ttttf  t| ddS )a  
    Used as a convenience wrapper to :class:`AtLeastN` when ``n`` is equal to ``0``.
    Use this when you want to match against any number of nodes in a sequence.

    For example, this will match any function call with zero or more arguments, as
    long as all of the arguments are integers::

        m.Call(args=[m.ZeroOrMore(m.Arg(m.Integer()))])

    This will match any function call where the first argument is an integer and
    it doesn't matter what the rest of the arguments are::

        m.Call(args=[m.Arg(m.Integer()), m.ZeroOrMore()])

    You will often want to use :class:`ZeroOrMore` on both sides of a concrete
    matcher in order to match against sequences that contain a particular node
    in an arbitrary location. For example, the following will match any function
    call that takes in at least one string argument anywhere::

        m.Call(args=[m.ZeroOrMore(), m.Arg(m.SimpleString()), m.ZeroOrMore()])
    r   r   )r   r   r   r'   r   r   r   r   r   
ZeroOrMore"  s   r   c                   @   r   )AtMostNa  
    Matcher that matches ``n`` or fewer LibCST nodes in a row in a sequence.
    :class:`AtMostN` defaults to matching against the :func:`DoNotCare` matcher,
    so if you do not specify a matcher as a child, :class:`AtMostN` will
    match only by count. If you do specify a matcher as a child,
    :class:`AtMostN` will instead make sure that each LibCST node matches the
    matcher supplied.

    For example, this will match all function calls with 3 or fewer arguments::

        m.Call(args=[m.AtMostN(n=3)])

    This will match all function calls with 0, 1 or 2 string arguments::

        m.Call(args=[m.AtMostN(n=2, matcher=m.Arg(m.SimpleString()))])

    You can combine sequence matchers with concrete matchers and special matchers
    and it will behave as you expect. For example, this will match all function
    calls that have 0, 1 or 2 string arguments in a row, followed by an arbitrary
    argument::

        m.Call(args=[m.AtMostN(n=2, matcher=m.Arg(m.SimpleString())), m.DoNotCare()])

    And finally, this will match all function calls that have at least 2
    arguments, the final one being a string::

        m.Call(args=[m.AtMostN(n=2), m.Arg(m.SimpleString())])
    r   r   r   Nc                C   r   r   r   r   r   r   r   rR   [  r   zAtMostN.__init__c                 C   rS   )aj  
        The number of nodes in a row that must match :attr:`AtLeastN.matcher` for
        this matcher to be considered a match. If there are less than or equal to
        ``n`` matches, then this matcher will be considered a match. Any more than
        ``n`` matches in a row and this matcher will stop matching and be considered
        not a match.
        r   r   r   r   r   r   f  s   	z	AtMostN.nc                 C   rS   r   r   r   r   r   r   r   q  r   zAtMostN.matcherr8   c                 C   ru   )Nz)AtMostN cannot be used in a OneOf matcherrv   r;   r   r   r   r6   y  rw   zAtMostN.__or__c                 C   ru   )Nz*AtMostN cannot be used in an AllOf matcherrv   r;   r   r   r   r?   |  rw   zAtMostN.__and__c                 C   ru   )Nz!Cannot invert an AtMostN matcher!rv   r   r   r   r   rB     rw   zAtMostN.__invert__c                 C   r   )N   z
ZeroOrOne(rh   zAtMostN(r   r   r   r   r   r   r      r   zAtMostN.__repr__r   r   r   r   r   r   =  s$    


r   c                 C   r   )a  
    Used as a convenience wrapper to :class:`AtMostN` when ``n`` is equal to ``1``.
    This is effectively a maybe clause.

    For example, this will match any function call with zero or one integer
    argument::

        m.Call(args=[m.ZeroOrOne(m.Arg(m.Integer()))])

    This will match any function call that has two or three arguments, and
    the first and last arguments are strings::

        m.Call(args=[m.Arg(m.SimpleString()), m.ZeroOrOne(), m.Arg(m.SimpleString())])

    r   r   )r   r   r   r'   r   r   r   r   r   	ZeroOrOne  s   r   objc                 C   s.   t | tttttfr|  }nt| }tt|S )a  
    Matcher helper that inverts the match result of its child. You can also invert a
    matcher by using Python's bitwise invert operator on concrete matchers or any
    special matcher.

    For example, the following matches against any identifier that isn't
    ``True``/``False``::

        m.DoesNotMatch(m.OneOf(m.Name("True"), m.Name("False")))

    Or you could use the shorthand, like::

        ~(m.Name("True") | m.Name("False"))

    This can be used in place of any concrete matcher as long as it is not the
    root matcher. Calling :func:`matches` directly on a :func:`DoesNotMatch` is
    redundant since you can invert the return of :func:`matches` using a bitwise not.
    )rF   r+   r   r   rA   r   r   r-   )r   inverser   r   r   r`     s   '
r`   r   c                 C   s   t tt| |S )a  
    Matcher helper that captures the matched node that matched against a matcher
    class, making it available in the dictionary returned by :func:`extract` or
    :func:`extractall`.

    For example, the following will match against any binary operation whose left
    and right operands are not integers, saving those expressions for later inspection.
    If used inside :func:`extract` or :func:`extractall`, the resulting dictionary
    will contain the keys ``left_operand`` and ``right_operand``::

        m.BinaryOperation(
            left=m.SaveMatchedNode(
                m.DoesNotMatch(m.Integer()),
                "left_operand",
            ),
            right=m.SaveMatchedNode(
                m.DoesNotMatch(m.Integer()),
                "right_operand",
            ),
        )

    This can be used in place of any concrete matcher as long as it is not the
    root matcher. Calling :func:`extract` directly on a :func:`SaveMatchedNode` is
    redundant since you already have the reference to the node itself.
    )r   r-   r   )r   r   r   r   r   SaveMatchedNode  s   r   c                 C   s>   t | tr| jdkrdS t | trdS t | trt| jS dS )Nr   TF)rF   r   r   r   r   _matches_zero_nodesr   r   r   r   r   r     s   	


r   )frozenc                   @   sP   e Zd ZU eeeeeje	ej f f  e
d< eeejee	ej f  e
d< dS )_SequenceMatchesResultsequence_capturematched_nodesN)r!   r"   r#   r   r	   r&   r   libcstCSTNoder   r   r   r   r   r   r   r     s   
 r   nodesmatchersmetadata_lookupc           	      C   sV  | s	|s	t i d S | s%|r%tdd |D r t dd |D dS t d d S | r.|s.t d d S | d }|d }t|trMt t| dd  |dd  |j|S t|trQt|tr|jdkrt	| d |j
|}|d urt| dd  t|j
|jd dg|dd  |}|jd ur|j}t|tsJ t i ||j|g|R S t t| |dd  |jdS t|trG|jdkrt	| d |j
|}|d urt| dd  t|j
|jd dg|dd  |}|jd ur|j}t|tsJ t i ||j|g|R S t d d S t	| d |j
|}|d ur9t| dd  ||}|jd ur9|j}t|ts+J t i ||j|g|R S t t| |dd  |jdS td	t| d
t|tr}t| |j
g|dd  |}|jd urxt |j|ji|j|jS t d d S t|||}|d urt| dd  |dd  |}|jd urt i ||j|S t d d S )Nc                 s   rb   r4   )r   r|   r   r   r   rf   #  rg   z$_sequence_matches.<locals>.<genexpr>c                 S   s   i | ]}t |tr|jd qS )r   )rF   r   r   r|   r   r   r   
<dictcomp>&  s    z%_sequence_matches.<locals>.<dictcomp>r   r   r   r   z"Logic error unrecognized wildcard !)r   allrF   r   _sequence_matchesr   r   r   r   _attribute_matchesr   r   r   r   rH   rZ   r   r   _matches)	r   r   r   r2   r   attribute_captureresultmatchedmatch_capturer   r   r   r     s   






 


 




	
	

r   r2   c              	   C   s(  t |tri S t |trt| |j|d u ri S d S t |tr4t| |j|}|d ur2i ||j| iS d S t |trB|| r@i S d S |d u rN| d u rLi S d S t |t	r[| |krYi S d S t |t
rh| |u rfi S d S t | tjjrtttttjf  | } t |tr|jD ],}t |tjjrt| ||}|jd ur|j  S qt |tr|| ri   S d   S qd S t |tri }|jD ]!}t |tjjrt| ||}|jd u r d S i ||j}q d S |S t |tjjrt| tttttttj tf  ||jS d S tttttjf | tttttf ||S r4   )rF   r   rA   r   r   r   r   r   r   r&   rl   collectionsabcr   r   r   r   r   r   r:   rE   r   r   r>   r+   r   r   r   )r2   r   r   r   r}   r   all_capturesr   r   r   r     s   










'


r   metadatac                 C   sD  t |tr|jD ]}t| ||}|d ur|  S qd S t |tr=i }|jD ]}t| ||}|d u r4 d S i ||}q%|S t |trOt| |j|d u rMi S d S t |trjt| |j|}|d urhi ||j| iS d S t |t	r||j
| }|tu r{d S ||ri S d S t |tr||j
| }|tu rd S ||jkri S d S td)NLogic error!)rF   r:   rE   _metadata_matchesr>   rA   r   r   r   r   r   _METADATA_MISSING_SENTINELr   r   r   rH   )r2   r   r   metadata_capturer   actual_valuer   r   r   r     sR   







r   c           
      C   sN  t |trt| |j|d u ri S d S t |tr-t| |j|}|d ur+i ||j| iS d S t |tr;|| r9i S d S t |tt	frHt
| ||S | jj|jjkrRd S i }t|D ]L}|jdkr`qX|jdkrt||j}t |trqqXt
| ||}|d u r~ d S i ||}qXt||j}t| |j}t|||}	|	d u r d S i ||	}qX|S )N	_metadatar   )rF   rA   _node_matchesr   r   r   r   r   r   r   r   r   r!   r   r   r   r   )
r2   r   r   node_capturer   fielddesiredr   actualr   r   r   r   r   P  sP   





r   c                 C   s   t | trt |tri S d S t |ttfr+|jD ]}t| ||}|d ur(|  S qd S t |trMi }|jD ]}t| ||}|d u rD d S i ||}q5|S t| ||S r4   )rF   r   rA   r:   r5   rE   r   r>   )r2   r   r   r   r   r   r   r   r     s$   



r   c                  C   s   dt jdtjdtfdd} | S )Nproviderr2   r   c                 S   s   t | j d)Nz3 is not resolved; did you forget a MetadataWrapper?)LookupErrorr!   r   r2   r   r   r   _fetch  s   
z0_construct_metadata_fetcher_null.<locals>._fetch)r   r   r   r   r   )r   r   r   r    _construct_metadata_fetcher_null  s   r   dependent_classc                    s"   dt jdtjdtf fdd}|S )Nr   r2   r   c                    s     | |tS r4   )get_metadatar   r   r  r   r   r     rC   z5_construct_metadata_fetcher_dependent.<locals>._fetchr   r   r   r   rm   )r  r   r   r  r   %_construct_metadata_fetcher_dependent  s   r  wrapperc                    s(   i  dt jdtjdtf fdd}|S )Nr   r2   r   c                    s:   |  vr |  | <  |  |t}t|tr| }|S r4   )resolvegetr   rF   r   )r   r2   node_metadatar   r  r   r   r     s   
z3_construct_metadata_fetcher_wrapper.<locals>._fetchr  )r  r   r   r
  r   #_construct_metadata_fetcher_wrapper  s    
r  metadata_resolverr  c                C   s^   t | trdS t |ttttfrdS |du rt }nt |tjr%t	|}nt
|}t| ||S )a;  
    Given an arbitrary node from a LibCST tree, and an arbitrary matcher, returns
    a dictionary of extracted children of the tree if the node matches the shape defined
    by the matcher. Note that the node can also be a :class:`~libcst.RemovalSentinel` or
    a :class:`~libcst.MaybeSentinel` in order to use extract directly on transform results
    and node attributes. In these cases, :func:`extract` will always return ``None``.

    If the node matches the shape defined by the matcher, the return will be a dictionary
    whose keys are defined by the :func:`SaveMatchedNode` name parameter, and the values
    will be the node or sequence that was present at that location in the shape defined
    by the matcher. In the case of multiple :func:`SaveMatchedNode` matches with the
    same name, parent nodes will take prioirity over child nodes, and nodes later in
    sequences will take priority over nodes earlier in sequences.

    The matcher can be any concrete matcher that subclasses from :class:`BaseMatcherNode`,
    or a :class:`OneOf`/:class:`AllOf` special matcher. It cannot be a
    :class:`MatchIfTrue` or a :func:`DoesNotMatch` matcher since these are redundant.
    It cannot be a :class:`AtLeastN` or :class:`AtMostN` matcher because these types are
    wildcards which can only be used inside sequences.
    N)rF   r   r   r   r   r   r   r   MetadataWrapperr  r  r   )r2   r   r  fetcherr   r   r   extract  s   

r  c                C   s   t | ||dduS )a  
    Given an arbitrary node from a LibCST tree, and an arbitrary matcher, returns
    ``True`` if the node matches the shape defined by the matcher. Note that the node
    can also be a :class:`~libcst.RemovalSentinel` or a :class:`~libcst.MaybeSentinel`
    in order to use matches directly on transform results and node attributes. In these
    cases, :func:`matches` will always return ``False``.

    The matcher can be any concrete matcher that subclasses from :class:`BaseMatcherNode`,
    or a :class:`OneOf`/:class:`AllOf` special matcher. It cannot be a
    :class:`MatchIfTrue` or a :func:`DoesNotMatch` matcher since these are redundant.
    It cannot be a :class:`AtLeastN` or :class:`AtMostN` matcher because these types
    are wildcards which can only be used inside sequences.
    r  N)r  )r2   r   r  r   r   r   matches'  s   r  c                
   @   sl   e Zd Zdeeeej ee	eeeej ef  f de
ejejgef ddfddZdejdefdd	ZdS )
_FindAllVisitorr   r   r   Nc                 C   s   || _ || _g | _g | _d S r4   )r   r   found_nodesextracted_nodes)r   r   r   r   r   r   rR   @  s
   z_FindAllVisitor.__init__r2   c                 C   s4   t || j| j}|d ur| j| | j| dS rW   )r   r   r   r  rK   r  )r   r2   matchr   r   r   on_visitW  s
   z_FindAllVisitor.on_visit)r!   r"   r#   r   r+   r   r   r   r   rA   r   r   r   rm   rR   rl   r  r   r   r   r   r  ?  s,    
r  treec                C   s   t | ttfrg g fS t |ttfrg g fS t | tjr"|d u r"| }|d u r*t }nt |tjr5t	|}nt
|}t||}| | |j|jfS r4   )rF   r   r   r   r   r   r  r   r   r  r  r  visitr  r  )r  r   r  r  finderr   r   r   _find_or_extract_all_  s   


r  c                C   s   t | ||d\}}|S )a:  
    Given an arbitrary node from a LibCST tree and an arbitrary matcher, iterates
    over that node and all children returning a sequence of all child nodes that
    match the given matcher. Note that the tree can also be a
    :class:`~libcst.RemovalSentinel` or a :class:`~libcst.MaybeSentinel`
    in order to use findall directly on transform results and node attributes. In these
    cases, :func:`findall` will always return an empty sequence. Note also that
    instead of a LibCST tree, you can instead pass in a
    :class:`~libcst.metadata.MetadataWrapper`. This mirrors the fact that you can
    call ``visit`` on a :class:`~libcst.metadata.MetadataWrapper` in order to iterate
    over it with a transform. If you provide a wrapper for the tree and do not set
    the ``metadata_resolver`` parameter specifically, it will automatically be set
    to the wrapper for you.

    The matcher can be any concrete matcher that subclasses from :class:`BaseMatcherNode`,
    or a :class:`OneOf`/:class:`AllOf` special matcher. Unlike :func:`matches`, it can
    also be a :class:`MatchIfTrue` or :func:`DoesNotMatch` matcher, since we are
    traversing the tree looking for matches. It cannot be a :class:`AtLeastN` or
    :class:`AtMostN` matcher because these types are wildcards which can only be used
    inside sequences.
    r  r  )r  r   r  r   _r   r   r   findall  s   r  c                C   s   t | ||d\}}|S )a-  
    Given an arbitrary node from a LibCST tree and an arbitrary matcher, iterates
    over that node and all children returning a sequence of dictionaries representing
    the saved and extracted children specified by :func:`SaveMatchedNode` for each
    match found in the tree. This is analogous to running a :func:`findall` over a
    tree, then running :func:`extract` with the same matcher over each of the returned
    nodes. Note that the tree can also be a :class:`~libcst.RemovalSentinel` or a
    :class:`~libcst.MaybeSentinel` in order to use extractall directly on transform
    results and node attributes. In these cases, :func:`extractall` will always
    return an empty sequence. Note also that instead of a LibCST tree, you can
    instead pass in a :class:`~libcst.metadata.MetadataWrapper`. This mirrors the
    fact that you can call ``visit`` on a :class:`~libcst.metadata.MetadataWrapper`
    in order to iterate over it with a transform. If you provide a wrapper for the
    tree and do not set the ``metadata_resolver`` parameter specifically, it will
    automatically be set to the wrapper for you.

    The matcher can be any concrete matcher that subclasses from :class:`BaseMatcherNode`,
    or a :class:`OneOf`/:class:`AllOf` special matcher. Unlike :func:`matches`, it can
    also be a :class:`MatchIfTrue` or :func:`DoesNotMatch` matcher, since we are
    traversing the tree looking for matches. It cannot be a :class:`AtLeastN` or
    :class:`AtMostN` matcher because these types are wildcards which can only be usedi
    inside sequences.
    r  r  )r  r   r  r  extractionsr   r   r   
extractall  s   
r  c                   @   s6  e Zd Zdeeeej ee	eeeej ef  f de
ejejgef deeeeje
ejeeeejeej f f geeeejf f f ddfddZdeejeej f deejeej f fd	d
Zdeeeejeej f f deeeejeej f f fddZdejdejdeejeef fddZdS )_ReplaceTransformerr   r   replacementr   Nc                    sX   || _ || _|  t r | _nt ttfr  fdd| _n fdd| _i | _d S )Nc                    s    S r4   r   r2   r  r!  r   r   r     s    z._ReplaceTransformer.__init__.<locals>.<lambda>c                    s      S r4   )
deep_cloner"  r#  r   r   r     s    )	r   r   inspect
isfunctionr!  rF   r   r   node_lut)r   r   r   r!  r   r#  r   rR     s   

z_ReplaceTransformer.__init__node_or_sequencec                    s*   t |trt fdd|D S  j| S )Nc                 3   s    | ]} j | V  qd S r4   )r'  )re   r2   r   r   r   rf     s    z6_ReplaceTransformer._node_translate.<locals>.<genexpr>)rF   r   rN   r'  )r   r(  r   r   r   _node_translate  s   

z#_ReplaceTransformer._node_translate	extractedc                    s    fdd|  D S )Nc                    s   i | ]
\}}|  |qS r   )r)  )re   r   r   r   r   r   r   "  s    z=_ReplaceTransformer._extraction_translate.<locals>.<dictcomp>)items)r   r*  r   r   r   _extraction_translate  rY   z)_ReplaceTransformer._extraction_translateoriginal_nodeupdated_nodec                 C   sh   || j |< t|| j| j}|d ur$z| |}W n ty#   d }Y nw |d ur2| j |= | ||S |S r4   )r'  r   r   r   r,  KeyErrorr!  )r   r-  r.  r*  r   r   r   on_leave$  s   
z_ReplaceTransformer.on_leave)r!   r"   r#   r   r+   r   r   r   r   rA   r   r   r   rm   r   r   r	   r&   r   rR   r)  r,  r0  r   r   r   r   r     sh    
<

r   r!  c                C   s   t | ttfr	| S t |ttfr)t | tjr|  S t | tj	r%| j
 S tdt | tj	r5|du r5| }|du r=t }nt |tj	rHt|}nt|}t|||}| |}t |tr`td|S )al  
    Given an arbitrary node from a LibCST tree and an arbitrary matcher, iterates
    over that node and all children and replaces each node that matches the supplied
    matcher with a supplied replacement. Note that the replacement can either be a
    valid node type, or a callable which takes the matched node and a dictionary of
    any extracted child values and returns a valid node type. If you provide a
    valid LibCST node type, :func:`replace` will replace every node that matches
    the supplied matcher with the replacement node. If you provide a callable,
    :func:`replace` will run :func:`extract` over all matched nodes and call the
    callable with both the node that should be replaced and the dictionary returned
    by :func:`extract`. Under all circumstances a new tree is returned.
    :func:`extract` should be viewed as a short-cut to writing a transform which
    also returns a new tree even when no changes are applied.

    Note that the tree can also be a :class:`~libcst.RemovalSentinel` or a
    :class:`~libcst.MaybeSentinel` in order to use replace directly on transform
    results and node attributes. In these cases, :func:`replace` will return the
    same :class:`~libcst.RemovalSentinel` or :class:`~libcst.MaybeSentinel`.
    Note also that instead of a LibCST tree, you can instead pass in a
    :class:`~libcst.metadata.MetadataWrapper`. This mirrors the fact that you can
    call ``visit`` on a :class:`~libcst.metadata.MetadataWrapper` in order to
    iterate over it with a transform. If you provide a wrapper for the tree and
    do not set the ``metadata_resolver`` parameter specifically, it will
    automatically be set to the wrapper for you.

    The matcher can be any concrete matcher that subclasses from :class:`BaseMatcherNode`,
    or a :class:`OneOf`/:class:`AllOf` special matcher. Unlike :func:`matches`, it can
    also be a :class:`MatchIfTrue` or :func:`DoesNotMatch` matcher, since we are
    traversing the tree looking for matches. It cannot be a :class:`AtLeastN` or
    :class:`AtMostN` matcher because these types are wildcards which can only be usedi
    inside sequences.
    r   Nz/Logic error, cannot get a FlattenSentinel here!)rF   r   r   r   r   r   r   r$  r   r  modulerH   r   r  r  r   r  r   )r  r   r!  r  r  replacernew_treer   r   r   replaceA  s(   4



r4  )`collections.abcr   r%  r   r   r   dataclassesr   r   enumr   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   libcst.metadatar   r   r   r   r   libcst._metadata_dependentr   r   r'   r)   r*   r-   r.   r/   r0   rm   r   r1   r+   rD   r5   r:   r>   rA   r   r   r&   r   r   r   r   r   r   r%   r   r   r   r`   r   r   rl   r   r   r   r   _AttributeValueT_AttributeMatcherTr   r   r   r   r   MetadataDependentr  r  r  r  r  
CSTVisitorr  r  r  r  CSTTransformerr   r4  r   r   r   r   <module>   sD  DN4K7V"5RV	L

M

:
	
 
l	

8
S
/

	

5
7
;
'
%y