o
    gl                     @   s  d Z ddlZddlZddlZddlZddlmZmZ ddlZddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZmZmZ erFddlmZ d	Zh d
Zedde dZedde dZedde dejZi ddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5Ze	je	je	j e	j!e	j"e	j#e	j$fZ%d6d7 Z&d8d9 Z'G d:d; d;eZ(G d<d= d=eZ)d>d? Z*d@dA Z+dBe,dCe-fdDdEZ.dBe,dCe,fdFdGZ/dBe,dCe-fdHdIZ0dS )Jz*Checker for string formatting operations.
    N)TYPE_CHECKINGIterable)nodes)BaseCheckerBaseTokenCheckerutils)check_messages)IAstroidCheckerIRawCheckerITokenChecker)Counter)z__builtin__.unicodez__builtin__.strbuiltins.str>   BFRUbfruBRBrFRFrRBRFRbRfbRbrfRfrrBrFrbrf(|z)?'''z)?"""z)?("|')E1300)z3Unsupported format character %r (%#02x) at index %dbad-format-characterzEUsed when an unsupported format character is used in a format string.E1301)z4Format string ends in middle of conversion specifiertruncated-format-stringzNUsed when a format string terminates before the end of a conversion specifier.E1302)z?Mixing named and unnamed conversion specifiers in format stringmixed-format-stringzUsed when a format string contains both named (e.g. '%(foo)d') and unnamed (e.g. '%d') conversion specifiers.  This is also used when a named conversion specifier contains * for the minimum field width and/or precision.E1303)z*Expected mapping for format string, not %sformat-needs-mappingzoUsed when a format string that uses named conversion specifiers is used with an argument that is not a mapping.W1300)z7Format string dictionary key should be a string, not %sbad-format-string-keyzyUsed when a format string that uses named conversion specifiers is used with a dictionary whose keys are not all strings.W1301)z)Unused key %r in format string dictionaryunused-format-string-keyzUsed when a format string that uses named conversion specifiers is used with a dictionary that contains keys not required by the format string.E1304)z*Missing key %r in format string dictionarymissing-format-string-keyzUsed when a format string that uses named conversion specifiers is used with a dictionary that doesn't contain all the keys required by the format string.E1305)z$Too many arguments for format stringtoo-many-format-argsz^Used when a format string that uses unnamed conversion specifiers is given too many arguments.E1306)z&Not enough arguments for format stringtoo-few-format-argsz\Used when a format string that uses unnamed conversion specifiers is given too few argumentsE1307)z)Argument %r does not match format type %rbad-string-format-typezSUsed when a type required by format string is not suitable for actual argument typeE1310)z!Suspicious argument in %s.%s callbad-str-strip-callzGThe argument to a str.{l,r,}strip call contains a duplicate character, W1302)zInvalid format stringbad-format-stringz.Used when a PEP 3101 format string is invalid.W1303)z-Missing keyword argument %r for format stringmissing-format-argument-keyzhUsed when a PEP 3101 format string that uses named fields doesn't receive one or more required keywords.W1304)zUnused format argument %runused-format-string-argumentz}Used when a PEP 3101 format string that uses named fields is used with an argument that is not required by the format string.W1305)zTFormat string contains both automatic field numbering and manual field specificationformat-combined-specificationzUsed when a PEP 3101 format string contains both automatic field numbering (e.g. '{}') and manual field specification (e.g. '{0}').W1306)z2Missing format attribute %r in format specifier %rmissing-format-attributezUsed when a PEP 3101 format string uses an attribute specifier ({0.length}), but the argument passed for formatting doesn't have that attribute.W1307)z2Using invalid lookup key %r in format specifier %rinvalid-format-indexzUsed when a PEP 3101 format string uses a lookup specifier ({a[1]}), but the argument passed for formatting doesn't contain or doesn't have that key as an attribute.)zKDuplicate string formatting argument %r, consider passing as named argument$duplicate-string-formatting-argumentzmUsed when we detect that a string formatting is repeating an argument instead of using named string arguments)z?Using an f-string that does not have any interpolated variablesf-string-without-interpolationzUsed when we detect an f-string that does not use any interpolation variables, in which case it can be either a normal string or a bug in the code.)zKUsing formatting for a string that does not have any interpolated variables#format-string-without-interpolationzUsed when we detect a string that does not have any interpolation variables, in which case it can be either a normal string without formatting or a bug in the code.)W1308W1309W1310c                 C   sL   g }|D ]\}}|r| d|  q| d|d qt| d| S )z]Given a list of format specifiers, returns
    the final access path (e.g. a.b.c[0][1]).
    .[] )appendstrjoin)keypartspathis_attribute	specifier r\   U/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/checkers/strings.pyget_access_path   s   r^   c                 C   sT   |dv rdS t | tjr(|  } | dkr|dkS | dkr |dv S | dkr&dS dS dS )	NsrTr   czbuiltins.floatz	deEfFgGn%zbuiltins.intF)
isinstanceastroidInstancepytype)arg_typeformat_typer\   r\   r]   arg_matches_format_type   s   rg   c                   @   s   e Zd ZdZefZdZeZe	ddddddd	d
ddd	dde
jddfddZe	dde
jddfddZe	e de
jddfddZdd Zdd Zdd ZdS )StringFormatCheckerzChecks string formatting operations to ensure that the format string
    is valid and the arguments match the format string.
    stringr)   r+   r-   r1   r5   r3   r;   r/   r7   r9   rL   nodereturnNc              
   C   sd  |j dkrd S |j}|j}t|tjrt|jtsd S |j}zt	|\}}}}W n8 tj
yP }	 z||	j }
| jd||
t|
|	jfd W Y d }	~	d S d }	~	w tjya   | jd|d Y d S w |so|so| jd|d d S |r||r|| jd|d d S |r-t|tjrt }d}|jD ]#\}}t|tjr|j}t|tr|| q| jd	||d qd
}q|s|D ]}||vr| jd||d q|D ]}||vr| jd||d q|jD ]7\}}t|tjsq||jd }t|}|d ur|r|tjkrt||s| jd|| |fd qd S t|ttjfr+t|j}| jd||d d S d S g }t|tjrLt|}d }t|tjrK|j}t|}nt|ttjtj ffr^|g}d}nd }|d ur||krr| jd|d n||k r~| jd|d t!||D ],\}}|sqt|}|r|tjkrt||s| jd|| |fd qd S d S )N%r)   rj   argsr+   rj   rL   r-   Fr1   Tr5   r3   r;   r/      r7   r9   )"opleftrightra   r   ConstvaluerU   r   parse_format_stringUnsupportedFormatCharacterindexadd_messageordIncompleteFormatStringDictsetitemsaddget
safe_inferrb   Uninferablerg   rd   OTHER_NODESTupletype__name__BaseContainereltslenDictCompzip)selfrj   rr   rn   format_stringrequired_keysrequired_num_argsrequired_key_typesrequired_arg_typesexc	formattedkeysunknown_keysk_rW   argrf   re   	type_name	args_elts	rhs_tuplenum_argsr\   r\   r]   visit_binop  s   












zStringFormatChecker.visit_binoprK   c                 C   sB   t |jtjr	d S |jD ]}t |tjr d S q| jd|d d S )NrK   ro   )ra   parentr   FormattedValuevaluesry   )r   rj   ru   r\   r\   r]   visit_joinedstr  s   
z#StringFormatChecker.visit_joinedstrc                 C   s   t |j}t|tjrat|jtjrc|jjdv re|jdv rT|j	rTt |j	d }t|t
jr5t|jts7d S t|jtt|jkrR| jd||jj|jfd d S d S |jdkrg| || d S d S d S d S d S )N)rU   unicodebytes)striplstriprstripr   r=   rm   format)r   r   funcra   rb   BoundMethodboundrc   namern   r   rt   ru   rU   r   r}   ry   _check_new_format)r   rj   r   r   r\   r\   r]   
visit_call  s,   


zStringFormatChecker.visit_callc                 C   sF   t dd |D }| D ]\}}|dkrq| jd||fd qd S )Nc                 s   s"    | ]}t |tjr|jV  qd S N)ra   r   Namer   ).0r   r\   r\   r]   	<genexpr>  s    
zAStringFormatChecker._detect_vacuous_formatting.<locals>.<genexpr>rp   rJ   rm   )collectionsr   r~   ry   )r   rj   positional_argumentscounterr   countr\   r\   r]   _detect_vacuous_formatting  s   
z.StringFormatChecker._detect_vacuous_formattingc                 C   s*  t |jtjrt |jjtjsdS |js|jrdS z	t|j	
 }W n tjy-   Y dS w t |tjr:t |jts<dS z	tjj|}W n tjyP   Y dS w zt|j\}}}W n tjyn   | jd|d Y dS w |j}|j}	dd |D }
|r|r| jd|d dS d}|tdd	 |
D 7 }|
r|
D ]}|r||	vr| jd
||fd q|	D ]}||
vr| jd||fd q|p|}|s|rtdd	 |
D }|	s|rd}nd}|r|p|}|s| jd|d dS t||kr| jd|d nt||k r| jd|d | || | |||	 dS )z Check the new string formatting.Nr?   ro   c                 S   s"   h | ]}t |d  tr|d  qS )r   )ra   rU   r   fieldr\   r\   r]   	<setcomp>  s   " z8StringFormatChecker._check_new_format.<locals>.<setcomp>rE   Fc                 s       | ]	}|d krdV  qdS )rS   rp   Nr\   r   r\   r\   r]   r         z8StringFormatChecker._check_new_format.<locals>.<genexpr>rA   rm   rC   c                 s   r   )rS   TNr\   r   r\   r\   r]   r     r   TrL   r7   r9   )ra   r   r   	Attributeexprrt   starargskwargsnextr   inferrb   InferenceErrorru   rU   	argumentsCallSite	from_callr   parse_format_method_stringr{   ry   r   keyword_argumentssumanyr   r   _check_new_format_specifiers)r   rj   r   strnode	call_sitefieldsr   
manual_posr   named_argumentsnamed_fields
check_argsr   emptyr\   r\   r]   r     s   
z%StringFormatChecker._check_new_formatc                 C   s2  |D ]\}}|dkrd}t |tjr&zt||}W n tjy%   Y qw ||vr+q|| }|tjdfv r7qzt|}W n
 tj	yH   Y qw |rM|sNq|j
rYt |j
tjrYq|}g }	|D ]\}
}|tju rj n|	|
|f |
rz	||d }W n tjy   t|dr| rY  nt||	}| jd||f|d Y  ntw d}t|drz
|t|}W n tjtjtjfy   d	}Y n tj	y   Y  nGw |tju r n?nz|d
 W  n5 tjy   d	}Y nw |rt||	}| jd||f|d  nzt| }W q_ tj	y   Y  nw qdS )zg
        Check attribute and index access in the format
        string ("{0.a}" and "{0[a]}").
        rS   r   Nhas_dynamic_getattrrG   )rn   rj   FgetitemT__getitem__rI   )ra   numbersNumberr   get_argument_from_callNoSuchArgumentErrorrb   r   r   r   r   r   	ArgumentsrT   getattrNotFoundErrorhasattrr   r^   ry   r   rt   AstroidIndexErrorAstroidTypeErrorAttributeInferenceErrorr   r   )r   rj   r   namedrW   
specifiersargnameargumentpreviousparsedrZ   r[   rY   
warn_errorr\   r\   r]   r     s   







z0StringFormatChecker._check_new_format_specifiers)r   
__module____qualname____doc__r	   __implements__r   MSGSmsgsr   r   BinOpr   	JoinedStrr   Callr   r   r   r   r\   r\   r\   r]   rh     s8     Trh   c                       sX  e Zd ZdZeeefZdZddddddd	gifd
ddZ	ddddddfddddddffZ
dZdZ fddZdejddfddZdd Zeddejddfd d!Zeddejddfd"d#Zeddejddfd$d%Zdejddfd&d'Zd(eej ddfd)d*Zd+d, Zd-d. Z d/d0 Z!ed1dej"ddfd2d3Z#dej"fd4d5Z$  Z%S )6StringConstantCheckerzCheck string literalsri   )zRAnomalous backslash in string: '%s'. String constant might be missing an r prefix.anomalous-backslash-in-stringzBUsed when a backslash is in a literal string but not as an escape.)zaAnomalous Unicode escape in byte string: '%s'. String constant might be missing an r or u prefix."anomalous-unicode-escape-in-stringzSUsed when an escape like \u is encountered in a byte string where it has no effect.z)Implicit string concatenation found in %simplicit-str-concatziString literals are implicitly concatenated in a literal iterable definition : maybe a comma is missing ?	old_names)W1403zimplicit-str-concat-in-sequence)z<Quote delimiter %s is inconsistent with the rest of the fileinconsistent-quoteszxQuote delimiters are not used consistently throughout a module (with allowances made for avoiding unnecessary escaping).)z?The u prefix for strings is no longer necessary in Python >=3.0redundant-u-string-prefixzUsed when we detect a string with a u prefix. These prefixes were necessary in Python 2 to indicate a string was Unicode, but since Python 3.0 strings are Unicode by default.)W1401W1402W1404W1405W1406z check-str-concat-over-line-jumpsFynz<y_or_n>zThis flag controls whether the implicit-str-concat should generate a warning on implicit string concatenation in sequences defined over several lines.)defaultr   metavarhelpzcheck-quote-consistencyzThis flag controls whether inconsistent-quotes generates a warning when the character used as a quote delimiter is used inconsistently within a module.zabfnrtvx
	\'"01234567uUNc                    s   t  j|i | i | _d S r   )super__init__string_tokens)r   rn   r   	__class__r\   r]   r    s   
zStringConstantChecker.__init__rj   rk   Nc                 C   s   d|j v | _d S )Nunicode_literals)future_imports_unicode_literalsr   rj   r\   r\   r]   process_module  s   z$StringConstantChecker.process_modulec                 C   s  d}t |D ]y\}\}}}}}|tjkr|}q|tjkr| ||d |d  |d }	|	t|k rU||	 jtjtjtj	fv rU|	d7 }	|	t|k rU||	 jtjtjtj	fv s>|	t|k r_||	 nd }
|dkrv|d t|d |d  
|f}t||
f| j|< q| jjr| | d S d S )Nasciir   rp   )	enumeratetokenizeENCODINGSTRINGprocess_string_tokenr   r   NEWLINENLCOMMENTencodestr_evalr  configcheck_quote_consistency&check_for_consistent_string_delimiters)r   tokensencodingitok_typetokenstartr   linej
next_tokenr\   r\   r]   process_tokens  s4   

"z$StringConstantChecker.process_tokensc                 C      |  |jd d S )Nlistcheck_for_concatenated_stringsr   r  r\   r\   r]   
visit_list     z StringConstantChecker.visit_listc                 C   r%  )Nr}   r'  r  r\   r\   r]   	visit_set  r*  zStringConstantChecker.visit_setc                 C   r%  )Ntupler'  r  r\   r\   r]   visit_tuple  r*  z!StringConstantChecker.visit_tuplec                 C   s8   t |jtjrt |jjtr| |jgd d S d S d S )N
assignment)ra   ru   r   rt   rU   r(  r  r\   r\   r]   visit_assign  s   z"StringConstantChecker.visit_assignr  c           	      C   s   t  }|D ]\}}}}}|tjkr t|r |t|  d7  < qt|dkrW|dd d }|D ]&\}}}}}|tjkr?q2t|}t|rV||krV| jd|d |fd q2dS dS )a\  Adds a message for each string using inconsistent quote delimiters.

        Quote delimiters are used inconsistently if " and ' are mixed in a module's
        shortstrings without having done so to avoid escaping an internal quote
        character.

        Args:
          tokens: The tokens to be checked against for consistent usage.
        rp   r   r   r!  rn   N)	r   r   r  r  !_is_quote_delimiter_chosen_freely_get_quote_delimiterr   most_commonry   )	r   r  string_delimitersr  r  r   most_common_delimiterr   quote_delimiterr\   r\   r]   r    s(   
z<StringConstantChecker.check_for_consistent_string_delimitersc                 C   s   |D ]O}t |tjr| tv sq|jdk rq|j|jf| jvr!q| j|j|jf \}}||jkrQ|d urQ|j	t
jkrQ|jd |jksG| jjrQ| jd|j|fd qd S )Nr   r   r0  )ra   r   rt   rd   _AST_NODE_STR_TYPES
col_offsetlinenor  ru   r   r  r  r   r   check_str_concat_over_line_jumpsry   )r   elementsiterable_typeeltmatching_tokenr#  r\   r\   r]   r(    s,   



z4StringConstantChecker.check_for_concatenated_stringsc                 C   s   d }d }t |D ]\}}|dv r|} nq|d u rd S |d |  }||d  }|d d |dd    kr=d| krAn ndnd}	||	|	  }
d|vr^| ||
||t| |	  d S d S )Nz'"   rp   r   )r  lowerprocess_non_raw_string_tokenr   )r   r  	start_row	start_col
quote_charrx   charprefixafter_prefixquote_lengthstring_bodyr\   r\   r]   r  6  s,   2z*StringConstantChecker.process_string_tokenc                 C   s   d}	 | d|}|dkrdS ||d  }|||d  }|dd|}|dkr/|}	|| }
n||dd| }	|| d }
|| jv rXd|v rHnd	|vrMn| jd
|	|f|
d n|| jvrg| jd|	|f|
d |d7 }q)a[  check for bad escapes in a non-raw string.

        prefix: lowercase string of eg 'ur' string prefix markers.
        string_body: the un-parsed body of the string, not including the quote
        marks.
        start_row: integer line number in the source.
        string_start_col: integer col number of the string start in the source.
        r   T\rp      
r   r   r   )r!  rn   r8  r   N)findrfindr   UNICODE_ESCAPE_CHARACTERSry   ESCAPE_CHARACTERS)r   rG  rJ  rC  string_start_colrx   	next_charmatchlast_newliner!  r8  r\   r\   r]   rB  P  sB   


	z2StringConstantChecker.process_non_raw_string_tokenr   c                 C   s0   |  dkrt|jtjs| | d S d S d S )Nr   )rd   ra   r   r   r   _detect_u_string_prefixr  r\   r\   r]   visit_const  s
   z!StringConstantChecker.visit_constc                 C   s&   |j dkr| jd|j|jd dS dS )z9Check whether strings include a 'u' prefix like u'String'r   r   )r!  r8  N)kindry   r9  r8  r  r\   r\   r]   rW    s   

z-StringConstantChecker._detect_u_string_prefix)&r   r   r   r   r	   r   r
   r   r   r   optionsrR  rQ  r  r   Moduler  r$  r   Listr)  Setr+  r   r-  Assignr/  r   r  	TokenInfor  r(  r  rB  rt   rX  rW  __classcell__r\   r\   r  r]   r   z  sh    
&
#>r   c                 C   s    |  t|  |  t|  dS )z-required method to auto register this checkerN)register_checkerrh   r   )linterr\   r\   r]   register  s   rc  c                 C   sf   | dd   dv r| dd } n| d   dv r| dd } | dd dv r-| dd	 S | dd
 S )a%  
    Mostly replicate `ast.literal_eval(token)` manually to avoid any performance hit.
    This supports f-strings, contrary to `ast.literal_eval`.
    We have to support all string literal notations:
    https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
    r   rM  )r!   r%   N)r   r   r   rp   r?  )z"""z'''r@  rL  )rA  )r  r\   r\   r]   r    s   r  string_tokenrk   c                 C   s   t t| p
