B
    A!p\ØÊ  ã               @   sP  d Z ddlmZmZ ddlmZ ddlZddlZddlZddl	m
Z
 ddlZddlmZ ddlmZmZ ddlmZmZ dd	lmZmZ dd
lmZ ddlmZ e d¡Zdd„ Zdd„ Zdd„ Zdddddddddddddd d!d"hZd#d$hZ d%d&d'hZ!d(d)d*hZ"d+d,„ Z#d-d.„ Z$ed/d0d1gƒZ%G d2d3„ d3ej&ƒZ'G d4d5„ d5ej(ƒZ)d6d7„ Z*dS )8z<Check Python 2 code for Python 2/3 source-compatible issues.é    )Úabsolute_importÚprint_function)Ú
namedtupleN)Ú	FrozenSet)Úbases)ÚcheckersÚ
interfaces)Únode_ignores_exceptionÚfind_try_except_wrapper_node)ÚINFERENCE_FAILUREÚ	INFERENCE)ÚWarningScope)Úutilsz^0+$c             C   sF   t  | ¡rdS t d| ¡rByt| dƒ W n tk
r<   dS X dS d S )NFz0\d+é   T)Ú_ZEROÚmatchÚreÚintÚ
ValueError)Úliteral© r   ú6lib/python3.7/site-packages/pylint/checkers/python3.pyÚ_is_old_octal0   s    
r   c             C   s&   t | tjƒrdS t | tjƒo$d| jkS )NTÚdict)Ú
isinstanceÚastroidZDictÚInstanceZ	basenames)Úvaluer   r   r   Ú_inferred_value_is_dict<   s    r   c             C   s   t | dd ƒdkS )NÚname)Ú__builtin__Úbuiltins)Úgetattr)Únoder   r   r   Ú_is_builtinB   s    r$   ÚiterÚlistÚtupleÚsortedÚsetÚsumÚanyÚallÚ	enumerater   ÚfilterÚreversedÚmaxÚminÚ	frozensetÚOrderedDictÚjoinÚfrom_iterablezbuiltins.list.extendzbuiltins.dict.updatezbuiltins.set.updateÚitemsÚkeysÚvaluesc             C   sx  | j }t|tjƒrdS t|tjƒr4|j| kr0dS n.t|tjƒrÖt|jtjƒr||j 	|jj
¡d }t|ƒrš|jj
tkršdS nt|jtjƒrš|jjtkršdS t |j¡}|rÔ| ¡ tkrºdS | ¡ }|rÔ|j
dkrÔdS nŒt|tjƒrt|jd tjtjfƒrt|jd jƒdkrbdS nHt|tjƒrPt|jƒdkrP|jd d dkrPdS t|tjƒrbdS t|tjƒrtdS dS )z{Check if the node is being used as an iterator.

    Definition is taken from lib2to3.fixer_util.in_special_context().
    Tr   Ú	itertoolsé   ÚinF)Úparentr   r   ZForZComprehensionr%   ZCallÚfuncÚNameÚlookupr   r$   Ú_ACCEPTS_ITERATORÚ	AttributeÚattrnameÚATTRIBUTES_ACCEPTS_ITERATORr   Ú
safe_inferÚqnameÚ _BUILTIN_METHOD_ACCEPTS_ITERATORÚrootZAssignZtargetsÚListÚTupleÚlenZeltsÚCompareZopsZ	YieldFromZStarred)r#   r<   Zparent_scopeÚinferredrG   r   r   r   Ú_in_iterating_contexta   sD    
rM   c             C   s    | j }t|tjtjtjtjfƒS )zAChecks if an import node is in the context of a conditional.
    )r<   r   r   Z	TryExceptÚExceptHandlerZIfZIfExp)r#   r<   r   r   r   Ú_is_conditional_importš   s    rO   ÚBranchr#   Úis_py2_onlyc            a       sŒ  e Zd ZejZdZdZddddddd	gifd
