a
    g	g                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZmZmZ d dlmZmZ d dlmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z( d dl)m*Z* d d	l+m,Z,m-Z- ed
Z.e/Z0e/Z1ee,e-f Z2eG dd dZ3eG dd dZ4eG dd dZ5eG dd dZ6ee. e	ee0e.f  dddZ7d-e3e3e
e, e8ddddZ9dde3ee:e8dddZ;e3e8dd d!Z<e4e/e8d"d#d$Z=e3e,e/e8d%d&d'Z>e3e,e/e8d(d)d*Z?e3e:dd+d,Z@dS ).    N)	dataclassfield)
CallableDictIteratorListOptionalSequenceTupleTypeVarUnioncast)COMMA_PRIORITYDOT_PRIORITYBracketTracker)ModePreview)BRACKETSCLOSING_BRACKETSOPENING_BRACKETSSTANDALONE_COMMENTTEST_DESCENDANTSchild_towards	is_importis_multiline_stringis_one_sequence_betweenis_type_commentis_with_or_async_with_stmtreplace_childsyms
whitespace)	str_width)token)LeafNodeTc                   @   sv  e Zd ZU dZeed< dZeed< ee	dZ
ee ed< eedZeeee f ed< eedZeed< d	Zeed
< d	Zeed< dZee ed< dLeeeddddZdMeeddddZeedddZeedddZeedddZeedddZeedddZeeddd Z eedd!d"Z!eedd#d$Z"eedd%d&Z#eedd'd(Z$dd)ee%egef  ed*d+d,Z&e'j(feed-d.d/Z)edd0d1Z*edd2d3Z+edd4d5Z,dNeeed6d7d8Z-eed9d:d;Z.eee d<d=d>Z/ddd?d@Z0eed<dAdBZ1dOee2e3e4eef  dCdDdEZ5d ddFdGZ6e7ddHdIZ8eddJdKZ9dS )PLinez;Holds leaves and comments. Can be printed with `str(line)`.moder   depthdefault_factoryleavescommentsbracket_trackerFinside_bracketsshould_split_rhsNmagic_trailing_comma)leafpreformattedtrack_bracketreturnc                 C   s   |j tv pt|j }|s dS tj|j kr>| jr>| jdd= | jrd|sd| j	t
|| |d7  _	| jsr|rr|r| j| | jjr| |r|| _n| j|ddr|   | |s| j| dS )ag  Add a new `leaf` to the end of the line.

        Unless `preformatted` is True, the `leaf` will receive a new consistent
        whitespace prefix and metadata applied by :class:`BracketTracker`.
        Trailing commas are maybe removed, unpacked for loop variables are
        demoted from being delimiters.

        Inline comments are put aside.
        N)Zcomplex_subscriptT)ensure_removable)typer   boolvaluestripr"   COLONis_class_paren_emptyr+   prefixr    is_complex_subscriptr.   r-   markr'   r0   has_magic_trailing_commaremove_trailing_commaappend_commentappend)selfr1   r2   r3   Z	has_value rE   N/var/www/html/llm_bihealth/app/venv/lib/python3.9/site-packages/black/lines.pyrC   =   s$    




