B
    t\}                 @   s  d dl 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
mZ dZdZdZd	Zd
ZdZdZdZdZee7 ZdZ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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)ErrorFinderErrorFinderConfig)Rule)search_ancestor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<<=z>>=z**=z//=)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 r.   r.   0lib/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configr2   r3   r.   r.   r/   __init__&   s    zIndentationNode.__init__c             C   s   d| j j S )Nz<%s>)	__class__r%   )r4   r.   r.   r/   __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,   r3   )r4   nr.   r.   r/   get_latest_suite_node-   s
    
z%IndentationNode.get_latest_suite_node)N)	r%   r&   r'   r$   r,   r9   r6   r8   r;   r.   r.   r.   r/   r0   #   s   
r0   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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=   r9   r$   r-   r3   hasattrlineget_previous_leaf
isinstancer<   r2   get_next_leafprefix#closing_bracket_hanging_indentationr1   r*   end_posr)   r,   )
r4   r5   r=   r3   in_suite_introducerZprevious_leafr:   parent_indentation	next_leafZexpected_end_indentr.   r.   r/   r6   7   s<    






zBracketNode.__init__N)F)r%   r&   r'   r6   r.   r.   r.   r/   r<   6   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                sH   t t| ||| tj| _| }|dkrDd|jkrD|  jd7  _d S )N:r>   rA   )	superrM   r6   r$   r-   r9   rF   rG   r2   )r4   r5   r=   r3   rL   )r7   r.   r/   r6   q   s
    zImplicitNode.__init__)r%   r&   r'   __doc__r6   __classcell__r.   r.   )r7   r/   rM   l   s   rM   c               @   s   e Zd ZejZdddZdS )BackslashNodeNc             C   s|   t |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_stmtr@   rA   r?   )r   childrenr2   rI   	start_posr1   r3   )r4   r5   rK   Zcontaining_leafspacingr3   rS   Zequalsr.   r.   r/   r6   }   s    


zBackslashNode.__init__)N)r%   r&   r'   r$   r+   r9   r6   r.   r.   r.   r/   rR   z   s   rR   c             C   s   | j do| j dS )N__)value
startswithendswith)namer.   r.   r/   _is_magic_name   s    r]   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 t| j|| d | _d | _d| _d| _d | _d| _d| _	d| _
t| jdd | _| _d| _d| jjkrxd| _d| _nd	| _d| _d S )
NTr   F )r2   rA   spacesr@   Ztabs)rO   r^   r6   _previous_part_previous_leaf_on_newline_newline_count_wanted_newline_count_max_new_lines_in_prefix_new_statement_implicit_indentation_possibler0   _config_indentation_tos_last_indentation_tos_in_suite_introducerr2   _indentation_type_wrong_indentation_char)r4   argskwargs)r7   r.   r/   r6      s     zPEP8Normalizer.__init__c          
   #   s<   t t| |" | | d V  W d Q R X W d Q R X d S )N)rO   r^   
visit_node_visit_node)r4   node)r7   r.   r/   rq      s    zPEP8Normalizer.visit_nodec             c   s  |j }|dkrJ| }t|dkrFx"|d d D ]}| |dd q0W nX|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 nX|dkr|jd }	|	 }
|	j	}|
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r(|s(qd"}|j dkrTtd#d | D rTq|j tkst|trp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r8| jj tjks&t| jj| _d | _nH|r`d | _| jj tjkr| jj| _n |rd | _|d(kr|  | _d S ))Nr	   r?   i  zMultiple imports on one linelambdefrS   c             s   s   | ]}|j d kV  qdS )r\   N)r9   ).0r:   r.   r.   r/   	<genexpr>   s    z-PEP8Normalizer._visit_node.<locals>.<genexpr>rT      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   trailerr\   r9   i  z'Do not compare types, use 'isinstance()Z
file_inputr>   i$  zNo newline at end of filesimple_stmtFstringTc             s   s   | ]}t |V  qd S )N)r]   )ru   r:   r.   r.   r/   rv      s    i  z&Module level import not at top of filesuite)r3   )r   r   )r9   Zget_defined_nameslen	add_issuer3   anyrU   rY   rD   rG   r[   _IMPORT_TYPESindexallrE   r   _IMPLICIT_INDENTATION_TYPES_SUITE_INTRODUCERSrl   rj   r$   r+   r0   ri   r2   rh   r,   AssertionErrorre   r-   _get_wanted_blank_lines_count)r4   rs   typnamesr\   rS   Zchildr{   Zatom	endmarkerprevrG   r}   moduler   rU   Zfound_docstringcZimplicit_indentation_possibleZin_introducerr.   r.   r/   rr      s    (













zPEP8Normalizer._visit_nodec             C   s(   | j |jkr$| |dd| j  dS dS )Ne   zIndentation contains TF)rn   rY   r   rm   )r4   rW   r.   r.   r/   _check_tabs_spaces  s    z!PEP8Normalizer._check_tabs_spacesc             C   s   | j  }t|jd kd S )Nr?   )rj   r;   intr3   )r4   Z
suite_noder.   r.   r/   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 )Nr?   r   rw   i.  i-  z expected %s blank line, found %sr   	decoratorclassasyncdef	decoratedr   r   )maxrf   rd   re   r9   r   r   rY   r3   rF   rb   rU   r   )r4   rW   r=   
is_commentZwantedZblank_linescodemessageZactualvalZneeds_linesZfunc_or_clsr   r.   r.   r/   _reset_newlines$  sB    