ddddgifdddej	dgdœfdddddifddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1dd2gifd3d4d5dd6gifd7d8d9d:d;d<d=d>d?d@dAdBddCgifdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^œCZ
ed_d`dadbdcdddedfdgdhdidjdkdldmdndodpgƒZedqdrdsdtdudvdwdxdydzd{gƒZed|d}d~dd€dd‚dƒd„d…d†d‡dˆd‰dŠd‹dŒdgƒZdŽedgƒided‘d’d“d”d•gƒid–ed—d˜d™dšd›dœddždŸd d¡d¢d£d¤d¥d¦d§d¨d©dªd«d¬d­d®d¯gƒie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Ï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ï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g]ƒde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,d-d.d/d0g$ƒid1ed2hƒid3ed4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNdOdPdQdRdSdTdUdVdWdXdYdZh'ƒidŽed[hƒid\œZd]ej  kr:d^k rFn neƒ Zned_d`„ daD ƒƒZ‡ fdbdc„Zd¬‡ fddde„	Zdfdg„ Zdhdi„ Zdjdk„ Zdldm„ Zdndo„ Zdpdq„ Zdrds„ Ze dt¡dudv„ ƒZe dw¡dxdy„ ƒZdzd{„ Z e d|¡d}d~„ ƒZ!d­d€d„Z"d‚dƒ„ Z#d„d…„ Z$e d†¡d‡dˆ„ ƒZ%e d‰¡dŠd‹„ ƒZ&dŒd„ Z'e(dŽd„ ƒZ)e(dd‘„ ƒZ*e(d’d“„ ƒZ+e(d”d•„ ƒZ,d–d—„ Z-d˜d™„ Z.e d0¡dšd›„ ƒZ/dœd„ Z0dždŸ„ Z1e d d¡¡d¢d£„ ƒZ2e ddw¡d¤d¥„ ƒZ3e d¡d¦d§„ ƒZ4e d4d¡d¨d©„ ƒZ5dªd«„ Z6‡  Z7S (®  ÚPython3CheckerFÚpython3)zprint statement usedzprint-statementzGUsed when a print statement is used (`print` is a function in Python 3))zParameter unpacking specifiedzparameter-unpackingzTUsed when parameter unpacking is specified for a function(Python 3 doesn't allow it)z=Implicit unpacking of exceptions is not supported in Python 3zunpacking-in-exceptzwPython3 will not allow implicit unpacking of exceptions in except clauses. See http://www.python.org/dev/peps/pep-3110/Ú	old_names)ZW0712zunpacking-in-exceptz=Use raise ErrorClass(args) instead of raise ErrorClass, args.zold-raise-syntaxzZUsed when the alternate raise syntax 'raise foo, bar' is used instead of 'raise foo(bar)'.)ZW0121zold-raise-syntaxzUse of the `` operatorÚbacktickzYUsed when the deprecated "``" (backtick) operator is used instead  of the str() function.)ZW0333rU   )ÚscoperT   z%Import * only allowed at module levelzimport-star-module-levelzNUsed when the import star syntax is used somewhere else than the module level.Ú
maxversion)é   r   )zapply built-in referencedzapply-builtinzKUsed when the apply built-in function is referenced (missing from Python 3))zbasestring built-in referencedzbasestring-builtinzPUsed when the basestring built-in function is referenced (missing from Python 3))zbuffer built-in referencedzbuffer-builtinzLUsed when the buffer built-in function is referenced (missing from Python 3))zcmp built-in referencedzcmp-builtinzIUsed when the cmp built-in function is referenced (missing from Python 3))zcoerce built-in referencedzcoerce-builtinzLUsed when the coerce built-in function is referenced (missing from Python 3))zexecfile built-in referencedzexecfile-builtinzNUsed when the execfile built-in function is referenced (missing from Python 3))zfile built-in referencedzfile-builtinzJUsed when the file built-in function is referenced (missing from Python 3))zlong built-in referencedzlong-builtinzJUsed when the long built-in function is referenced (missing from Python 3))zraw_input built-in referencedzraw_input-builtinzOUsed when the raw_input built-in function is referenced (missing from Python 3))zreduce built-in referencedzreduce-builtinzLUsed when the reduce built-in function is referenced (missing from Python 3))z!StandardError built-in referencedzstandarderror-builtinzSUsed when the StandardError built-in function is referenced (missing from Python 3))zunicode built-in referencedzunicode-builtinzMUsed when the unicode built-in function is referenced (missing from Python 3))zxrange built-in referencedzxrange-builtinzLUsed when the xrange built-in function is referenced (missing from Python 3))z__coerce__ method definedzcoerce-methodzIUsed when a __coerce__ method is defined (method is not used by Python 3))z__delslice__ method definedzdelslice-methodzKUsed when a __delslice__ method is defined (method is not used by Python 3))z__getslice__ method definedzgetslice-methodzKUsed when a __getslice__ method is defined (method is not used by Python 3))z__setslice__ method definedzsetslice-methodzKUsed when a __setslice__ method is defined (method is not used by Python 3))z7import missing `from __future__ import absolute_import`zno-absolute-importztUsed when an import is not accompanied by ``from __future__ import absolute_import`` (default behaviour in Python 3))z!division w/o __future__ statementzold-divisionz’Used for non-floor division w/o a float literal or ``from __future__ import division`` (Python 3 returns a float for int division unconditionally))zCalling a dict.iter*() methodzdict-iter-methodz]Used for calls to dict.iterkeys(), itervalues() or iteritems() (Python 3 lacks these methods))zCalling a dict.view*() methodzdict-view-methodz]Used for calls to dict.viewkeys(), viewvalues() or viewitems() (Python 3 lacks these methods))z#Called a next() method on an objectznext-method-calledzZUsed when an object's next() method is called (Python 3 uses the next() built-in function))z.Assigning to a class's __metaclass__ attributezmetaclass-assignmentzUsed when a metaclass is specified by assigning to __metaclass__ (Python 3 specifies the metaclass as a class statement argument)z-Indexing exceptions will not work on Python 3zindexing-exceptionzSIndexing exceptions will not work on Python 3. Use `exception.args[index]` instead.)ZW0713zindexing-exceptionzRaising a string exceptionzraising-stringzGUsed when a string exception is raised. This will not work on Python 3.)ZW0701zraising-string)zreload built-in referencedzreload-builtinzUsed when the reload built-in function is referenced (missing from Python 3). You can use instead imp.reload or importlib.reload.)z__oct__ method definedz
oct-methodzGUsed when an __oct__ method is defined (method is not used by Python 3))z__hex__ method definedz
hex-methodzFUsed when a __hex__ method is defined (method is not used by Python 3))z__nonzero__ method definedznonzero-methodzJUsed when a __nonzero__ method is defined (method is not used by Python 3))z__cmp__ method definedz
cmp-methodzFUsed when a __cmp__ method is defined (method is not used by Python 3))zinput built-in referencedzinput-builtinzYUsed when the input built-in is referenced (backwards-incompatible semantics in Python 3))zround built-in referencedzround-builtinzYUsed when the round built-in is referenced (backwards-incompatible semantics in Python 3))zintern built-in referencedzintern-builtinzMUsed when the intern built-in is referenced (Moved to sys.intern in Python 3))zunichr built-in referencedzunichr-builtinzAUsed when the unichr built-in is referenced (Use chr in Python 3)z*map built-in referenced when not iteratingzmap-builtin-not-iteratingzeUsed when the map built-in is referenced in a non-iterating context (returns an iterator in Python 3))ZW1631zimplicit-map-evaluation)z*zip built-in referenced when not iteratingzzip-builtin-not-iteratingzeUsed when the zip built-in is referenced in a non-iterating context (returns an iterator in Python 3))z,range built-in referenced when not iteratingzrange-builtin-not-iteratingzgUsed when the range built-in is referenced in a non-iterating context (returns an iterator in Python 3))z-filter built-in referenced when not iteratingzfilter-builtin-not-iteratingzhUsed when the filter built-in is referenced in a non-iterating context (returns an iterator in Python 3))z-Using the cmp argument for list.sort / sortedzusing-cmp-argumentz±Using the cmp argument for list.sort or the sorted builtin should be avoided, since it was removed in Python 3. Using either `key` or `functools.cmp_to_key` should be preferred.)z6Implementing __eq__ without also implementing __hash__zeq-without-hashzãUsed when a class implements __eq__ but not __hash__.  In Python 2, objects get object.__hash__ as the default implementation, in Python 3 objects get None as their default __hash__ implementation if they also implement __eq__.)z__div__ method definedz
div-methodz‘Used when a __div__ method is defined.  Using `__truediv__` and setting__div__ = __truediv__ should be preferred.(method is not used by Python 3))z__idiv__ method definedzidiv-methodz–Used when an __idiv__ method is defined.  Using `__itruediv__` and setting__idiv__ = __itruediv__ should be preferred.(method is not used by Python 3))z__rdiv__ method definedzrdiv-methodz•Used when a __rdiv__ method is defined.  Using `__rtruediv__` and setting__rdiv__ = __rtruediv__ should be preferred.(method is not used by Python 3))z%Exception.message removed in Python 3zexception-message-attributezYUsed when the message attribute is accessed on an Exception.  Use str(exception) instead.)z$non-text encoding used in str.decodezinvalid-str-codeczqUsed when using str.encode or str.decode with a non-text encoding.  Use codecs module to handle arbitrary codecs.)zsys.maxint removed in Python 3zsys-max-intz9Used when accessing sys.maxint.  Use sys.maxsize instead.)zModule moved in Python 3zbad-python3-importz?Used when importing a module that no longer exists in Python 3.)z4Accessing a deprecated function on the string modulezdeprecated-string-functionzKUsed when accessing a string function that has been deprecated in Python 3.)z:Using str.translate with deprecated deletechars parameterszdeprecated-str-translate-callzwUsed when using the deprecated deletechars parameters from str.translate.  Use re.sub to remove the desired characters )z7Accessing a deprecated function on the itertools modulezdeprecated-itertools-functionzNUsed when accessing a function on itertools that has been removed in Python 3.)z1Accessing a deprecated fields on the types modulezdeprecated-types-fieldzGUsed when accessing a field on types that has been removed in Python 3.)znext method definedznext-method-definedzyUsed when a next method is defined that would be an iterator in Python 2 but is treated as a normal function in Python 3.)z(dict.items referenced when not iteratingzdict-items-not-iteratingz_Used when dict.items is referenced in a non-iterating context (returns an iterator in Python 3))z'dict.keys referenced when not iteratingzdict-keys-not-iteratingz^Used when dict.keys is referenced in a non-iterating context (returns an iterator in Python 3))z)dict.values referenced when not iteratingzdict-values-not-iteratingz`Used when dict.values is referenced in a non-iterating context (returns an iterator in Python 3))z4Accessing a removed attribute on the operator modulezdeprecated-operator-functionzQUsed when accessing a field on operator module that has been removed in Python 3.)z2Accessing a removed attribute on the urllib modulezdeprecated-urllib-functionzXUsed when accessing a field on urllib module that has been removed or moved in Python 3.)z(Accessing a removed xreadlines attributezxreadlines-attributezTUsed when accessing the xreadlines() function on a file stream, removed in Python 3.)z/Accessing a removed attribute on the sys modulezdeprecated-sys-functionzLUsed when accessing a field on sys module that has been removed in Python 3.)z=Using an exception object that was bound by an except handlerzexception-escapezºEmitted when using an exception, that was bound in an except handler, outside of the except handler. On Python 3 these exceptions will be deleted once they get out of the except handler.)z6Using a variable that was bound inside a comprehensionzcomprehension-escapez¸Emitted when using a variable, that was bound in a comprehension handler, outside of the comprehension itself. On Python 3 these variables will be deleted outside of the comprehension.)CZE1601ZE1602ZE1603ZE1604ZE1605ZE1609ZW1601ZW1602ZW1603ZW1604ZW1605ZW1606ZW1607ZW1608ZW1609ZW1610ZW1611ZW1612ZW1613ZW1614ZW1615ZW1616ZW1617ZW1618ZW1619ZW1620ZW1621ZW1622ZW1623ZW1624ZW1625ZW1626ZW1627ZW1628ZW1629ZW1630ZW1632ZW1633ZW1634ZW1635ZW1636ZW1637ZW1638ZW1639ZW1640ZW1641ZW1642ZW1643ZW1644ZW1645ZW1646ZW1647ZW1648ZW1649ZW1650ZW1651ZW1652ZW1653ZW1654ZW1655ZW1656ZW1657ZW1658ZW1659ZW1660ZW1661ZW1662ZapplyZ
basestringÚbufferÚcmpZcoerceZexecfileÚfileÚinputÚinternZlongZ	raw_inputÚreduceÚroundZStandardErrorZunichrZunicodeZxrangeÚreloadZ
__coerce__Z__delslice__Z__getslice__Z__setslice__Z__oct__Z__hex__Z__nonzero__Z__cmp__Z__div__Z__idiv__Z__rdiv__Úbase64_codecÚbase64Úbase_64Ú	bz2_codecÚbz2Ú	hex_codecÚhexÚquopri_codecÚquopriÚquotedprintableÚquoted_printableÚuu_codecÚuuÚ
zlib_codecÚzlibÚzipÚrot13Úrot_13ÚsysZmaxintr9   ZizipZifilterZimapZizip_longestZifilterfalseÚtypesZEllipsisTypeZ
XRangeTypeZComplexTypeZ
StringTypeZTypeTypeZLongTypeZUnicodeTypeZ	ClassTypeZ
BufferTypeZStringTypesZNotImplementedTypeZNoneTypeZInstanceTypeZ	FloatTypeZ	SliceTypeZUnboundMethodTypeZ
ObjectTypeZIntTypeZ	TupleTypeZListTypeZDictTypeZFileTypeZDictionaryTypeZBooleanTypeZDictProxyTypeZanydbmZBaseHTTPServerr    ZCGIHTTPServerZConfigParserZcopy_regZcPickleZ	cStringIOZCookieZ	cookielibZdbhashZdumbdbmZdumbdbZDialogZDocXMLRPCServerZ
FileDialogZFixTkZgdbmZhtmlentitydefsZ
HTMLParserZhttplibZ
markupbaseZQueueÚreprZrobotparserZScrolledTextZSimpleDialogZSimpleHTTPServerZSimpleXMLRPCServerÚStringIOZdummy_threadZSocketServerztest.test_supportZTkinterZTixZTkconstantsZtkColorChooserZtkCommonDialogZTkdndZtkFileDialogZtkFontZtkMessageBoxZtkSimpleDialogÚUserListÚ
UserStringZwhichdbÚ_winregZ	xmlrpclibZaudiodevZBastionZbsddb185Zbsddb3ZCanvasZcfmfileZclZcommandsZcompilerZdircacheZdlZ	exceptionZfpformatZhtmllibZihooksZimageopZimputilZlinuxaudiodevZmd5ZmhlibZ	mimetoolsZ
MimeWriterZmimifyZ	multifileZmutexÚnewZpopen2Z	posixfileZpureZrexecZrfc822ZsetsZshaZsgmllibZsreZ	stringoldZsunaudioZsvztest.testallZthreadZtimingZtoaiffÚuserZurllib2ZurlparseÚstringÚ	maketransZatofZatoiZatolÚ
capitalizeÚ
expandtabsÚfindÚrfindÚindexÚrindexÚcountÚlowerZlettersÚsplitÚrsplitZsplitfieldsr4   Z
joinfieldsÚlstripÚrstripÚstripÚswapcaseÚ	translateÚupperÚljustÚrjustÚcenterÚzfillÚreplaceZ	lowercaseZ	uppercaseZ
atol_errorZ
atof_errorZ
atoi_errorZindex_errorÚoperatorZdivZurllibZaddbaseZaddclosehookZaddinfoZ
addinfourlZalways_safeZbasejoinZftpcacheZ	ftperrorsZ
ftpwrapperZ
getproxiesZgetproxies_environmentZgetproxies_macosx_sysconfÚmainZ	noheadersZpathname2urlZproxy_bypassZproxy_bypass_environmentZproxy_bypass_macosx_sysconfZquoteZ
quote_plusZ
reporthookZ	splitattrZ	splithostZ
splitnportZsplitpasswdZ	splitportZ
splitqueryZsplittagZ	splittypeZ	splituserZ
splitvalueZunquoteZunquote_plusZunwrapZurl2pathnameZ
urlcleanupZ	urlencodeZurlopenZurlretrieveZ	exc_clear)zsys-max-intzdeprecated-itertools-functionzdeprecated-types-fieldzbad-python3-importzdeprecated-string-functionzdeprecated-operator-functionzdeprecated-urllib-functionzdeprecated-sys-function)rX   é   )rX   r•   r•   c             C   s   g | ]}t  |¡ ¡ ‘qS r   )r   Zextract_nodeÚ	repr_tree)Ú.0Úxr   r   r   ú
<listcomp>y  s   zPython3Checker.<listcomp>)zsys.version_info[0] == 2zsys.version_info[0] < 3zsys.version_info == (2, 7)zsys.version_info <= (2, 7)zsys.version_info < (3, 0)c                s0   d| _ d| _tƒ | _g | _tt| ƒj||Ž d S )NF)Ú_future_divisionÚ_future_absolute_importr)   Ú_modules_warned_aboutÚ_branch_stackÚsuperrR   Ú__init__)ÚselfÚargsÚkwargs)Ú	__class__r   r   rŸ   „  s
    zPython3Checker.__init__c                s2   |s| j r| j d js.tt| ƒj|f|ž|Ž d S )Néÿÿÿÿ)r   rQ   rž   rR   Úadd_message)r    Zmsg_idÚalways_warnr¡   r¢   )r£   r   r   r¥   Œ  s    zPython3Checker.add_messagec             C   sd   t |jtjƒr>t |jjtjƒr>|jjjdkr`|jjdkr`dS n"t |jtjƒr`|j 	¡ | j
