U
    nuf                     @   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)Ztermr
   power)([{))]})+-~)*r   r   **/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 r2   r2   5/tmp/pip-unpacked-wheel-bhkpgl14/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configr7   r8   r2   r2   r3   __init__(   s    zIndentationNode.__init__c                 C   s   d| j j S )Nz<%s>)	__class__r)   )r9   r2   r2   r3   __repr__,   s    zIndentationNode.__repr__c                 C   s(   | }|d k	r$|j tjkr|S |j}qd S r5   )typer(   r0   r8   )r9   nr2   r2   r3   get_latest_suite_node/   s
    z%IndentationNode.get_latest_suite_node)N)	r)   r*   r+   r(   r0   r>   r;   r=   r@   r2   r2   r2   r3   r4   %   s   
r4   c                   @   s   e Zd ZdddZdS )BracketNodeFc           
      C   s"  || _ |}|}|jtjkr |j}t|dr:|j|j jkr:q`| }t|t	r`||j krXq`|j}q |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
   	 )rB   r>   r(   r1   r8   hasattrlineget_previous_leaf
isinstancerA   r7   get_next_leafprefix#closing_bracket_hanging_indentationr6   r.   end_posr-   r0   )
r9   r:   rB   r8   in_suite_introducerZprevious_leafr?   parent_indentation	next_leafZexpected_end_indentr2   r2   r3   r;   9   s>    



zBracketNode.__init__N)F)r)   r*   r+   r;   r2   r2   r2   r3   rA   8   s   rA   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:rC   rD   rG   )superr;   r(   r1   r>   rL   rM   r7   )r9   r:   rB   r8   rR   r<   r2   r3   r;   r   s
    zImplicitNode.__init__)r)   r*   r+   __doc__r;   __classcell__r2   r2   rV   r3   rS   m   s   rS   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_stmtrF   rG   rE   )search_ancestorchildrenr7   rO   	start_posr6   r8   )r9   r:   rQ   Zcontaining_leafspacingr8   rZ   equalsr2   r2   r3   r;   ~   s    


zBackslashNode.__init__)N)r)   r*   r+   r(   r/   r>   r;   r2   r2   r2   r3   rY   {   s   rY   c                 C   s   | j do| j dS )N__)value
startswithendswith)namer2   r2   r3   _is_magic_name   s    rf   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 )r7   rG   spacesrF   tabs)rU   r;   _previous_part_previous_leaf_on_newline_newline_count_wanted_newline_count_max_new_lines_in_prefix_new_statement_implicit_indentation_possibler4   _config_indentation_tos_last_indentation_tos_in_suite_introducerr7   _indentation_type_wrong_indentation_char)r9   argskwargsrV   r2   r3   r;      s"    
zPEP8Normalizer.__init__c              
   #   s8   t  |" | | d V  W 5 Q R X W 5 Q R X d S r5   )rU   
visit_node_visit_node)r9   noderV   r2   r3   r{      s    zPEP8Normalizer.visit_nodec                 c   s  |j }|dkrF| }t|dkrB|d d D ]}| |dd q.n^|dkr|j}|j dkrtdd |jd d	d
 D r| |dd n|dkr|jD ]&}|j dkr|jdkr| |dd qn|dkr@|jD ]f}|j dkrqt|jd
krq|jd }|jd }|j dkr|j dkr|jdkr| |dd  qqnd|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|}|jd | D ]}|g}|j d!kr|jd d }d"}|D ]x}|j d#kr&|s&qd$}|j dkrRtd%d | D rRq|j tkst|trnq| |d&d'  qqq qq|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r:| jj tjks(t| jj| _d | _nH|rbd"| _| jj tjkr| jj| _n |rd"| _|d*kr|  | _d S )+Nr	   rE   i  zMultiple imports on one linelambdefrZ   c                 s   s   | ]}|j d kV  qdS )re   N)r>   .0r?   r2   r2   r3   	<genexpr>   s   z-PEP8Normalizer._visit_node.<locals>.<genexpr>r[      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   trailerre   r>   i  z'Do not compare types, use 'isinstance()Z
file_inputrC   rD      
rD   rC   i$  zNo newline at end of filesimple_stmtFstringTc                 s   s   | ]}t |V  qd S r5   )rf   r   r2   r2   r3   r      s     i  z&Module level import not at top of filesuiter8   )r   r   )r>   Zget_defined_nameslen	add_issuer8   anyr]   rb   rJ   rM   rd   _IMPORT_TYPESindexallrK   r   _IMPLICIT_INDENTATION_TYPES_SUITE_INTRODUCERSrv   rt   r(   r/   r4   rs   r7   rr   r0   AssertionErrorro   r1   _get_wanted_blank_lines_count)r9   r}   typnamesre   rZ   childr   Zatom	endmarkerprevrM   r   moduler   r]   Zfound_docstringcZimplicit_indentation_possibleZin_introducerr2   r2   r3   r|      s    

















