o
    DDi-*                    @   s 	  d dl Z d dlmZmZ d dlm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 d dl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!m"Z"m#Z# d dl$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z: d dl;m<Z< ee	ddG dd de"Z=ee	ddG dd de"Z>ee	ddG dd de"Z?ee	ddG dd de"Z@ee	ddG dd de"ZAee	ddG dd de"ZBee	ddG dd de"ZCG d d! d!e"eZDG d"d# d#eZEG d$d% d%eDeZFG d&d' d'eFeZGG d(d) d)eFeZHee	ddG d*d+ d+eGeHZIee	ddG d,d- d-eFZJG d.d/ d/eFeZKee	ddG d0d1 d1eKZLee	ddG d2d3 d3eKZMee	ddG d4d5 d5eKZNG d6d7 d7eFeZOed8 ZPG d9d: d:eOeZQee	ddG d;d< d<eQZRG d=d> d>e"eZSee	ddG d?d@ d@eSZTee	ddG dAdB dBeSZUee	ddG dCdD dDeQZVee	ddG dEdF dFeOZWee	ddG dGdH dHe"ZXee	ddG dIdJ dJeFZYee	ddG dKdL dLeFZZee	ddG dMdN dNeFZ[ee	ddG dOdP dPeFZ\ee	ddG dQdR dReGeHZ]G dSdT dTe"eZ^ee	ddG dUdV dVe^Z_ee	ddG dWdX dXe^Z`ee	ddG dYdZ dZe"Zaee	ddG d[d\ d\eGeHZbee	ddG d]d^ d^e"Zcee	ddG d_d` d`e"Zdee	ddG dadb dbe"Zeee	ddG dcdd dde"Zfee	ddG dedf dfe"Zgee	ddG dgdh dheFZhee	ddG didj dje"ZiG dkdl dleFeZjee	ddG dmdn dnejZkee	ddG dodp dpeFZlee	ddG dqdr dreFZmee	ddG dsdt dte"Znee	ddG dudv dveFZoG dwdx dxe"eZpG dydz dzepeZqG d{d| d|epeZree	ddG d}d~ d~eqZsee	ddG dd derZtee	ddG dd deqeFeDZuee	ddG dd derZvee	ddG dd deGeHZwG dd deFeZxee	ddG dd dexeGeHZyG dd deFeZzG dd dezeZ{ee	ddG dd de{Z|G dd dezeZ}ee	ddG dd de}Z~ee	ddG dd de"Zee	ddG dd de"ZG dd deFeZG dd deeZee	ddG dd deZee	ddG dd dexeZee	ddG dd de{eZee	ddG dd de}eZee	ddG dd deFZdS )    N)ABCabstractmethod)literal_eval)contextmanager)	dataclassfield)autoEnum)Floatnumber
Imagnumber	Intnumber)Callable	GeneratorLiteralOptionalSequenceUnion)	add_slots)MaybeSentinel)CSTCodegenErrorCSTNodeCSTValidationError)CodegenStatevisit_optionalvisit_requiredvisit_sentinelvisit_sequence)AssignEqualBaseBinaryOpBaseBooleanOp
BaseCompOpBaseUnaryOpColonCommaDotInIsIsNotNotNotIn)BaseParenthesizableWhitespaceSimpleWhitespace)CSTVisitorTT)frozenc                   @   H   e Zd ZU dZedZeed< de	dd fddZ
dedd	fd
dZd	S )LeftSquareBracketz
    Used by various nodes to denote a subscript or list section. This doesn't own
    the whitespace to the left of it since this is owned by the parent node.
     whitespace_aftervisitorreturnc                 C      t t| d| j|dS Nr1   r1   )r/   r   r1   selfr2    r9   Q/var/www/Datamplify/venv/lib/python3.10/site-packages/libcst/_nodes/expression.py_visit_and_replace_children<   
   
z-LeftSquareBracket._visit_and_replace_childrenstateNc                 C      | d | j| d S )N[	add_tokenr1   _codegenr8   r=   r9   r9   r:   _codegen_implC      
zLeftSquareBracket._codegen_impl__name__
__module____qualname____doc__r+   r   r1   r*   __annotations__r,   r;   r   rD   r9   r9   r9   r:   r/   1   
   
 r/   c                   @   r.   )RightSquareBracketz
    Used by various nodes to denote a subscript or list section. This doesn't own
    the whitespace to the right of it since this is owned by the parent node.
    r0   whitespace_beforer2   r3   c                 C   r4   NrN   )rN   )rM   r   rN   r7   r9   r9   r:   r;   S   r<   z.RightSquareBracket._visit_and_replace_childrenr=   Nc                 C      | j | |d d S )N]rN   rB   rA   rC   r9   r9   r:   rD   Z      z RightSquareBracket._codegen_implrG   rH   rI   rJ   r+   r   rN   r*   rK   r,   r;   r   rD   r9   r9   r9   r:   rM   H   rL   rM   c                   @   r.   )LeftCurlyBracez
    Used by various nodes to denote a dict or set. This doesn't own the whitespace to
    the left of it since this is owned by the parent node.
    r0   r1   r2   r3   c                 C   r4   r5   )rU   r   r1   r7   r9   r9   r:   r;   j   r<   z*LeftCurlyBrace._visit_and_replace_childrenr=   Nc                 C   r>   )N{r@   rC   r9   r9   r:   rD   q   rE   zLeftCurlyBrace._codegen_implrF   r9   r9   r9   r:   rU   _   rL   rU   c                   @   r.   )RightCurlyBracez
    Used by various nodes to denote a dict or set. This doesn't own the whitespace to
    the right of it since this is owned by the parent node.
    r0   rN   r2   r3   c                 C   r4   rO   )rW   r   rN   r7   r9   r9   r:   r;      r<   z+RightCurlyBrace._visit_and_replace_childrenr=   Nc                 C   rP   )N}rR   rC   r9   r9   r:   rD      rS   zRightCurlyBrace._codegen_implrT   r9   r9   r9   r:   rW   v   rL   rW   c                   @   r.   )	LeftParenz
    Used by various nodes to denote a parenthesized section. This doesn't own
    the whitespace to the left of it since this is owned by the parent node.
    r0   r1   r2   r3   c                 C   r4   r5   )rY   r   r1   r7   r9   r9   r:   r;      r<   z%LeftParen._visit_and_replace_childrenr=   Nc                 C   r>   )N(r@   rC   r9   r9   r:   rD      rE   zLeftParen._codegen_implrF   r9   r9   r9   r:   rY      rL   rY   c                   @   r.   )
RightParenz
    Used by various nodes to denote a parenthesized section. This doesn't own
    the whitespace to the right of it since this is owned by the parent node.
    r0   rN   r2   r3   c                 C   r4   rO   )r[   r   rN   r7   r9   r9   r:   r;      r<   z&RightParen._visit_and_replace_childrenr=   Nc                 C   rP   )N)rR   rC   r9   r9   r:   rD      rS   zRightParen._codegen_implrT   r9   r9   r9   r:   r[      rL   r[   c                   @   sR   e Zd ZU dZedZeed< dddZde	dd fd	d
Z
deddfddZdS )Asynchronousze
    Used by asynchronous function definitions, as well as ``async for`` and
    ``async with``.
     r1   r3   Nc                 C   s   t | jjdk rtdd S )N   z0Must have at least one space after Asynchronous.)lenr1   valuer   r8   r9   r9   r:   	_validate   s   zAsynchronous._validater2   c                 C   r4   r5   )r]   r   r1   r7   r9   r9   r:   r;      r<   z(Asynchronous._visit_and_replace_childrenr=   c                 C   sD   | |  |d W d    n1 sw   Y  | j| d S )Nasync)record_syntactic_positionrA   r1   rB   rC   r9   r9   r:   rD      s   zAsynchronous._codegen_implr3   N)rG   rH   rI   rJ   r+   r   r1   rK   rc   r,   r;   r   rD   r9   r9   r9   r:   r]      s   
 
r]   c                   @   sZ   e Zd ZU dZdZdZee ed< dZ	ee
 ed< dddZed	eded
 fddZdS )_BaseParenthesizedNodea  
    We don't want to have another level of indirection for parenthesis in
    our tree, since that makes us more of a CST than an AST. So, all the
    expressions or atoms that can be wrapped in parenthesis will subclass
    this to get that functionality.
    r9   lparrparr3   Nc                 C   sH   | j r
| js
td| j s| jrtdt| j t| jkr"tdd S )Nz+Cannot have left paren without right paren.z+Cannot have right paren without left paren.zCannot have unbalanced parens.)rh   ri   r   r`   rb   r9   r9   r:   rc      s   z _BaseParenthesizedNode._validater=   NNNc                 c   sb    | j D ]}|| q||  d V  W d    n1 sw   Y  | jD ]}|| q'd S N)rh   rB   re   ri   )r8   r=   rh   ri   r9   r9   r:   _parenthesize   s   

z$_BaseParenthesizedNode._parenthesizerf   )rG   rH   rI   rJ   	__slots__rh   r   rY   rK   ri   r[   rc   r   r   r   rl   r9   r9   r9   r:   rg      s   
 
rg   c                   @   s   e Zd Ze Ze ZdS )ExpressionPositionN)rG   rH   rI   r   LEFTRIGHTr9   r9   r9   r:   rn      s    
rn   c                   @   s@   e Zd ZdZdZdedefddZdedd dd defd	d
ZdS )BaseExpressionzX
    An base class for all expressions. :class:`BaseExpression` contains no fields.
    r9   positionr3   c                 C   s   t | jdkot | jdkS )a  
        Returns true if this expression is safe to be use with a word operator
        such as "not" without space between the operator an ourselves. Examples
        where this is true are "not(True)", "(1)in[1,2,3]", etc. This base
        function handles parenthesized nodes, but certain nodes such as tuples,
        dictionaries and lists will override this to signifiy that they're always
        safe.
        r   )r`   rh   ri   r8   rr   r9   r9   r:   _safe_to_use_with_word_operator  s   
z.BaseExpression._safe_to_use_with_word_operatorleftrightc                 C   s0   |t jkr|t jS |t jkr|t jS dS NF)rn   rp   rt   ro   )r8   rr   ru   rv   r9   r9   r:   +_check_left_right_word_concatenation_safety  s
   

z:BaseExpression._check_left_right_word_concatenation_safetyN)	rG   rH   rI   rJ   rm   rn   boolrt   rx   r9   r9   r9   r:   rq      s    rq   c                   @      e Zd ZdZdZdS )BaseAssignTargetExpressiona  
    An expression that's valid on the left side of an assignment. That assignment may
    be part an :class:`Assign` node, or it may be part of a number of other control
    structures that perform an assignment, such as a :class:`For` loop.

    Python's grammar defines all expression as valid in this position, but the AST
    compiler further restricts the allowed types, which is what this type attempts to
    express.

    This is similar to a :class:`BaseDelTargetExpression`, but it also includes
    :class:`StarredElement` as a valid node.

    The set of valid nodes are defined as part of `CPython's AST context computation
    <https://github.com/python/cpython/blob/v3.8.0a4/Python/ast.c#L1120>`_.
    r9   NrG   rH   rI   rJ   rm   r9   r9   r9   r:   r{         r{   c                   @   rz   )BaseDelTargetExpressiona  
    An expression that's valid on the right side of a :class:`Del` statement.

    Python's grammar defines all expression as valid in this position, but the AST
    compiler further restricts the allowed types, which is what this type attempts to
    express.

    This is similar to a :class:`BaseAssignTargetExpression`, but it excludes
    :class:`StarredElement`.

    The set of valid nodes are defined as part of `CPython's AST context computation
    <https://github.com/python/cpython/blob/v3.8.0a4/Python/ast.c#L1120>`_ and as part
    of `CPython's bytecode compiler
    <https://github.com/python/cpython/blob/v3.8.0a4/Python/compile.c#L4854>`_.
    r9   Nr|   r9   r9   r9   r:   r~   0  r}   r~   c                       sp   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< dedd fdd	Zd fddZdedd