zLine.append)r1   r2   r4   c                 C   sD   | j jdkr2| jrtd| jr2|jtkr2td| j||d dS )zLike :func:`append()` but disallow invalid standalone comment structure.

        Raises ValueError when any `leaf` is appended after a standalone comment
        or when a standalone comment is not the first leaf on the line.
        r   z$cannot append to standalone commentsz5cannot append standalone comments to a populated liner2   N)r-   r(   
is_comment
ValueErrorr+   r7   r   rC   )rD   r1   r2   rE   rE   rF   append_safe_   s    zLine.append_safer4   c                 C   s   t | jdko| jd jtkS )z"Is this line a standalone comment?   r   )lenr+   r7   r   rD   rE   rE   rF   rH   p   s    zLine.is_commentc                 C   s   t | o| jd jtjkS )zIs this line a decorator?r   )r8   r+   r7   r"   ATrN   rE   rE   rF   is_decoratoru   s    zLine.is_decoratorc                 C   s   t | ot| jd S )zIs this an import line?r   )r8   r   r+   rN   rE   rE   rF   r   z   s    zLine.is_importc                 C   s   t | ot| jd S )zIs this a with_stmt line?r   )r8   r   r+   rN   rE   rE   rF   r      s    zLine.is_with_or_async_with_stmtc                 C   s*   t | o(| jd jtjko(| jd jdkS )z Is this line a class definition?r   class)r8   r+   r7   r"   NAMEr9   rN   rE   rE   rF   is_class   s
    zLine.is_classc                 C   s&   | j o$| jdd dd tdD kS )zEIs this line a class definition with a body consisting only of "..."?Nc                 S   s   g | ]}t tjd qS ).)r#   r"   DOT).0_rE   rE   rF   
<listcomp>   s   z&Line.is_stub_class.<locals>.<listcomp>   )rS   r+   rangerN   rE   rE   rF   is_stub_class   s    zLine.is_stub_classc                 C   s   z| j d }W n ty"   Y dS 0 z| j d }W n tyH   d}Y n0 |jtjkr`|jdkp|jtjko|duo|jtjko|jdkS )zBIs this a function definition? (Also returns True for async defs.)r   FrL   Ndef)r+   
IndexErrorr7   r"   rR   r9   ASYNC)rD   
first_leafZsecond_leafrE   rE   rF   is_def   s     