kr`dS dS )NZsixZPY2TF)r   Ztestr   rA   Úexprr>   r   rB   rK   r–   Ú_python_2_tests)r    r#   r   r   r   Ú_is_py2_test’  s    zPython3Checker._is_py2_testc             C   s   | j  t||  |¡ƒ¡ d S )N)r   ÚappendrP   r©   )r    r#   r   r   r   Úvisit_ifŸ  s    zPython3Checker.visit_ifc             C   s   | j  ¡ j|kst‚d S )N)r   Úpopr#   ÚAssertionError)r    r#   r   r   r   Úleave_if¢  s    zPython3Checker.leave_ifc             C   s   | j  t||  |¡ƒ¡ d S )N)r   rª   rP   r©   )r    r#   r   r   r   Úvisit_ifexp¥  s    zPython3Checker.visit_ifexpc             C   s   | j  ¡ j|kst‚d S )N)r   r¬   r#   r­   )r    r#   r   r   r   Úleave_ifexp¨  s    zPython3Checker.leave_ifexpc             C   s   d| _ d| _dS )z*Clear checker state after previous module.FN)rš   r›   )r    r#   r   r   r   Úvisit_module«  s    zPython3Checker.visit_modulec             C   s   |  ¡ rŒ|j| jkrH|j}|j d¡r4|jdd… }| j|d |d nD|jdkrŒd}t |tjd g¡rnd	}t	|j
j
ƒ|krŒ| jd
|d d S )NÚ__é   éþÿÿÿz-method)r#   Únextr:   z.staticmethodr   znext-method-defined)Z	is_methodr   Ú_unused_magic_methodsÚ
startswithr¥   r   Zdecorated_withr   ÚBUILTINSrJ   r¡   )r    r#   Zmethod_nameZfailing_arg_countr   r   r   Úvisit_functiondef°  s    
z Python3Checker.visit_functiondefzparameter-unpackingc             C   s.   x(|j D ]}t|tjƒr| jd|d qW d S )Nzparameter-unpacking)r#   )r¡   r   r   rI   r¥   )r    r#   Úargr   r   r   Úvisit_argumentsÃ  s    zPython3Checker.visit_argumentszcomprehension-escapec                sÂ   dd„ ˆj D ƒ‰ ˆj ¡ }|jtjtjd}t‡ ‡fdd„|jtjtjdD ƒƒ}|rZd S t	ƒ }t
|ƒ}xT|D ]L}|jˆ ksn|jˆjksn|j|ksn| ¡ ˆkr qn| |j¡ | jd|d qnW d S )Nc             S   s"   h | ]}t |jtjƒr|jj’qS r   )r   Útargetr   Ú
AssignNamer   )r—   Ú	generatorr   r   r   ú	<setcomp>Ë  s   z0Python3Checker.visit_listcomp.<locals>.<setcomp>)Ú
skip_klassc             3   s(   | ] }|j ˆ kr|jˆjkr|V  qd S )N)r   Úlineno)r—   Úassign_name)Únamesr#   r   r   ú	<genexpr>Ó  s   z0Python3Checker.visit_listcomp.<locals>.<genexpr>zcomprehension-escape)r#   )Z
generatorsr<   rV   Únodes_of_classr   r>   ÚFunctionDefr+   r½   r)   r&   r   rÁ   Úaddr¥   )r    r#   rV   Úscope_namesZhas_redefined_assign_nameZemitted_for_namesÚ
scope_namer   )rÃ   r#   r   Úvisit_listcompÉ  s(    




zPython3Checker.visit_listcompc             C   sh   |  |j¡\}}t|ƒsdS |j| jkr,dS t|ƒsDtt|ƒtjƒrHdS |j 	¡ d }| j
||d dS )z+Detect when a "bad" built-in is referenced.Nz-builtin)r#   )r?   r   r$   Ú_bad_builtinsr	   r   r
   r   rN   r…   r¥   )r    r#   Ú
found_nodeÚ_Úmessager   r   r   Ú
visit_nameê  s    
zPython3Checker.visit_namezprint-statementc             C   s   | j d|dd d S )Nzprint-statementT)r#   r¦   )r¥   )r    r#   r   r   r   Úvisit_printù  s    zPython3Checker.visit_printTc             C   sx   xr| j  ¡ D ]d\}}||kr|| jkrt|tƒrP|rp| j |¡ | j||d q|r||  |¡r| j||d qW d S )N)r#   )Ú_bad_python3_module_mapr6   rœ   r   r2   rÇ   r¥   Úintersection)r    r#   ÚmoduleZ
attributesÚreport_on_modulesrÎ   Z
module_mapr   r   r   Ú_warn_if_deprecatedý  s    
z"Python3Checker._warn_if_deprecatedc             C   sÎ   |j dkr>x‚|jD ]&\}}|dkr*d| _q|dkrd| _qW nP| jsd| j d¡rd| jd|d d| _t|ƒsŽ|jsŽ|  	||j dd„ |jD ƒ¡ |jd	 d	 d
krÊ| j d¡rÊt
| ¡ tjƒsÊ| jd|d d S )NÚ
__future__ZdivisionTr   zno-absolute-import)r#   c             S   s   h | ]}|d  ’qS )r   r   )r—   r˜   r   r   r   r¿     s    z2Python3Checker.visit_importfrom.<locals>.<setcomp>r   Ú*zimport-star-module-level)ÚmodnamerÃ   rš   r›   ÚlinterÚis_message_enabledr¥   rO   ÚlevelrÕ   r   rV   r   ÚModule)r    r#   r   rÍ   r   r   r   Úvisit_importfrom  s     
zPython3Checker.visit_importfromc             C   sT   | j s&| j d¡r&| jd|d d| _ t|ƒsPx |jD ]\}}|  ||d ¡ q6W d S )Nzno-absolute-import)r#   T)r›   rÙ   rÚ   r¥   rO   rÃ   rÕ   )r    r#   r   rÍ   r   r   r   Úvisit_import  s    zPython3Checker.visit_importzmetaclass-assignmentc             C   sX   d|j kr| jd|d t|j ƒ dd„ | ¡ D ƒ¡}d|krTd|krT| jd|d d S )	NZ__metaclass__zmetaclass-assignment)r#   c             s   s   | ]}|j V  qd S )N)r   )r—   r˜   r   r   r   rÄ   (  s    z0Python3Checker.visit_classdef.<locals>.<genexpr>Ú__eq__Ú__hash__zeq-without-hash)Úlocalsr¥   r)   ÚunionZ	mymethods)r    r#   Zlocals_and_methodsr   r   r   Úvisit_classdef$  s
    
zPython3Checker.visit_classdefzold-divisionc             C   sR   | j sN|jdkrNx<|j|jfD ]}t|tjƒrt|jtƒrP qW | j	d|d d S )Nú/zold-division)r#   )
rš   ÚopÚleftÚrightr   r   ÚConstr   Úfloatr¥   )r    r#   rº   r   r   r   Úvisit_binop,  s
    zPython3Checker.visit_binopc             C   sÞ   g }t |jtjƒr`|jjdkr`t |jj¡}|s4d S d t	j
¡}t |tjƒsX| ¡ |kr¬|j}nLt |jtjƒr¬|jjdkr¬t |j¡}|sŽd S d t	j
¡}| ¡ |kr¬|j}x,|p´g D ] }|jdkr¶| jd|d d S q¶W d S )NÚsortz{}.listr(   z	{}.sortedrZ   zusing-cmp-argument)r#   )r   r=   r   rA   rB   r   rD   r§   Úformatr   r¸   rH   rE   Úkeywordsr>   r   rº   r¥   )r    r#   r¢   rL   Zbuiltins_listZbuiltins_sortedÚkwargr   r   r   Ú_check_cmp_argument5  s&    
z"Python3Checker._check_cmp_argumentc             C   s$   t | tjƒrt | jtƒS t | tjƒS )N)r   r   rè   r   Ústrr>   )r#   r   r   r   Ú_is_constant_string_or_nameO  s    z*Python3Checker._is_constant_string_or_namec             C   s   t | tjƒo| jd kS )N)r   r   rè   r   )r#   r   r   r   Ú_is_noneU  s    zPython3Checker._is_nonec             C   s    t | jƒ|kot| jƒo| j S )N)rJ   r¡   r,   rí   )r#   Znumber_of_argsr   r   r   Ú_has_only_n_positional_argsY  s    z*Python3Checker._has_only_n_positional_argsc             C   sJ   | rt nt}x8| D ]0}|tjkr&t}qt|tjƒr>t|jtƒsd S qW |S )N)r   r   r   ÚUninferabler   rè   r   rð   )Úinferred_typesÚ
confidenceZinferred_typer   r   r   Ú_could_be_string]  s    

zPython3Checker._could_be_stringc       
      C   sŒ  |   |¡ t|jtjƒrØtƒ }yŽxˆ|jj ¡ D ]x}|tjkr@q0| 	|¡ t|tj
ƒrp| j||j|jjhdd t|ƒr0|jjtkr0t|ƒs0d |jj¡}| j||d q0W W n tjk
rÄ   Y nX |jrr|  |¡}|rn|jjdkrt|jƒdkr|jd r|jd }|  ||¡ |jjdkrn|  |d	¡rn|  |jd ¡rn|  |jd ¡rn| jd
||d d S |jr~d S |jjdkrœ| jd|d n:|jjdkrº| jd|d n|jjdkrˆ| jd|d n°t|jtjƒrˆ|j |jj¡d }t|ƒrˆ|jjdkr:t|ƒs:d |jj¡}| j||d |jjdkrˆ|jrˆ|j}x0|p`g D ]"}	|	jdkrb|  |	j |¡ P qbW d S )NF)rÔ   zdict-{}-not-iterating)r#   )ÚencodeÚdecoder:   r   rŒ   r³   zdeprecated-str-translate-call)r#   rö   rµ   znext-method-called)ZiterkeysZ
itervaluesZ	iteritemszdict-iter-method)ZviewkeysZ
viewvaluesZ	viewitemszdict-view-method)r.   ÚmapÚrangerp   z{}-builtin-not-iteratingÚopenÚencoding)!rï   r   r=   r   rA   r)   r§   Úinferrô   rÇ   rÜ   rÕ   r   rB   r   ÚDICT_METHODSrM   rì   r¥   ÚInferenceErrorr¡   r÷   rJ   Ú_validate_encodingró   rò   rñ   rí   r>   r?   r$   rº   r   )
r    r#   rõ   Zinferred_receiverZcheckerZis_str_confidenceZ	first_argrÌ   r¢   rî   r   r   r   Ú
visit_callj  sv    