t| S )ay  Is this string token a "longstring" (is it triple-quoted)?

    Long strings are triple-quoted as defined in
    https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

    This function only checks characters up through the open quotes.  Because it's meant
    to be applied only to tokens that represent string literals, it doesn't bother to
    check for close-quotes (demonstrating that the literal is a well-formed string).

    Args:
        string_token: The string token to be parsed.

    Returns:
        A boolean representing whether or not this token matches a longstring
        regex.
    )boolSINGLE_QUOTED_REGEXrU  DOUBLE_QUOTED_REGEX)rd  r\   r\   r]   _is_long_string  s   
rh  c                 C   s(   t | }|std|  d|dS )a  Returns the quote character used to delimit this token string.

    This function does little checking for whether the token is a well-formed
    string.

    Args:
        string_token: The token to be parsed.

    Returns:
        A string containing solely the first quote delimiter character in the passed
        string.

    Raises:
      ValueError: No quote delimiter characters are present.
    zstring token z is not a well-formed stringrM  )QUOTE_DELIMITER_REGEXrU  
ValueErrorgroup)rd  rU  r\   r\   r]   r2    s   

r2  c                 C   s6   t | }|dkr
dnd}t|ot|  o|t| vS )a  Was there a non-awkward option for the quote delimiter?

    Args:
        string_token: The quoted string whose delimiters are to be checked.

    Returns:
        Whether there was a choice in this token's quote character that would
        not have involved backslash-escaping an interior quote character.  Long
        strings are excepted from this analysis under the assumption that their
        quote characters are set by policy.
    '")r2  re  rh  r  )rd  r6  unchosen_delimiterr\   r\   r]   r1    s   
r1  )1r   r   r   rer  typingr   r   rb   r   pylint.checkersr   r   r   pylint.checkers.utilsr   pylint.interfacesr	   r
   r   r   r7  	_PREFIXEScompilerV   rf  rg  DOTALLri  r   rt   r\  LambdaFunctionDefListCompSetCompGeneratorExpr   r^   rg   rh   r   rc  r  rU   re  rh  r2  r1  r\   r\   r\   r]   <module>   s   " '.4:@EJPW_fm   {  '