B
    +d                 @   s  U d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZ dZdZd	Zd
ZdZdZdZdZdZeedf ed< ee7 ZdZdZG dd dZG dd deZG dd deZG dd deZG dd deZdd Z G d d! d!eZ!G d"d# d#eZ"G d$d% d%e	Z#dS )&    N)contextmanager)Tuple)ErrorFinderErrorFinderConfig)Rule)FlowScope)import_nameimport_from)classdeffuncdefZif_stmtZ
while_stmtZfor_stmttry_stmtZ	with_stmt)termr
   power)([{))]})+-~)*r   r   z**/z//@)z<<z>>|&^)=%z-><>z==z>=z<=z<>z!=z+=z-=z*=z@=z/=z%=z&=z|=z^=z<<=z>>=z**=z//=._NEEDS_SPACE)dictorsetmakerargument)Z	subscriptZsubscriptlistZsliceopc               @   s*   e Zd Ze Ze Ze Ze Ze ZdS )IndentationTypesN)	__name__
__module____qualname__objectVERTICAL_BRACKETHANGING_BRACKET	BACKSLASHSUITEIMPLICIT r0   r0   ^/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/parso/python/pep8.pyr&      s
   r&   c               @   s,   e Zd ZejZdddZdd Zdd ZdS )	IndentationNodeNc             C   s   | | _ | _|| _d S )N)bracket_indentationindentationparent)selfconfigr4   r5   r0   r0   r1   __init__(   s    zIndentationNode.__init__c             C   s   d| j j S )Nz<%s>)	__class__r'   )r6   r0   r0   r1   __repr__,   s    zIndentationNode.__repr__c             C   s,   | }x"|d k	r&|j tjkr|S |j}qW d S )N)typer&   r.   r5   )r6   nr0   r0   r1   get_latest_suite_node/   s
    
z%IndentationNode.get_latest_suite_node)N)	r'   r(   r)   r&   r.   r;   r8   r:   r=   r0   r0   r0   r1   r2   %   s   
r2   c               @   s   e Zd ZdddZdS )BracketNodeFc       
      C   s&  || _ |}|}|jtjkr |j}xBt|dr<|j|j jkr<P | }t|t	rX||j krZP |j}q"W |j
}| }d|jksd|jkr||j | _||j
 | _
tj| _n6|jd }	d|j
krd | _
n
d|	 | _
| j
| _tj| _|r|jtjkr| j
||j
 kr|  j
|j
7  _
| j
| _|| _d S )Nleaf
   	 )r?   r;   r&   r/   r5   hasattrlineget_previous_leaf
isinstancer>   r4   get_next_leafprefix#closing_bracket_hanging_indentationr3   r,   end_posr+   r.   )
r6   r7   r?   r5   in_suite_introducerZprevious_leafr<   parent_indentation	next_leafZexpected_end_indentr0   r0   r1   r8   9   s<    





zBracketNode.__init__N)F)r'   r(   r)   r8   r0   r0   r0   r1   r>   8   s   r>   c                   s    e Zd ZdZ fddZ  ZS )ImplicitNodezk
    Implicit indentation after keyword arguments, default arguments,
    annotations and dict values.
    c                sN   t  ||| tj| _| }|dkrJd|jkrJd|jkrJ|  jd7  _d S )N:r@   rA   rD   )superr8   r&   r/   r;   rI   rJ   r4   )r6   r7   r?   r5   rO   )r9   r0   r1   r8   r   s
    zImplicitNode.__init__)r'   r(   r)   __doc__r8   __classcell__r0   r0   )r9   r1   rP   m   s   rP   c               @   s   e Zd ZejZdddZdS )BackslashNodeNc             C   s|   | d}|d k	r^|jd }d|jkr.d | _qj|j|jkrH||j | _qjd|jd d  | _n||j | _| j| _|| _d S )N	expr_stmtrC   rD   rB   )search_ancestorchildrenr4   rL   	start_posr3   r5   )r6   r7   rN   Zcontaining_leafspacingr5   rV   equalsr0   r0   r1   r8   ~   s    