zPython3Checker.visit_callc             C   s.   t |tjƒr*|j}|| jkr*| jd|d d S )Nzinvalid-str-codec)r#   )r   r   rè   r   Ú_invalid_encodingsr¥   )r    rý   r#   r   r   r   r   r  ½  s    
z!Python3Checker._validate_encodingc             C   sZ   y>x8|j  ¡ D ]*}t|tjƒs qt |¡r| jd|d qW W n tjk
rT   dS X dS )z Look for indexing exceptions. zindexing-exception)r#   N)	r   rþ   r   r   r   r   Úinherit_from_std_exr¥   r   )r    r#   rL   r   r   r   Úvisit_subscriptÃ  s    
zPython3Checker.visit_subscriptc             C   s   t | ¡ tjƒr|  |¡ d S )N)r   Zassign_typer   Z	AugAssignÚvisit_attribute)r    r#   r   r   r   Úvisit_assignattrÏ  s    zPython3Checker.visit_assignattrc             C   s   |   |¡ d S )N)r  )r    r#   r   r   r   Úvisit_delattrÓ  s    zPython3Checker.visit_delattrzexception-message-attributezxreadlines-attributec             C   s²   |j dkr| jd|d dS d}yvxp|j ¡ D ]b}t|tjƒrlt |¡rl|j |krl||j	kr^q.| jd|d t|tj
ƒr.| j||j|j hdd q.W W n tjk
r¬   dS X dS )	zLook for removed attributesZ
xreadlineszxreadlines-attribute)r#   NrÎ   zexception-message-attributeF)rÔ   )rB   r¥   r§   rþ   r   r   r   r   r  Zinstance_attrsrÜ   rÕ   r   r   )r    r#   Zexception_messagerL   r   r   r   r  Ö  s"    