zLine.is_defc                 C   s`   t | o^t| jdko^| jo^| jd jtjko^| jd jdko^| jd jtjko^| jd jdkS )zzIs this a class with no base classes but using parentheses?

        Those are unnecessary and should be removed.
              (rZ   ))	r8   rM   r+   rS   r7   r"   LPARr9   RPARrN   rE   rE   rF   r<      s    zLine.is_class_paren_emptyc                 C   s,   t | o*| jd jtjko*| jd jdS )z#Is the line a triple quoted string?r   )z"""z''')r8   r+   r7   r"   STRINGr9   
startswithrN   rE   rE   rF   is_triple_quoted_string   s
    zLine.is_triple_quoted_stringc                 C   s$   t | jdkrdS | jd jtjkS )z/Does this line open a new level of indentation.r   F)rM   r+   r7   r"   r;   rN   rE   rE   rF   opens_block   s    zLine.opens_blockfirst_leaf_matches)rn   r4   c                C   sF   t | jdkrdS | jd }|jtks0|jdu r4dS |du pD||jS )zIs this line converted from fmt off/skip code?

        If first_leaf_matches is not None, it only returns True if the first
        leaf of converted code matches.
        rL   Fr   N)rM   r+   r7   r   Zfmt_pass_converted_first_leaf)rD   rn   r1   rE   rE   rF   is_fmt_pass_converted   s    

zLine.is_fmt_pass_converted)depth_limitr4   c                 C   s*   | j D ]}|jtkr|j|kr dS qdS )z)If so, needs to be split before emitting.TF)r+   r7   r   bracket_depth)rD   rp   r1   rE   rE   rF   contains_standalone_comments   s    
z!Line.contains_standalone_commentsc                 C   s   t  }zR| jd }|t| |jtjks>|jtjkrV|jsV| jd }|t| W n t	yl   Y dS 0 d}| j
 D ]<\}}|D ].}t|r|st|ds||vr  dS d}qq|dS )Nrk   r5   F ignoreT)setr+   addidr7   r"   COMMArg   r9   r^   r,   itemsr   )rD   Zignored_ids	last_leafZcomment_seenZleaf_idr,   commentrE   rE   rF   $contains_uncollapsable_type_comments   s2    


z)Line.contains_uncollapsable_type_commentsc                 C   s   | j s
dS tdd | j D d}tdd t| j D d}||kr| j dd  D ].}| jt|g D ]}t|drf  dS qfqPdS )	NFc                 s   s   | ]}|j d kr|j V  qdS r   NlinenorW   r1   rE   rE   rF   	<genexpr>      z9Line.contains_unsplittable_type_ignore.<locals>.<genexpr>r   c                 s   s   | ]}|j d kr|j V  qdS r|   r}   r   rE   rE   rF   r     r   r5   rs   T)r+   nextreversedr,   getrv   r   )rD   
first_line	last_linenoderz   rE   rE   rF   !contains_unsplittable_type_ignore  s    
z&Line.contains_unsplittable_type_ignorec                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S N)r   r   rE   rE   rF   r   )  r   z2Line.contains_multiline_strings.<locals>.<genexpr>)anyr+   rN   rE   rE   rF   contains_multiline_strings(  s    zLine.contains_multiline_strings)closingr6   r4   c                 C   s   |j tv r"| jr"| jd j tjks&dS |j tjkr6dS |j tjkr|jr||jj tj	kr||j
r|t|j
|| jtjtjfdr|dS |sdS | jd }|jdu rdS |jj tjkp|j
du pt|j
|| jtjtjfd S | jrdS |j
durt|j
|| jsdS dS )aT  Return True if we have a magic trailing comma, that is when:
        - there's a trailing comma here
        - it's not a one-tuple
        - it's not a single-element subscript
        Additionally, if ensure_removable:
        - it's not from square bracket indexing
        (specifically, single-element square bracket indexing)
        rk   FT)ZbracketsN)r7   r   r+   r"   rw   RBRACERSQBparentr   traileropening_bracketr   LSQBsubscriptlistr   )rD   r   r6   commarE   rE   rF   r@   +  s\    




zLine.has_magic_trailing_comma)rz   r4   c                 C   s   |j tkr| j rd|_dS |j tjkr.dS | jsDt|_ d|_dS | jd }|j tjkr|j	s|j
rtt|j
 dkrt|st| jdk rt|_ d|_dS | jd }| jt|g | dS )z0Add an inline or standalone comment to the line. Frk   rZ   rc   r5   T)r7   r   r-   Zany_open_bracketsr=   r"   COMMENTr+   rg   r9   r   rM   listr   r,   
setdefaultrv   rC   )rD   rz   ry   rE   rE   rF   rB   i  s<    



zLine.append_comment)r1   r4   c                 C   s   | j t|g S )z;Generate comments that should appear directly after `leaf`.)r,   r   rv   )rD   r1   rE   rE   rF   comments_after  s    zLine.comments_afterc                 C   s>   | j  }| jt|g }| jt| j d g | dS )z@Remove the trailing comma and moves the comments attached to it.rk   N)r+   popr,   rv   r   extend)rD   Ztrailing_commaZtrailing_comma_commentsrE   rE   rF   rA     s
    
zLine.remove_trailing_commac                 C   sj   | j  }|du rdS |j}t|trL|jtjkr6dS |jtjkrLt	||}|duoht
dd | D S )zAReturn True iff `leaf` is part of a slice with non-trivial exprs.NFc                 s   s   | ]}|j tv V  qd S r   )r7   r   )rW   nrE   rE   rF   r     s   z,Line.is_complex_subscript.<locals>.<genexpr>)r-   Zget_open_lsqbZnext_sibling
isinstancer$   r7   r   Z	listmakerr   r   r   Z	pre_order)rD   r1   Z	open_lsqbZsubscript_startrE   rE   rF   r>     s    


zLine.is_complex_subscript)r   r4   c                 c   s   t ttt gttttf  f |r&tnt}|| j	D ]V\}}t
|jt
|j }d|jv rb dS | |D ]}|t
|j7 }ql|||fV  q6dS )zReturn an enumeration of leaves with their length.

        Stops prematurely on multiline strings and standalone comments.
        
N)r   r   r	   r#   r   r
   Indexenumerate_reversed	enumerater+   rM   r=   r9   r   )rD   r   opindexr1   lengthrz   rE   rE   rF   enumerate_with_length  s    

zLine.enumerate_with_lengthc                 C   s   t | j| j| j| j| jdS )N)r'   r(   r.   r/   r0   )r&   r'   r(   r.   r/   r0   rN   rE   rE   rF   clone  s    z
Line.clonec                 C   sz   | sdS d| j  }t| j}t|}|j | |j }|D ]}|t|7 }q<tj	| j
 D ]}|t|7 }q`|d S )zRender the line.r   z    )r(   iterr+   r   r=   r9   str	itertoolschainfrom_iterabler,   values)rD   indentr+   firstresr1   rz   rE   rE   rF   __str__  s    