zPEP8Normalizer._reset_newlinesc                s   t 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 )NrW   newlinerN   FelifTr   )rO   r^   
visit_leafZ_split_prefixr9   _visit_partZcreate_spacing_part_analyse_non_prefixrj   rk   rg   r$   r+   r3   rY   r   rl   r   Z_max_blank_linesrb   )r4   r=   part)r7   r.   r/   r   W  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#!)r?   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 decoratorr?   	backslashi  z+The backslash is redundant between brackets)r3   i#  z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)r3   rJ   )r   rN   )r   r   Zbom)(rY   r9   r3   rj   rZ   lstripr   rc   rematchrV   r   rd   r   r$   r+   r,   r2   rl   ri   rR   ra   r   rk   r   rg   rm   r1   r;   r)   r-   _check_spacing_check_line_length_OPENING_BRACKETSr<   r   rh   r   rM   _previous_spacing)r4   r   rW   r=   rY   type_rs   r2   Zshould_be_indentationr:   sr.   r.   r/   r   y  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   r?   Tr   r   H   Fi  z"Line too long (%s > %s characters))	r9   rV   rI   ri   max_charactersrY   splitr   r   )r4   r   rW   Zlast_columnZreportZsplittedr.   r.   r/   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| nl|dks0|dkrJ|j	jt
krJd|j  }||d| n0|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sz|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 nz j}
|tkr<|
j kr<d1 jjkr<d2} |d3| rz|tkrz|tkrzd,}	|tkrzd|j  }||d4| d S )5Nc                 s   r j |  S d S )N)r   )ro   )r4   r`   r.   r/   add_if_spaces!  s    z4PEP8Normalizer._check_spacing.<locals>.add_if_spacesc                 s   s j |  S d S )N)r   )ro   )r4   r`   r.   r/   add_not_spaces%  s    z5PEP8Normalizer._check_spacing.<locals>.add_not_spacesr   r@      zUsed tab to separate tokensr   i  z)At least two spaces before inline commentr   i#  zTrailing whitespacer?      zMultiple spaces usedzWhitespace after '%s'   zWhitespace before '%s'   )r   ;rN      )r   r   rN      zmissing whitespace after '%s')r   z**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   rx   i  z!Missing whitespace around keywordr>   z>Whitespace before operator doesn't match with whitespace after      )rY   ra   r9   r   r   ri   spaces_before_commentr   _CLOSING_BRACKETSr3   _POSSIBLE_SLICE_PARENTS_NON_STAR_TYPES_FACTOR_NEEDS_SPACEZ
annotation_BITWISE_OPERATORr   _ALLOW_SPACErb   rG   )r4   r   rW   r   r   r   r   r   r   Zmessage_225Zprev_spacingr.   )r4   r`   r/   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) nz|d*krxnt|j dd  D ].\}
}td+|d,}|j|
 t|f}qZW n$|d-kr| jd.kr| |d/d0 d S )1Nr\   )lOIz0Do not define %s named 'l', 'O', or 'I' one liner   i  classesZfunctioni  i  Z	variablesrN   rt   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!=r?   rx   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isrz   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   rw   i  zBlank line at end of file)r9   rY   Zis_definitionr3   r\   r   Zadd_issuadd_issuerE   r   r   rF   rU   r   	enumerate
splitlinesr   r   grouprC   r   rd   )r4   r=   r   r   rL   rz   r   leftrightrs   irC   r2   rV   r.   r.   r/   r   w  s`    



 
z"PEP8Normalizer._analyse_non_prefixc                sv   | j d k	r.t| j dd k	rd S | j jdkr.d S t|dd k	r@d S |dkr^tt| ||| ntt| ||| d S )Nr   r   )i  i  )rb   r   r9   rO   r^   r   r   )r4   rs   r   r   )r7   r.   r/   r     s    
zPEP8Normalizer.add_issue)F)r%   r&   r'   r6   r   rq   rr   r   r   r   r   r   r   r   r   r   rQ   r.   r.   )r7   r/   r^      s   f
3" W8r^   c               @   s   e Zd ZeZdddZdS )PEP8NormalizerConfig    NO   rw   c             C   s4   || _ |d kr|}|| _d| _d| _|| _|| _d S )Nr_   F)r2   hanging_indentationrH   Zbreak_after_binaryr   r   )r4   r2   r   r   r   r.   r.   r/   r6     s    zPEP8NormalizerConfig.__init__)r   Nr   rw   )r%   r&   r'   r^   Znormalizer_classr6   r.   r.   r.   r/   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 )Nrw   )rd   )r4   r=   r.   r.   r/   is_issue  s    zBlankLineAtEnd.is_issueN)r%   r&   r'   r   r   r   r.   r.   r.   r/   r     s   r   )!r   
contextlibr   Zparso.python.errorsr   r   Zparso.normalizerr   Zparso.python.treer   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r$   r0   r<   rM   rR   r]   r^   r   r   r.   r.   r.   r/   <module>   s:   6    ,