zPython3Checker.visit_attributec                sÊ   dd„ ‰ t ˆjtjtjfƒr.| jdˆd dS ˆjs8dS ˆj ¡ }|jtj	tj
d}t|ƒ}‡ ‡fdd„|D ƒ}‡fd	d
„|jtjtj
dD ƒ}x4|D ],‰t‡‡fdd„|D ƒƒr´q–| jdˆd q–W dS )z@Visit an except handler block and check for exception unpacking.c             S   sF   |   ¡ }| }x"|r.||kr.t|tjƒs.|j}qW t|tjƒoD|j| kS )N)rV   r   r   rN   r<   Útype)r#   rV   Zcurrentr   r   r   Ú_is_used_in_except_blockô  s    
zDPython3Checker.visit_excepthandler.<locals>._is_used_in_except_blockzunpacking-in-except)r#   N)rÀ   c                s2   g | ]*}|j ˆj j kr|jˆjkrˆ |ƒs|‘qS r   )r   rÁ   )r—   rÉ   )r
  r#   r   r   r™     s   z6Python3Checker.visit_excepthandler.<locals>.<listcomp>c                s    h | ]}|j ˆ j j kr|j’qS r   )r   rÁ   )r—   rÂ   )r#   r   r   r¿     s   z5Python3Checker.visit_excepthandler.<locals>.<setcomp>c             3   s*   | ]"}ˆj |  k oˆ j k n  V  qd S )N)rÁ   )r—   Úelem)Úleaked_namer#   r   r   rÄ     s   z5Python3Checker.visit_excepthandler.<locals>.<genexpr>zexception-escape)r   r   r   rI   rH   r¥   r<   rV   rÅ   r>   rÆ   r&   r½   r+   )r    r#   rV   rÈ   Zpotential_leaked_namesZreassignments_for_same_namer   )r
  r  r#   r   Úvisit_excepthandlerð  s(    