zBackslashNode.__init__)N)r'   r(   r)   r&   r-   r;   r8   r0   r0   r0   r1   rU   {   s   rU   c             C   s   | j do| j dS )N__)value
startswithendswith)namer0   r0   r1   _is_magic_name   s    rb   c                   s   e Zd Z fddZe fddZedd Zdd Zd	d
 ZdddZ	 fddZ
dd Zdd Zdd Zdd Z fddZ  ZS )PEP8Normalizerc                s   t  j|| d | _d | _d| _d| _d | _d| _d| _d| _	t
| jdd | _| _d| _d| jjkrtd| _d| _nd	| _d| _d S )
NTr   F )r4   rD   spacesrC   tabs)rR   r8   _previous_part_previous_leaf_on_newline_newline_count_wanted_newline_count_max_new_lines_in_prefix_new_statement_implicit_indentation_possibler2   _config_indentation_tos_last_indentation_tos_in_suite_introducerr4   _indentation_type_wrong_indentation_char)r6   argskwargs)r9   r0   r1   r8      s     zPEP8Normalizer.__init__c          
   #   s8   t  |" | | d V  W d Q R X W d Q R X d S )N)rR   
visit_node_visit_node)r6   node)r9   r0   r1   rw      s    zPEP8Normalizer.visit_nodec             c   s  |j }|dkrJ| }t|dkrFx"|d d D ]}| |dd q0W nd|dkr|j}|j dkrtdd |jd d	d
 D r| |dd n|dkrx0|jD ]&}|j dkr|jdkr| |dd qW n|dkrJx|jD ]d}|j dkrqt|jd
