B
    Ma                 @   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<<=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 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configr3   r4   r/   r/   r0   __init__(   s    zIndentationNode.__init__c             C   s   d| j j S )Nz<%s>)	__class__r&   )r5   r/   r/   r0   __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-   r4   )r5   nr/   r/   r0   get_latest_suite_node/   s
    
z%IndentationNode.get_latest_suite_node)N)	r&   r'   r(   r%   r-   r:   r7   r9   r<   r/   r/   r/   r0   r1   %   s   
r1   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.   r4   hasattrlineget_previous_leaf
isinstancer=   r3   get_next_leafprefix#closing_bracket_hanging_indentationr2   r+   end_posr*   r-   )
r5   r6   r>   r4   in_suite_introducerZprevious_leafr;   parent_indentation	next_leafZexpected_end_indentr/   r/   r0   r7   9   s<    





zBracketNode.__init__N)F)r&   r'   r(   r7   r/   r/   r/   r0   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?   r@   rC   )superr7   r%   r.   r:   rH   rI   r3   )r5   r6   r>   r4   rN   )r8   r/   r0   r7   r   s
    zImplicitNode.__init__)r&   r'   r(   __doc__r7   __classcell__r/   r/   )r8   r0   rO   m   s   rO   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_stmtrB   rC   rA   )search_ancestorchildrenr3   rK   	start_posr2   r4   )r5   r6   rM   Zcontaining_leafspacingr4   rU   Zequalsr/   r/   r0   r7   ~   s    


zBackslashNode.__init__)N)r&   r'   r(   r%   r,   r:   r7   r/   r/   r/   r0   rT   {   s   rT   c             C   s   | j do| j dS )N__)value
startswithendswith)namer/   r/   r0   _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  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 )r3   rC   spacesrB   Ztabs)rQ   r7   _previous_part_previous_leaf_on_newline_newline_count_wanted_newline_count_max_new_lines_in_prefix_new_statement_implicit_indentation_possibler1   _config_indentation_tos_last_indentation_tos_in_suite_introducerr3   _indentation_type_wrong_indentation_char)r5   argskwargs)r8   r/   r0   r7      s     zPEP8Normalizer.__init__c          
   #   s8   t  |" | | d V  W d Q R X W d Q R X d S )N)rQ   