z"Python3Checker.visit_excepthandlerc             C   s   | j d|d d S )NrU   )r#   )r¥   )r    r#   r   r   r   Ú
visit_repr   s    zPython3Checker.visit_reprc             C   s^   |j dkrdS |j }|  ||¡r$dS ytt |¡ƒ}W n tjk
rL   dS X |  ||¡ dS )z[Visit a raise statement and check for raising
        strings or old-raise-syntax.
        N)ÚexcÚ_check_raise_valuerµ   r   Zunpack_inferr   )r    r#   r§   r   r   r   r   Úvisit_raise$  s    
zPython3Checker.visit_raisec             C   s2   t |tjƒr.|j}t |tƒr.| jd|d dS d S )Nzraising-string)r#   T)r   r   rè   r   rð   r¥   )r    r#   r§   r   r   r   r   r  6  s    
z!Python3Checker._check_raise_value)F)T)8Ú__name__Ú
__module__Ú__qualname__r   ZIAstroidCheckerÚ__implements__Úenabledr   r   ZNODEÚmsgsr2   rË   r¶   r  rÑ   rs   Úversion_infor¨   rŸ   r¥   r©   r«   r®   r¯   r°   r±   r¹   r   Zcheck_messagesr»   rÊ   rÏ   rÐ   rÕ   rÝ   rÞ   rã   rê   rï   Ústaticmethodrñ   rò   ró   r÷   r  r  r  r  r  r  r  r  r  r  Ú__classcell__r   r   )r£   r   rR   ¦   sú  




	