fddZ  ZS )Namea8  
    A simple variable name. Names are typically used in the context of a variable
    access, an assignment, or a deletion.

    Dotted variable names (``a.b.c``) are represented with :class:`Attribute` nodes,
    and subscripted variable names (``a[b]``) are represented with :class:`Subscript`
    nodes.
    ra   r9   rh   ri   r2   r3   c                 C   (   t t| d| j|| jt| d| j|dS Nrh   ri   )rh   ra   ri   )r   r   rh   ra   ri   r7   r9   r9   r:   r;   W  
   z Name._visit_and_replace_childrenNc                    sD   t t|   t| jdkrtd| j s td| jdd S )Nr   z"Cannot have empty name identifier.zName z is not a valid identifier.)superr   rc   r`   ra   r   isidentifierrb   	__class__r9   r:   rc   ^  s   
zName._validater=   c                 C   <   |  | || j W d    d S 1 sw   Y  d S rk   rl   rA   ra   rC   r9   r9   r:   rD   e     "zName._codegen_implrf   )rG   rH   rI   rJ   strrK   rh   r   rY   ri   r[   r,   r;   rc   r   rD   __classcell__r9   r9   r   r:   r   D  s   
 
r   c                   @   sh   e Zd ZU dZdZee ed< dZee	 ed< de
dd fddZd	edefd
dZdeddfddZdS )Ellipsisa$  
    An ellipsis ``...``. When used as an expression, it evaluates to the
    `Ellipsis constant`_. Ellipsis are often used as placeholders in code or in
    conjunction with :class:`SubscriptElement`.

    .. _Ellipsis constant: https://docs.python.org/3/library/constants.html#Ellipsis
    r9   rh   ri   r2   r3   c                 C   $   t t| d| j|t| d| j|dS )Nrh   ri   )rh   ri   )r   r   rh   ri   r7   r9   r9   r:   r;   z     z$Ellipsis._visit_and_replace_childrenrr   c                 C      dS NTr9   rs   r9   r9   r:   rt        z(Ellipsis._safe_to_use_with_word_operatorr=   Nc                 C   s:   |  | |d W d    d S 1 sw   Y  d S )Nz...)rl   rA   rC   r9   r9   r:   rD     s   "zEllipsis._codegen_impl)rG   rH   rI   rJ   rh   r   rY   rK   ri   r[   r,   r;   rn   ry   rt   r   rD   r9   r9   r9   r:   r   j  s   
 r   c                       s.   e Zd ZdZdZdedef fddZ  ZS )
BaseNumberz
    A type such as :class:`Integer`, :class:`Float`, or :class:`Imaginary` that can be
    used anywhere that you need to explicitly take any number type.
    r9   rr   r3   c                    s   |t jkrdS tt| |S )z
        Numbers are funny. The expression "5in [1,2,3,4,5]" is a valid expression
        which evaluates to "True". So, encapsulate that here by allowing zero spacing
        with the left hand side of an expression with a comparison operator.
        T)rn   ro   r   r   rt   rs   r   r9   r:   rt     s   
z*BaseNumber._safe_to_use_with_word_operator)	rG   rH   rI   rJ   rm   rn   ry   rt   r   r9   r9   r   r:   r     s    r   c                       ~   e Zd ZU eed< dZee ed< dZee	 ed< de
dd fddZd fd
dZdedd	fddZedefddZ  ZS )Integerra   r9   rh   ri   r2   r3   c                 C   r   r   )r   r   rh   ra   ri   r7   r9   r9   r:   r;     r   z#Integer._visit_and_replace_childrenNc                    (   t t|   tt| jstdd S )NzNumber is not a valid integer.)r   r   rc   re	fullmatchINTNUMBER_REra   r   rb   r   r9   r:   rc        zInteger._validater=   c                 C   r   rk   r   rC   r9   r9   r:   rD     r   zInteger._codegen_implc                 C   
   t | jS )zW
        Return an :func:`ast.literal_eval` evaluated int of :py:attr:`value`.
        r   ra   rb   r9   r9   r:   evaluated_value     
zInteger.evaluated_valuerf   )rG   rH   rI   r   rK   rh   r   rY   ri   r[   r,   r;   rc   r   rD   propertyintr   r   r9   r9   r   r:   r        
 r   c                       r   )Floatra   r9   rh   ri   r2   r3   c                 C   r   r   )r   r   rh   ra   ri   r7   r9   r9   r:   r;     r   z!Float._visit_and_replace_childrenNc                    r   )NzNumber is not a valid float.)r   r   rc   r   r   FLOATNUMBER_REra   r   rb   r   r9   r:   rc     r   zFloat._validater=   c                 C   r   rk   r   rC   r9   r9   r:   rD     r   zFloat._codegen_implc                 C   r   )zY
        Return an :func:`ast.literal_eval` evaluated float of :py:attr:`value`.
        r   rb   r9   r9   r:   r     r   zFloat.evaluated_valuerf   )rG   rH   rI   r   rK   rh   r   rY   ri   r[   r,   r;   rc   r   rD   r   floatr   r   r9   r9   r   r:   r     s   
 r   c                       r   )	Imaginaryra   r9   rh   ri   r2   r3   c                 C   r   r   )r   r   rh   ra   ri   r7   r9   r9   r:   r;     r   z%Imaginary._visit_and_replace_childrenNc                    r   )Nz Number is not a valid imaginary.)r   r   rc   r   r   IMAGNUMBER_REra   r   rb   r   r9   r:   rc     r   zImaginary._validater=   c                 C   r   rk   r   rC   r9   r9   r:   rD     r   zImaginary._codegen_implc                 C   r   )z[
        Return an :func:`ast.literal_eval` evaluated complex of :py:attr:`value`.
        r   rb   r9   r9   r:   r     r   zImaginary.evaluated_valuerf   )rG   rH   rI   r   rK   rh   r   rY   ri   r[   r,   r;   rc   r   rD   r   complexr   r   r9   r9   r   r:   r     r   r   c                   @   rz   )