zLine.__str__c                 C   s   t | jp| jS )z/Return True if the line has leaves or comments.)r8   r+   r,   rN   rE   rE   rF   __bool__  s    zLine.__bool__)FF)F)F)F):__name__
__module____qualname____doc__r   __annotations__r(   intr   r   r+   r   r#   dictr,   r   LeafIDr   r-   r.   r8   r/   r0   r   rC   rJ   propertyrH   rP   r   r   rS   r\   ra   r<   rj   rl   r   ro   sysmaxsizerr   r{   r   r   r@   rB   r   rA   r>   r   r
   r   r   r   r   r   r   rE   rE   rE   rF   r&   /   sz   
 "$! >% 	r&   c                   @   s:   e Zd ZU dZeed< eed< eed< eed< eed< dS )	RHSResultz2Intermediate split result from a right hand split.headbodytailr   Zclosing_bracketN)r   r   r   r   r&   r   r#   rE   rE   rE   rF   r     s   
r   c                   @   sn   e Zd ZU dZeed< ed  ed< eed< dZe	ed< e
edZee ed< dZe	ed	< ee d
ddZdS )
LinesBlockzClass that holds information about a block of formatted lines.

    This is introduced so that the EmptyLineTracker can look behind the standalone
    comments and adjust their empty lines for class or def lines.
    r'   previous_blockoriginal_liner   beforer)   content_linesafterrK   c                 C   s.   t t| jd}|| j g| j || j g S )N)r'   )r   r&   r'   r   r   r   )rD   Z
empty_linerE   rE   rF   	all_lines  s    zLinesBlock.all_linesN)r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r   rE   rE   rE   rF   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< eedZee ed< dZee
 ed< ee
d	d
dZeeeef d	ddZeeeeef dddZdS )EmptyLineTrackera{  Provides a stateful method that returns the number of potential extra
    empty lines needed before and after the currently processed line.

    Note: this tracker works on lines that haven't been split yet.  It assumes
    the prefix of the first leaf consists of optional newlines.  Those newlines
    are consumed by `maybe_empty_lines()` and included in the computation.
    r'   Nprevious_liner   r)   previous_defssemantic_leading_comment)current_liner4   c                 C   s   |  |\}}| jr| jjnd}| jdu r.dn|| }t| j| j|||d}|jr| jdu s~| jjs| jjrp|r| jdu s~|r|| _n|jr|rd| _|| _|| _|S )zReturn the number of extra empty lines before and after the `current_line`.

        This is for separating `def`, `async def` and `class` with extra empty
        lines (two on module-level).
        r   N)r'   r   r   r   r   )	_maybe_empty_linesr   r   r   r   r'   rH   rP   r   )rD   r   r   r   Zprevious_afterblockrE   rE   rF   maybe_empty_lines  s>    	