visit_node_visit_node)r5   node)r8   r/   r0   rt      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	   rA   i  zMultiple imports on one linelambdefrU   c             s   s   | ]}|j d kV  qdS )r_   N)r:   ).0r;   r/   r/   r0   	<genexpr>   s   z-PEP8Normalizer._visit_node.<locals>.<genexpr>rV      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_   r:   i  z'Do not compare types, use 'isinstance()Z
file_inputr?   r@   >   r@   
r?   i$  zNo newline at end of filesimple_stmtFstringTc             s   s   | ]}t |V  qd S )N)r`   )rx   r;   r/   r/   r0   ry      s    i  z&Module level import not at top of filesuite)r4   )r   r   )r:   Zget_defined_nameslen	add_issuer4   anyrX   r\   rF   rI   r^   _IMPORT_TYPESindexallrG   r   _IMPLICIT_INDENTATION_TYPES_SUITE_INTRODUCERSro   rm   r%   r,   r1   rl   r3   rk   r-   AssertionErrorrh   r.   _get_wanted_blank_lines_count)r5   rv   typnamesr_   rU   Zchildr~   Zatom	endmarkerprevrI   r   moduler   rX   Zfound_docstringcZimplicit_indentation_possibleZin_introducerr/   r/   r0   ru      s    













zPEP8Normalizer._visit_nodec             C   s(   | j |jkr$| |dd| j  dS dS )Ne   zIndentation contains TF)rq   r\   r   rp   )r5   rZ   r/   r/   r0   _check_tabs_spaces  s    z!PEP8Normalizer._check_tabs_spacesc             C   s   | j  }t|jd kd S )NrA   )rm   r<   intr4   )r5   Z
suite_noder/   r/   r0   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 )NrA   r   rz   i.  i-  z expected %s blank line, found %sr   	decoratorclassasyncdef	decoratedr   r   )maxri   rg   rh   r:   r   r   r\   r4   rH   re   rX   r   )r5   rZ   r>   
is_commentZwantedZblank_linescodemessageactualvalZneeds_linesZfunc_or_clsr   r/   r/   r0   _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 )NrZ   newlinerP   FelifTr   )rQ   
visit_leafZ_split_prefixr:   _visit_partZcreate_spacing_part_analyse_non_prefixrm   rn   rj   r%   r,   r4   r\   r   ro   r   Z_max_blank_linesre   )r5   r>   part)r8   r/   r0   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#!)rA   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 decoratorrA   	backslashi  z+The backslash is redundant between brackets)r4   i#  zTrailing whitespacez%s %so   z!Indentation is not a multiple of z])}   z+Line with same indent as next logical block>   r@   
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)r4   rL   )r   rP   )r   r   Zbom)(r\   r:   r4   rm   r]   lstripr   rf   rematchrY   r   rg   r   r%   r,   r-   r3   ro   rl   rT   rd   r   rn   r   rj   rp   r2   r<   r*   r.   _check_spacing_check_line_length_OPENING_BRACKETSr=   r   rk   r   rO   _previous_spacing)r5   r   rZ   r>   r\   type_rv   r3   Zshould_be_indentationr;   sr/   r/   r0   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   rA   Tr   r   H   Fi  z"Line too long (%s > %s characters))	r:   rY   rK   rl   max_charactersr\   splitr   r   )r5   r   rZ   Zlast_columnZreportZsplittedr/   r/   r0   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   )rr   )r5   rc   r/   r0   add_if_spaces?  s    z4PEP8Normalizer._check_spacing.<locals>.add_if_spacesc                 s   s j |  S d S )N)r   )rr   )r5   rc   r/   r0   add_not_spacesC  s    z5PEP8Normalizer._check_spacing.<locals>.add_not_spacesr   rB      zUsed tab to separate tokensr   i  z)At least two spaces before inline commentr   i#  zTrailing whitespacerA      zMultiple spaces usedzWhitespace after '%s'   zWhitespace before '%s'   )r   ;rP      )r   r   rP      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   r{   i  z!Missing whitespace around keywordr?   r@   z>Whitespace before operator doesn't match with whitespace after      )r\   rd   r:   r   r   rl   spaces_before_commentr   _CLOSING_BRACKETSr4   _POSSIBLE_SLICE_PARENTS_NON_STAR_TYPES_FACTORr"   Z
annotation_BITWISE_OPERATORr   _ALLOW_SPACEre   rI   )r5   r   rZ   r   r   r   r   r   r   Zmessage_225Zprev_spacingr/   )r5   rc   r0   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 )1Nr_   )lOIz0Do not define %s named 'l', 'O', or 'I' one liner   i  classesZfunctioni  i  Z	variablesrP   rw   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!=rA   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   rz   i  zBlank line at end of file)r:   r\   Zis_definitionr4   r_   r   Zadd_issuadd_issuerG   r   r   rH   rX   r   	enumerate
splitlinesr   r   grouprE   r   rg   )r5   r>   r   r   rN   r}   r   leftrightrv   irE   r3   rY   r/   r/   r0   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  )re   rW   r:   rQ   r   r   )r5   rv   r   r   )r8   r/   r0   r     s    
zPEP8Normalizer.add_issue)F)r&   r'   r(   r7   r   rt   ru   r   r   r   r   r   r   r   r   r   rS   r/   r/   )r8   r0   ra      s   f
3" ._:ra   c               @   s   e Zd ZeZdddZdS )PEP8NormalizerConfig    NO   rz   c             C   s4   || _ |d kr|}|| _d| _d| _|| _|| _d S )Nrb   F)r3   hanging_indentationrJ   Zbreak_after_binaryr   r   )r5   r3   r   r   r   r/   r/   r0   r7     s    zPEP8NormalizerConfig.__init__)r   Nr   rz   )r&   r'   r(   ra   Znormalizer_classr7   r/   r/   r/   r0   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 )Nrz   )rg   )r5   r>   r/   r/   r0   is_issue  s    zBlankLineAtEnd.is_issueN)r&   r'   r(   r   r   r   r/   r/   r/   r0   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)   r1   r=   rO   rT   r`   ra   r   r   r/   r/   r/   r0   <module>   s>    5    S