krq|jd }|jd }|j dkr|j dkr|jdkr| |dd P qW nd|dkr|jd }	|	 }
|	j	}|
ds|
ds|s|
d ks|
jdkr| |	dd  |tkr|j}|j}|j dkr|j|}x|jd | D ]}|g}|j d!kr|jd d }d"}x|D ]t}|j d#kr4|s4qd$}|j dkr`td%d | D r`q|j tkst|tr|q| |d&d' P qW qP qW |tk}|tk}|rd$| _nR|d(kr| jj tjkr| jj| _t| j| jj| jj | jd)| _n|rd$| _d V  |d(krD| jj tjks2t| jj| _d | _nH|rld"| _| jj tjkr| jj| _n |rd"| _|d*kr|  | _d S )+Nr	   rB   i  zMultiple imports on one linelambdefrV   c             s   s   | ]}|j d kV  qdS )ra   N)r;   ).0r<   r0   r0   r1   	<genexpr>   s   z-PEP8Normalizer._visit_node.<locals>.<genexpr>rW      i  z,Do not assign a lambda expression, use a defr   keywordexcepti  z1Do not use bare except, specify exception instead
comparison)Z	atom_exprr   r   trailerra   r;   i  z'Do not compare types, use 'isinstance()Z
file_inputr@   rA   >   rA   
r@   i$  zNo newline at end of filesimple_stmtFstringTc             s   s   | ]}t |V  qd S )N)rb   )r{   r<   r0   r0   r1   r|      s    i  z&Module level import not at top of filesuite)r5   )r   r   )r;   Zget_defined_nameslen	add_issuer5   anyrY   r^   rG   rJ   r`   _IMPORT_TYPESindexallrH   r   _IMPLICIT_INDENTATION_TYPES_SUITE_INTRODUCERSrr   rp   r&   r-   r2   ro   r4   rn   r.   AssertionErrorrk   r/   _get_wanted_blank_lines_count)r6   ry   typnamesra   rV   childr   Zatom	endmarkerprevrJ   r   moduler   rY   Zfound_docstringcZimplicit_indentation_possibleZin_introducerr0   r0   r1   rx      s    













zPEP8Normalizer._visit_nodec             C   s(   | j |jkr$| |dd| j  dS dS )Ne   zIndentation contains TF)rt   r^   r   rs   )r6   r[   r0   r0   r1   _check_tabs_spaces  s    z!PEP8Normalizer._check_tabs_spacesc             C   s   | j  }t|jd kd S )NrB   )rp   r=   intr5   )r6   Z
suite_noder0   r0   r1   r   !  s    
z,PEP8Normalizer._get_wanted_blank_lines_countFc             C   sh  t | j| j| _| j}|d k	rv| jd }||krp|jdkrp|sv|dkrJdnd}d||f }| ||| d | _nd | _|s^|  }| jd }|j}	|	dkr|jjdkp|	d	ks|	d
kr|	 dks|	dko| j
d
ko|jjjdk}
|
rX||k rX|j}|j}|jdkr|j}|jt|jdk |krX|dkr:dnd}d||f }| ||| d| _d| _d S )NrB   r   r}   i.  i-  z expected %s blank line, found %sr   	decoratorclassasyncdef	decoratedr   r   )maxrl   rj   rk   r;   r   r   r^   r5   rI   rh   rY   r   )r6   r[   r?   
is_commentwantedblank_linescodemessageactualvalZneeds_linesZfunc_or_clsr   r0   r0   r1   _reset_newlines%  sB    



zPEP8Normalizer._reset_newlinesc                s   t  | x.| D ]"}|jdkr&P | || | qW | | | ||| | j| _|jdk| _	|jdkr| jjt
jkr| jj| _|jdkr|jjtkrd| _n|jdkrd| _| j	s| || d| _|| _|jS )Nr[   newlinerQ   FelifTr   )rR   
visit_leafZ_split_prefixr;   _visit_partZcreate_spacing_part_analyse_non_prefixrp   rq   rm   r&   r-   r5   r^   r   rr   r   Z_max_blank_linesrh   )r6   r?   part)r9   r0   r1   r   X  s*    




zPEP8Normalizer.visit_leafc             C   s  |j }|j}|dkrd S |dkr6|jjdkr6| jj| _| j}|dkr|drh|dr| |dd nX| jrt	d	|s|dks|d
r|j
dks| |dd nt	d|s| |dd | j||dd nf|dkr8| j|  kr| |dd| j  n(|dkr*|jjjdkr*| |dd |  jd7  _|dkr|jtjkr|jtjkrn| |dd n@|j}| jr|jtjkr|| jj7 }t| j|||| jd| _nh| jr|j }|jtjkr| jjdkr| jj| _| |s|j}|dkrD| j}	x:t|t|	jkr P |	j}|	| _|	|kr8P |	j}	q
W | jr|dkrl|r| |dd  n2||krd!t| jj| jf }
| |d"d#|
  q|d$kr|j}n|j}| jr|| j| jj kr| |d%d& q||kr| |s|j d'kr|d$krF|jtjkr6| |d(d) n| |d*d+ nt|t|k r|jtjkrv| |d,d- nJ|jtjkr| |d.d/ n,|jtjkr| |d0d1 n| |d2d3 nJ|jtjkr| |d4d5 n,|jtjkr| |d6d1 n| |d7d8 n| || |  || |r|d9kr|dkr|jjd:kr|t!krrt"| j|| j| jd;| _n|jtjkst#| jj| _n:|d<kr| j$r|jjt%kr|j}t&| j|| jd| _|d=k| _|| _|| _'d S )>N
error_leaf,r$   commentz###i
  z'Too many leading '#' for block comment.z#:? z#!)rB   r   i	  z$Block comment should start with '# 'z#:? [^ ]i  z%Inline comment should start with '# 'T)r   r   i/  zToo many blank lines (%s))r   r   r   i0  z*Blank lines found after function decoratorrB   	backslashi  z+The backslash is redundant between brackets)r5   i#  zTrailing whitespacez%s %so   z!Indentation is not a multiple of z])}   z+Line with same indent as next logical block>   rA   
r@   |   z1Closing bracket does not match visual indentation{   zCLosing bracket does not match indentation of opening bracket's line   z2Continuation line under-indented for visual indentz   z2Continuation line missing indentation or outdented   Zxxxy   z3Continuation line under-indented for hanging indent   z1Continuation line over-indented for visual indent   ~   z2Continuation line over-indented for hanging indentz()[]{}
error_node)r5   rM   )r   rQ   )r   r   bom)(r^   r;   r5   rp   r_   lstripr   ri   rematchrZ   r   rj   r   r&   r-   r.   r4   rr   ro   rU   rg   r   rq   r   rm   rs   r3   r=   r+   r/   _check_spacing_check_line_length_OPENING_BRACKETSr>   r   rn   r   rP   _previous_spacing)r6   r   r[   r?   r^   type_ry   r4   Zshould_be_indentationr<   sr0   r0   r1   r   z  s   
















zPEP8Normalizer._visit_partc             C   s   |j dkr|jd d }n
|jd }|| jjkr|jd | jjkrd}|j dkr|jdd   }t|dkr|jd t|d  dk rd}|r| |dd	|| jjf  d S )
Nr   rB   Tr   r   H   Fi  z"Line too long (%s > %s characters))	r;   rZ   rL   ro   max_charactersr^   splitr   r   )r6   r   r[   last_columnreportZsplittedr0   r0   r1   r   '  s"    


z!PEP8Normalizer._check_line_lengthc                s   fdd} fdd}|j  j}|d k	r:|jdksD|jdkrHd S |j}dkrh |dd n |d	krt jjk r |d
d n|dkr||dd ntdkrȈ |dd n|tkrd|j  }||d| n|tkrd|j  }||d| nz|dks0|dkrJ|j	jt
krJd|j  }||d| n>|dkrf|j	jt
krfn"|dkr||dd n|dkrn|dkr|j	jtks|dkr|j	jtkrn|tkr|j	jdkrސn|dkr|j	jdkrn|tks|tkr|d kr&|j	jd!ks>|d kr|j	jd!kr|d krP|j	}n|j	}|jd"krx|jrx||d#d$ n||d%d& nT|tks|tkr||d'd( n2|d)ks|d)kr||d*d+ nd,}	||d-|	 n|d.ks|jd.kr ||d/d0 n j}
|tkrJ|
j krJd1 jjkrJd2 jjkrJd3} |d4| r|tkr|tkrd,}	|tkrd|j  }||d5| d S )6Nc                 s   r j |  S d S )N)r   )ru   )r6   re   r0   r1   add_if_spaces?  s    z4PEP8Normalizer._check_spacing.<locals>.add_if_spacesc                 s   s j |  S d S )N)r   )ru   )r6   re   r0   r1   add_not_spacesC  s    z5PEP8Normalizer._check_spacing.<locals>.add_not_spacesr   rC      zUsed tab to separate tokensr   i  z)At least two spaces before inline commentr   i#  zTrailing whitespacerB      zMultiple spaces usedzWhitespace after '%s'   zWhitespace before '%s'   )r   ;rQ      )r   r   rQ      zmissing whitespace after '%s')r   z**factorr   r   r   )r%   paramr      z(Expected spaces around annotation equals   z3Unexpected spaces around keyword / parameter equals   z3Missing whitespace around bitwise or shift operatorr       z)Missing whitespace around modulo operatorz!Missing whitespace between tokens   r~   i  z!Missing whitespace around keywordr@   rA   z>Whitespace before operator doesn't match with whitespace after      )r^   rg   r;   r   r   ro   spaces_before_commentr   _CLOSING_BRACKETSr5   _POSSIBLE_SLICE_PARENTS_NON_STAR_TYPES_FACTORr#   
annotation_BITWISE_OPERATORr   _ALLOW_SPACErh   rJ   )r6   r   r[   r   r   r   r   r   r   Zmessage_225Zprev_spacingr0   )r6   re   r1   r   >  s    









zPEP8Normalizer._check_spacingc             C   s  |j }|dkr|jdkr| rd}|jj dkrP|jj|krP| |d|d  n>|jj dkr||jj|kr|| |d|d  n| d	|d
 | n$|jdkrt|jtt	fr|jj dkr|
 }|j dkr|jj dkr| |dd n| |dd n|jdkr6|
 j dkr$| |dd n| |dd n|jdkr|j}|j|}|j|d  }|j|d  }xl||fD ]`}	|	j dks|	j dkrz|	jdkrd}| |d| P n |	jdkrzd }| |d!| P qzW n|jd"kr8|j}|j d#kr|jj d$kr|jd%kr(| |d&d' n| |d(d) n~|d*krxrt|j dd  D ]2\}
}td+|d,}|j|
 t|f}| qZW n$|d-kr| jd.kr| |d/d0 d S )1Nra   )lOIz0Do not define %s named 'l', 'O', or 'I' one liner   i  classesfunctioni  i  	variablesrQ   rz   r   r   i  z%Multiple statements on one line (def)i  z'Multiple statements on one line (colon)r   )r   r   i  zStatement ends with a semicoloni  z+Multiple statements on one line (semicolon))z==z!=rB   r~   Nonez/comparison to None should be 'if cond is None:'i  )TrueFalsezCcomparison to False/True should be 'if cond is True:' or 'if cond:'i  )inisr   Znot_testr   i  z&test for membership should be 'not in'i  z+test for object identity should be 'is not'r   z[ \t]*r   r   r}   i  zBlank line at end of file)r;   r^   Zis_definitionr5   ra   r   Zadd_issuadd_issuerH   r   r   rI   rY   r   	enumerate