z"EmptyLineTracker.maybe_empty_linesc                 C   s  d}|j dkr| jjrdnd}|jrL|jd }|jd}t||}d|_nd}|j }| jr8| jd j |kr8| jjr| jd usJ |r|j	s| jj	rtd|}n8t
j| jv r| jd jr| jd jsd}n|rd}nd}nN|rd}nD|s(| jd j r(|jd jtjkr(|jd jdvr(d}nd}| j  qV|jsP|j	sP|jr\| ||S | jr| jjr|js|jtds|| jj kr|pddfS | jr| jjr|jr|dfS | jr| jjrd	S |dfS )
NrL   r   rc   r   r   rk   )withtryforwhileifmatchrm   r   r   )r(   r'   is_pyir+   r=   countminr   r   ra   r   Z"blank_line_after_nested_stub_classrS   r\   r7   r"   r;   r9   r   rP   #_maybe_empty_lines_for_class_or_defr   ro   rj   rl   )rD   r   Zmax_allowedr`   r   r(   rE   rE   rF   r   8  s~    









z#EmptyLineTracker._maybe_empty_lines)r   r   r4   c                 C   s  |j s| j| | jd u r dS | jj r>| jjr:|jr:dS dS | jj|jk r`| jjs\| jj	r`dS d }| jj
r| jj|jkr|dkr| j}|d ur|jd ur|jjjs|jjjs|jdkr|}ndS | jjrt|js| jjr$| jj|jk rd}n2| jj|jkrd}n|jr| jjrd}nd}nN|j	s4|j rX| jj	sX|jrRtd|}nd}n| jj|jkrnd}nd}n|jrdnd}|d ur|j}|d urt|j||j |_d}|dfS )Nr   )r   rL   r   rL   rc   )rP   r   rC   r   r'   r   r\   r(   rS   ra   rH   r   r   r   rl   r   r   maxr   )rD   r   r   Zcomment_to_add_newlinesZslcnewlinesr   rE   rE   rF   r     s~    



z4EmptyLineTracker._maybe_empty_lines_for_class_or_def)r   r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r   r   r
   r   r   r   rE   rE   rE   rF   r      s   
(L
r   )sequencer4   c                 c   s0   t | d }t| D ]}||fV  |d8 }qdS )z;Like `reversed(enumerate(sequence))` if that were possible.rL   N)rM   r   )r   r   elementrE   rE   rF   r     s    
r   F)new_lineold_liner+   r2   r4   c                 C   sR   |D ]H}t |j|j}t|| | j||d ||D ]}| j|dd q8qdS )a  
    Append leaves (taken from @old_line) to @new_line, making sure to fix the
    underlying Node structure where appropriate.

    All of the leaves in @leaves are duplicated. The duplicates are then
    appended to @new_line and used to replace their originals in the underlying
    Node structure. Any comments attached to the old leaves are reattached to
    the new leaves.

    Pre-conditions:
        set(@leaves) is a subset of set(@old_line.leaves).
    rG   TN)r#   r7   r9   r   rC   r   )r   r   r+   r2   Zold_leafZnew_leafZcomment_leafrE   rE   rF   append_leaves  s    
r   r   )line_str)liner'   r   r4   c                C   s.  |st | }|jrtnt}tj|vrD|||jkoBd|voB|   S |  rPdS d|vrf|||jkS |d^}}}|||jks|||jkrdS g }d}g }	t	j
}
t| jD ]R\}}|
t	j
krDd}|jd t|kr|d n|jd t|k r| }|durD|durD|j|jd krD|j}
|dkrD dS |j|
kr|jtjkrdg}||	7 }|j|v r|t| jd ks||j  d7  < |
t	j
krt|
|j}
t|rt|	dkr dS |}|}t||v r|	| |jdu rq|j}qqt|	dkrdS tdd |D S )	a  For non-multiline strings, return True if `line` is no longer than `line_length`.
    For multiline strings, looks at the context around `line` to determine
    if it should be inlined or split up.
    Uses the provided `line_str` rendering, if any, otherwise computes a new one.
    r   FNrL   r   Tc                 s   s   | ]}|d kV  qdS r|   rE   )rW   valrE   rE   rF   r   H  r   z'is_line_short_enough.<locals>.<genexpr>)line_to_stringpreviewr!   rM   r   Zmultiline_string_handlingline_lengthrr   splitmathinfr   r+   rq   rC   r   r7   r"   rw   Zprev_siblingr   r   r   r   all)r   r'   r   widthr   rX   lastZcommasZmultiline_stringZmultiline_string_contextsZmax_level_to_updateir1   Z	had_commaZignore_ctxsctxrE   rE   rF   is_line_short_enough  sp    

 