BaseStringz
    A type that can be used anywhere that you need to take any string. This includes
    :class:`SimpleString`, :class:`ConcatenatedString`, and :class:`FormattedString`.
    r9   Nr|   r9   r9   r9   r:   r         r   "'z"""z'''c                       sN   e Zd ZdZedefddZedefddZde	de
f fdd	Z  ZS )
_BasePrefixedStringr9   r3   c                 C   r   )z
        Returns the string's prefix, if any exists.

        See `String and Bytes literals
        <https://docs.python.org/3.7/reference/lexical_analysis.html#string-and-bytes-literals>`_
        for more information.
        Nr9   rb   r9   r9   r:   prefix  s   	z_BasePrefixedString.prefixc                 C   r   )z
        Returns the quotation used to denote the string. Can be either ``'``,
        ``"``, ``'''`` or ``"""``.
        Nr9   rb   r9   r9   r:   quote%     z_BasePrefixedString.quoterr   c                    s,   |t jkrdS | jdkrdS tt| |S )a	  
        ``"a"in"abc`` is okay, but if you add a prefix, (e.g. ``b"a"inb"abc"``), the string
        is no longer valid on the RHS of the word operator, because it's not clear where
        the keyword ends and the prefix begins, unless it's parenthesized.
        Tr0   )rn   ro   r   r   r   rt   rs   r   r9   r:   rt   -  s   


z3_BasePrefixedString._safe_to_use_with_word_operator)rG   rH   rI   rm   r   r   r   StringQuoteLiteralr   rn   ry   rt   r   r9   r9   r   r:   r     s    
r   c                       s   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< d fdd	Zede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edeeef fddZ  ZS )SimpleStringz
    Any sort of literal string expression that is not a :class:`FormattedString`
    (f-string), including triple-quoted multi-line strings.
    ra   r9   rh   ri   r3   Nc                    s   t t|   | j}|dvrtdt|}t| j|d k r#td| j| dvs4| j| | jd kr8tdt| j|d krq| j| | j|d	  krs| j| | j|d  ksm| j| | jd
 ksm| j| | jd krutdd S d S d S )N)r0   rubbrrbzInvalid string prefix.   z"String must have enclosing quotes.r   r   z+String must have matching enclosing quotes.   r_   )r   r   rc   r   r   r`   ra   )r8   r   	prefixlenr   r9   r:   rc   O  s*   zSimpleString._validatec                 C   2   d}| j D ]}|dv r | S ||7 }q| S )z~
        Returns the string's prefix, if any exists. The prefix can be ``r``,
        ``u``, ``b``, ``br`` or ``rb``.
        r0   r   )ra   lowerr8   r   cr9   r9   r:   r   o     

zSimpleString.prefixc                 C   s   d}| j t| jd D ]}|dvr n|r||d kr n||7 }qt|dkr0|dd }ndt|  k r<dkrDn n|dd }t|d	vrRtd
| j  |S )r   r0   N>   r   r   r   r   r_      r   >   r_   r   zInvalid string )ra   r`   r   	Exception)r8   r   charr9   r9   r:   r   }  s   
zSimpleString.quotec                 C   s(   t | j}t | j}| j|| |  S )a  
        Returns the raw value of the string as it appears in source, without
        the beginning or end quotes and without the prefix. This is often
        useful when constructing transforms which need to manipulate strings
        in source code.
        )r`   r   r   ra   )r8   
prefix_len	quote_lenr9   r9   r:   	raw_value  s   
	
zSimpleString.raw_valuer2   c                 C   r   r   )r   r   rh   ra   ri   r7   r9   r9   r:   r;     r   z(SimpleString._visit_and_replace_childrenr=   c                 C   r   rk   r   rC   r9   r9   r:   rD     r   zSimpleString._codegen_implc                 C   r   )zW
        Return an :func:`ast.literal_eval` evaluated str of :py:attr:`value`.
        r   rb   r9   r9   r:   r     r   zSimpleString.evaluated_valuerf   )rG   rH   rI   rJ   r   rK   rh   r   rY   ri   r[   rc   r   r   r   r   r   r,   r;   r   rD   r   bytesr   r   r9   r9   r   r:   r   =  s    
 	 % r   c                   @   rz   )BaseFormattedStringContentz
    The base type for :class:`FormattedStringText` and
    :class:`FormattedStringExpression`. A :class:`FormattedString` is composed of a
    sequence of :class:`BaseFormattedStringContent` parts.
    r9   Nr|   r9   r9   r9   r:   r     s    r   c                   @   s>   e Zd ZU dZeed< dedd fddZdeddfd	d
Z	dS )FormattedStringTexta  
    Part of a :class:`FormattedString` that is not inside curly braces (``{`` or ``}``).
    For example, in::

        f"ab{cd}ef"

    ``ab`` and ``ef`` are :class:`FormattedStringText` nodes, but ``{cd}`` is a
    :class:`FormattedStringExpression`.
    ra   r2   r3   c                 C   s   t | jdS )N)ra   )r   ra   r7   r9   r9   r:   r;     s   z/FormattedStringText._visit_and_replace_childrenr=   Nc                 C   s   | | j d S rk   )rA   ra   rC   r9   r9   r:   rD     s   z!FormattedStringText._codegen_impl)
rG   rH   rI   rJ   r   rK   r,   r;   r   rD   r9   r9   r9   r:   r     s   
 
r   c                   @   s   e Zd ZU dZeed< dZee ed< dZ	ee
e  ed< edZeed< edZeed< dZee ed	< dddZded
d fddZded
dfddZdS )FormattedStringExpressionaK  
    Part of a :class:`FormattedString` that is inside curly braces (``{`` or ``}``),
    including the surrounding curly braces. For example, in::

        f"ab{cd}ef"

    ``{cd}`` is a :class:`FormattedStringExpression`, but ``ab`` and ``ef`` are
    :class:`FormattedStringText` nodes.

    An f-string expression may contain ``conversion`` and ``format_spec`` suffixes that
    control how the expression is converted to a string. See `Python's language
    reference
    <https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals>`__
    for details.
    
expressionN
conversionformat_specr0   whitespace_before_expressionwhitespace_after_expressionequalr3   c                 C   s$   | j d ur| j dvrtdd S d S )N)sr   azInvalid f-string conversion.)r   r   rb   r9   r9   r:   rc     s   z#FormattedStringExpression._validater2   c                 C   sf   | j }tt| d| j|t| d| j|t| d| j|t| d| j|| j|d ur/t	| d||dS d dS )Nr   r   r   r   r   )r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r8   r2   r   r9   r9   r:   r;     s,   z5FormattedStringExpression._visit_and_replace_childrenr=   c                 C   s   | d | j| | j| | j}|d ur|| | j| | j}|d ur4| d | | | j}|d urJ| d |D ]}|| qB| d d S )NrV   !:rX   )rA   r   rB   r   r   r   r   r   )r8   r=   r   r   r   specr9   r9   r:   rD   4  s"   




z'FormattedStringExpression._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r   r   r   r   r   r   r+   r   r   r*   r   r   r   rc   r,   r;   r   rD   r9   r9   r9   r:   r     s$   
 


r   c                       s   e Zd ZU dZee ed< dZeed< dZ	e
d ed< dZee ed	< dZee 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  ZS )FormattedStringa	  
    An "f-string". These formatted strings are string literals prefixed by the letter
    "f". An f-string may contain interpolated expressions inside curly braces (``{`` and
    ``}``).

    F-strings are defined in `PEP 498`_ and documented in `Python's language
    reference
    <https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals>`__.

    >>> import libcst as cst
    >>> cst.parse_expression('f"ab{cd}ef"')
    FormattedString(
        parts=[
            FormattedStringText(
                value='ab',
            ),
            FormattedStringExpression(
                expression=Name(
                    value='cd',
                    lpar=[],
                    rpar=[],
                ),
                conversion=None,
                format_spec=None,
                whitespace_before_expression=SimpleWhitespace(
                    value='',
                ),
                whitespace_after_expression=SimpleWhitespace(
                    value='',
                ),
            ),
            FormattedStringText(
                value='ef',
            ),
        ],
        start='f"',
        end='"',
        lpar=[],
        rpar=[],
    )

    .. _PEP 498: https://www.python.org/dev/peps/pep-0498/#specification
    partszf"startr   r   endr9   rh   ri   r3   Nc                    s\   t t|   | j}|dvrtd| jt|d  }|| jkr$td|dvr,tdd S )N)ffrrfzInvalid f-string prefix.z-f-string must have matching enclosing quotes.r   z"Invalid f-string enclosing quotes.)r   r   rc   r   r   r   r`   r   )r8   r   
starttokenr   r9   r:   rc     s   
zFormattedString._validatec                 C   r   )zq
        Returns the string's prefix, if any exists. The prefix can be ``f``,
        ``fr``, or ``rf``.
        r0   r   )r   r   r   r9   r9   r:   r     r   zFormattedString.prefixc                 C   s   | j S )r   )r   rb   r9   r9   r:   r     s   zFormattedString.quoter2   c              
   C   s:   t t| d| j|| jt| d| j|| jt| d| j|dS )Nrh   r   ri   )rh   r   r   r   ri   )r   r   rh   r   r   r   ri   r7   r9   r9   r:   r;     s   z+FormattedString._visit_and_replace_childrenr=   c                 C   s^   |  |  || j | jD ]}|| q|| j W d    d S 1 s(w   Y  d S rk   )rl   rA   r   r   rB   r   )r8   r=   partr9   r9   r:   rD     s   
"zFormattedString._codegen_implrf   )rG   rH   rI   rJ   r   r   rK   r   r   r   r   rh   rY   ri   r[   rc   r   r   r   r   r,   r;   r   rD   r   r9   r9   r   r:   r   H  s   
 .	r   c                       s   e Zd ZU dZeeef ed< eeed f ed< dZe	e
 ed< dZe	e ed< edZeed< d	ed
ef fddZd fddZded
d fddZded
dfddZed
eeedf fddZ  ZS )ConcatenatedStringaK  
    Represents an implicitly concatenated string, such as::

        "abc" "def" == "abcdef"

    .. warning::
       This is different from two strings joined in a :class:`BinaryOperation` with an
       :class:`Add` operator, and is `sometimes viewed as an antifeature of Python
       <https://lwn.net/Articles/551426/>`_.
    ru   rv   r9   rh   ri   r0   whitespace_betweenrr   r3   c                    &   t t| |r
dS | || j| jS r   )r   r   rt   rx   ru   rv   rs   r   r9   r:   rt     s
   
z2ConcatenatedString._safe_to_use_with_word_operatorNc                    s   t t|   t| jjst| jjrtdt| jjs#t| jjr'tdd| jj	v }| j}t
|tr<d|jj	v }nt
|trGd|j	v }nt
|trRd|j	v }ntd||kr^tdd S )Nz)Cannot concatenate parenthesized strings.r   Logic error!z$Cannot concatenate string and bytes.)r   r   rc   ry   ru   rh   ri   r   rv   r   
isinstancer   r   r   )r8   	leftbytesrv   
rightbytesr   r9   r:   rc     s"   


zConcatenatedString._validater2   c              
   C   N   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   ru   r   rv   ri   )rh   ru   r   rv   ri   )r   r   rh   r   ru   r   rv   ri   r7   r9   r9   r:   r;     s   
z.ConcatenatedString._visit_and_replace_childrenr=   c                 C   T   |  | | j| | j| | j| W d    d S 1 s#w   Y  d S rk   )rl   ru   rB   r   rv   rC   r9   r9   r:   rD     
   "z ConcatenatedString._codegen_implc                 C   sx   | j }| j}t|tst|trdS |j}|j}|du rdS t|tr,t|tr,|| S t|tr:t|tr:|| S dS )aA  
        Return an :func:`ast.literal_eval` evaluated str of recursively concatenated :py:attr:`left` and :py:attr:`right`
        if and only if both :py:attr:`left` and :py:attr:`right` are composed by :class:`SimpleString` or :class:`ConcatenatedString`
        (:class:`FormattedString` cannot be evaluated).
        N)ru   rv   r   r   r   r   r   )r8   ru   rv   left_val	right_valr9   r9   r:   r   
  s   z"ConcatenatedString.evaluated_valuerf   )rG   rH   rI   rJ   r   r   r   rK   rh   r   rY   ri   r[   r+   r   r   r*   rn   ry   rt   rc   r,   r;   r   rD   r   r   r   r   r   r9   r9   r   r:   r     s   
 "r   c                   @   sP   e Zd ZU dZeed< eed< dddZdedd fd	d
Z	de
ddfddZdS )ComparisonTargetzz
    A target for a :class:`Comparison`. Owns the comparison operator and the value to
    the right of the operator.
    operator
comparatorr3   Nc                 C   sB   | j }t|ttttfr|jjr| j	t
jstdd S d S d S )N8Must have at least one space around comparison operator.)r   r   r%   r)   r&   r'   r1   emptyr   rt   rn   rp   r   )r8   r   r9   r9   r:   rc   .  s   zComparisonTarget._validater2   c                 C   r   )Nr   r   )r   r   )r   r   r   r   r7   r9   r9   r:   r;   <  r   z,ComparisonTarget._visit_and_replace_childrenr=   c                 C   s   | j | | j| d S rk   )r   rB   r   rC   r9   r9   r:   rD   B  s   zComparisonTarget._codegen_implrf   )rG   rH   rI   rJ   r    rK   rq   rc   r,   r;   r   rD   r9   r9   r9   r:   r      s   
 
r   c                       s   e Zd ZU dZeed< ee ed< dZee	 ed< dZ
ee ed< dedef fd	d
Zd fddZdedd fddZdeddfddZ  ZS )
Comparisonap  
    A comparison between multiple values such as ``x < y``, ``x < y < z``, or
    ``x in [y, z]``. These comparisions typically result in boolean values.

    Unlike :class:`BinaryOperation` and :class:`BooleanOperation`, comparisons are not
    restricted to a left and right child. Instead they can contain an arbitrary number
    of :class:`ComparisonTarget` children.

    ``x < y < z`` is not equivalent to ``(x < y) < z`` or ``x < (y < z)``. Instead,
    it's roughly equivalent to ``x < y and y < z``.

    For more details, see `Python's documentation on comparisons
    <https://docs.python.org/3/reference/expressions.html#comparisons>`_.

    ::

        # x < y < z

        Comparison(
            Name("x"),
            [
                ComparisonTarget(LessThan(), Name("y")),
                ComparisonTarget(LessThan(), Name("z")),
            ],
        )
    ru   comparisonsr9   rh   ri   rr   r3   c                    s,   t t| |r
dS | || j| jd jS )NTr   )r   r   rt   rx   ru   r   r   rs   r   r9   r:   rt   r  s
   z*Comparison._safe_to_use_with_word_operatorNc                    st   t t|   t| jdkrtd| j}| jD ]}|j}t|t	t
ttfr4|jjr4|tjs4td|j}qd S )Nr   z(Must have at least one ComparisonTarget.r   )r   r   rc   r`   r   r   ru   r   r   r%   r)   r&   r'   rN   r   rt   rn   ro   r   )r8   previous_comparatortargetr   r   r9   r:   rc   z  s&   
zComparison._validater2   c              	   C   s@   t t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   ru   r   ri   )rh   ru   r   ri   )r   r   rh   r   ru   r   ri   r7   r9   r9   r:   r;        z&Comparison._visit_and_replace_childrenr=   c                 C   sR   |  | | j| | jD ]}|| qW d    d S 1 s"w   Y  d S rk   )rl   ru   rB   r   )r8   r=   compr9   r9   r:   rD     s   
"zComparison._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r   r   rh   rY   ri   r[   rn   ry   rt   rc   r,   r;   r   rD   r   r9   r9   r   r:   r   G  s   
 r   c                       s   e Zd ZU dZeed< eed< dZee	 ed< dZ
ee ed< d fd	d
Zdedd fddZdedef fddZdeddfddZ  ZS )UnaryOperationz
    Any generic unary expression, such as ``not x`` or ``-x``. :class:`UnaryOperation`
    nodes apply a :class:`BaseUnaryOp` to an expression.
    r   r   r9   rh   ri   r3   Nc                    sF   t t|   t| jtr| jjjr| j	t
js!tdd S d S d S )Nz0Must have at least one space after not operator.)r   r   rc   r   r   r(   r1   r   r   rt   rn   rp   r   rb   r   r9   r:   rc     s   
zUnaryOperation._validater2   c              	   C   @   t t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   r   r   ri   )rh   r   r   ri   )r   r   rh   r   r   r   ri   r7   r9   r9   r:   r;     r   z*UnaryOperation._visit_and_replace_childrenrr   c                    sH   t t| |r
dS |tjkrt| jt S |tjkr"| j	tjS dS )zu
        As long as we aren't comprised of the Not unary operator, we are safe to use
        without space.
        TF)
r   r   rt   rn   rp   r   r   r(   ro   r   rs   r   r9   r:   rt     s   

z.UnaryOperation._safe_to_use_with_word_operatorr=   c                 C   H   |  | | j| | j| W d    d S 1 sw   Y  d S rk   )rl   r   rB   r   rC   r9   r9   r:   rD        "zUnaryOperation._codegen_implrf   )rG   rH   rI   rJ   r!   rK   rq   rh   r   rY   ri   r[   rc   r,   r;   rn   ry   rt   r   rD   r   r9   r9   r   r:   r     s   
 r   c                       s   e Zd ZU dZeed< eed< eed< dZee	 ed< dZ
ee ed< ded	d fd
dZded	ef fddZded	dfddZ  ZS )BinaryOperationaJ  
    An operation that combines two expression such as ``x << y`` or ``y + z``.
    :class:`BinaryOperation` nodes apply a :class:`BaseBinaryOp` to an expression.

    Binary operations do not include operations performed with :class:`BaseBooleanOp`
    nodes, such as ``and`` or ``or``. Instead, those operations are provided by
    :class:`BooleanOperation`.

    It also does not include support for comparision operators performed with
    :class:`BaseCompOp`, such as ``<``, ``>=``, ``==``, ``is``, or ``in``. Instead,
    those operations are provided by :class:`Comparison`.
    ru   r   rv   r9   rh   ri   r2   r3   c              
   C   r   Nrh   ru   r   rv   ri   )rh   ru   r   rv   ri   )r   r   rh   r   ru   r   rv   ri   r7   r9   r9   r:   r;        z+BinaryOperation._visit_and_replace_childrenrr   c                    r   r   )r   r   rt   rx   ru   rv   rs   r   r9   r:   rt     
   
z/BinaryOperation._safe_to_use_with_word_operatorr=   Nc                 C   r   rk   rl   ru   rB   r   rv   rC   r9   r9   r:   rD     r   zBinaryOperation._codegen_impl)rG   rH   rI   rJ   rq   rK   r   rh   r   rY   ri   r[   r,   r;   rn   ry   rt   r   rD   r   r9   r9   r   r:   r     s   
 	r   c                       s   e Zd ZU dZeed< eed< eed< dZee	 ed< dZ
ee ed< d fd
dZdedd fddZdedef fddZdedd	fddZ  ZS )BooleanOperationaH  
    An operation that combines two booleans such as ``x or y`` or ``z and w``
    :class:`BooleanOperation` nodes apply a :class:`BaseBooleanOp` to an expression.

    Boolean operations do not include operations performed with :class:`BaseBinaryOp`
    nodes, such as ``+`` or ``<<``. Instead, those operations are provided by
    :class:`BinaryOperation`.

    It also does not include support for comparision operators performed with
    :class:`BaseCompOp`, such as ``<``, ``>=``, ``==``, ``is``, or ``in``. Instead,
    those operations are provided by :class:`Comparison`.
    ru   r   rv   r9   rh   ri   r3   Nc                    sV   t t|   | jjjr| jtj	st
d| jjjr'| jtjs)t
dd S d S )Nz5Must have at least one space around boolean operator.)r   r  rc   r   rN   r   ru   rt   rn   ro   r   r1   rv   rp   rb   r   r9   r:   rc   1  s    zBooleanOperation._validater2   c              
   C   r   r   )r  r   rh   r   ru   r   rv   ri   r7   r9   r9   r:   r;   D  r   z,BooleanOperation._visit_and_replace_childrenrr   c                    r   r   )r   r  rt   rx   ru   rv   rs   r   r9   r:   rt   M  r   z0BooleanOperation._safe_to_use_with_word_operatorr=   c                 C   r   rk   r  rC   r9   r9   r:   rD   T  r   zBooleanOperation._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r   rh   r   rY   ri   r[   rc   r,   r;   rn   ry   rt   r   rD   r   r9   r9   r   r:   r    s   
 	r  c                       s   e Zd ZU dZeed< eed< e Zeed< dZ	e
e ed< dZe
e ed< ded	d fd
dZded	ef fddZded	dfddZ  ZS )	Attributea  
    An attribute reference, such as ``x.y``.

    Note that in the case of ``x.y.z``, the outer attribute will have an attr of ``z``
    and the value will be another :class:`Attribute` referencing the ``y`` attribute on
    ``x``::

        Attribute(
            value=Attribute(
                value=Name("x")
                attr=Name("y")
            ),
            attr=Name("z"),
        )
    ra   attrdotr9   rh   ri   r2   r3   c              
   C   r   )Nrh   ra   r  r  ri   )rh   ra   r  r  ri   )r  r   rh   r   ra   r  r  ri   r7   r9   r9   r:   r;   }  r   z%Attribute._visit_and_replace_childrenrr   c                    r   r   )r   r  rt   rx   ra   r  rs   r   r9   r:   rt     r   z)Attribute._safe_to_use_with_word_operatorr=   Nc                 C   r   rk   )rl   ra   rB   r  r  rC   r9   r9   r:   rD     r   zAttribute._codegen_impl)rG   rH   rI   rJ   rq   rK   r   r$   r  rh   r   rY   ri   r[   r,   r;   rn   ry   rt   r   rD   r   r9   r9   r   r:   r  [  s   
 	r  c                   @   rz   )	BaseSlicezl
    Any slice type that can slot into a :class:`SubscriptElement`.
    This node is purely for typing.
    r9   Nr|   r9   r9   r9   r:   r    r   r  c                   @   sb   e Zd ZU dZeed< dZeed  ed< dZ	ee
 ed< dedd fd	d
ZdeddfddZdS )Indexzf
    Any index as passed to a :class:`Subscript`. In ``x[2]``, this would be the ``2``
    value.
    ra   N*starwhitespace_after_starr2   r3   c                 C   s(   t | jt| d| j|t| d| j|dS )Nr
  ra   )r	  r
  ra   )r  r	  r   r
  r   ra   r7   r9   r9   r:   r;     s   
z!Index._visit_and_replace_childrenr=   c                 C   s@   | j }|d ur|| | j}|d ur|| | j| d S rk   )r	  rA   r
  rB   ra   )r8   r=   r	  wsr9   r9   r:   rD     s   

zIndex._codegen_impl)rG   rH   rI   rJ   rq   rK   r	  r   r   r
  r*   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 	r  c                   @   s   e Zd ZU dZee ed< ee ed< dZee ed< e	 Z
eed< ejZeeef ed< ded	d fd
dZded	dfddZdS )Slicez
    Any slice operation in a :class:`Subscript`, such as ``1:``, ``2:3:4``, etc.

    Note that the grammar does NOT allow parenthesis around a slice so they are not
    supported here.
    r   upperNstepfirst_colonsecond_colonr2   r3   c              
   C   sN   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nr   r  r  r  r  )r   r  r  r  r  )	r  r   r   r   r  r  r   r  r  r7   r9   r9   r:   r;     s   
z!Slice._visit_and_replace_childrenr=   c                 C   s   | j }|d ur|| | j| | j}|d ur|| | j}|tju r1| jd ur1|d n
t	|t
r;|| | j}|d urI|| d S d S Nr   )r   rB   r  r  r  r   DEFAULTr  rA   r   r"   )r8   r=   r   r  r  r  r9   r9   r:   rD     s    



zSlice._codegen_impl)rG   rH   rI   rJ   r   rq   rK   r  r"   r   r  r   r  r  r   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 r  c                   @   sZ   e Zd ZU dZeed< ejZe	e
ef ed< dedd fddZdd	ed
eddfddZdS )SubscriptElementa]  
    Part of a sequence of slices in a :class:`Subscript`, such as ``1:2, 3``. This is
    not used in Python's standard library, but it is used in some third-party
    libraries. For example, `NumPy uses it to select values and ranges from
    multi-dimensional arrays
    <https://docs.scipy.org/doc/numpy-1.10.1/user/basics.indexing.html>`_.
    slicecommar2   r3   c                 C   $   t t| d| j|t| d| j|dS )Nr  r  )r  r  )r  r   r  r   r  r7   r9   r9   r:   r;   	  r   z,SubscriptElement._visit_and_replace_childrenFr=   default_commaNc                 C   st   | |  | j| W d    n1 sw   Y  | j}|tju r,|r,|d d S t|tr8|| d S d S )N, )	re   r  rB   r  r   r  rA   r   r#   r8   r=   r  r  r9   r9   r:   rD     s   
zSubscriptElement._codegen_implF)rG   rH   rI   rJ   r  rK   r   r  r  r   r#   r,   r;   r   ry   rD   r9   r9   r9   r:   r    s   
 	r  c                       s   e Zd ZU dZeed< ee ed< e	 Z
eed< e	 Zeed< dZee ed< dZee ed< e	d	Zeed
< d fddZdedd fddZdedef fddZdeddfddZ  ZS )	Subscriptz
    A indexed subscript reference (:class:`Index`) such as ``x[2]``, a :class:`Slice`
    such as ``x[1:-1]``, or an extended slice (:class:`SubscriptElement`) such as ``x[1:2, 3]``.
    ra   r  lbracketrbracketr9   rh   ri   r0   whitespace_after_valuer3   Nc                    s(   t t|   t| jdk rtdd S )Nr_   z#Cannot have empty SubscriptElement.)r   r  rc   r`   r  r   rb   r   r9   r:   rc   4  s   zSubscript._validater2   c                 C   sj   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|dS )	Nrh   ra   r  r  r  r  ri   )rh   ra   r  r  r  r  ri   )
r  r   rh   r   ra   r  r  r  r  ri   r7   r9   r9   r:   r;   :  s   
z%Subscript._visit_and_replace_childrenrr   c                    >   |t jkrdS tt| |rdS |t jkr| jt jS dS )NTF)rn   ro   r   r  rt   rp   ra   rs   r   r9   r:   rt   G  s   

z)Subscript._safe_to_use_with_word_operatorr=   c                 C   s   |  |; | j| | j| | j| t| jd }t| jD ]\}}|j|||kd q$| j| W d    d S 1 sCw   Y  d S )Nr_   r  )	rl   ra   rB   r  r  r`   r  	enumerater  )r8   r=   	lastsliceir  r9   r9   r:   rD   P  s   "zSubscript._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r   r  r/   r   r  rM   r  rh   rY   ri   r[   r+   r  r*   rc   r,   r;   rn   ry   rt   r   rD   r   r9   r9   r   r:   r    s   
 	r  c                   @   sr   e Zd ZU dZeed< ejZe	e
ef ed< edZe
ed< dedd fdd	Z	
ddedee dd
fddZd
S )
Annotationa  
    An annotation for a function (`PEP 3107`_) or on a variable (`PEP 526`_). Typically
    these are used in the context of type hints (`PEP 484`_), such as::

        # a variable with a type
        good_ideas: List[str] = []

        # a function with type annotations
        def concat(substrings: Sequence[str]) -> str:
            ...

    .. _PEP 3107: https://www.python.org/dev/peps/pep-3107/
    .. _PEP 526: https://www.python.org/dev/peps/pep-0526/
    .. _PEP 484: https://www.python.org/dev/peps/pep-0484/
    
annotationwhitespace_before_indicatorr^   whitespace_after_indicatorr2   r3   c                 C   2   t t| d| j|t| d| j|t| d| j|dS )Nr&  r'  r%  )r&  r'  r%  )r$  r   r&  r   r'  r%  r7   r9   r9   r:   r;   y  s   z&Annotation._visit_and_replace_childrenNr=   default_indicatorc                 C   s   |d u rt d| j}t|tr|| nt|tr%|dkr$|d ntd|| | j| |	|  | j
| W d    d S 1 sKw   Y  d S )NzGMust specify a concrete default_indicator if default used on indicator.z->r^   r   )r   r&  r   r*   rB   r   rA   r   r'  re   r%  )r8   r=   r)  r&  r9   r9   r:   rD     s"   



"zAnnotation._codegen_implrk   )rG   rH   rI   rJ   rq   rK   r   r  r&  r   r*   r+   r   r'  r,   r;   r   r   r   rD   r9   r9   r9   r:   r$  [  s"   
 r$  c                   @   sN   e Zd ZU dZejeddZeed< de	dd fddZ
d	edd
fddZd
S )	ParamStarz
    A sentinel indicator on a :class:`Parameters` list to denote that the subsequent
    params are keyword-only args.

    This syntax is described in `PEP 3102`_.

    .. _PEP 3102: https://www.python.org/dev/peps/pep-3102/#specification
    r^   r6   r  r2   r3   c                 C   r4   )Nr  )r  )r*  r   r  r7   r9   r9   r:   r;     s   z%ParamStar._visit_and_replace_childrenr=   Nc                 C   r>   Nr  )rA   r  rB   rC   r9   r9   r:   rD     rE   zParamStar._codegen_impl)rG   rH   rI   rJ   r#   r   r+   r  rK   r,   r;   r   rD   r9   r9   r9   r:   r*    s
   
 
r*  c                   @   sd   e Zd ZU dZejZeeef e	d< e
dZee	d< dedd fddZdd
ededdfddZdS )
ParamSlashz
    A sentinel indicator on a :class:`Parameters` list to denote that the previous
    params are positional-only args.

    This syntax is described in `PEP 570`_.

    .. _PEP 570: https://www.python.org/dev/peps/pep-0570/#specification
    r  r0   r1   r2   r3   c                 C   r  )Nr  r1   )r  r1   )r,  r   r  r   r1   r7   r9   r9   r:   r;     s   
z&ParamSlash._visit_and_replace_childrenFr=   r  Nc                 C   sT   | d | j| | j}|tju r|r| d d S t|tr(|| d S d S )N/r  )rA   r1   rB   r  r   r  r   r#   r  r9   r9   r:   rD     s   

zParamSlash._codegen_implr  )rG   rH   rI   rJ   r   r  r  r   r#   rK   r+   r   r1   r*   r,   r;   r   ry   rD   r9   r9   r9   r:   r,    s   
 r,  c                	   @   s   e Zd ZU dZeed< dZee ed< e	j
Zeee	f ed< dZee ed< e	j
Zeee	f ed< e	j
Zeee	f ed< ed	Zeed
< ed	Zeed< dddZdedd fddZ		ddedee deddfddZdS )Paramz
    A positional or keyword argument in a :class:`Parameters` list. May contain an
    :class:`Annotation` and, in some cases, a ``default``.
    nameNr%  r   defaultr  r	  r0   r
  whitespace_after_paramr3   c                 C   sD   | j d u rt| jtrtdt| jtr| jdvr tdd S d S )Nz3Must have a default when specifying an AssignEqual.r0   r  **-Must specify either '', '*' or '**' for star.)r0  r   r   r   r   r	  r   rb   r9   r9   r:   rc     s   zParam._validater2   c                 C   sn   t | jt| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|t| d| j
|t| d| j|dS )	Nr
  r/  r%  r   r0  r  r1  )r	  r
  r/  r%  r   r0  r  r1  )r.  r	  r   r
  r/  r   r%  r   r   r0  r  r1  r7   r9   r9   r:   r;     s   

z!Param._visit_and_replace_childrenFr=   default_starr  c           	      C   s,  | | 0 | j}t|tr|d u rtd|}t|tr"|| | j| | j	| W d    n1 s8w   Y  | j
}|d urK|j|dd | j}|tju r^| jd ur^|d n
t|trh|| | j}|d urt|| | j}|tju r|r|d n
t|tr|| | j| d S )Nz=Must specify a concrete default_star if default used on star.r   )r)   = r  )re   r	  r   r   r   r   rA   r
  rB   r/  r%  r   r  r0  r   r  r#   r1  )	r8   r=   r5  r  r	  r%  r   r0  r  r9   r9   r:   rD     s<   







zParam._codegen_implrf   rw   )rG   rH   rI   rJ   r   rK   r%  r   r$  r   r  r   r   r   r0  rq   r  r#   r	  r   r+   r   r
  r*   r1  rc   r,   r;   r   ry   rD   r9   r9   r9   r:   r.    s.   
 
r.  c                   @   s   e Zd ZU dZdZee ed< ej	Z
eeeef ed< dZee ed< dZee ed< dZee ed< ej	Zeeef ed	< d
ee deddfddZd!ddZd!ddZd!ddZd!ddZd!ddZdedd fddZdefddZdeddfdd ZdS )"
Parametersz.
    A function or lambda parameter list.
    r9   paramsstar_argkwonly_paramsN
star_kwargposonly_paramsposonly_indvalssectionr3   c                C   sD   t |dkrd S |D ]}t|jtr|jdkrtd| dq
d S )Nr   r0   z"Expecting a star prefix of '' for z Param.)r`   r   r	  r   r   )r8   r>  r?  valr9   r9   r:   _validate_stars_sequencec  s   
z#Parameters._validate_stars_sequencec                 C   *   t | jtrt| jdkrtdd S d S )Nr   z;Must have at least one posonly param if ParamSlash is used.)r   r=  r,  r`   r<  r   rb   r9   r9   r:   _validate_posonly_indl  
   z Parameters._validate_posonly_indc                 C   rB  )Nr   z9Must have at least one kwonly param if ParamStar is used.)r   r9  r*  r`   r:  r   rb   r9   r9   r:   _validate_kwonly_starr  rD  z Parameters._validate_kwonly_starc                 C   s   d}g | j | jR D ]}|jr|sd}q|rtdq| j}t|tr-|jd ur-td| j}|d ur=|jd ur?tdd S d S )NFTzCCannot have param without defaults following a param with defaults.z!Cannot have default for star_arg.z#Cannot have default for star_kwarg.)r<  r8  r0  r   r9  r   r.  r;  )r8   seen_defaultparamr9  r;  r9   r9   r:   _validate_defaultsx  s$   zParameters._validate_defaultsc                 C   s   t | jdkr| j| jdd t | jdkr| j| jdd | j}t|tr5t|jtr5|jdkr5t	dt | j
dkrD| j| j
dd | j}|d urZt|jtr\|jdkr^t	d	d S d S d S )
Nr   r8  )r?  r<  r  z2Expecting a star prefix of '*' for star_arg Param.r:  r3  z5Expecting a star prefix of '**' for star_kwarg Param.)r`   r8  rA  r<  r9  r   r.  r	  r   r   r:  r;  )r8   r9  r;  r9   r9   r:   _validate_stars  s2   



zParameters._validate_starsc                 C   s$   |    |   |   |   d S rk   )rC  rE  rH  rI  rb   r9   r9   r:   rc     s   zParameters._validater2   c                 C   s\   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|dS )Nr<  r=  r8  r9  r:  r;  )r<  r=  r8  r9  r:  r;  )
r7  r   r<  r   r=  r8  r9  r:  r   r;  r7   r9   r9   r:   r;     s   

z&Parameters._visit_and_replace_childrenc                 C   s8   t | jdkr	dS t | jdkr| jd jdvrdS dS )a  
        Determine if Parameters need a space after the `lambda` keyword. Returns True
        iff it's safe to omit the space between `lambda` and these Parameters.

        See also `BaseExpression._safe_to_use_with_word_operator`.

        For example: `lambda*_: pass`
        r   F>   r  r3  T)r`   r<  r8  r	  rb   r9   r9   r:   _safe_to_join_with_lambda  s
   	z$Parameters._safe_to_join_with_lambdar=   c           
      C   s  | j }t|trt| jdk}nt|ttfrd}nd}t| jD ]\}}|j	|ddd q!|pBt| j
dkpBt| jdkpB| jd u}| j}t|trS|j	||d nt| jdkrg|rb|d n|d t| j
d	 }|p{t| jdkp{| jd u}t| j
D ]\}}|j	|d||k p|d qt|tr|r|d
 n$t|trt| jdkp| jd u}|j	|d|d n
t|tr|	| t| jd	 }| jd u}t| jD ]\}}|j	|d||k p|d q| j}	|	d ur|	j	|ddd d S d S )Nr   TFr0   )r5  r  r   z/, r-  r_   z*, r  r3  )r9  r   r   r`   r:  r.  r*  r!  r<  rB   r8  r;  r=  r,  rA   )
r8   r=   r9  starincludedr#  rG  more_valuesr=  	lastparamr;  r9   r9   r:   rD     s`   








zParameters._codegen_implrf   ) rG   rH   rI   rJ   r8  r   r.  rK   r   r  r9  r   r*  r:  r;  r   r<  r=  r,  r   rA  rC  rE  rH  rI  rc   r,   r;   ry   rJ  r   rD   r9   r9   r9   r:   r7  F  s"   
 
	




r7  c                       s   e Zd ZU dZeed< eed< eje	ddZ
eed< dZee ed< dZee ed	< ejZeeef ed
< dedef fddZd fddZdedd fddZdeddfddZ  ZS )Lambdaa:  
    A lambda expression that creates an anonymous function.

    ::

        Lambda(
            params=Parameters([Param(Name("arg"))]),
            body=Ellipsis(),
        )

    Represents the following code::

        lambda arg: ...

    Named functions statements are provided by :class:`FunctionDef`.
    r8  bodyr^   r6   colonr9   rh   ri   whitespace_after_lambdarr   r3   c                    s0   |t jkrt| jdkp| j|S t |S Nr   )rn   ro   r`   ri   rO  rt   r   rs   r   r9   r:   rt   <  s
   
z&Lambda._safe_to_use_with_word_operatorNc                    s   t t|   g | jj| jj| jj}| jj}t|tr#|	| | jj
}|d ur0|	| t|dkrX|D ]}|jd urCtdq8| j}t|trZ|jr\| j s^tdd S d S d S d S )Nr   z+Lambda params cannot have type annotations.z@Must have at least one space after lambda when specifying params)r   rN  rc   r8  r<  r:  r9  r   r.  appendr;  r`   r%  r   rQ  r*   r   rJ  )r8   
all_paramsr9  r;  rG  rQ  r   r9   r:   rc   C  sB   



zLambda._validater2   c                 C   s\   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|dS )Nrh   rQ  r8  rP  rO  ri   )rh   rQ  r8  rP  rO  ri   )
rN  r   rh   r   rQ  r   r8  rP  rO  ri   r7   r9   r9   r:   r;   d  s   
z"Lambda._visit_and_replace_childrenr=   c                 C   s   |  |] |d | j}t|tr>t| jjdkr8t| jjdkr8t| jjt	s8t| jj
dkr8| jjd u s=|d n
t|trH|| | j| | j| | j| W d    d S 1 sew   Y  d S )Nlambdar   r^   )rl   rA   rQ  r   r   r`   r8  r<  r9  r.  r:  r;  r*   rB   rP  rO  )r8   r=   rQ  r9   r9   r:   rD   p  s$   




"zLambda._codegen_implrf   )rG   rH   rI   rJ   r7  rK   rq   r"   r   r+   rP  rh   r   rY   ri   r[   r   r  rQ  r   r*   rn   ry   rt   rc   r,   r;   r   rD   r   r9   r9   r   r:   rN    s   
 !rN  c                   @   s   e Zd ZU dZeed< dZee ed< e	j
Zeee	f ed< e	j
Zeee	f ed< dZed ed	< edZeed
< edZeed< dddZdedd fddZddededdfddZdS )Argz
    A single argument to a :class:`Call`.

    This supports named keyword arguments in the form of ``keyword=value`` and variable
    argument expansion using ``*args`` or ``**kwargs`` syntax.
    ra   Nkeywordr   r  r0   r2  r	  r
  whitespace_after_argr3   c                 C   sT   | j d u rt| jtrtd| jdvrtd| jdv r&| j d ur(tdd S d S )Nz3Must have a keyword when specifying an AssignEqual.r2  r4  )r  r3  z-Cannot specify a star and a keyword together.)rW  r   r   r   r   r	  rb   r9   r9   r:   rc     s   
zArg._validater2   c                 C   s`   t | jt| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|t| d| j
|dS )Nr
  rW  r   ra   r  rX  )r	  r
  rW  r   ra   r  rX  )rV  r	  r   r
  r   rW  r   r   ra   r  rX  r7   r9   r9   r:   r;     s   

zArg._visit_and_replace_childrenFr=   r  c                 C   s   | | C || j | j| | j}|d ur|| | j}|tju r1| jd ur1|d n
t	|t
r;|| | j| W d    n1 sKw   Y  | j}|tju r`|r`|d n
t	|trj|| | j| d S )Nr6  r  )re   rA   r	  r
  rB   rW  r   r   r  r   r   ra   r  r#   rX  )r8   r=   r  rW  r   r  r9   r9   r:   rD     s&   




zArg._codegen_implrf   r  )rG   rH   rI   rJ   rq   rK   rW  r   r   r   r  r   r   r   r  r#   r	  r   r+   r   r
  r*   rX  rc   r,   r;   r   ry   rD   r9   r9   r9   r:   rV    s   
 	

rV  c                       s   e Zd ZU dZdZdZee ed< deddfddZ	dede
eegeegdf f  fd	d
Zdede
eegeegeegdf f f  fddZd fddZ  ZS )_BaseExpressionWithArgsz
    Arguments are complicated enough that we can't represent them easily
    in typing. So, we have common validation functions here.
    r9   argsargr3   Nc                 C   s6   |j durdS |jdkrdS |jdkrtdtd)z[
        Validates that we only have a mix of "keyword=arg" and "**arg" expansion.
        Nr3  r  zICannot have iterable argument unpacking after keyword argument unpacking.zACannot have positional argument after keyword argument unpacking.)rW  r	  r   r8   r[  r9   r9   r:   _check_kwargs_or_keywords  s   


z1_BaseExpressionWithArgs._check_kwargs_or_keywordsc                 C   s4   |j durdS |jdkr| jS |jdkrdS td)zZ
        Validates that we only have a mix of "*arg" expansion and "keyword=arg".
        Nr3  r  z7Cannot have positional argument after keyword argument.)rW  r	  r]  r   r\  r9   r9   r:   _check_starred_or_keywords  s   


z2_BaseExpressionWithArgs._check_starred_or_keywordsc                 C   s2   |j dur| jS |jdkr| jS |jdkrdS dS )z\
        Validates that we only have a mix of positional args and "*arg" expansion.
        Nr3  r  )rW  r^  r	  r]  r\  r9   r9   r:   _check_positional	  s   


z)_BaseExpressionWithArgs._check_positionalc                    s,   t    | j}| jD ]}||p|}qd S rk   )r   rc   r_  rZ  )r8   	validatorr[  r   r9   r:   rc   ,	  s
   


z!_BaseExpressionWithArgs._validaterf   )rG   rH   rI   rJ   rm   rZ  r   rV  rK   r]  r   r   r^  r_  rc   r   r9   r9   r   r:   rY    s    
 
$
rY  c                       s   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< dZee ed< edZeed< edZeed	< d
edef fddZdedd fddZdeddfddZ  ZS )Callz
    An expression representing a function call, such as ``do_math(1, 2)`` or
    ``picture.post_on_instagram()``.

    Function calls consist of a function name and a sequence of arguments wrapped in
    :class:`Arg` nodes.
    funcr9   rZ  rh   ri   r0   whitespace_after_funcwhitespace_before_argsrr   r3   c                    r  )z
        Calls have a close paren on the right side regardless of whether they're
        parenthesized as a whole. As a result, they are safe to use directly against
        an adjacent node to the right.
        TF)rn   ro   r   ra  rt   rp   rb  rs   r   r9   r:   rt   ]	  s   

z$Call._safe_to_use_with_word_operatorr2   c                 C   s\   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   rb  rc  rd  rZ  ri   )rh   rb  rc  rd  rZ  ri   )	ra  r   rh   r   rb  rc  rd  rZ  ri   r7   r9   r9   r:   r;   k	     

z Call._visit_and_replace_childrenr=   Nc                 C   s   |  |? | j| | j| |d | j| t| jd }t| jD ]\}}|j|||kd q)|d W d    d S 1 sGw   Y  d S )NrZ   r_   r   r\   )	rl   rb  rB   rc  rA   rd  r`   rZ  r!  )r8   r=   lastargr#  r[  r9   r9   r:   rD   y	  s   
"zCall._codegen_impl)rG   rH   rI   rJ   rq   rK   rZ  r   rV  rh   rY   ri   r[   r+   r   rc  r*   rd  rn   ry   rt   r,   r;   r   rD   r   r9   r9   r   r:   ra  =	  s   
 
ra  c                       s   e Zd ZU dZeed< dZee ed< dZ	ee
 ed< edZeed< d fd
dZdedd fddZdedd	fddZ  ZS )Awaitz
    An await expression. Await expressions are only valid inside the body of an
    asynchronous :class:`FunctionDef` or (as of Python 3.7) inside of an asynchronous
    :class:`GeneratorExp` nodes.
    r   r9   rh   ri   r^   whitespace_after_awaitr3   Nc                    s4   t t|   | jjr| jtjst	dd S d S )Nz(Must have at least one space after await)
r   rg  rc   rh  r   r   rt   rn   rp   r   rb   r   r9   r:   rc   	  s   zAwait._validater2   c              	   C   r   )Nrh   rh  r   ri   )rh   rh  r   ri   )rg  r   rh   r   rh  r   ri   r7   r9   r9   r:   r;   	     
z!Await._visit_and_replace_childrenr=   c                 C   sR   |  | |d | j| | j| W d    d S 1 s"w   Y  d S )Nawait)rl   rA   rh  rB   r   rC   r9   r9   r:   rD   	  s
   
"zAwait._codegen_implrf   )rG   rH   rI   rJ   rq   rK   rh   r   rY   ri   r[   r+   r   rh  r*   rc   r,   r;   r   rD   r   r9   r9   r   r:   rg  	  s   
 
rg  c                       s   e Zd ZU dZeed< eed< eed< dZee ed< dZ	ee
 ed< edZeed	< edZeed
< edZeed< edZeed< dedefddZd fddZdedd fddZdeddfddZ  ZS )IfExpz
    An if expression of the form ``body if test else orelse``.

    If statements are provided by :class:`If` and :class:`Else` nodes.
    testrO  orelser9   rh   ri   r^   whitespace_before_ifwhitespace_after_ifwhitespace_before_elsewhitespace_after_elserr   r3   c                 C   s"   |t jkr| j|S | j|S rk   )rn   rp   rO  rt   rm  rs   r9   r9   r:   rt   	  s   
z%IfExp._safe_to_use_with_word_operatorNc                    s   t t|   | jjr| jtjst	d| j
jr%| jtjs%t	d| jjr4| jtjs4t	d| jjrC| jtjsEt	dd S d S )N1Must have at least one space before 'if' keyword.0Must have at least one space after 'if' keyword.z3Must have at least one space before 'else' keyword.z2Must have at least one space after 'else' keyword.)r   rk  rc   rn  r   rO  rt   rn   ro   r   ro  rl  rp   rp  rq  rm  rb   r   r9   r:   rc   	  s<   zIfExp._validater2   c                 C      t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|t| d| j
|t| d	| j|d
	S )Nrh   rO  rn  ro  rl  rp  rq  rm  ri   )	rh   rO  rn  ro  rl  rp  rq  rm  ri   )rk  r   rh   r   rO  rn  ro  rl  rp  rq  rm  ri   r7   r9   r9   r:   r;   	  s&   



z!IfExp._visit_and_replace_childrenr=   c                 C   s   |  |= | j| | j| |d | j| | j| | j| |d | j| | j	| W d    d S 1 sEw   Y  d S )Nifelse)
rl   rO  rB   rn  rA   ro  rl  rp  rq  rm  rC   r9   r9   r:   rD   
  s   

"zIfExp._codegen_implrf   )rG   rH   rI   rJ   rq   rK   rh   r   rY   ri   r[   r+   r   rn  r*   ro  rp  rq  rn   ry   rt   rc   r,   r;   r   rD   r   r9   r9   r   r:   rk  	  s   
 !rk  c                   @   sv   e Zd ZU dZeed< ejZe	e
ef ed< edZe
ed< ddd	Zd
edd fddZddededdfddZdS )FromzD
    A ``from x`` stanza in a :class:`Yield` or :class:`Raise`.
    itemwhitespace_before_fromr^   whitespace_after_fromr3   Nc                 C   s6   t | jtr| jjr| jtjstdd S d S d S )Nz2Must have at least one space after 'from' keyword.)	r   rz  r*   r   rx  rt   rn   rp   r   rb   r9   r9   r:   rc   3
  s   
zFrom._validater2   c                 C   r(  )Nry  rz  rx  )ry  rz  rx  )rw  r   ry  r   rz  rx  r7   r9   r9   r:   r;   =
     

z From._visit_and_replace_childrenr0   r=   default_spacec                 C   sx   | j }t|tr|| n|| ||  |d | j| | j| W d    d S 1 s5w   Y  d S )Nfrom)ry  r   r*   rB   rA   re   rz  rx  )r8   r=   r|  ry  r9   r9   r:   rD   H
  s   


"zFrom._codegen_implrf   )r0   )rG   rH   rI   rJ   rq   rK   r   r  ry  r   r*   r+   r   rz  rc   r,   r;   r   r   rD   r9   r9   r9   r:   rw  !
  s   
 

rw  c                       s   e Zd ZU dZdZeeeef  e	d< dZ
ee e	d< dZee e	d< ejZeeef e	d< d fd	d
Zdedd fddZdeddfddZ  ZS )Yielda  
    A yield expression similar to ``yield x`` or ``yield from fun()``.

    To learn more about the ways that yield can be used in generators, refer to
    `Python's language reference
    <https://docs.python.org/3/reference/expressions.html#yieldexpr>`__.
    Nra   r9   rh   ri   whitespace_after_yieldr3   c                    sj   t t|   | j}t|tr-|jr/| j}t|trt	dt|t
r1|tjs3t	dd S d S d S d S )Nz3Must have at least one space after 'yield' keyword.)r   r~  rc   r  r   r*   r   ra   rw  r   rq   rt   rn   rp   )r8   r  ra   r   r9   r:   rc   m
  s*   

	zYield._validater2   c              	   C   s@   t t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   r  ra   ri   )rh   r  ra   ri   )r~  r   rh   r   r  r   ra   ri   r7   r9   r9   r:   r;   
  ri  z!Yield._visit_and_replace_childrenr=   c                 C   s   |  |O |d | j}t|tr|| n
| jd ur#|d | j}t|tr3|j|dd n|d urD|| W d    d S W d    d S W d    d S 1 sWw   Y  d S )Nyieldr^   r0   )r|  )rl   rA   r  r   r*   rB   ra   rw  )r8   r=   r  ra   r9   r9   r:   rD   
  s"   




"zYield._codegen_implrf   )rG   rH   rI   rJ   ra   r   r   rq   rw  rK   rh   r   rY   ri   r[   r   r  r  r*   rc   r,   r;   r   rD   r   r9   r9   r   r:   r~  U
  s   
 

r~  c                
   @   sx   e Zd ZU dZdZeed< ejZ	e
eef ed< 		ddededed	d
fddZe		ddededed	d
fddZd
S )_BaseElementImplzO
    An internal base class for :class:`Element` and :class:`DictElement`.
    r9   ra   r  Fr=   r  default_comma_whitespacer3   Nc                 C   sP   | j }|tju r|r|r|d dS |d dS t|tr&|| dS dS )zP
        Called by `_codegen_impl` in subclasses to generate the comma.
        r  ,N)r  r   r  rA   r   r#   rB   )r8   r=   r  r  r  r9   r9   r:   _codegen_comma
  s   	
z_BaseElementImpl._codegen_commac                 C   s   d S rk   r9   r8   r=   r  r  r9   r9   r:   rD   
  r   z_BaseElementImpl._codegen_implFF)rG   rH   rI   rJ   rm   rq   rK   r   r  r  r   r#   r   ry   r  r   rD   r9   r9   r9   r:   r  
  s8   
 
r  c                   @   rz   )BaseElementzo
    An element of a literal list, tuple, or set. For elements of a literal dict, see
    BaseDictElement.
    r9   Nr|   r9   r9   r9   r:   r  
  r   r  c                   @   rz   )BaseDictElementzc
    An element of a literal dict. For elements of a list, tuple, or set, see
    BaseElement.
    r9   Nr|   r9   r9   r9   r:   r  
  r   r  c                	   @   sb   e Zd ZU dZeed< ejZe	e
ef ed< dedd fddZ		dd	ed
ededdfddZdS )Elementz
    A simple value in a literal :class:`List`, :class:`Tuple`, or :class:`Set`.
    These a literal collection may also contain a :class:`StarredElement`.

    If you're using a literal :class:`Dict`, see :class:`DictElement` instead.
    ra   r  r2   r3   c                 C   r  )Nra   r  )ra   r  )r  r   ra   r   r  r7   r9   r9   r:   r;   
  r   z#Element._visit_and_replace_childrenFr=   r  r  Nc                 C   sH   | |  | j| W d    n1 sw   Y  | ||| d S rk   )re   ra   rB   r  r  r9   r9   r:   rD   
  s   zElement._codegen_implr  )rG   rH   rI   rJ   rq   rK   r   r  r  r   r#   r,   r;   r   ry   rD   r9   r9   r9   r:   r  
  s    
 	r  c                	   @   s   e Zd ZU dZeed< eed< ejZe	e
ef ed< edZeed< edZeed< d	ed
d fddZ		ddededed
dfddZdS )DictElementa  
    A simple ``key: value`` pair that represents a single entry in a literal
    :class:`Dict`. :class:`Dict` nodes may also contain a
    :class:`StarredDictElement`.

    If you're using a literal :class:`List`, :class:`Tuple`, or :class:`Set`,
    see :class:`Element` instead.
    keyra   r  r0   whitespace_before_colonr^   whitespace_after_colonr2   r3   c              
   C   sN   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nr  r  r  ra   r  )r  r  r  ra   r  )r  r   r  r  r  ra   r   r  r7   r9   r9   r:   r;     s   

z'DictElement._visit_and_replace_childrenFr=   r  r  Nc                 C   sv   | | % | j| | j| |d | j| | j| W d    n1 s-w   Y  | ||| d S r  )re   r  rB   r  rA   r  ra   r  r  r9   r9   r:   rD     s   
zDictElement._codegen_implr  )rG   rH   rI   rJ   rq   rK   r   r  r  r   r#   r+   r   r  r*   r  r,   r;   r   ry   rD   r9   r9   r9   r:   r  
  s&   
 	r  c                	   @   s   e Zd ZU dZeed< ejZe	e
ef ed< dZee ed< dZee ed< edZeed< d	ed
d fddZ		ddededed
dfddZdS )StarredElementa3  
    A starred ``*value`` element that expands to represent multiple values in a literal
    :class:`List`, :class:`Tuple`, or :class:`Set`.

    If you're using a literal :class:`Dict`, see :class:`StarredDictElement` instead.

    If this node owns parenthesis, those parenthesis wrap the leading asterisk, but not
    the trailing comma. For example::

        StarredElement(
            cst.Name("el"),
            comma=cst.Comma(),
            lpar=[cst.LeftParen()],
            rpar=[cst.RightParen()],
        )

    will generate::

        (*el),
    ra   r  r9   rh   ri   r0   whitespace_before_valuer2   r3   c              
   C   sN   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   r  ra   ri   r  )rh   r  ra   ri   r  )	r  r   rh   r   r  ra   ri   r   r  r7   r9   r9   r:   r;   L  s   
z*StarredElement._visit_and_replace_childrenFr=   r  r  Nc                 C   s^   |  | |d | j| | j| W d    n1 s!w   Y  | ||| d S r+  )rl   rA   r  rB   ra   r  r  r9   r9   r:   rD   W     
zStarredElement._codegen_implr  )rG   rH   rI   rJ   rq   rK   r   r  r  r   r#   rh   r   rY   ri   r[   r+   r   r  r*   r,   r;   r   ry   rD   r9   r9   r9   r:   r  '  s&   
 r  c                	   @   st   e Zd ZU dZeed< ejZe	e
ef ed< edZeed< dedd fdd	Z	
	
ddedededdfddZdS )StarredDictElementa  
    A starred ``**value`` element that expands to represent multiple values in a literal
    :class:`Dict`.

    If you're using a literal :class:`List`, :class:`Tuple`, or :class:`Set`,
    see :class:`StarredElement` instead.

    Unlike :class:`StarredElement`, this node does not own left or right parenthesis,
    but the ``value`` field may still contain parenthesis. This is due to some
    asymmetry in Python's grammar.
    ra   r  r0   r  r2   r3   c                 C   s2   t t| d| j|t| d| j|t| d| j|dS )Nr  ra   r  )r  ra   r  )r  r   r  ra   r   r  r7   r9   r9   r:   r;   {  s   
z.StarredDictElement._visit_and_replace_childrenFr=   r  r  Nc                 C   s^   | |  |d | j| | j| W d    n1 s!w   Y  | ||| d S )Nr3  )re   rA   r  rB   ra   r  r  r9   r9   r:   rD     r  z StarredDictElement._codegen_implr  )rG   rH   rI   rJ   rq   rK   r   r  r  r   r#   r+   r   r  r*   r,   r;   r   ry   rD   r9   r9   r9   r:   r  d  s"   
 r  c                       s   e Zd ZU dZee ed< edd dZee	 ed< edd dZ
ee ed< d	ed
ef fddZd fddZded
d fddZded
dfddZ  ZS )Tuplea)  
    An immutable literal tuple. Tuples are often (but not always) parenthesized.

    ::

        Tuple([
            Element(Integer("1")),
            Element(Integer("2")),
            StarredElement(Name("others")),
        ])

    generates the following code::

        (1, 2, *others)
    elementsc                   C      t  fS rk   rY   r9   r9   r9   r:   <lambda>      zTuple.<lambda>default_factoryrh   c                   C   r  rk   r[   r9   r9   r9   r:   r    r  ri   rr   r3   c                    sz   t t| |r
dS | j}|tjkr.|d }t|jtp-t|t	o't
|jdkp-|j|S |d }t|t	p<|j|S )NTr   r   )r   r  rt   r  rn   ro   r   r  r#   r  r`   ri   ra   )r8   rr   r  last_elementfirst_elementr   r9   r:   rt     s"   


	
z%Tuple._safe_to_use_with_word_operatorNc                    s:   t t|   t| jdkrt| jdkrtdd S d S )Nr   z3A zero-length tuple must be wrapped in parentheses.)r   r  rc   r`   r  rh   r   rb   r   r9   r:   rc     s   zTuple._validater2   c                 C   2   t t| d| j|t| d| j|t| d| j|dS )Nrh   r  ri   )rh   r  ri   )r  r   rh   r  ri   r7   r9   r9   r:   r;     s
   z!Tuple._visit_and_replace_childrenr=   c                 C   s   |  |< | j}t|dkr|d j|ddd nt|D ]\}}|j||t|d k dd qW d    d S W d    d S 1 sDw   Y  d S )Nr_   r   TFr  r  )rl   r  r`   rB   r!  r8   r=   r  idxelr9   r9   r:   rD     s    "zTuple._codegen_implrf   )rG   rH   rI   rJ   r   r  rK   r   rh   rY   ri   r[   rn   ry   rt   rc   r,   r;   r   rD   r   r9   r9   r   r:   r    s   
 r  c                   @      e Zd ZU dZdZe Zeed< e	 Z
e	ed< dZee ed< dZee ed< dedefd	d
Zededed fddZdS )BaseListzv
    A base class for :class:`List` and :class:`ListComp`, which both result in a list
    object when evaluated.
    r9   r  r  rh   ri   rr   r3   c                 C   r   r   r9   rs   r9   r9   r:   rt     r   z(BaseList._safe_to_use_with_word_operatorr=   rj   c                 c   $    | j | d V  | j| d S rk   )r  rB   r  rC   r9   r9   r:   _bracketize     zBaseList._bracketizeN)rG   rH   rI   rJ   rm   r/   r   r  rK   rM   r  rh   r   rY   ri   r[   rn   ry   rt   r   r   r   r  r9   r9   r9   r:   r    s   
 r  c                   @      e Zd ZU dZee ed< e Z	eed< e
 Ze
ed< dZee ed< dZee ed< ded	d fd
dZded	dfddZdS )Lista;  
    A mutable literal list.

    ::

        List([
            Element(Integer("1")),
            Element(Integer("2")),
            StarredElement(Name("others")),
        ])

    generates the following code::

        [1, 2, *others]

    List comprehensions are represented with a :class:`ListComp` node.
    r  r  r  r9   rh   ri   r2   r3   c              
   C   N   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   r  r  r  ri   )rh   r  r  r  ri   )r  r   rh   r   r  r  r  ri   r7   r9   r9   r:   r;   "  r   z List._visit_and_replace_childrenr=   Nc              	   C      |  |@ | |" | j}t|D ]\}}|j||t|d k dd qW d    n1 s0w   Y  W d    d S W d    d S 1 sHw   Y  d S Nr_   Tr  )rl   r  r  r!  rB   r`   r  r9   r9   r:   rD   +     PzList._codegen_impl)rG   rH   rI   rJ   r   r  rK   r/   r   r  rM   r  rh   rY   ri   r[   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 	r  c                   @   r  )_BaseSetOrDicta  
    An abstract base class for :class:`BaseSet` and :class:`BaseDict`.

    Literal sets and dicts are syntactically similar (hence this shared base class), but
    are semantically different. This base class is an implementation detail and
    shouldn't be exported.
    r9   lbracerbracerh   ri   rr   r3   c                 C   r   r   r9   rs   r9   r9   r:   rt   I  r   z._BaseSetOrDict._safe_to_use_with_word_operatorr=   rj   c                 c   r  rk   )r  rB   r  rC   r9   r9   r:   	_braceizeM  r  z_BaseSetOrDict._braceizeN)rG   rH   rI   rJ   rm   rU   r   r  rK   rW   r  rh   r   rY   ri   r[   rn   ry   rt   r   r   r   r  r9   r9   r9   r:   r  6  s   
 r  c                   @   rz   )BaseSetz}
    An abstract base class for :class:`Set` and :class:`SetComp`, which both result in
    a set object when evaluated.
    r9   Nr|   r9   r9   r9   r:   r  T  r   r  c                       s   e Zd ZU dZee ed< e Z	eed< e
 Ze
ed< dZee ed< dZee ed< d fd
dZdedd fddZdedd	fddZ  ZS )Seta7  
    A mutable literal set.

    ::

        Set([
            Element(Integer("1")),
            Element(Integer("2")),
            StarredElement(Name("others")),
        ])

    generates the following code::

        {1, 2, *others}

    Set comprehensions are represented with a :class:`SetComp` node.
    r  r  r  r9   rh   ri   r3   Nc                    s(   t t|   t| jdkrtdd S )Nr   zzA literal set must have at least one element. A zero-element set would be syntatically ambiguous with an empty dict, `{}`.)r   r  rc   r`   r  r   rb   r   r9   r:   rc   ~  s   zSet._validater2   c              
   C   r  Nrh   r  r  r  ri   )rh   r  r  r  ri   )r  r   rh   r   r  r  r  ri   r7   r9   r9   r:   r;     r   zSet._visit_and_replace_childrenr=   c              	   C   r  r  rl   r  r  r!  rB   r`   r  r9   r9   r:   rD     r  zSet._codegen_implrf   )rG   rH   rI   rJ   r   r  rK   rU   r   r  rW   r  rh   rY   ri   r[   rc   r,   r;   r   rD   r   r9   r9   r   r:   r  ]  s   
 		r  c                   @   rz   )BaseDictz
    An abstract base class for :class:`Dict` and :class:`DictComp`, which both result in
    a dict object when evaluated.
    r9   Nr|   r9   r9   r9   r:   r    r   r  c                   @   r  )Dicta  
    A literal dictionary. Key-value pairs are stored in ``elements`` using
    :class:`DictElement` nodes.

    It's possible to expand one dictionary into another, as in ``{k: v, **expanded}``.
    Expanded elements are stored as :class:`StarredDictElement` nodes.

    ::

        Dict([
            DictElement(Name("k1"), Name("v1")),
            DictElement(Name("k2"), Name("v2")),
            StarredDictElement(Name("expanded")),
        ])

    generates the following code::

        {k1: v1, k2: v2, **expanded}
    r  r  r  r9   rh   ri   r2   r3   c              
   C   r  r  )r  r   rh   r   r  r  r  ri   r7   r9   r9   r:   r;     r   z Dict._visit_and_replace_childrenr=   Nc              	   C   r  r  r  r  r9   r9   r:   rD     r  zDict._codegen_impl)rG   rH   rI   rJ   r   r  rK   rU   r   r  rW   r  rh   rY   ri   r[   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 	r  c                   @   s   e Zd ZU dZeed< eed< dZed ed< dZ	e
d  ed< dZe
e ed	< ed
Zeed< ed
Zeed< ed
Zeed< ed
Zeed< dddZdedd fddZdeddfddZdS )CompFora  
    One ``for`` clause in a :class:`BaseComp`, or a nested hierarchy of
    ``for`` clauses.

    Nested loops in comprehensions are difficult to get right, but they can be thought
    of as a flat representation of nested clauses.

    ``elt for a in b for c in d if e`` can be thought of as::

        for a in b:
            for c in d:
                if e:
                    yield elt

    And that would form the following CST::

        ListComp(
            elt=Name("elt"),
            for_in=CompFor(
                target=Name("a"),
                iter=Name("b"),
                ifs=[],
                inner_comp_for=CompFor(
                    target=Name("c"),
                    iter=Name("d"),
                    ifs=[
                        CompIf(
                            test=Name("e"),
                        ),
                    ],
                ),
            ),
        )

    Normal ``for`` statements are provided by :class:`For`.
    r   iterr9   CompIfifsNinner_for_inasynchronousr^   rN   whitespace_after_forwhitespace_before_inwhitespace_after_inr3   c                 C   s   | j jr| jtjstd| jjr| jtjstd| j	jr-| j
tjs-td| j
}| jD ]}|jjrC|tjsCtd|j}q3| j}|d urg|jjri|tjsk|jr]dnd}td| dd S d S d S )	Nz1Must have at least one space after 'for' keyword.z1Must have at least one space before 'in' keyword.z0Must have at least one space after 'in' keyword.rr  rd   for%Must have at least one space before '
' keyword.)r  r   r   rt   rn   rp   r   r  ro   r  r  r  rN   rl  r  r  )r8   	prev_expr	if_clauser  rW  r9   r9   r:   rc   '  sX   


zCompFor._validater2   c                 C   s   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j	|t
| d| j|t| d	| j|d
	S )NrN   r  r  r   r  r  r  r  r  )	rN   r  r  r   r  r  r  r  r  )r  r   rN   r   r  r  r   r  r  r  r   r  r  r7   r9   r9   r:   r;   X  s.   





z#CompFor._visit_and_replace_childrenr=   c                 C   s   | j | | j}|d ur|| |d | j| | j| | j| |d | j| | j| | j	}|D ]}|| q?| j
}|d urU|| d S d S )Nr  in)rN   rB   r  rA   r  r   r  r  r  r  r  )r8   r=   r  r  r  r  r9   r9   r:   rD   q  s$   


zCompFor._codegen_implrf   )rG   rH   rI   rJ   r{   rK   rq   r  r   r  r   r  r]   r+   r   rN   r*   r  r  r  rc   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 )
1r  c                   @   sl   e Zd ZU dZeed< edZe	ed< edZ
e	ed< ddd	Zd
edd fddZdeddfddZdS )r  z
    A conditional clause in a :class:`CompFor`, used as part of a generator or
    comprehension expression.

    If the ``test`` fails, the current element in the :class:`CompFor` will be skipped.
    rl  r^   rN   whitespace_before_testr3   Nc                 C   s&   | j jr| jtjstdd S d S )Nrs  )r  r   rl  rt   rn   rp   r   rb   r9   r9   r:   rc     s   zCompIf._validater2   c                 C   r  )NrN   r  rl  )rN   r  rl  )r  r   rN   r  rl  r7   r9   r9   r:   r;     r{  z"CompIf._visit_and_replace_childrenr=   c                 C   s2   | j | |d | j| | j| d S )Nru  )rN   rB   rA   r  rl  rC   r9   r9   r:   rD     s   
zCompIf._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r+   r   rN   r*   r  rc   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 
r  c                   @   s   e Zd ZU dZdZeed< dS )BaseCompz
    A base class for all comprehension and generator expressions, including
    :class:`GeneratorExp`, :class:`ListComp`, :class:`SetComp`, and :class:`DictComp`.
    r9   for_inN)rG   rH   rI   rJ   rm   r  rK   r9   r9   r9   r:   r    s   
 r  c                       s8   e Zd ZU dZdZeed< eed< d	 fddZ  Z	S )
BaseSimpleCompz
    The base class for :class:`ListComp`, :class:`SetComp`, and :class:`GeneratorExp`.
    :class:`DictComp` is not a :class:`BaseSimpleComp`, because it uses ``key`` and
    ``value``.
    r9   eltr  r3   Nc                    P   t t|   | j}|jjr$| jtj	s&|j
rdnd}td| dd S d S Nrd   r  r  r  )r   r  rc   r  rN   r   r  rt   rn   ro   r  r   r8   r  rW  r   r9   r:   rc        
zBaseSimpleComp._validaterf   )
rG   rH   rI   rJ   rm   rq   rK   r  rc   r   r9   r9   r   r:   r    s   
 r  c                   @   s   e Zd ZU dZeed< eed< edd dZe	e
 ed< edd dZe	e ed	< d
edefddZdedd fddZdeddfddZdS )GeneratorExpz
    A generator expression. ``elt`` represents the value yielded for each item in
    :attr:`CompFor.iter`.

    All ``for ... in ...`` and ``if ...`` clauses are stored as a recursive
    :class:`CompFor` data structure inside ``for_in``.
    r  r  c                   C   r  rk   r  r9   r9   r9   r:   r    r  zGeneratorExp.<lambda>r  rh   c                   C   r  rk   r  r9   r9   r9   r:   r    r  ri   rr   r3   c                 C   r   r   r9   rs   r9   r9   r:   rt     s   z,GeneratorExp._safe_to_use_with_word_operatorr2   c              	   C   r   )Nrh   r  r  ri   )rh   r  r  ri   )r  r   rh   r   r  r  ri   r7   r9   r9   r:   r;     r   z(GeneratorExp._visit_and_replace_childrenr=   Nc                 C   r   rk   )rl   r  rB   r  rC   r9   r9   r:   rD   
  r   zGeneratorExp._codegen_impl)rG   rH   rI   rJ   rq   rK   r  r   rh   r   rY   ri   r[   rn   ry   rt   r,   r;   r   rD   r9   r9   r9   r:   r    s   
 	r  c                   @      e Zd ZU dZeed< eed< e Z	eed< e
 Ze
ed< dZee ed< dZee ed< d	ed
d fddZded
dfddZdS )ListCompz
    A list comprehension. ``elt`` represents the value stored for each item in
    :attr:`CompFor.iter`.

    All ``for ... in ...`` and ``if ...`` clauses are stored as a recursive
    :class:`CompFor` data structure inside ``for_in``.
    r  r  r  r  r9   rh   ri   r2   r3   c                 C   \   t t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|t| d| j|dS )Nrh   r  r  r  r  ri   )rh   r  r  r  r  ri   )	r  r   rh   r   r  r  r  r  ri   r7   r9   r9   r:   r;   *     z$ListComp._visit_and_replace_childrenr=   Nc              	   C      |  |2 | | | j| | j| W d    n1 s"w   Y  W d    d S W d    d S 1 s:w   Y  d S rk   )rl   r  r  rB   r  rC   r9   r9   r:   rD   4     PzListComp._codegen_impl)rG   rH   rI   rJ   rq   rK   r  r/   r   r  rM   r  rh   r   rY   ri   r[   r,   r;   r   rD   r9   r9   r9   r:   r       
 	
r  c                   @   r  )SetCompz
    A set comprehension. ``elt`` represents the value stored for each item in
    :attr:`CompFor.iter`.

    All ``for ... in ...`` and ``if ...`` clauses are stored as a recursive
    :class:`CompFor` data structure inside ``for_in``.
    r  r  r  r  r9   rh   ri   r2   r3   c                 C   r  )Nrh   r  r  r  r  ri   )rh   r  r  r  r  ri   )	r  r   rh   r   r  r  r  r  ri   r7   r9   r9   r:   r;   T  r  z#SetComp._visit_and_replace_childrenr=   Nc              	   C   r  rk   )rl   r  r  rB   r  rC   r9   r9   r:   rD   ^  r  zSetComp._codegen_impl)rG   rH   rI   rJ   rq   rK   r  rU   r   r  rW   r  rh   r   rY   ri   r[   r,   r;   r   rD   r9   r9   r9   r:   r  :  r  r  c                       s   e Zd ZU dZeed< eed< eed< e Z	eed< e
 Ze
ed< dZee ed< dZee ed	< ed
Zeed< edZeed< d fddZdedd fddZdeddfddZ  ZS )DictCompz
    A dictionary comprehension. ``key`` and ``value`` represent the dictionary entry
    evaluated for each item.

    All ``for ... in ...`` and ``if ...`` clauses are stored as a recursive
    :class:`CompFor` data structure inside ``for_in``.
    r  ra   r  r  r  r9   rh   ri   r0   r  r^   r  r3   Nc                    r  r  )r   r  rc   r  rN   r   ra   rt   rn   ro   r  r   r  r   r9   r:   rc     r  zDictComp._validater2   c                 C   rt  )Nrh   r  r  r  r  ra   r  r  ri   )	rh   r  r  r  r  ra   r  r  ri   )r  r   rh   r   r  r  r  r  ra   r  r  ri   r7   r9   r9   r:   r;     s   

z$DictComp._visit_and_replace_childrenr=   c              	   C   s   |  |I | |+ | j| | j| |d | j| | j| | j| W d    n1 s9w   Y  W d    d S W d    d S 1 sQw   Y  d S r  )	rl   r  r  rB   r  rA   r  ra   r  rC   r9   r9   r:   rD     s   
PzDictComp._codegen_implrf   )rG   rH   rI   rJ   rq   rK   r  rU   r   r  rW   r  rh   r   rY   ri   r[   r+   r  r*   r  rc   r,   r;   r   rD   r   r9   r9   r   r:   r  d  s   
 	r  c                   @   s   e Zd ZU dZeed< eed< dZee ed< dZ	ee
 ed< edZeed< edZeed	< d
edd fddZdedefddZdeddfddZdS )	NamedExpra=  
    An expression that is also an assignment, such as ``x := y + z``. Affectionately
    known as the walrus operator, this expression allows you to make an assignment
    inside an expression. This greatly simplifies loops::

        while line := read_some_line_or_none():
            do_thing_with_line(line)
    r   ra   r9   rh   ri   r^   whitespace_before_walruswhitespace_after_walrusr2   r3   c                 C   r  )Nrh   r   r  r  ra   ri   )rh   r   r  r  ra   ri   )	r  r   rh   r   r   r  r  ra   ri   r7   r9   r9   r:   r;     re  z%NamedExpr._visit_and_replace_childrenrr   c                 C   s>   |t jkrt| jdkp| j|S t| jdkp| j|S rR  )rn   ro   r`   ri   ra   rt   rh   r   rs   r9   r9   r:   rt     s   
z)NamedExpr._safe_to_use_with_word_operatorr=   Nc                 C   sj   |  |& | j| | j| |d | j| | j| W d    d S 1 s.w   Y  d S )Nz:=)rl   r   rB   r  rA   r  ra   rC   r9   r9   r:   rD     s   
"zNamedExpr._codegen_impl)rG   rH   rI   rJ   rq   rK   rh   r   rY   ri   r[   r+   r   r  r*   r  r,   r;   rn   ry   rt   r   rD   r9   r9   r9   r:   r    s   
 
	r  )r   abcr   r   astr   
contextlibr   dataclassesr   r   enumr   r	   tokenizer
   r   r   r   r   r   typingr   r   r   r   r   r   libcst._add_slotsr   libcst._maybe_sentinelr   libcst._nodes.baser   r   r   libcst._nodes.internalr   r   r   r   r   libcst._nodes.opr   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   libcst._nodes.whitespacer*   r+   libcst._visitorsr,   r/   rM   rU   rW   rY   r[   r]   rg   rn   rq   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  r$  r*  r,  r.  r7  rN  rV  rY  ra  rg  rk  rw  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  r9   r9   r9   r:   <module>   s   <  $#$#	& 
_u_%W<3F7	#4 ?H%a MoOgF/i2F%		1;+T3	<	/ /*!1((I