"

!
		S0rR   c               @   sd   e Zd ZejZdZdZdddddifdd	d
ddgdœfdddddifdddddifdœZdd„ Z	dS )ÚPython3TokenCheckerrS   FzUse of long suffixzlong-suffixz~Used when "l" or "L" is used to mark a long integer. This will not work in Python 3, since `int` and `long` types have merged.rW   )rX   r   zUse of the <> operatorzold-ne-operatorz\Used when the deprecated "<>" operator is used instead of "!=". This is removed in Python 3.)ZW0331zold-ne-operator)rW   rT   zUse of old octal literalzold-octal-literalzrUsed when encountering the old octal syntax, removed in Python 3. To use the new syntax, prepend 0o on the number.z-Non-ascii bytes literals not supported in 3.xznon-ascii-bytes-literalzdUsed when non-ascii bytes literals are found in a program. They are no longer supported in Python 3.)ZE1606ZE1607ZE1608ZE1610c             C   sÌ   xÆt |ƒD ]º\}\}}}}}|tjkrb| ¡  d¡rH| jd|d d nt|ƒrb| jd|d d || d dkrŒ| jd|| d	 d d |tjkr
| d
¡r
t	dd„ |D ƒƒr
| jd|d d q
W d S )NÚlzlong-suffixr   )Úlinezold-octal-literalr:   z<>zold-ne-operatorr³   Úbc             s   s   | ]}t |ƒd kr|V  qdS )é   N)Úord)r—   r  r   r   r   rÄ   p  s    z5Python3TokenChecker.process_tokens.<locals>.<genexpr>znon-ascii-bytes-literal)
r-   ÚtokenizeÚNUMBERr…   Úendswithr¥   r   ÚSTRINGr·   r+   )r    ÚtokensÚidxÚtok_typeÚtokenÚstartrÍ   r   r   r   Úprocess_tokense  s    
z"Python3TokenChecker.process_tokensN)
r  r  r  r   ZITokenCheckerr  r   r  r  r*  r   r   r   r   r  ?  s(   r  c             C   s    |   t| ƒ¡ |   t| ƒ¡ d S )N)Zregister_checkerrR   r  )rÙ   r   r   r   Úregistert  s    r+  )+Ú__doc__rÖ   r   r   Úcollectionsr   r   rs   r!  Útypingr   r   r   Zpylintr   r   Zpylint.checkers.utilsr	   r
   Zpylint.interfacesr   r   Zpylint.utilsr   Zpylint.checkersr   Úcompiler   r   r   r$   r@   rC   rF   rÿ   rM   rO   rP   ZBaseCheckerrR   ZBaseTokenCheckerr  r+  r   r   r   r   Ú<module>   sj   

9	         "5