r   )r   r4   c                 C   s   | j }t|dk rdS |d jtjkr|d jtjkrd}d}|d }|ddd D ]}|jtv r||jtvrr dS |d7 }nP|jtjkr|d7 }n:|jtjkr|jtjks|jtv s dS n|jtvr dS |dkrT|dkrT dS qTdS )	zReturn False if the line cannot be split *for sure*.

    This is not an exhaustive search but a cheap heuristic that we can use to
    avoid some unfortunate formattings (mostly around wrapping unsplittable code
    in unnecessary parentheses).
    rc   Fr   rL   rk   r5   NT)	r+   rM   r7   r"   rh   rV   r   r   rR   )r   r+   Z
call_count	dot_countr   r1   rE   rE   rF   can_be_splitK  s,     




r   )rhsr   r4   c           
      C   sD  | j }|j}|jsdS | }||}|dkr4dS |dkr\tj|jv r\|tkr\| j	j
r\dS |tkrhdS t|jdks~J d|jd }|jd }|jtv r|jtvrt|||drdS |jd }|jd	 }	|	jtjks|	jtjks|	jtjkr@|	jr@|	jjtjkr@|jtv rdS t|r,dS t||	|d
r@dS dS )a  Does `rhs.body` have a shape safe to reformat without optional parens around it?

    Returns True for only a subset of potentially nice looking formattings but
    the point is to not return false positives that end up producing lines that
    are too long.
    TrL   Frc   zStranded delimiterr   )r   r   r5   rk   )r   r   )r   r-   
delimitersZmax_delimiter_priorityZdelimiter_count_with_priorityr   Z(wrap_multiple_context_managers_in_parensr'   r   r   r   r   rM   r+   r7   r   r   _can_omit_opening_parenr"   rg   r   r   r   r   r   r   _can_omit_closing_paren)
r   r   r   ZbtZmax_priorityZdelimiter_countr   secondZpenultimater   rE   rE   rF   can_omit_invisible_parenso  sV    


	







r   )r   r   r   r4   c                C   s~   d}d| j  }d}|  D ]H\}}}|jtv r<|j|u r<d}|r||7 }||krT qz|jtv rd}qt| j|d krzdS dS ) See `can_omit_invisible_parens`.Frb   rk   TrL   )r(   r   r7   r   r   r   rM   r+   )r   r   r   	remainderr   _indexr1   leaf_lengthrE   rE   rF   r     s    

r   )r   r   r   r4   c                C   sZ   d| j  }d}|  D ]>\}}}||7 }||ju rF|s>||krT dS q|jtv rd}qdS )r   rb   FT)r(   r   r   r7   r   )r   r   r   r   Zseen_other_bracketsr   r1   r   rE   rE   rF   r     s    


r   c                 C   s   t | dS )zmReturns the string representation of @line.

    WARNING: This is known to be computationally expensive.
    r   )r   r:   )r   rE   rE   rF   r     s    r   )F)Ar   r   r   Zdataclassesr   r   typingr   r   r   r   r   r	   r
   r   r   r   Zblack.bracketsr   r   r   Z
black.moder   r   Zblack.nodesr   r   r   r   r   r   r   r   r   r   r   r   r   r    Zblack.stringsr!   Zblib2to3.pgen2r"   Zblib2to3.pytreer#   r$   r%   r   r   r   LNr&   r   r   r   r   r8   r   r   r   r   r   r   r   r   rE   rE   rE   rF   <module>   sZ   0@   2
 P 	 [%T