splitlinesr   r   grouprF   r   rj   )r6   r?   r   r   rO   r   r   leftrightry   irF   r4   rZ   r0   r0   r1   r     sb    



 
z"PEP8Normalizer._analyse_non_prefixc                sr   | j d k	r.| j dd k	rd S | j jdkr.d S |dd k	r@d S |dkrZt ||| ntt| ||| d S )Nr   r   )i  i  )rh   rX   r;   rR   r   r   )r6   ry   r   r   )r9   r0   r1   r     s    
zPEP8Normalizer.add_issue)F)r'   r(   r)   r8   r   rw   rx   r   r   r   r   r   r   r   r   r   rT   r0   r0   )r9   r1   rc      s   f
3" ._:rc   c               @   s   e Zd ZeZdddZdS )PEP8NormalizerConfig    NO   r}   c             C   s4   || _ |d kr|}|| _d| _d| _|| _|| _d S )Nrd   F)r4   hanging_indentationrK   Zbreak_after_binaryr   r   )r6   r4   r   r   r   r0   r0   r1   r8     s    zPEP8NormalizerConfig.__init__)r   Nr   r}   )r'   r(   r)   rc   Znormalizer_classr8   r0   r0   r0   r1   r     s    r   c               @   s   e Zd ZdZdZdd ZdS )BlankLineAtEndi  zBlank line at end of filec             C   s
   | j dkS )Nr}   )rj   )r6   r?   r0   r0   r1   is_issue  s    zBlankLineAtEnd.is_issueN)r'   r(   r)   r   r   r   r0   r0   r0   r1   r     s   r   )$r   
contextlibr   typingr   Zparso.python.errorsr   r   Zparso.normalizerr   Zparso.python.treer   r   r   r   r   r   r   r   r   r   r#   str__annotations__r   r   r&   r*   r2   r>   rP   rU   rb   rc   r   r   r0   r0   r0   r1   <module>   s>    5    S