zPEP8Normalizer._visit_nodec                 C   s(   | j |jkr$| |dd| j  dS dS )Ne   zIndentation contains TF)rx   rb   r   rw   )r9   r_   r2   r2   r3   _check_tabs_spaces  s    z!PEP8Normalizer._check_tabs_spacesc                 C   s   | j  }t|jd kd S )NrE   )rt   r@   intr8   )r9   Z
suite_noder2   r2   r3   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 )NrE   r   r   i.  i-  z expected %s blank line, found %sr   	decoratorclassasyncdef	decoratedr   r   )maxrp   rn   ro   r>   r   r   rb   r8   rL   rl   r]   r   )r9   r_   rB   
is_commentwantedZblank_linescodemessageactualvalZneeds_linesZfunc_or_clsr   r2   r2   r3   _reset_newlines%  sN    

zPEP8Normalizer._reset_newlinesc                    s   t  | | D ]$}|jdkr& q:| || | q| | | ||| | 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_   newlinerT   FelifTr   )rU   
visit_leafZ_split_prefixr>   _visit_partZcreate_spacing_part_analyse_non_prefixrt   ru   rq   r(   r/   r8   rb   r   rv   r   Z_max_blank_linesrl   )r9   rB   partrV   r2   r3   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}	t|t|	jkr qD|	j}|	| _|	|kr:qD|	j}	q| 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#!)rE   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 decoratorrE   	backslashi  z+The backslash is redundant between bracketsr   #  Trailing whitespacez%s %so   z!Indentation is not a multiple of z])}   z+Line with same indent as next logical blockr   |   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)r8   rP   )r   rT   )r   r   bom)(rb   r>   r8   rt   rc   lstripr   rm   rematchr^   r   rn   r   r(   r/   r0   r7   rv   rs   rY   rk   r   ru   r   rq   rw   r6   r@   r-   r1   _check_spacing_check_line_length_OPENING_BRACKETSrA   r   rr   r   rS   _previous_spacing)r9   r   r_   rB   rb   type_r}   r7   Zshould_be_indentationr?   sr2   r2   r3   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   rE   Tr   r   H   Fi  z"Line too long (%s > %s characters))	r>   r^   rO   rs   max_charactersrb   splitr   r   )r9   r   r_   Zlast_columnreportZsplittedr2   r2   r3   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 r5   r   ry   r9   ri   r2   r3   add_if_spaces?  s    z4PEP8Normalizer._check_spacing.<locals>.add_if_spacesc                     s   s j |  S d S r5   r   r   r   r2   r3   add_not_spacesC  s    z5PEP8Normalizer._check_spacing.<locals>.add_not_spacesr   rF      zUsed tab to separate tokensr   i  z)At least two spaces before inline commentr   r   r   rE      zMultiple spaces usedzWhitespace after '%s'   zWhitespace before '%s'   )r   ;rT      )r   r   rT      zmissing whitespace after '%s')r   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 keywordrC   rD   z>Whitespace before operator doesn't match with whitespace after      )rb   rk   r>   r   r   rs   spaces_before_commentr   _CLOSING_BRACKETSr8   _POSSIBLE_SLICE_PARENTS_NON_STAR_TYPES_FACTORr%   
annotation_BITWISE_OPERATORr   _ALLOW_SPACErl   rM   )r9   r   r_   r   r   r   r   r   r   Zmessage_225Zprev_spacingr2   r   r3   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  }||fD ]h}	|	j dks|	j dkrx|	jdkrd}| |d|  qn$|	jdkrxd }| |d!|  qqxn|jd"kr<|j}|j d#kr|jj d$kr|jd%kr,| |d&d' n| |d(d) nz|d*krt|j dd  D ]2\}
}td+|d,}|j|
 t|f}| q\n$|d-kr| jd.kr| |d/d0 d S )1Nre   )lOIz0Do not define %s named 'l', 'O', or 'I' one liner   i  classesfunctioni  i  	variablesrT   r~   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))r#   r$   rE   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  Blank line at end of file)r>   rb   Zis_definitionr8   re   r   Zadd_issuadd_issuerK   r   r   rL   r]   r   	enumerate
splitlinesr   r   grouprI   r   rn   )r9   rB   r   r   rR   r   r   leftrightr}   irI   r7   r^   r2   r2   r3   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  )rl   r\   r>   rU   r   r   )r9   r}   r   r   rV   r2   r3   r     s    
zPEP8Normalizer.add_issue)F)r)   r*   r+   r;   r   r{   r|   r   r   r   r   r   r   r   r   r   rX   r2   r2   rV   r3   rg      s   
e
3" ._:rg   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 )Nrh   F)r7   hanging_indentationrN   Zbreak_after_binaryr   r   )r9   r7   r  r   r   r2   r2   r3   r;     s    zPEP8NormalizerConfig.__init__)r  Nr  r   )r)   r*   r+   rg   Znormalizer_classr;   r2   r2   r2   r3   r    s
       r  c                   @   s   e Zd ZdZdZdd ZdS )BlankLineAtEndi  r   c                 C   s
   | j dkS )Nr   )rn   )r9   rB   r2   r2   r3   is_issue  s    zBlankLineAtEnd.is_issueN)r)   r*   r+   r   r   r	  r2   r2   r2   r3   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,   r4   rA   rS   rY   rf   rg   r  r  r2   r2   r2   r3   <module>   s>    5    S