base.py 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591
  1. # Copyright (c) 2006-2016 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2010 Daniel Harding <dharding@gmail.com>
  3. # Copyright (c) 2012-2014 Google, Inc.
  4. # Copyright (c) 2013-2020 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  6. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  7. # Copyright (c) 2015 Nick Bastin <nick.bastin@gmail.com>
  8. # Copyright (c) 2015 Michael Kefeder <oss@multiwave.ch>
  9. # Copyright (c) 2015 Dmitry Pribysh <dmand@yandex.ru>
  10. # Copyright (c) 2015 Stephane Wirtel <stephane@wirtel.be>
  11. # Copyright (c) 2015 Cosmin Poieana <cmin@ropython.org>
  12. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  13. # Copyright (c) 2015 Radu Ciorba <radu@devrandom.ro>
  14. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  15. # Copyright (c) 2016, 2019 Ashley Whetter <ashley@awhetter.co.uk>
  16. # Copyright (c) 2016, 2018 Jakub Wilk <jwilk@jwilk.net>
  17. # Copyright (c) 2016-2017 Łukasz Rogalski <rogalski.91@gmail.com>
  18. # Copyright (c) 2016 Glenn Matthews <glenn@e-dad.net>
  19. # Copyright (c) 2016 Elias Dorneles <eliasdorneles@gmail.com>
  20. # Copyright (c) 2016 Yannack <yannack@users.noreply.github.com>
  21. # Copyright (c) 2016 Alex Jurkiewicz <alex@jurkiewi.cz>
  22. # Copyright (c) 2017, 2019-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  23. # Copyright (c) 2017, 2019-2021 hippo91 <guillaume.peillex@gmail.com>
  24. # Copyright (c) 2017 danields <danields761@gmail.com>
  25. # Copyright (c) 2017 Jacques Kvam <jwkvam@gmail.com>
  26. # Copyright (c) 2017 ttenhoeve-aa <ttenhoeve@appannie.com>
  27. # Copyright (c) 2018-2019, 2021 Nick Drozd <nicholasdrozd@gmail.com>
  28. # Copyright (c) 2018-2019, 2021 Ville Skyttä <ville.skytta@iki.fi>
  29. # Copyright (c) 2018 Sergei Lebedev <185856+superbobry@users.noreply.github.com>
  30. # Copyright (c) 2018 Lucas Cimon <lucas.cimon@gmail.com>
  31. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  32. # Copyright (c) 2018 Natalie Serebryakova <natalie.serebryakova@Natalies-MacBook-Pro.local>
  33. # Copyright (c) 2018 Sushobhit <31987769+sushobhit27@users.noreply.github.com>
  34. # Copyright (c) 2018 SergeyKosarchuk <sergeykosarchuk@gmail.com>
  35. # Copyright (c) 2018 Steven M. Vascellaro <svascellaro@gmail.com>
  36. # Copyright (c) 2018 Mike Frysinger <vapier@gmail.com>
  37. # Copyright (c) 2018 Chris Lamb <chris@chris-lamb.co.uk>
  38. # Copyright (c) 2018 glmdgrielson <32415403+glmdgrielson@users.noreply.github.com>
  39. # Copyright (c) 2019 Daniel Draper <Germandrummer92@users.noreply.github.com>
  40. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  41. # Copyright (c) 2019 Niko Wenselowski <niko@nerdno.de>
  42. # Copyright (c) 2019 Nikita Sobolev <mail@sobolevn.me>
  43. # Copyright (c) 2019 Oisín Moran <OisinMoran@users.noreply.github.com>
  44. # Copyright (c) 2019 Fantix King <fantix@uchicago.edu>
  45. # Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com>
  46. # Copyright (c) 2020 ethan-leba <ethanleba5@gmail.com>
  47. # Copyright (c) 2020 へーさん <hira9603859504@gmail.com>
  48. # Copyright (c) 2020 Damien Baty <damien.baty@polyconseil.fr>
  49. # Copyright (c) 2020 Ram Rachum <ram@rachum.com>
  50. # Copyright (c) 2020 Anthony Sottile <asottile@umich.edu>
  51. # Copyright (c) 2020 bernie gray <bfgray3@users.noreply.github.com>
  52. # Copyright (c) 2020 Gabriel R Sezefredo <g@briel.dev>
  53. # Copyright (c) 2020 Benny <benny.mueller91@gmail.com>
  54. # Copyright (c) 2020 Anubhav <35621759+anubh-v@users.noreply.github.com>
  55. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  56. # Copyright (c) 2021 Tushar Sadhwani <tushar.sadhwani000@gmail.com>
  57. # Copyright (c) 2021 Tim Martin <tim@asymptotic.co.uk>
  58. # Copyright (c) 2021 Jaehoon Hwang <jaehoonhwang@users.noreply.github.com>
  59. # Copyright (c) 2021 jaydesl <35102795+jaydesl@users.noreply.github.com>
  60. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  61. # Copyright (c) 2021 bot <bot@noreply.github.com>
  62. # Copyright (c) 2021 Yilei "Dolee" Yang <yileiyang@google.com>
  63. # Copyright (c) 2021 Lorena B <46202743+lorena-b@users.noreply.github.com>
  64. # Copyright (c) 2021 David Liu <david@cs.toronto.edu>
  65. # Copyright (c) 2021 Andreas Finkler <andi.finkler@gmail.com>
  66. # Copyright (c) 2021 Or Bahari <orbahari@mail.tau.ac.il>
  67. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  68. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  69. """basic checker for Python code"""
  70. import collections
  71. import itertools
  72. import re
  73. import sys
  74. from typing import Any, Dict, Iterator, Optional, Pattern, cast
  75. import astroid
  76. from astroid import nodes
  77. from pylint import checkers, interfaces
  78. from pylint import utils as lint_utils
  79. from pylint.checkers import utils
  80. from pylint.checkers.utils import (
  81. infer_all,
  82. is_overload_stub,
  83. is_property_deleter,
  84. is_property_setter,
  85. )
  86. from pylint.reporters.ureports import nodes as reporter_nodes
  87. from pylint.utils import LinterStats
  88. from pylint.utils.utils import get_global_option
  89. if sys.version_info >= (3, 8):
  90. from typing import Literal
  91. else:
  92. from typing_extensions import Literal
  93. class NamingStyle:
  94. """It may seem counterintuitive that single naming style has multiple "accepted"
  95. forms of regular expressions, but we need to special-case stuff like dunder names
  96. in method names."""
  97. ANY: Pattern[str] = re.compile(".*")
  98. CLASS_NAME_RGX: Pattern[str] = ANY
  99. MOD_NAME_RGX: Pattern[str] = ANY
  100. CONST_NAME_RGX: Pattern[str] = ANY
  101. COMP_VAR_RGX: Pattern[str] = ANY
  102. DEFAULT_NAME_RGX: Pattern[str] = ANY
  103. CLASS_ATTRIBUTE_RGX: Pattern[str] = ANY
  104. @classmethod
  105. def get_regex(cls, name_type):
  106. return {
  107. "module": cls.MOD_NAME_RGX,
  108. "const": cls.CONST_NAME_RGX,
  109. "class": cls.CLASS_NAME_RGX,
  110. "function": cls.DEFAULT_NAME_RGX,
  111. "method": cls.DEFAULT_NAME_RGX,
  112. "attr": cls.DEFAULT_NAME_RGX,
  113. "argument": cls.DEFAULT_NAME_RGX,
  114. "variable": cls.DEFAULT_NAME_RGX,
  115. "class_attribute": cls.CLASS_ATTRIBUTE_RGX,
  116. "class_const": cls.CONST_NAME_RGX,
  117. "inlinevar": cls.COMP_VAR_RGX,
  118. }[name_type]
  119. class SnakeCaseStyle(NamingStyle):
  120. """Regex rules for snake_case naming style."""
  121. CLASS_NAME_RGX = re.compile(r"[^\W\dA-Z][^\WA-Z]+$")
  122. MOD_NAME_RGX = re.compile(r"[^\W\dA-Z][^\WA-Z]*$")
  123. CONST_NAME_RGX = re.compile(r"([^\W\dA-Z][^\WA-Z]*|__.*__)$")
  124. COMP_VAR_RGX = re.compile(r"[^\W\dA-Z][^\WA-Z]*$")
  125. DEFAULT_NAME_RGX = re.compile(
  126. r"([^\W\dA-Z][^\WA-Z]{2,}|_[^\WA-Z]*|__[^\WA-Z\d_][^\WA-Z]+__)$"
  127. )
  128. CLASS_ATTRIBUTE_RGX = re.compile(r"([^\W\dA-Z][^\WA-Z]{2,}|__.*__)$")
  129. class CamelCaseStyle(NamingStyle):
  130. """Regex rules for camelCase naming style."""
  131. CLASS_NAME_RGX = re.compile(r"[^\W\dA-Z][^\W_]+$")
  132. MOD_NAME_RGX = re.compile(r"[^\W\dA-Z][^\W_]*$")
  133. CONST_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]*|__.*__)$")
  134. COMP_VAR_RGX = re.compile(r"[^\W\dA-Z][^\W_]*$")
  135. DEFAULT_NAME_RGX = re.compile(r"([^\W\dA-Z][^\W_]{2,}|__[^\W\dA-Z_]\w+__)$")
  136. CLASS_ATTRIBUTE_RGX = re.compile(r"([^\W\dA-Z][^\W_]{2,}|__.*__)$")
  137. class PascalCaseStyle(NamingStyle):
  138. """Regex rules for PascalCase naming style."""
  139. CLASS_NAME_RGX = re.compile(r"[^\W\da-z][^\W_]+$")
  140. MOD_NAME_RGX = re.compile(r"[^\W\da-z][^\W_]+$")
  141. CONST_NAME_RGX = re.compile(r"([^\W\da-z][^\W_]*|__.*__)$")
  142. COMP_VAR_RGX = re.compile(r"[^\W\da-z][^\W_]+$")
  143. DEFAULT_NAME_RGX = re.compile(r"([^\W\da-z][^\W_]{2,}|__[^\W\dA-Z_]\w+__)$")
  144. CLASS_ATTRIBUTE_RGX = re.compile(r"[^\W\da-z][^\W_]{2,}$")
  145. class UpperCaseStyle(NamingStyle):
  146. """Regex rules for UPPER_CASE naming style."""
  147. CLASS_NAME_RGX = re.compile(r"[^\W\da-z][^\Wa-z]+$")
  148. MOD_NAME_RGX = re.compile(r"[^\W\da-z][^\Wa-z]+$")
  149. CONST_NAME_RGX = re.compile(r"([^\W\da-z][^\Wa-z]*|__.*__)$")
  150. COMP_VAR_RGX = re.compile(r"[^\W\da-z][^\Wa-z]+$")
  151. DEFAULT_NAME_RGX = re.compile(r"([^\W\da-z][^\Wa-z]{2,}|__[^\W\dA-Z_]\w+__)$")
  152. CLASS_ATTRIBUTE_RGX = re.compile(r"[^\W\da-z][^\Wa-z]{2,}$")
  153. class AnyStyle(NamingStyle):
  154. pass
  155. NAMING_STYLES = {
  156. "snake_case": SnakeCaseStyle,
  157. "camelCase": CamelCaseStyle,
  158. "PascalCase": PascalCaseStyle,
  159. "UPPER_CASE": UpperCaseStyle,
  160. "any": AnyStyle,
  161. }
  162. # do not require a doc string on private/system methods
  163. NO_REQUIRED_DOC_RGX = re.compile("^_")
  164. REVERSED_PROTOCOL_METHOD = "__reversed__"
  165. SEQUENCE_PROTOCOL_METHODS = ("__getitem__", "__len__")
  166. REVERSED_METHODS = (SEQUENCE_PROTOCOL_METHODS, (REVERSED_PROTOCOL_METHOD,))
  167. TYPECHECK_COMPARISON_OPERATORS = frozenset(("is", "is not", "==", "!="))
  168. LITERAL_NODE_TYPES = (nodes.Const, nodes.Dict, nodes.List, nodes.Set)
  169. UNITTEST_CASE = "unittest.case"
  170. TYPE_QNAME = "builtins.type"
  171. ABC_METACLASSES = {"_py_abc.ABCMeta", "abc.ABCMeta"} # Python 3.7+,
  172. # Name categories that are always consistent with all naming conventions.
  173. EXEMPT_NAME_CATEGORIES = {"exempt", "ignore"}
  174. # A mapping from qname -> symbol, to be used when generating messages
  175. # about dangerous default values as arguments
  176. DEFAULT_ARGUMENT_SYMBOLS = dict(
  177. zip(
  178. [".".join(["builtins", x]) for x in ("set", "dict", "list")],
  179. ["set()", "{}", "[]"],
  180. ),
  181. **{
  182. x: f"{x}()"
  183. for x in (
  184. "collections.deque",
  185. "collections.ChainMap",
  186. "collections.Counter",
  187. "collections.OrderedDict",
  188. "collections.defaultdict",
  189. "collections.UserDict",
  190. "collections.UserList",
  191. )
  192. },
  193. )
  194. COMPARISON_OPERATORS = frozenset(("==", "!=", "<", ">", "<=", ">="))
  195. # List of methods which can be redefined
  196. REDEFINABLE_METHODS = frozenset(("__module__",))
  197. TYPING_FORWARD_REF_QNAME = "typing.ForwardRef"
  198. def _redefines_import(node):
  199. """Detect that the given node (AssignName) is inside an
  200. exception handler and redefines an import from the tryexcept body.
  201. Returns True if the node redefines an import, False otherwise.
  202. """
  203. current = node
  204. while current and not isinstance(current.parent, nodes.ExceptHandler):
  205. current = current.parent
  206. if not current or not utils.error_of_type(current.parent, ImportError):
  207. return False
  208. try_block = current.parent.parent
  209. for import_node in try_block.nodes_of_class((nodes.ImportFrom, nodes.Import)):
  210. for name, alias in import_node.names:
  211. if alias:
  212. if alias == node.name:
  213. return True
  214. elif name == node.name:
  215. return True
  216. return False
  217. LOOPLIKE_NODES = (
  218. nodes.For,
  219. nodes.ListComp,
  220. nodes.SetComp,
  221. nodes.DictComp,
  222. nodes.GeneratorExp,
  223. )
  224. def in_loop(node: nodes.NodeNG) -> bool:
  225. """Return whether the node is inside a kind of for loop"""
  226. return any(isinstance(parent, LOOPLIKE_NODES) for parent in node.node_ancestors())
  227. def in_nested_list(nested_list, obj):
  228. """return true if the object is an element of <nested_list> or of a nested
  229. list
  230. """
  231. for elmt in nested_list:
  232. if isinstance(elmt, (list, tuple)):
  233. if in_nested_list(elmt, obj):
  234. return True
  235. elif elmt == obj:
  236. return True
  237. return False
  238. def _get_break_loop_node(break_node):
  239. """
  240. Returns the loop node that holds the break node in arguments.
  241. Args:
  242. break_node (astroid.Break): the break node of interest.
  243. Returns:
  244. astroid.For or astroid.While: the loop node holding the break node.
  245. """
  246. loop_nodes = (nodes.For, nodes.While)
  247. parent = break_node.parent
  248. while not isinstance(parent, loop_nodes) or break_node in getattr(
  249. parent, "orelse", []
  250. ):
  251. break_node = parent
  252. parent = parent.parent
  253. if parent is None:
  254. break
  255. return parent
  256. def _loop_exits_early(loop):
  257. """
  258. Returns true if a loop may ends up in a break statement.
  259. Args:
  260. loop (astroid.For, astroid.While): the loop node inspected.
  261. Returns:
  262. bool: True if the loop may ends up in a break statement, False otherwise.
  263. """
  264. loop_nodes = (nodes.For, nodes.While)
  265. definition_nodes = (nodes.FunctionDef, nodes.ClassDef)
  266. inner_loop_nodes = [
  267. _node
  268. for _node in loop.nodes_of_class(loop_nodes, skip_klass=definition_nodes)
  269. if _node != loop
  270. ]
  271. return any(
  272. _node
  273. for _node in loop.nodes_of_class(nodes.Break, skip_klass=definition_nodes)
  274. if _get_break_loop_node(_node) not in inner_loop_nodes
  275. )
  276. def _is_multi_naming_match(match, node_type, confidence):
  277. return (
  278. match is not None
  279. and match.lastgroup is not None
  280. and match.lastgroup not in EXEMPT_NAME_CATEGORIES
  281. and (node_type != "method" or confidence != interfaces.INFERENCE_FAILURE)
  282. )
  283. BUILTIN_PROPERTY = "builtins.property"
  284. def _get_properties(config):
  285. """Returns a tuple of property classes and names.
  286. Property classes are fully qualified, such as 'abc.abstractproperty' and
  287. property names are the actual names, such as 'abstract_property'.
  288. """
  289. property_classes = {BUILTIN_PROPERTY}
  290. property_names = set() # Not returning 'property', it has its own check.
  291. if config is not None:
  292. property_classes.update(config.property_classes)
  293. property_names.update(
  294. prop.rsplit(".", 1)[-1] for prop in config.property_classes
  295. )
  296. return property_classes, property_names
  297. def _determine_function_name_type(node: nodes.FunctionDef, config=None):
  298. """Determine the name type whose regex the a function's name should match.
  299. :param node: A function node.
  300. :param config: Configuration from which to pull additional property classes.
  301. :type config: :class:`optparse.Values`
  302. :returns: One of ('function', 'method', 'attr')
  303. :rtype: str
  304. """
  305. property_classes, property_names = _get_properties(config)
  306. if not node.is_method():
  307. return "function"
  308. if is_property_setter(node) or is_property_deleter(node):
  309. # If the function is decorated using the prop_method.{setter,getter}
  310. # form, treat it like an attribute as well.
  311. return "attr"
  312. decorators = node.decorators.nodes if node.decorators else []
  313. for decorator in decorators:
  314. # If the function is a property (decorated with @property
  315. # or @abc.abstractproperty), the name type is 'attr'.
  316. if isinstance(decorator, nodes.Name) or (
  317. isinstance(decorator, nodes.Attribute)
  318. and decorator.attrname in property_names
  319. ):
  320. inferred = utils.safe_infer(decorator)
  321. if (
  322. inferred
  323. and hasattr(inferred, "qname")
  324. and inferred.qname() in property_classes
  325. ):
  326. return "attr"
  327. return "method"
  328. def _has_abstract_methods(node):
  329. """
  330. Determine if the given `node` has abstract methods.
  331. The methods should be made abstract by decorating them
  332. with `abc` decorators.
  333. """
  334. return len(utils.unimplemented_abstract_methods(node)) > 0
  335. def report_by_type_stats(
  336. sect,
  337. stats: LinterStats,
  338. old_stats: Optional[LinterStats],
  339. ):
  340. """make a report of
  341. * percentage of different types documented
  342. * percentage of different types with a bad name
  343. """
  344. # percentage of different types documented and/or with a bad name
  345. nice_stats: Dict[str, Dict[str, str]] = {}
  346. for node_type in ("module", "class", "method", "function"):
  347. node_type = cast(Literal["function", "class", "method", "module"], node_type)
  348. total = stats.get_node_count(node_type)
  349. nice_stats[node_type] = {}
  350. if total != 0:
  351. undocumented_node = stats.get_undocumented(node_type)
  352. documented = total - undocumented_node
  353. percent = (documented * 100.0) / total
  354. nice_stats[node_type]["percent_documented"] = f"{percent:.2f}"
  355. badname_node = stats.get_bad_names(node_type)
  356. percent = (badname_node * 100.0) / total
  357. nice_stats[node_type]["percent_badname"] = f"{percent:.2f}"
  358. lines = ["type", "number", "old number", "difference", "%documented", "%badname"]
  359. for node_type in ("module", "class", "method", "function"):
  360. node_type = cast(Literal["function", "class", "method", "module"], node_type)
  361. new = stats.get_node_count(node_type)
  362. old = old_stats.get_node_count(node_type) if old_stats else None
  363. diff_str = lint_utils.diff_string(old, new) if old else None
  364. lines += [
  365. node_type,
  366. str(new),
  367. str(old) if old else "NC",
  368. diff_str if diff_str else "NC",
  369. nice_stats[node_type].get("percent_documented", "0"),
  370. nice_stats[node_type].get("percent_badname", "0"),
  371. ]
  372. sect.append(reporter_nodes.Table(children=lines, cols=6, rheaders=1))
  373. def redefined_by_decorator(node):
  374. """return True if the object is a method redefined via decorator.
  375. For example:
  376. @property
  377. def x(self): return self._x
  378. @x.setter
  379. def x(self, value): self._x = value
  380. """
  381. if node.decorators:
  382. for decorator in node.decorators.nodes:
  383. if (
  384. isinstance(decorator, nodes.Attribute)
  385. and getattr(decorator.expr, "name", None) == node.name
  386. ):
  387. return True
  388. return False
  389. class _BasicChecker(checkers.BaseChecker):
  390. __implements__ = interfaces.IAstroidChecker
  391. name = "basic"
  392. class BasicErrorChecker(_BasicChecker):
  393. msgs = {
  394. "E0100": (
  395. "__init__ method is a generator",
  396. "init-is-generator",
  397. "Used when the special class method __init__ is turned into a "
  398. "generator by a yield in its body.",
  399. ),
  400. "E0101": (
  401. "Explicit return in __init__",
  402. "return-in-init",
  403. "Used when the special class method __init__ has an explicit "
  404. "return value.",
  405. ),
  406. "E0102": (
  407. "%s already defined line %s",
  408. "function-redefined",
  409. "Used when a function / class / method is redefined.",
  410. ),
  411. "E0103": (
  412. "%r not properly in loop",
  413. "not-in-loop",
  414. "Used when break or continue keywords are used outside a loop.",
  415. ),
  416. "E0104": (
  417. "Return outside function",
  418. "return-outside-function",
  419. 'Used when a "return" statement is found outside a function or method.',
  420. ),
  421. "E0105": (
  422. "Yield outside function",
  423. "yield-outside-function",
  424. 'Used when a "yield" statement is found outside a function or method.',
  425. ),
  426. "E0106": (
  427. "Return with argument inside generator",
  428. "return-arg-in-generator",
  429. 'Used when a "return" statement with an argument is found '
  430. "outside in a generator function or method (e.g. with some "
  431. '"yield" statements).',
  432. {"maxversion": (3, 3)},
  433. ),
  434. "E0107": (
  435. "Use of the non-existent %s operator",
  436. "nonexistent-operator",
  437. "Used when you attempt to use the C-style pre-increment or "
  438. "pre-decrement operator -- and ++, which doesn't exist in Python.",
  439. ),
  440. "E0108": (
  441. "Duplicate argument name %s in function definition",
  442. "duplicate-argument-name",
  443. "Duplicate argument names in function definitions are syntax errors.",
  444. ),
  445. "E0110": (
  446. "Abstract class %r with abstract methods instantiated",
  447. "abstract-class-instantiated",
  448. "Used when an abstract class with `abc.ABCMeta` as metaclass "
  449. "has abstract methods and is instantiated.",
  450. ),
  451. "W0120": (
  452. "Else clause on loop without a break statement",
  453. "useless-else-on-loop",
  454. "Loops should only have an else clause if they can exit early "
  455. "with a break statement, otherwise the statements under else "
  456. "should be on the same scope as the loop itself.",
  457. ),
  458. "E0112": (
  459. "More than one starred expression in assignment",
  460. "too-many-star-expressions",
  461. "Emitted when there are more than one starred "
  462. "expressions (`*x`) in an assignment. This is a SyntaxError.",
  463. ),
  464. "E0113": (
  465. "Starred assignment target must be in a list or tuple",
  466. "invalid-star-assignment-target",
  467. "Emitted when a star expression is used as a starred assignment target.",
  468. ),
  469. "E0114": (
  470. "Can use starred expression only in assignment target",
  471. "star-needs-assignment-target",
  472. "Emitted when a star expression is not used in an assignment target.",
  473. ),
  474. "E0115": (
  475. "Name %r is nonlocal and global",
  476. "nonlocal-and-global",
  477. "Emitted when a name is both nonlocal and global.",
  478. ),
  479. "E0116": (
  480. "'continue' not supported inside 'finally' clause",
  481. "continue-in-finally",
  482. "Emitted when the `continue` keyword is found "
  483. "inside a finally clause, which is a SyntaxError.",
  484. {"maxversion": (3, 8)},
  485. ),
  486. "E0117": (
  487. "nonlocal name %s found without binding",
  488. "nonlocal-without-binding",
  489. "Emitted when a nonlocal variable does not have an attached "
  490. "name somewhere in the parent scopes",
  491. ),
  492. "E0118": (
  493. "Name %r is used prior to global declaration",
  494. "used-prior-global-declaration",
  495. "Emitted when a name is used prior a global declaration, "
  496. "which results in an error since Python 3.6.",
  497. {"minversion": (3, 6)},
  498. ),
  499. }
  500. @utils.check_messages("function-redefined")
  501. def visit_classdef(self, node: nodes.ClassDef) -> None:
  502. self._check_redefinition("class", node)
  503. def _too_many_starred_for_tuple(self, assign_tuple):
  504. starred_count = 0
  505. for elem in assign_tuple.itered():
  506. if isinstance(elem, nodes.Tuple):
  507. return self._too_many_starred_for_tuple(elem)
  508. if isinstance(elem, nodes.Starred):
  509. starred_count += 1
  510. return starred_count > 1
  511. @utils.check_messages("too-many-star-expressions", "invalid-star-assignment-target")
  512. def visit_assign(self, node: nodes.Assign) -> None:
  513. # Check *a, *b = ...
  514. assign_target = node.targets[0]
  515. # Check *a = b
  516. if isinstance(node.targets[0], nodes.Starred):
  517. self.add_message("invalid-star-assignment-target", node=node)
  518. if not isinstance(assign_target, nodes.Tuple):
  519. return
  520. if self._too_many_starred_for_tuple(assign_target):
  521. self.add_message("too-many-star-expressions", node=node)
  522. @utils.check_messages("star-needs-assignment-target")
  523. def visit_starred(self, node: nodes.Starred) -> None:
  524. """Check that a Starred expression is used in an assignment target."""
  525. if isinstance(node.parent, nodes.Call):
  526. # f(*args) is converted to Call(args=[Starred]), so ignore
  527. # them for this check.
  528. return
  529. if isinstance(node.parent, (nodes.List, nodes.Tuple, nodes.Set, nodes.Dict)):
  530. # PEP 448 unpacking.
  531. return
  532. stmt = node.statement()
  533. if not isinstance(stmt, nodes.Assign):
  534. return
  535. if stmt.value is node or stmt.value.parent_of(node):
  536. self.add_message("star-needs-assignment-target", node=node)
  537. @utils.check_messages(
  538. "init-is-generator",
  539. "return-in-init",
  540. "function-redefined",
  541. "return-arg-in-generator",
  542. "duplicate-argument-name",
  543. "nonlocal-and-global",
  544. "used-prior-global-declaration",
  545. )
  546. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  547. self._check_nonlocal_and_global(node)
  548. self._check_name_used_prior_global(node)
  549. if not redefined_by_decorator(
  550. node
  551. ) and not utils.is_registered_in_singledispatch_function(node):
  552. self._check_redefinition(node.is_method() and "method" or "function", node)
  553. # checks for max returns, branch, return in __init__
  554. returns = node.nodes_of_class(
  555. nodes.Return, skip_klass=(nodes.FunctionDef, nodes.ClassDef)
  556. )
  557. if node.is_method() and node.name == "__init__":
  558. if node.is_generator():
  559. self.add_message("init-is-generator", node=node)
  560. else:
  561. values = [r.value for r in returns]
  562. # Are we returning anything but None from constructors
  563. if any(v for v in values if not utils.is_none(v)):
  564. self.add_message("return-in-init", node=node)
  565. # Check for duplicate names by clustering args with same name for detailed report
  566. arg_clusters = collections.defaultdict(list)
  567. arguments: Iterator[Any] = filter(None, [node.args.args, node.args.kwonlyargs])
  568. for arg in itertools.chain.from_iterable(arguments):
  569. arg_clusters[arg.name].append(arg)
  570. # provide detailed report about each repeated argument
  571. for argument_duplicates in arg_clusters.values():
  572. if len(argument_duplicates) != 1:
  573. for argument in argument_duplicates:
  574. self.add_message(
  575. "duplicate-argument-name",
  576. line=argument.lineno,
  577. node=argument,
  578. args=(argument.name,),
  579. )
  580. visit_asyncfunctiondef = visit_functiondef
  581. def _check_name_used_prior_global(self, node):
  582. scope_globals = {
  583. name: child
  584. for child in node.nodes_of_class(nodes.Global)
  585. for name in child.names
  586. if child.scope() is node
  587. }
  588. if not scope_globals:
  589. return
  590. for node_name in node.nodes_of_class(nodes.Name):
  591. if node_name.scope() is not node:
  592. continue
  593. name = node_name.name
  594. corresponding_global = scope_globals.get(name)
  595. if not corresponding_global:
  596. continue
  597. global_lineno = corresponding_global.fromlineno
  598. if global_lineno and global_lineno > node_name.fromlineno:
  599. self.add_message(
  600. "used-prior-global-declaration", node=node_name, args=(name,)
  601. )
  602. def _check_nonlocal_and_global(self, node):
  603. """Check that a name is both nonlocal and global."""
  604. def same_scope(current):
  605. return current.scope() is node
  606. from_iter = itertools.chain.from_iterable
  607. nonlocals = set(
  608. from_iter(
  609. child.names
  610. for child in node.nodes_of_class(nodes.Nonlocal)
  611. if same_scope(child)
  612. )
  613. )
  614. if not nonlocals:
  615. return
  616. global_vars = set(
  617. from_iter(
  618. child.names
  619. for child in node.nodes_of_class(nodes.Global)
  620. if same_scope(child)
  621. )
  622. )
  623. for name in nonlocals.intersection(global_vars):
  624. self.add_message("nonlocal-and-global", args=(name,), node=node)
  625. @utils.check_messages("return-outside-function")
  626. def visit_return(self, node: nodes.Return) -> None:
  627. if not isinstance(node.frame(), nodes.FunctionDef):
  628. self.add_message("return-outside-function", node=node)
  629. @utils.check_messages("yield-outside-function")
  630. def visit_yield(self, node: nodes.Yield) -> None:
  631. self._check_yield_outside_func(node)
  632. @utils.check_messages("yield-outside-function")
  633. def visit_yieldfrom(self, node: nodes.YieldFrom) -> None:
  634. self._check_yield_outside_func(node)
  635. @utils.check_messages("not-in-loop", "continue-in-finally")
  636. def visit_continue(self, node: nodes.Continue) -> None:
  637. self._check_in_loop(node, "continue")
  638. @utils.check_messages("not-in-loop")
  639. def visit_break(self, node: nodes.Break) -> None:
  640. self._check_in_loop(node, "break")
  641. @utils.check_messages("useless-else-on-loop")
  642. def visit_for(self, node: nodes.For) -> None:
  643. self._check_else_on_loop(node)
  644. @utils.check_messages("useless-else-on-loop")
  645. def visit_while(self, node: nodes.While) -> None:
  646. self._check_else_on_loop(node)
  647. @utils.check_messages("nonexistent-operator")
  648. def visit_unaryop(self, node: nodes.UnaryOp) -> None:
  649. """check use of the non-existent ++ and -- operator operator"""
  650. if (
  651. (node.op in "+-")
  652. and isinstance(node.operand, nodes.UnaryOp)
  653. and (node.operand.op == node.op)
  654. ):
  655. self.add_message("nonexistent-operator", node=node, args=node.op * 2)
  656. def _check_nonlocal_without_binding(self, node, name):
  657. current_scope = node.scope()
  658. while True:
  659. if current_scope.parent is None:
  660. break
  661. if not isinstance(current_scope, (nodes.ClassDef, nodes.FunctionDef)):
  662. self.add_message("nonlocal-without-binding", args=(name,), node=node)
  663. return
  664. if name not in current_scope.locals:
  665. current_scope = current_scope.parent.scope()
  666. continue
  667. # Okay, found it.
  668. return
  669. if not isinstance(current_scope, nodes.FunctionDef):
  670. self.add_message("nonlocal-without-binding", args=(name,), node=node)
  671. @utils.check_messages("nonlocal-without-binding")
  672. def visit_nonlocal(self, node: nodes.Nonlocal) -> None:
  673. for name in node.names:
  674. self._check_nonlocal_without_binding(node, name)
  675. @utils.check_messages("abstract-class-instantiated")
  676. def visit_call(self, node: nodes.Call) -> None:
  677. """Check instantiating abstract class with
  678. abc.ABCMeta as metaclass.
  679. """
  680. for inferred in infer_all(node.func):
  681. self._check_inferred_class_is_abstract(inferred, node)
  682. def _check_inferred_class_is_abstract(self, inferred, node):
  683. if not isinstance(inferred, nodes.ClassDef):
  684. return
  685. klass = utils.node_frame_class(node)
  686. if klass is inferred:
  687. # Don't emit the warning if the class is instantiated
  688. # in its own body or if the call is not an instance
  689. # creation. If the class is instantiated into its own
  690. # body, we're expecting that it knows what it is doing.
  691. return
  692. # __init__ was called
  693. abstract_methods = _has_abstract_methods(inferred)
  694. if not abstract_methods:
  695. return
  696. metaclass = inferred.metaclass()
  697. if metaclass is None:
  698. # Python 3.4 has `abc.ABC`, which won't be detected
  699. # by ClassNode.metaclass()
  700. for ancestor in inferred.ancestors():
  701. if ancestor.qname() == "abc.ABC":
  702. self.add_message(
  703. "abstract-class-instantiated", args=(inferred.name,), node=node
  704. )
  705. break
  706. return
  707. if metaclass.qname() in ABC_METACLASSES:
  708. self.add_message(
  709. "abstract-class-instantiated", args=(inferred.name,), node=node
  710. )
  711. def _check_yield_outside_func(self, node):
  712. if not isinstance(node.frame(), (nodes.FunctionDef, nodes.Lambda)):
  713. self.add_message("yield-outside-function", node=node)
  714. def _check_else_on_loop(self, node):
  715. """Check that any loop with an else clause has a break statement."""
  716. if node.orelse and not _loop_exits_early(node):
  717. self.add_message(
  718. "useless-else-on-loop",
  719. node=node,
  720. # This is not optimal, but the line previous
  721. # to the first statement in the else clause
  722. # will usually be the one that contains the else:.
  723. line=node.orelse[0].lineno - 1,
  724. )
  725. def _check_in_loop(self, node, node_name):
  726. """check that a node is inside a for or while loop"""
  727. for parent in node.node_ancestors():
  728. if isinstance(parent, (nodes.For, nodes.While)):
  729. if node not in parent.orelse:
  730. return
  731. if isinstance(parent, (nodes.ClassDef, nodes.FunctionDef)):
  732. break
  733. if (
  734. isinstance(parent, nodes.TryFinally)
  735. and node in parent.finalbody
  736. and isinstance(node, nodes.Continue)
  737. ):
  738. self.add_message("continue-in-finally", node=node)
  739. self.add_message("not-in-loop", node=node, args=node_name)
  740. def _check_redefinition(self, redeftype, node):
  741. """check for redefinition of a function / method / class name"""
  742. parent_frame = node.parent.frame()
  743. # Ignore function stubs created for type information
  744. redefinitions = [
  745. i
  746. for i in parent_frame.locals[node.name]
  747. if not (isinstance(i.parent, nodes.AnnAssign) and i.parent.simple)
  748. ]
  749. defined_self = next(
  750. (local for local in redefinitions if not utils.is_overload_stub(local)),
  751. node,
  752. )
  753. if defined_self is not node and not astroid.are_exclusive(node, defined_self):
  754. # Additional checks for methods which are not considered
  755. # redefined, since they are already part of the base API.
  756. if (
  757. isinstance(parent_frame, nodes.ClassDef)
  758. and node.name in REDEFINABLE_METHODS
  759. ):
  760. return
  761. # Skip typing.overload() functions.
  762. if utils.is_overload_stub(node):
  763. return
  764. # Exempt functions redefined on a condition.
  765. if isinstance(node.parent, nodes.If):
  766. # Exempt "if not <func>" cases
  767. if (
  768. isinstance(node.parent.test, nodes.UnaryOp)
  769. and node.parent.test.op == "not"
  770. and isinstance(node.parent.test.operand, nodes.Name)
  771. and node.parent.test.operand.name == node.name
  772. ):
  773. return
  774. # Exempt "if <func> is not None" cases
  775. # pylint: disable=too-many-boolean-expressions
  776. if (
  777. isinstance(node.parent.test, nodes.Compare)
  778. and isinstance(node.parent.test.left, nodes.Name)
  779. and node.parent.test.left.name == node.name
  780. and node.parent.test.ops[0][0] == "is"
  781. and isinstance(node.parent.test.ops[0][1], nodes.Const)
  782. and node.parent.test.ops[0][1].value is None
  783. ):
  784. return
  785. # Check if we have forward references for this node.
  786. try:
  787. redefinition_index = redefinitions.index(node)
  788. except ValueError:
  789. pass
  790. else:
  791. for redefinition in redefinitions[:redefinition_index]:
  792. inferred = utils.safe_infer(redefinition)
  793. if (
  794. inferred
  795. and isinstance(inferred, astroid.Instance)
  796. and inferred.qname() == TYPING_FORWARD_REF_QNAME
  797. ):
  798. return
  799. dummy_variables_rgx = lint_utils.get_global_option(
  800. self, "dummy-variables-rgx", default=None
  801. )
  802. if dummy_variables_rgx and dummy_variables_rgx.match(node.name):
  803. return
  804. self.add_message(
  805. "function-redefined",
  806. node=node,
  807. args=(redeftype, defined_self.fromlineno),
  808. )
  809. class BasicChecker(_BasicChecker):
  810. """checks for :
  811. * doc strings
  812. * number of arguments, local variables, branches, returns and statements in
  813. functions, methods
  814. * required module attributes
  815. * dangerous default values as arguments
  816. * redefinition of function / method / class
  817. * uses of the global statement
  818. """
  819. __implements__ = interfaces.IAstroidChecker
  820. name = "basic"
  821. msgs = {
  822. "W0101": (
  823. "Unreachable code",
  824. "unreachable",
  825. 'Used when there is some code behind a "return" or "raise" '
  826. "statement, which will never be accessed.",
  827. ),
  828. "W0102": (
  829. "Dangerous default value %s as argument",
  830. "dangerous-default-value",
  831. "Used when a mutable value as list or dictionary is detected in "
  832. "a default value for an argument.",
  833. ),
  834. "W0104": (
  835. "Statement seems to have no effect",
  836. "pointless-statement",
  837. "Used when a statement doesn't have (or at least seems to) any effect.",
  838. ),
  839. "W0105": (
  840. "String statement has no effect",
  841. "pointless-string-statement",
  842. "Used when a string is used as a statement (which of course "
  843. "has no effect). This is a particular case of W0104 with its "
  844. "own message so you can easily disable it if you're using "
  845. "those strings as documentation, instead of comments.",
  846. ),
  847. "W0106": (
  848. 'Expression "%s" is assigned to nothing',
  849. "expression-not-assigned",
  850. "Used when an expression that is not a function call is assigned "
  851. "to nothing. Probably something else was intended.",
  852. ),
  853. "W0108": (
  854. "Lambda may not be necessary",
  855. "unnecessary-lambda",
  856. "Used when the body of a lambda expression is a function call "
  857. "on the same argument list as the lambda itself; such lambda "
  858. "expressions are in all but a few cases replaceable with the "
  859. "function being called in the body of the lambda.",
  860. ),
  861. "W0109": (
  862. "Duplicate key %r in dictionary",
  863. "duplicate-key",
  864. "Used when a dictionary expression binds the same key multiple times.",
  865. ),
  866. "W0122": (
  867. "Use of exec",
  868. "exec-used",
  869. 'Used when you use the "exec" statement (function for Python '
  870. "3), to discourage its usage. That doesn't "
  871. "mean you cannot use it !",
  872. ),
  873. "W0123": (
  874. "Use of eval",
  875. "eval-used",
  876. 'Used when you use the "eval" function, to discourage its '
  877. "usage. Consider using `ast.literal_eval` for safely evaluating "
  878. "strings containing Python expressions "
  879. "from untrusted sources. ",
  880. ),
  881. "W0150": (
  882. "%s statement in finally block may swallow exception",
  883. "lost-exception",
  884. "Used when a break or a return statement is found inside the "
  885. "finally clause of a try...finally block: the exceptions raised "
  886. "in the try clause will be silently swallowed instead of being "
  887. "re-raised.",
  888. ),
  889. "W0199": (
  890. "Assert called on a 2-item-tuple. Did you mean 'assert x,y'?",
  891. "assert-on-tuple",
  892. "A call of assert on a tuple will always evaluate to true if "
  893. "the tuple is not empty, and will always evaluate to false if "
  894. "it is.",
  895. ),
  896. "W0124": (
  897. 'Following "as" with another context manager looks like a tuple.',
  898. "confusing-with-statement",
  899. "Emitted when a `with` statement component returns multiple values "
  900. "and uses name binding with `as` only for a part of those values, "
  901. "as in with ctx() as a, b. This can be misleading, since it's not "
  902. "clear if the context manager returns a tuple or if the node without "
  903. "a name binding is another context manager.",
  904. ),
  905. "W0125": (
  906. "Using a conditional statement with a constant value",
  907. "using-constant-test",
  908. "Emitted when a conditional statement (If or ternary if) "
  909. "uses a constant value for its test. This might not be what "
  910. "the user intended to do.",
  911. ),
  912. "W0126": (
  913. "Using a conditional statement with potentially wrong function or method call due to missing parentheses",
  914. "missing-parentheses-for-call-in-test",
  915. "Emitted when a conditional statement (If or ternary if) "
  916. "seems to wrongly call a function due to missing parentheses",
  917. ),
  918. "W0127": (
  919. "Assigning the same variable %r to itself",
  920. "self-assigning-variable",
  921. "Emitted when we detect that a variable is assigned to itself",
  922. ),
  923. "W0128": (
  924. "Redeclared variable %r in assignment",
  925. "redeclared-assigned-name",
  926. "Emitted when we detect that a variable was redeclared in the same assignment.",
  927. ),
  928. "E0111": (
  929. "The first reversed() argument is not a sequence",
  930. "bad-reversed-sequence",
  931. "Used when the first argument to reversed() builtin "
  932. "isn't a sequence (does not implement __reversed__, "
  933. "nor __getitem__ and __len__",
  934. ),
  935. "E0119": (
  936. "format function is not called on str",
  937. "misplaced-format-function",
  938. "Emitted when format function is not called on str object. "
  939. 'e.g doing print("value: {}").format(123) instead of '
  940. 'print("value: {}".format(123)). This might not be what the user '
  941. "intended to do.",
  942. ),
  943. "W0129": (
  944. "Assert statement has a string literal as its first argument. The assert will %s fail.",
  945. "assert-on-string-literal",
  946. "Used when an assert statement has a string literal as its first argument, which will "
  947. "cause the assert to always pass.",
  948. ),
  949. }
  950. reports = (("RP0101", "Statistics by type", report_by_type_stats),)
  951. def __init__(self, linter):
  952. super().__init__(linter)
  953. self._tryfinallys = None
  954. def open(self):
  955. """initialize visit variables and statistics"""
  956. py_version = get_global_option(self, "py-version")
  957. self._py38_plus = py_version >= (3, 8)
  958. self._tryfinallys = []
  959. self.linter.stats.reset_node_count()
  960. @utils.check_messages("using-constant-test", "missing-parentheses-for-call-in-test")
  961. def visit_if(self, node: nodes.If) -> None:
  962. self._check_using_constant_test(node, node.test)
  963. @utils.check_messages("using-constant-test", "missing-parentheses-for-call-in-test")
  964. def visit_ifexp(self, node: nodes.IfExp) -> None:
  965. self._check_using_constant_test(node, node.test)
  966. @utils.check_messages("using-constant-test", "missing-parentheses-for-call-in-test")
  967. def visit_comprehension(self, node: nodes.Comprehension) -> None:
  968. if node.ifs:
  969. for if_test in node.ifs:
  970. self._check_using_constant_test(node, if_test)
  971. def _check_using_constant_test(self, node, test):
  972. const_nodes = (
  973. nodes.Module,
  974. nodes.GeneratorExp,
  975. nodes.Lambda,
  976. nodes.FunctionDef,
  977. nodes.ClassDef,
  978. astroid.bases.Generator,
  979. astroid.UnboundMethod,
  980. astroid.BoundMethod,
  981. nodes.Module,
  982. )
  983. structs = (nodes.Dict, nodes.Tuple, nodes.Set, nodes.List)
  984. # These nodes are excepted, since they are not constant
  985. # values, requiring a computation to happen.
  986. except_nodes = (
  987. nodes.Call,
  988. nodes.BinOp,
  989. nodes.BoolOp,
  990. nodes.UnaryOp,
  991. nodes.Subscript,
  992. )
  993. inferred = None
  994. emit = isinstance(test, (nodes.Const,) + structs + const_nodes)
  995. if not isinstance(test, except_nodes):
  996. inferred = utils.safe_infer(test)
  997. if emit:
  998. self.add_message("using-constant-test", node=node)
  999. elif isinstance(inferred, const_nodes):
  1000. # If the constant node is a FunctionDef or Lambda then
  1001. # it may be an illicit function call due to missing parentheses
  1002. call_inferred = None
  1003. try:
  1004. if isinstance(inferred, nodes.FunctionDef):
  1005. call_inferred = inferred.infer_call_result()
  1006. elif isinstance(inferred, nodes.Lambda):
  1007. call_inferred = inferred.infer_call_result(node)
  1008. except astroid.InferenceError:
  1009. call_inferred = None
  1010. if call_inferred:
  1011. try:
  1012. for inf_call in call_inferred:
  1013. if inf_call != astroid.Uninferable:
  1014. self.add_message(
  1015. "missing-parentheses-for-call-in-test", node=node
  1016. )
  1017. break
  1018. except astroid.InferenceError:
  1019. pass
  1020. self.add_message("using-constant-test", node=node)
  1021. def visit_module(self, _: nodes.Module) -> None:
  1022. """check module name, docstring and required arguments"""
  1023. self.linter.stats.node_count["module"] += 1
  1024. def visit_classdef(self, _: nodes.ClassDef) -> None:
  1025. """check module name, docstring and redefinition
  1026. increment branch counter
  1027. """
  1028. self.linter.stats.node_count["klass"] += 1
  1029. @utils.check_messages(
  1030. "pointless-statement", "pointless-string-statement", "expression-not-assigned"
  1031. )
  1032. def visit_expr(self, node: nodes.Expr) -> None:
  1033. """Check for various kind of statements without effect"""
  1034. expr = node.value
  1035. if isinstance(expr, nodes.Const) and isinstance(expr.value, str):
  1036. # treat string statement in a separated message
  1037. # Handle PEP-257 attribute docstrings.
  1038. # An attribute docstring is defined as being a string right after
  1039. # an assignment at the module level, class level or __init__ level.
  1040. scope = expr.scope()
  1041. if isinstance(scope, (nodes.ClassDef, nodes.Module, nodes.FunctionDef)):
  1042. if isinstance(scope, nodes.FunctionDef) and scope.name != "__init__":
  1043. pass
  1044. else:
  1045. sibling = expr.previous_sibling()
  1046. if (
  1047. sibling is not None
  1048. and sibling.scope() is scope
  1049. and isinstance(sibling, (nodes.Assign, nodes.AnnAssign))
  1050. ):
  1051. return
  1052. self.add_message("pointless-string-statement", node=node)
  1053. return
  1054. # Ignore if this is :
  1055. # * a direct function call
  1056. # * the unique child of a try/except body
  1057. # * a yield statement
  1058. # * an ellipsis (which can be used on Python 3 instead of pass)
  1059. # warn W0106 if we have any underlying function call (we can't predict
  1060. # side effects), else pointless-statement
  1061. if (
  1062. isinstance(expr, (nodes.Yield, nodes.Await, nodes.Call))
  1063. or (isinstance(node.parent, nodes.TryExcept) and node.parent.body == [node])
  1064. or (isinstance(expr, nodes.Const) and expr.value is Ellipsis)
  1065. ):
  1066. return
  1067. if any(expr.nodes_of_class(nodes.Call)):
  1068. self.add_message(
  1069. "expression-not-assigned", node=node, args=expr.as_string()
  1070. )
  1071. else:
  1072. self.add_message("pointless-statement", node=node)
  1073. @staticmethod
  1074. def _filter_vararg(node, call_args):
  1075. # Return the arguments for the given call which are
  1076. # not passed as vararg.
  1077. for arg in call_args:
  1078. if isinstance(arg, nodes.Starred):
  1079. if (
  1080. isinstance(arg.value, nodes.Name)
  1081. and arg.value.name != node.args.vararg
  1082. ):
  1083. yield arg
  1084. else:
  1085. yield arg
  1086. @staticmethod
  1087. def _has_variadic_argument(args, variadic_name):
  1088. if not args:
  1089. return True
  1090. for arg in args:
  1091. if isinstance(arg.value, nodes.Name):
  1092. if arg.value.name != variadic_name:
  1093. return True
  1094. else:
  1095. return True
  1096. return False
  1097. @utils.check_messages("unnecessary-lambda")
  1098. def visit_lambda(self, node: nodes.Lambda) -> None:
  1099. """check whether or not the lambda is suspicious"""
  1100. # if the body of the lambda is a call expression with the same
  1101. # argument list as the lambda itself, then the lambda is
  1102. # possibly unnecessary and at least suspicious.
  1103. if node.args.defaults:
  1104. # If the arguments of the lambda include defaults, then a
  1105. # judgment cannot be made because there is no way to check
  1106. # that the defaults defined by the lambda are the same as
  1107. # the defaults defined by the function called in the body
  1108. # of the lambda.
  1109. return
  1110. call = node.body
  1111. if not isinstance(call, nodes.Call):
  1112. # The body of the lambda must be a function call expression
  1113. # for the lambda to be unnecessary.
  1114. return
  1115. if isinstance(node.body.func, nodes.Attribute) and isinstance(
  1116. node.body.func.expr, nodes.Call
  1117. ):
  1118. # Chained call, the intermediate call might
  1119. # return something else (but we don't check that, yet).
  1120. return
  1121. call_site = astroid.arguments.CallSite.from_call(call)
  1122. ordinary_args = list(node.args.args)
  1123. new_call_args = list(self._filter_vararg(node, call.args))
  1124. if node.args.kwarg:
  1125. if self._has_variadic_argument(call.kwargs, node.args.kwarg):
  1126. return
  1127. if node.args.vararg:
  1128. if self._has_variadic_argument(call.starargs, node.args.vararg):
  1129. return
  1130. elif call.starargs:
  1131. return
  1132. if call.keywords:
  1133. # Look for additional keyword arguments that are not part
  1134. # of the lambda's signature
  1135. lambda_kwargs = {keyword.name for keyword in node.args.defaults}
  1136. if len(lambda_kwargs) != len(call_site.keyword_arguments):
  1137. # Different lengths, so probably not identical
  1138. return
  1139. if set(call_site.keyword_arguments).difference(lambda_kwargs):
  1140. return
  1141. # The "ordinary" arguments must be in a correspondence such that:
  1142. # ordinary_args[i].name == call.args[i].name.
  1143. if len(ordinary_args) != len(new_call_args):
  1144. return
  1145. for arg, passed_arg in zip(ordinary_args, new_call_args):
  1146. if not isinstance(passed_arg, nodes.Name):
  1147. return
  1148. if arg.name != passed_arg.name:
  1149. return
  1150. self.add_message("unnecessary-lambda", line=node.fromlineno, node=node)
  1151. @utils.check_messages("dangerous-default-value")
  1152. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  1153. """check function name, docstring, arguments, redefinition,
  1154. variable names, max locals
  1155. """
  1156. if node.is_method():
  1157. self.linter.stats.node_count["method"] += 1
  1158. else:
  1159. self.linter.stats.node_count["function"] += 1
  1160. self._check_dangerous_default(node)
  1161. visit_asyncfunctiondef = visit_functiondef
  1162. def _check_dangerous_default(self, node):
  1163. """Check for dangerous default values as arguments."""
  1164. def is_iterable(internal_node):
  1165. return isinstance(internal_node, (nodes.List, nodes.Set, nodes.Dict))
  1166. defaults = node.args.defaults or [] + node.args.kw_defaults or []
  1167. for default in defaults:
  1168. if not default:
  1169. continue
  1170. try:
  1171. value = next(default.infer())
  1172. except astroid.InferenceError:
  1173. continue
  1174. if (
  1175. isinstance(value, astroid.Instance)
  1176. and value.qname() in DEFAULT_ARGUMENT_SYMBOLS
  1177. ):
  1178. if value is default:
  1179. msg = DEFAULT_ARGUMENT_SYMBOLS[value.qname()]
  1180. elif isinstance(value, astroid.Instance) or is_iterable(value):
  1181. # We are here in the following situation(s):
  1182. # * a dict/set/list/tuple call which wasn't inferred
  1183. # to a syntax node ({}, () etc.). This can happen
  1184. # when the arguments are invalid or unknown to
  1185. # the inference.
  1186. # * a variable from somewhere else, which turns out to be a list
  1187. # or a dict.
  1188. if is_iterable(default):
  1189. msg = value.pytype()
  1190. elif isinstance(default, nodes.Call):
  1191. msg = f"{value.name}() ({value.qname()})"
  1192. else:
  1193. msg = f"{default.as_string()} ({value.qname()})"
  1194. else:
  1195. # this argument is a name
  1196. msg = f"{default.as_string()} ({DEFAULT_ARGUMENT_SYMBOLS[value.qname()]})"
  1197. self.add_message("dangerous-default-value", node=node, args=(msg,))
  1198. @utils.check_messages("unreachable", "lost-exception")
  1199. def visit_return(self, node: nodes.Return) -> None:
  1200. """1 - check is the node has a right sibling (if so, that's some
  1201. unreachable code)
  1202. 2 - check is the node is inside the finally clause of a try...finally
  1203. block
  1204. """
  1205. self._check_unreachable(node)
  1206. # Is it inside final body of a try...finally block ?
  1207. self._check_not_in_finally(node, "return", (nodes.FunctionDef,))
  1208. @utils.check_messages("unreachable")
  1209. def visit_continue(self, node: nodes.Continue) -> None:
  1210. """check is the node has a right sibling (if so, that's some unreachable
  1211. code)
  1212. """
  1213. self._check_unreachable(node)
  1214. @utils.check_messages("unreachable", "lost-exception")
  1215. def visit_break(self, node: nodes.Break) -> None:
  1216. """1 - check is the node has a right sibling (if so, that's some
  1217. unreachable code)
  1218. 2 - check is the node is inside the finally clause of a try...finally
  1219. block
  1220. """
  1221. # 1 - Is it right sibling ?
  1222. self._check_unreachable(node)
  1223. # 2 - Is it inside final body of a try...finally block ?
  1224. self._check_not_in_finally(node, "break", (nodes.For, nodes.While))
  1225. @utils.check_messages("unreachable")
  1226. def visit_raise(self, node: nodes.Raise) -> None:
  1227. """check if the node has a right sibling (if so, that's some unreachable
  1228. code)
  1229. """
  1230. self._check_unreachable(node)
  1231. def _check_misplaced_format_function(self, call_node):
  1232. if not isinstance(call_node.func, nodes.Attribute):
  1233. return
  1234. if call_node.func.attrname != "format":
  1235. return
  1236. expr = utils.safe_infer(call_node.func.expr)
  1237. if expr is astroid.Uninferable:
  1238. return
  1239. if not expr:
  1240. # we are doubtful on inferred type of node, so here just check if format
  1241. # was called on print()
  1242. call_expr = call_node.func.expr
  1243. if not isinstance(call_expr, nodes.Call):
  1244. return
  1245. if (
  1246. isinstance(call_expr.func, nodes.Name)
  1247. and call_expr.func.name == "print"
  1248. ):
  1249. self.add_message("misplaced-format-function", node=call_node)
  1250. @utils.check_messages(
  1251. "eval-used", "exec-used", "bad-reversed-sequence", "misplaced-format-function"
  1252. )
  1253. def visit_call(self, node: nodes.Call) -> None:
  1254. """visit a Call node -> check if this is not a disallowed builtin
  1255. call and check for * or ** use
  1256. """
  1257. self._check_misplaced_format_function(node)
  1258. if isinstance(node.func, nodes.Name):
  1259. name = node.func.name
  1260. # ignore the name if it's not a builtin (i.e. not defined in the
  1261. # locals nor globals scope)
  1262. if not (name in node.frame() or name in node.root()):
  1263. if name == "exec":
  1264. self.add_message("exec-used", node=node)
  1265. elif name == "reversed":
  1266. self._check_reversed(node)
  1267. elif name == "eval":
  1268. self.add_message("eval-used", node=node)
  1269. @utils.check_messages("assert-on-tuple", "assert-on-string-literal")
  1270. def visit_assert(self, node: nodes.Assert) -> None:
  1271. """check whether assert is used on a tuple or string literal."""
  1272. if (
  1273. node.fail is None
  1274. and isinstance(node.test, nodes.Tuple)
  1275. and len(node.test.elts) == 2
  1276. ):
  1277. self.add_message("assert-on-tuple", node=node)
  1278. if isinstance(node.test, nodes.Const) and isinstance(node.test.value, str):
  1279. if node.test.value:
  1280. when = "never"
  1281. else:
  1282. when = "always"
  1283. self.add_message("assert-on-string-literal", node=node, args=(when,))
  1284. @utils.check_messages("duplicate-key")
  1285. def visit_dict(self, node: nodes.Dict) -> None:
  1286. """check duplicate key in dictionary"""
  1287. keys = set()
  1288. for k, _ in node.items:
  1289. if isinstance(k, nodes.Const):
  1290. key = k.value
  1291. elif isinstance(k, nodes.Attribute):
  1292. key = k.as_string()
  1293. else:
  1294. continue
  1295. if key in keys:
  1296. self.add_message("duplicate-key", node=node, args=key)
  1297. keys.add(key)
  1298. def visit_tryfinally(self, node: nodes.TryFinally) -> None:
  1299. """update try...finally flag"""
  1300. self._tryfinallys.append(node)
  1301. def leave_tryfinally(self, _: nodes.TryFinally) -> None:
  1302. """update try...finally flag"""
  1303. self._tryfinallys.pop()
  1304. def _check_unreachable(self, node):
  1305. """check unreachable code"""
  1306. unreach_stmt = node.next_sibling()
  1307. if unreach_stmt is not None:
  1308. if (
  1309. isinstance(node, nodes.Return)
  1310. and isinstance(unreach_stmt, nodes.Expr)
  1311. and isinstance(unreach_stmt.value, nodes.Yield)
  1312. ):
  1313. # Don't add 'unreachable' for empty generators.
  1314. # Only add warning if 'yield' is followed by another node.
  1315. unreach_stmt = unreach_stmt.next_sibling()
  1316. if unreach_stmt is None:
  1317. return
  1318. self.add_message("unreachable", node=unreach_stmt)
  1319. def _check_not_in_finally(self, node, node_name, breaker_classes=()):
  1320. """check that a node is not inside a finally clause of a
  1321. try...finally statement.
  1322. If we found before a try...finally block a parent which its type is
  1323. in breaker_classes, we skip the whole check."""
  1324. # if self._tryfinallys is empty, we're not an in try...finally block
  1325. if not self._tryfinallys:
  1326. return
  1327. # the node could be a grand-grand...-children of the try...finally
  1328. _parent = node.parent
  1329. _node = node
  1330. while _parent and not isinstance(_parent, breaker_classes):
  1331. if hasattr(_parent, "finalbody") and _node in _parent.finalbody:
  1332. self.add_message("lost-exception", node=node, args=node_name)
  1333. return
  1334. _node = _parent
  1335. _parent = _node.parent
  1336. def _check_reversed(self, node):
  1337. """check that the argument to `reversed` is a sequence"""
  1338. try:
  1339. argument = utils.safe_infer(utils.get_argument_from_call(node, position=0))
  1340. except utils.NoSuchArgumentError:
  1341. pass
  1342. else:
  1343. if argument is astroid.Uninferable:
  1344. return
  1345. if argument is None:
  1346. # Nothing was inferred.
  1347. # Try to see if we have iter().
  1348. if isinstance(node.args[0], nodes.Call):
  1349. try:
  1350. func = next(node.args[0].func.infer())
  1351. except astroid.InferenceError:
  1352. return
  1353. if getattr(
  1354. func, "name", None
  1355. ) == "iter" and utils.is_builtin_object(func):
  1356. self.add_message("bad-reversed-sequence", node=node)
  1357. return
  1358. if isinstance(argument, (nodes.List, nodes.Tuple)):
  1359. return
  1360. # dicts are reversible, but only from Python 3.8 onwards. Prior to
  1361. # that, any class based on dict must explicitly provide a
  1362. # __reversed__ method
  1363. if not self._py38_plus and isinstance(argument, astroid.Instance):
  1364. if any(
  1365. ancestor.name == "dict" and utils.is_builtin_object(ancestor)
  1366. for ancestor in itertools.chain(
  1367. (argument._proxied,), argument._proxied.ancestors()
  1368. )
  1369. ):
  1370. try:
  1371. argument.locals[REVERSED_PROTOCOL_METHOD]
  1372. except KeyError:
  1373. self.add_message("bad-reversed-sequence", node=node)
  1374. return
  1375. if hasattr(argument, "getattr"):
  1376. # everything else is not a proper sequence for reversed()
  1377. for methods in REVERSED_METHODS:
  1378. for meth in methods:
  1379. try:
  1380. argument.getattr(meth)
  1381. except astroid.NotFoundError:
  1382. break
  1383. else:
  1384. break
  1385. else:
  1386. self.add_message("bad-reversed-sequence", node=node)
  1387. else:
  1388. self.add_message("bad-reversed-sequence", node=node)
  1389. @utils.check_messages("confusing-with-statement")
  1390. def visit_with(self, node: nodes.With) -> None:
  1391. # a "with" statement with multiple managers corresponds
  1392. # to one AST "With" node with multiple items
  1393. pairs = node.items
  1394. if pairs:
  1395. for prev_pair, pair in zip(pairs, pairs[1:]):
  1396. if isinstance(prev_pair[1], nodes.AssignName) and (
  1397. pair[1] is None and not isinstance(pair[0], nodes.Call)
  1398. ):
  1399. # Don't emit a message if the second is a function call
  1400. # there's no way that can be mistaken for a name assignment.
  1401. # If the line number doesn't match
  1402. # we assume it's a nested "with".
  1403. self.add_message("confusing-with-statement", node=node)
  1404. def _check_self_assigning_variable(self, node):
  1405. # Detect assigning to the same variable.
  1406. scope = node.scope()
  1407. scope_locals = scope.locals
  1408. rhs_names = []
  1409. targets = node.targets
  1410. if isinstance(targets[0], nodes.Tuple):
  1411. if len(targets) != 1:
  1412. # A complex assignment, so bail out early.
  1413. return
  1414. targets = targets[0].elts
  1415. if len(targets) == 1:
  1416. # Unpacking a variable into the same name.
  1417. return
  1418. if isinstance(node.value, nodes.Name):
  1419. if len(targets) != 1:
  1420. return
  1421. rhs_names = [node.value]
  1422. elif isinstance(node.value, nodes.Tuple):
  1423. rhs_count = len(node.value.elts)
  1424. if len(targets) != rhs_count or rhs_count == 1:
  1425. return
  1426. rhs_names = node.value.elts
  1427. for target, lhs_name in zip(targets, rhs_names):
  1428. if not isinstance(lhs_name, nodes.Name):
  1429. continue
  1430. if not isinstance(target, nodes.AssignName):
  1431. continue
  1432. if isinstance(scope, nodes.ClassDef) and target.name in scope_locals:
  1433. # Check that the scope is different than a class level, which is usually
  1434. # a pattern to expose module level attributes as class level ones.
  1435. continue
  1436. if target.name == lhs_name.name:
  1437. self.add_message(
  1438. "self-assigning-variable", args=(target.name,), node=target
  1439. )
  1440. def _check_redeclared_assign_name(self, targets):
  1441. dummy_variables_rgx = lint_utils.get_global_option(
  1442. self, "dummy-variables-rgx", default=None
  1443. )
  1444. for target in targets:
  1445. if not isinstance(target, nodes.Tuple):
  1446. continue
  1447. found_names = []
  1448. for element in target.elts:
  1449. if isinstance(element, nodes.Tuple):
  1450. self._check_redeclared_assign_name([element])
  1451. elif isinstance(element, nodes.AssignName) and element.name != "_":
  1452. if dummy_variables_rgx and dummy_variables_rgx.match(element.name):
  1453. return
  1454. found_names.append(element.name)
  1455. names = collections.Counter(found_names)
  1456. for name, count in names.most_common():
  1457. if count > 1:
  1458. self.add_message(
  1459. "redeclared-assigned-name", args=(name,), node=target
  1460. )
  1461. @utils.check_messages("self-assigning-variable", "redeclared-assigned-name")
  1462. def visit_assign(self, node: nodes.Assign) -> None:
  1463. self._check_self_assigning_variable(node)
  1464. self._check_redeclared_assign_name(node.targets)
  1465. @utils.check_messages("redeclared-assigned-name")
  1466. def visit_for(self, node: nodes.For) -> None:
  1467. self._check_redeclared_assign_name([node.target])
  1468. KNOWN_NAME_TYPES = {
  1469. "module",
  1470. "const",
  1471. "class",
  1472. "function",
  1473. "method",
  1474. "attr",
  1475. "argument",
  1476. "variable",
  1477. "class_attribute",
  1478. "class_const",
  1479. "inlinevar",
  1480. }
  1481. HUMAN_READABLE_TYPES = {
  1482. "module": "module",
  1483. "const": "constant",
  1484. "class": "class",
  1485. "function": "function",
  1486. "method": "method",
  1487. "attr": "attribute",
  1488. "argument": "argument",
  1489. "variable": "variable",
  1490. "class_attribute": "class attribute",
  1491. "class_const": "class constant",
  1492. "inlinevar": "inline iteration",
  1493. }
  1494. DEFAULT_NAMING_STYLES = {
  1495. "module": "snake_case",
  1496. "const": "UPPER_CASE",
  1497. "class": "PascalCase",
  1498. "function": "snake_case",
  1499. "method": "snake_case",
  1500. "attr": "snake_case",
  1501. "argument": "snake_case",
  1502. "variable": "snake_case",
  1503. "class_attribute": "any",
  1504. "class_const": "UPPER_CASE",
  1505. "inlinevar": "any",
  1506. }
  1507. def _create_naming_options():
  1508. name_options = []
  1509. for name_type in sorted(KNOWN_NAME_TYPES):
  1510. human_readable_name = HUMAN_READABLE_TYPES[name_type]
  1511. default_style = DEFAULT_NAMING_STYLES[name_type]
  1512. name_type = name_type.replace("_", "-")
  1513. name_options.append(
  1514. (
  1515. f"{name_type}-naming-style",
  1516. {
  1517. "default": default_style,
  1518. "type": "choice",
  1519. "choices": list(NAMING_STYLES.keys()),
  1520. "metavar": "<style>",
  1521. "help": f"Naming style matching correct {human_readable_name} names.",
  1522. },
  1523. )
  1524. )
  1525. name_options.append(
  1526. (
  1527. f"{name_type}-rgx",
  1528. {
  1529. "default": None,
  1530. "type": "regexp",
  1531. "metavar": "<regexp>",
  1532. "help": f"Regular expression matching correct {human_readable_name} names. Overrides {name_type}-naming-style.",
  1533. },
  1534. )
  1535. )
  1536. return tuple(name_options)
  1537. class NameChecker(_BasicChecker):
  1538. msgs = {
  1539. "C0103": (
  1540. '%s name "%s" doesn\'t conform to %s',
  1541. "invalid-name",
  1542. "Used when the name doesn't conform to naming rules "
  1543. "associated to its type (constant, variable, class...).",
  1544. ),
  1545. "C0104": (
  1546. 'Disallowed name "%s"',
  1547. "disallowed-name",
  1548. "Used when the name matches bad-names or bad-names-rgxs- (unauthorized names).",
  1549. {
  1550. "old_names": [
  1551. ("C0102", "blacklisted-name"),
  1552. ]
  1553. },
  1554. ),
  1555. "C0144": (
  1556. '%s name "%s" contains a non-ASCII unicode character',
  1557. "non-ascii-name",
  1558. "Used when the name contains at least one non-ASCII unicode character.",
  1559. ),
  1560. "W0111": (
  1561. "Name %s will become a keyword in Python %s",
  1562. "assign-to-new-keyword",
  1563. "Used when assignment will become invalid in future "
  1564. "Python release due to introducing new keyword.",
  1565. ),
  1566. }
  1567. options = (
  1568. (
  1569. "good-names",
  1570. {
  1571. "default": ("i", "j", "k", "ex", "Run", "_"),
  1572. "type": "csv",
  1573. "metavar": "<names>",
  1574. "help": "Good variable names which should always be accepted,"
  1575. " separated by a comma.",
  1576. },
  1577. ),
  1578. (
  1579. "good-names-rgxs",
  1580. {
  1581. "default": "",
  1582. "type": "regexp_csv",
  1583. "metavar": "<names>",
  1584. "help": "Good variable names regexes, separated by a comma. If names match any regex,"
  1585. " they will always be accepted",
  1586. },
  1587. ),
  1588. (
  1589. "bad-names",
  1590. {
  1591. "default": ("foo", "bar", "baz", "toto", "tutu", "tata"),
  1592. "type": "csv",
  1593. "metavar": "<names>",
  1594. "help": "Bad variable names which should always be refused, "
  1595. "separated by a comma.",
  1596. },
  1597. ),
  1598. (
  1599. "bad-names-rgxs",
  1600. {
  1601. "default": "",
  1602. "type": "regexp_csv",
  1603. "metavar": "<names>",
  1604. "help": "Bad variable names regexes, separated by a comma. If names match any regex,"
  1605. " they will always be refused",
  1606. },
  1607. ),
  1608. (
  1609. "name-group",
  1610. {
  1611. "default": (),
  1612. "type": "csv",
  1613. "metavar": "<name1:name2>",
  1614. "help": (
  1615. "Colon-delimited sets of names that determine each"
  1616. " other's naming style when the name regexes"
  1617. " allow several styles."
  1618. ),
  1619. },
  1620. ),
  1621. (
  1622. "include-naming-hint",
  1623. {
  1624. "default": False,
  1625. "type": "yn",
  1626. "metavar": "<y or n>",
  1627. "help": "Include a hint for the correct naming format with invalid-name.",
  1628. },
  1629. ),
  1630. (
  1631. "property-classes",
  1632. {
  1633. "default": ("abc.abstractproperty",),
  1634. "type": "csv",
  1635. "metavar": "<decorator names>",
  1636. "help": "List of decorators that produce properties, such as "
  1637. "abc.abstractproperty. Add to this list to register "
  1638. "other decorators that produce valid properties. "
  1639. "These decorators are taken in consideration only for invalid-name.",
  1640. },
  1641. ),
  1642. ) + _create_naming_options()
  1643. KEYWORD_ONSET = {(3, 7): {"async", "await"}}
  1644. def __init__(self, linter):
  1645. super().__init__(linter)
  1646. self._name_category = {}
  1647. self._name_group = {}
  1648. self._bad_names = {}
  1649. self._name_regexps = {}
  1650. self._name_hints = {}
  1651. self._good_names_rgxs_compiled = []
  1652. self._bad_names_rgxs_compiled = []
  1653. self._non_ascii_rgx_compiled = re.compile("[^\u0000-\u007F]")
  1654. def open(self):
  1655. self.linter.stats.reset_bad_names()
  1656. for group in self.config.name_group:
  1657. for name_type in group.split(":"):
  1658. self._name_group[name_type] = f"group_{group}"
  1659. regexps, hints = self._create_naming_rules()
  1660. self._name_regexps = regexps
  1661. self._name_hints = hints
  1662. self._good_names_rgxs_compiled = [
  1663. re.compile(rgxp) for rgxp in self.config.good_names_rgxs
  1664. ]
  1665. self._bad_names_rgxs_compiled = [
  1666. re.compile(rgxp) for rgxp in self.config.bad_names_rgxs
  1667. ]
  1668. def _create_naming_rules(self):
  1669. regexps = {}
  1670. hints = {}
  1671. for name_type in KNOWN_NAME_TYPES:
  1672. naming_style_option_name = f"{name_type}_naming_style"
  1673. naming_style_name = getattr(self.config, naming_style_option_name)
  1674. regexps[name_type] = NAMING_STYLES[naming_style_name].get_regex(name_type)
  1675. custom_regex_setting_name = f"{name_type}_rgx"
  1676. custom_regex = getattr(self.config, custom_regex_setting_name, None)
  1677. if custom_regex is not None:
  1678. regexps[name_type] = custom_regex
  1679. if custom_regex is not None:
  1680. hints[name_type] = f"{custom_regex.pattern!r} pattern"
  1681. else:
  1682. hints[name_type] = f"{naming_style_name} naming style"
  1683. return regexps, hints
  1684. @utils.check_messages("disallowed-name", "invalid-name", "non-ascii-name")
  1685. def visit_module(self, node: nodes.Module) -> None:
  1686. self._check_name("module", node.name.split(".")[-1], node)
  1687. self._bad_names = {}
  1688. def leave_module(self, _: nodes.Module) -> None:
  1689. for all_groups in self._bad_names.values():
  1690. if len(all_groups) < 2:
  1691. continue
  1692. groups = collections.defaultdict(list)
  1693. min_warnings = sys.maxsize
  1694. prevalent_group, _ = max(all_groups.items(), key=lambda item: len(item[1]))
  1695. for group in all_groups.values():
  1696. groups[len(group)].append(group)
  1697. min_warnings = min(len(group), min_warnings)
  1698. if len(groups[min_warnings]) > 1:
  1699. by_line = sorted(
  1700. groups[min_warnings],
  1701. key=lambda group: min(warning[0].lineno for warning in group),
  1702. )
  1703. warnings = itertools.chain(*by_line[1:])
  1704. else:
  1705. warnings = groups[min_warnings][0]
  1706. for args in warnings:
  1707. self._raise_name_warning(prevalent_group, *args)
  1708. @utils.check_messages(
  1709. "disallowed-name", "invalid-name", "assign-to-new-keyword", "non-ascii-name"
  1710. )
  1711. def visit_classdef(self, node: nodes.ClassDef) -> None:
  1712. self._check_assign_to_new_keyword_violation(node.name, node)
  1713. self._check_name("class", node.name, node)
  1714. for attr, anodes in node.instance_attrs.items():
  1715. if not any(node.instance_attr_ancestors(attr)):
  1716. self._check_name("attr", attr, anodes[0])
  1717. @utils.check_messages(
  1718. "disallowed-name", "invalid-name", "assign-to-new-keyword", "non-ascii-name"
  1719. )
  1720. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  1721. # Do not emit any warnings if the method is just an implementation
  1722. # of a base class method.
  1723. self._check_assign_to_new_keyword_violation(node.name, node)
  1724. confidence = interfaces.HIGH
  1725. if node.is_method():
  1726. if utils.overrides_a_method(node.parent.frame(), node.name):
  1727. return
  1728. confidence = (
  1729. interfaces.INFERENCE
  1730. if utils.has_known_bases(node.parent.frame())
  1731. else interfaces.INFERENCE_FAILURE
  1732. )
  1733. self._check_name(
  1734. _determine_function_name_type(node, config=self.config),
  1735. node.name,
  1736. node,
  1737. confidence,
  1738. )
  1739. # Check argument names
  1740. args = node.args.args
  1741. if args is not None:
  1742. self._recursive_check_names(args)
  1743. visit_asyncfunctiondef = visit_functiondef
  1744. @utils.check_messages("disallowed-name", "invalid-name", "non-ascii-name")
  1745. def visit_global(self, node: nodes.Global) -> None:
  1746. for name in node.names:
  1747. self._check_name("const", name, node)
  1748. @utils.check_messages(
  1749. "disallowed-name", "invalid-name", "assign-to-new-keyword", "non-ascii-name"
  1750. )
  1751. def visit_assignname(self, node: nodes.AssignName) -> None:
  1752. """check module level assigned names"""
  1753. self._check_assign_to_new_keyword_violation(node.name, node)
  1754. frame = node.frame()
  1755. assign_type = node.assign_type()
  1756. if isinstance(assign_type, nodes.Comprehension):
  1757. self._check_name("inlinevar", node.name, node)
  1758. elif isinstance(frame, nodes.Module):
  1759. if isinstance(assign_type, nodes.Assign):
  1760. if isinstance(utils.safe_infer(assign_type.value), nodes.ClassDef):
  1761. self._check_name("class", node.name, node)
  1762. # Don't emit if the name redefines an import
  1763. # in an ImportError except handler.
  1764. elif not _redefines_import(node) and isinstance(
  1765. utils.safe_infer(assign_type.value), nodes.Const
  1766. ):
  1767. self._check_name("const", node.name, node)
  1768. elif isinstance(
  1769. assign_type, nodes.AnnAssign
  1770. ) and utils.is_assign_name_annotated_with(node, "Final"):
  1771. self._check_name("const", node.name, node)
  1772. elif isinstance(frame, nodes.FunctionDef):
  1773. # global introduced variable aren't in the function locals
  1774. if node.name in frame and node.name not in frame.argnames():
  1775. if not _redefines_import(node):
  1776. self._check_name("variable", node.name, node)
  1777. elif isinstance(frame, nodes.ClassDef):
  1778. if not list(frame.local_attr_ancestors(node.name)):
  1779. for ancestor in frame.ancestors():
  1780. if (
  1781. ancestor.name == "Enum"
  1782. and ancestor.root().name == "enum"
  1783. or utils.is_assign_name_annotated_with(node, "Final")
  1784. ):
  1785. self._check_name("class_const", node.name, node)
  1786. break
  1787. else:
  1788. self._check_name("class_attribute", node.name, node)
  1789. def _recursive_check_names(self, args):
  1790. """check names in a possibly recursive list <arg>"""
  1791. for arg in args:
  1792. if isinstance(arg, nodes.AssignName):
  1793. self._check_name("argument", arg.name, arg)
  1794. else:
  1795. self._recursive_check_names(arg.elts)
  1796. def _find_name_group(self, node_type):
  1797. return self._name_group.get(node_type, node_type)
  1798. def _raise_name_warning(
  1799. self,
  1800. prevalent_group: Optional[str],
  1801. node: nodes.NodeNG,
  1802. node_type: str,
  1803. name: str,
  1804. confidence,
  1805. warning: str = "invalid-name",
  1806. ) -> None:
  1807. type_label = HUMAN_READABLE_TYPES[node_type]
  1808. hint = self._name_hints[node_type]
  1809. if prevalent_group:
  1810. # This happens in the multi naming match case. The expected
  1811. # prevalent group needs to be spelled out to make the message
  1812. # correct.
  1813. hint = f"the `{prevalent_group}` group in the {hint}"
  1814. if self.config.include_naming_hint:
  1815. hint += f" ({self._name_regexps[node_type].pattern!r} pattern)"
  1816. args = (
  1817. (type_label.capitalize(), name, hint)
  1818. if warning == "invalid-name"
  1819. else (type_label.capitalize(), name)
  1820. )
  1821. self.add_message(warning, node=node, args=args, confidence=confidence)
  1822. self.linter.stats.increase_bad_name(node_type, 1)
  1823. def _name_allowed_by_regex(self, name: str) -> bool:
  1824. return name in self.config.good_names or any(
  1825. pattern.match(name) for pattern in self._good_names_rgxs_compiled
  1826. )
  1827. def _name_disallowed_by_regex(self, name: str) -> bool:
  1828. return name in self.config.bad_names or any(
  1829. pattern.match(name) for pattern in self._bad_names_rgxs_compiled
  1830. )
  1831. def _check_name(self, node_type, name, node, confidence=interfaces.HIGH):
  1832. """check for a name using the type's regexp"""
  1833. non_ascii_match = self._non_ascii_rgx_compiled.match(name)
  1834. if non_ascii_match is not None:
  1835. self._raise_name_warning(
  1836. None, node, node_type, name, confidence, warning="non-ascii-name"
  1837. )
  1838. def _should_exempt_from_invalid_name(node):
  1839. if node_type == "variable":
  1840. inferred = utils.safe_infer(node)
  1841. if isinstance(inferred, nodes.ClassDef):
  1842. return True
  1843. return False
  1844. if self._name_allowed_by_regex(name=name):
  1845. return
  1846. if self._name_disallowed_by_regex(name=name):
  1847. self.linter.stats.increase_bad_name(node_type, 1)
  1848. self.add_message("disallowed-name", node=node, args=name)
  1849. return
  1850. regexp = self._name_regexps[node_type]
  1851. match = regexp.match(name)
  1852. if _is_multi_naming_match(match, node_type, confidence):
  1853. name_group = self._find_name_group(node_type)
  1854. bad_name_group = self._bad_names.setdefault(name_group, {})
  1855. warnings = bad_name_group.setdefault(match.lastgroup, [])
  1856. warnings.append((node, node_type, name, confidence))
  1857. if match is None and not _should_exempt_from_invalid_name(node):
  1858. self._raise_name_warning(None, node, node_type, name, confidence)
  1859. def _check_assign_to_new_keyword_violation(self, name, node):
  1860. keyword_first_version = self._name_became_keyword_in_version(
  1861. name, self.KEYWORD_ONSET
  1862. )
  1863. if keyword_first_version is not None:
  1864. self.add_message(
  1865. "assign-to-new-keyword",
  1866. node=node,
  1867. args=(name, keyword_first_version),
  1868. confidence=interfaces.HIGH,
  1869. )
  1870. @staticmethod
  1871. def _name_became_keyword_in_version(name, rules):
  1872. for version, keywords in rules.items():
  1873. if name in keywords and sys.version_info < version:
  1874. return ".".join(str(v) for v in version)
  1875. return None
  1876. class DocStringChecker(_BasicChecker):
  1877. msgs = {
  1878. "C0112": (
  1879. "Empty %s docstring",
  1880. "empty-docstring",
  1881. "Used when a module, function, class or method has an empty "
  1882. "docstring (it would be too easy ;).",
  1883. {"old_names": [("W0132", "old-empty-docstring")]},
  1884. ),
  1885. "C0114": (
  1886. "Missing module docstring",
  1887. "missing-module-docstring",
  1888. "Used when a module has no docstring."
  1889. "Empty modules do not require a docstring.",
  1890. {"old_names": [("C0111", "missing-docstring")]},
  1891. ),
  1892. "C0115": (
  1893. "Missing class docstring",
  1894. "missing-class-docstring",
  1895. "Used when a class has no docstring."
  1896. "Even an empty class must have a docstring.",
  1897. {"old_names": [("C0111", "missing-docstring")]},
  1898. ),
  1899. "C0116": (
  1900. "Missing function or method docstring",
  1901. "missing-function-docstring",
  1902. "Used when a function or method has no docstring."
  1903. "Some special methods like __init__ do not require a "
  1904. "docstring.",
  1905. {"old_names": [("C0111", "missing-docstring")]},
  1906. ),
  1907. }
  1908. options = (
  1909. (
  1910. "no-docstring-rgx",
  1911. {
  1912. "default": NO_REQUIRED_DOC_RGX,
  1913. "type": "regexp",
  1914. "metavar": "<regexp>",
  1915. "help": "Regular expression which should only match "
  1916. "function or class names that do not require a "
  1917. "docstring.",
  1918. },
  1919. ),
  1920. (
  1921. "docstring-min-length",
  1922. {
  1923. "default": -1,
  1924. "type": "int",
  1925. "metavar": "<int>",
  1926. "help": (
  1927. "Minimum line length for functions/classes that"
  1928. " require docstrings, shorter ones are exempt."
  1929. ),
  1930. },
  1931. ),
  1932. )
  1933. def open(self):
  1934. self.linter.stats.reset_undocumented()
  1935. @utils.check_messages("missing-docstring", "empty-docstring")
  1936. def visit_module(self, node: nodes.Module) -> None:
  1937. self._check_docstring("module", node)
  1938. @utils.check_messages("missing-docstring", "empty-docstring")
  1939. def visit_classdef(self, node: nodes.ClassDef) -> None:
  1940. if self.config.no_docstring_rgx.match(node.name) is None:
  1941. self._check_docstring("class", node)
  1942. @utils.check_messages("missing-docstring", "empty-docstring")
  1943. def visit_functiondef(self, node: nodes.FunctionDef) -> None:
  1944. if self.config.no_docstring_rgx.match(node.name) is None:
  1945. ftype = "method" if node.is_method() else "function"
  1946. if (
  1947. is_property_setter(node)
  1948. or is_property_deleter(node)
  1949. or is_overload_stub(node)
  1950. ):
  1951. return
  1952. if isinstance(node.parent.frame(), nodes.ClassDef):
  1953. overridden = False
  1954. confidence = (
  1955. interfaces.INFERENCE
  1956. if utils.has_known_bases(node.parent.frame())
  1957. else interfaces.INFERENCE_FAILURE
  1958. )
  1959. # check if node is from a method overridden by its ancestor
  1960. for ancestor in node.parent.frame().ancestors():
  1961. if ancestor.qname() == "builtins.object":
  1962. continue
  1963. if node.name in ancestor and isinstance(
  1964. ancestor[node.name], nodes.FunctionDef
  1965. ):
  1966. overridden = True
  1967. break
  1968. self._check_docstring(
  1969. ftype, node, report_missing=not overridden, confidence=confidence # type: ignore[arg-type]
  1970. )
  1971. elif isinstance(node.parent.frame(), nodes.Module):
  1972. self._check_docstring(ftype, node) # type: ignore[arg-type]
  1973. else:
  1974. return
  1975. visit_asyncfunctiondef = visit_functiondef
  1976. def _check_docstring(
  1977. self,
  1978. node_type: Literal["class", "function", "method", "module"],
  1979. node,
  1980. report_missing=True,
  1981. confidence=interfaces.HIGH,
  1982. ):
  1983. """check the node has a non empty docstring"""
  1984. docstring = node.doc
  1985. if docstring is None:
  1986. docstring = _infer_dunder_doc_attribute(node)
  1987. if docstring is None:
  1988. if not report_missing:
  1989. return
  1990. lines = utils.get_node_last_lineno(node) - node.lineno
  1991. if node_type == "module" and not lines:
  1992. # If the module has no body, there's no reason
  1993. # to require a docstring.
  1994. return
  1995. max_lines = self.config.docstring_min_length
  1996. if node_type != "module" and max_lines > -1 and lines < max_lines:
  1997. return
  1998. if node_type == "class":
  1999. self.linter.stats.undocumented["klass"] += 1
  2000. else:
  2001. self.linter.stats.undocumented[node_type] += 1
  2002. if (
  2003. node.body
  2004. and isinstance(node.body[0], nodes.Expr)
  2005. and isinstance(node.body[0].value, nodes.Call)
  2006. ):
  2007. # Most likely a string with a format call. Let's see.
  2008. func = utils.safe_infer(node.body[0].value.func)
  2009. if isinstance(func, astroid.BoundMethod) and isinstance(
  2010. func.bound, astroid.Instance
  2011. ):
  2012. # Strings.
  2013. if func.bound.name in {"str", "unicode", "bytes"}:
  2014. return
  2015. if node_type == "module":
  2016. message = "missing-module-docstring"
  2017. elif node_type == "class":
  2018. message = "missing-class-docstring"
  2019. else:
  2020. message = "missing-function-docstring"
  2021. self.add_message(message, node=node, confidence=confidence)
  2022. elif not docstring.strip():
  2023. if node_type == "class":
  2024. self.linter.stats.undocumented["klass"] += 1
  2025. else:
  2026. self.linter.stats.undocumented[node_type] += 1
  2027. self.add_message(
  2028. "empty-docstring", node=node, args=(node_type,), confidence=confidence
  2029. )
  2030. class PassChecker(_BasicChecker):
  2031. """check if the pass statement is really necessary"""
  2032. msgs = {
  2033. "W0107": (
  2034. "Unnecessary pass statement",
  2035. "unnecessary-pass",
  2036. 'Used when a "pass" statement that can be avoided is encountered.',
  2037. )
  2038. }
  2039. @utils.check_messages("unnecessary-pass")
  2040. def visit_pass(self, node: nodes.Pass) -> None:
  2041. if len(node.parent.child_sequence(node)) > 1 or (
  2042. isinstance(node.parent, (nodes.ClassDef, nodes.FunctionDef))
  2043. and (node.parent.doc is not None)
  2044. ):
  2045. self.add_message("unnecessary-pass", node=node)
  2046. def _is_one_arg_pos_call(call):
  2047. """Is this a call with exactly 1 argument,
  2048. where that argument is positional?
  2049. """
  2050. return isinstance(call, nodes.Call) and len(call.args) == 1 and not call.keywords
  2051. def _infer_dunder_doc_attribute(node):
  2052. # Try to see if we have a `__doc__` attribute.
  2053. try:
  2054. docstring = node["__doc__"]
  2055. except KeyError:
  2056. return None
  2057. docstring = utils.safe_infer(docstring)
  2058. if not docstring:
  2059. return None
  2060. if not isinstance(docstring, nodes.Const):
  2061. return None
  2062. return docstring.value
  2063. class ComparisonChecker(_BasicChecker):
  2064. """Checks for comparisons
  2065. - singleton comparison: 'expr == True', 'expr == False' and 'expr == None'
  2066. - yoda condition: 'const "comp" right' where comp can be '==', '!=', '<',
  2067. '<=', '>' or '>=', and right can be a variable, an attribute, a method or
  2068. a function
  2069. """
  2070. msgs = {
  2071. "C0121": (
  2072. "Comparison %s should be %s",
  2073. "singleton-comparison",
  2074. "Used when an expression is compared to singleton "
  2075. "values like True, False or None.",
  2076. ),
  2077. "C0123": (
  2078. "Use isinstance() rather than type() for a typecheck.",
  2079. "unidiomatic-typecheck",
  2080. "The idiomatic way to perform an explicit typecheck in "
  2081. "Python is to use isinstance(x, Y) rather than "
  2082. "type(x) == Y, type(x) is Y. Though there are unusual "
  2083. "situations where these give different results.",
  2084. {"old_names": [("W0154", "old-unidiomatic-typecheck")]},
  2085. ),
  2086. "R0123": (
  2087. "Comparison to literal",
  2088. "literal-comparison",
  2089. "Used when comparing an object to a literal, which is usually "
  2090. "what you do not want to do, since you can compare to a different "
  2091. "literal than what was expected altogether.",
  2092. ),
  2093. "R0124": (
  2094. "Redundant comparison - %s",
  2095. "comparison-with-itself",
  2096. "Used when something is compared against itself.",
  2097. ),
  2098. "W0143": (
  2099. "Comparing against a callable, did you omit the parenthesis?",
  2100. "comparison-with-callable",
  2101. "This message is emitted when pylint detects that a comparison with a "
  2102. "callable was made, which might suggest that some parenthesis were omitted, "
  2103. "resulting in potential unwanted behaviour.",
  2104. ),
  2105. "W0177": (
  2106. "Comparison %s should be %s",
  2107. "nan-comparison",
  2108. "Used when an expression is compared to NaN"
  2109. "values like numpy.NaN and float('nan')",
  2110. ),
  2111. }
  2112. def _check_singleton_comparison(
  2113. self, left_value, right_value, root_node, checking_for_absence: bool = False
  2114. ):
  2115. """Check if == or != is being used to compare a singleton value"""
  2116. singleton_values = (True, False, None)
  2117. def _is_singleton_const(node) -> bool:
  2118. return isinstance(node, nodes.Const) and any(
  2119. node.value is value for value in singleton_values
  2120. )
  2121. if _is_singleton_const(left_value):
  2122. singleton, other_value = left_value.value, right_value
  2123. elif _is_singleton_const(right_value):
  2124. singleton, other_value = right_value.value, left_value
  2125. else:
  2126. return
  2127. singleton_comparison_example = {False: "'{} is {}'", True: "'{} is not {}'"}
  2128. # True/False singletons have a special-cased message in case the user is
  2129. # mistakenly using == or != to check for truthiness
  2130. if singleton in {True, False}:
  2131. suggestion_template = (
  2132. "{} if checking for the singleton value {}, or {} if testing for {}"
  2133. )
  2134. truthiness_example = {False: "not {}", True: "{}"}
  2135. truthiness_phrase = {True: "truthiness", False: "falsiness"}
  2136. # Looks for comparisons like x == True or x != False
  2137. checking_truthiness = singleton is not checking_for_absence
  2138. suggestion = suggestion_template.format(
  2139. singleton_comparison_example[checking_for_absence].format(
  2140. left_value.as_string(), right_value.as_string()
  2141. ),
  2142. singleton,
  2143. (
  2144. "'bool({})'"
  2145. if not utils.is_test_condition(root_node) and checking_truthiness
  2146. else "'{}'"
  2147. ).format(
  2148. truthiness_example[checking_truthiness].format(
  2149. other_value.as_string()
  2150. )
  2151. ),
  2152. truthiness_phrase[checking_truthiness],
  2153. )
  2154. else:
  2155. suggestion = singleton_comparison_example[checking_for_absence].format(
  2156. left_value.as_string(), right_value.as_string()
  2157. )
  2158. self.add_message(
  2159. "singleton-comparison",
  2160. node=root_node,
  2161. args=(f"'{root_node.as_string()}'", suggestion),
  2162. )
  2163. def _check_nan_comparison(
  2164. self, left_value, right_value, root_node, checking_for_absence: bool = False
  2165. ):
  2166. def _is_float_nan(node):
  2167. try:
  2168. if isinstance(node, nodes.Call) and len(node.args) == 1:
  2169. if (
  2170. node.args[0].value.lower() == "nan"
  2171. and node.inferred()[0].pytype() == "builtins.float"
  2172. ):
  2173. return True
  2174. return False
  2175. except AttributeError:
  2176. return False
  2177. def _is_numpy_nan(node):
  2178. if isinstance(node, nodes.Attribute) and node.attrname == "NaN":
  2179. if isinstance(node.expr, nodes.Name):
  2180. return node.expr.name in {"numpy", "nmp", "np"}
  2181. return False
  2182. def _is_nan(node) -> bool:
  2183. return _is_float_nan(node) or _is_numpy_nan(node)
  2184. nan_left = _is_nan(left_value)
  2185. if not nan_left and not _is_nan(right_value):
  2186. return
  2187. absence_text = ""
  2188. if checking_for_absence:
  2189. absence_text = "not "
  2190. if nan_left:
  2191. suggestion = f"'{absence_text}math.isnan({right_value.as_string()})'"
  2192. else:
  2193. suggestion = f"'{absence_text}math.isnan({left_value.as_string()})'"
  2194. self.add_message(
  2195. "nan-comparison",
  2196. node=root_node,
  2197. args=(f"'{root_node.as_string()}'", suggestion),
  2198. )
  2199. def _check_literal_comparison(self, literal, node: nodes.Compare):
  2200. """Check if we compare to a literal, which is usually what we do not want to do."""
  2201. is_other_literal = isinstance(literal, (nodes.List, nodes.Dict, nodes.Set))
  2202. is_const = False
  2203. if isinstance(literal, nodes.Const):
  2204. if isinstance(literal.value, bool) or literal.value is None:
  2205. # Not interested in this values.
  2206. return
  2207. is_const = isinstance(literal.value, (bytes, str, int, float))
  2208. if is_const or is_other_literal:
  2209. self.add_message("literal-comparison", node=node)
  2210. def _check_logical_tautology(self, node: nodes.Compare):
  2211. """Check if identifier is compared against itself.
  2212. :param node: Compare node
  2213. :Example:
  2214. val = 786
  2215. if val == val: # [comparison-with-itself]
  2216. pass
  2217. """
  2218. left_operand = node.left
  2219. right_operand = node.ops[0][1]
  2220. operator = node.ops[0][0]
  2221. if isinstance(left_operand, nodes.Const) and isinstance(
  2222. right_operand, nodes.Const
  2223. ):
  2224. left_operand = left_operand.value
  2225. right_operand = right_operand.value
  2226. elif isinstance(left_operand, nodes.Name) and isinstance(
  2227. right_operand, nodes.Name
  2228. ):
  2229. left_operand = left_operand.name
  2230. right_operand = right_operand.name
  2231. if left_operand == right_operand:
  2232. suggestion = f"{left_operand} {operator} {right_operand}"
  2233. self.add_message("comparison-with-itself", node=node, args=(suggestion,))
  2234. def _check_callable_comparison(self, node):
  2235. operator = node.ops[0][0]
  2236. if operator not in COMPARISON_OPERATORS:
  2237. return
  2238. bare_callables = (nodes.FunctionDef, astroid.BoundMethod)
  2239. left_operand, right_operand = node.left, node.ops[0][1]
  2240. # this message should be emitted only when there is comparison of bare callable
  2241. # with non bare callable.
  2242. if (
  2243. sum(
  2244. 1
  2245. for operand in (left_operand, right_operand)
  2246. if isinstance(utils.safe_infer(operand), bare_callables)
  2247. )
  2248. == 1
  2249. ):
  2250. self.add_message("comparison-with-callable", node=node)
  2251. @utils.check_messages(
  2252. "singleton-comparison",
  2253. "unidiomatic-typecheck",
  2254. "literal-comparison",
  2255. "comparison-with-itself",
  2256. "comparison-with-callable",
  2257. )
  2258. def visit_compare(self, node: nodes.Compare) -> None:
  2259. self._check_callable_comparison(node)
  2260. self._check_logical_tautology(node)
  2261. self._check_unidiomatic_typecheck(node)
  2262. # NOTE: this checker only works with binary comparisons like 'x == 42'
  2263. # but not 'x == y == 42'
  2264. if len(node.ops) != 1:
  2265. return
  2266. left = node.left
  2267. operator, right = node.ops[0]
  2268. if operator in {"==", "!="}:
  2269. self._check_singleton_comparison(
  2270. left, right, node, checking_for_absence=operator == "!="
  2271. )
  2272. if operator in {"==", "!=", "is", "is not"}:
  2273. self._check_nan_comparison(
  2274. left, right, node, checking_for_absence=operator in {"!=", "is not"}
  2275. )
  2276. if operator in {"is", "is not"}:
  2277. self._check_literal_comparison(right, node)
  2278. def _check_unidiomatic_typecheck(self, node):
  2279. operator, right = node.ops[0]
  2280. if operator in TYPECHECK_COMPARISON_OPERATORS:
  2281. left = node.left
  2282. if _is_one_arg_pos_call(left):
  2283. self._check_type_x_is_y(node, left, operator, right)
  2284. def _check_type_x_is_y(self, node, left, operator, right):
  2285. """Check for expressions like type(x) == Y."""
  2286. left_func = utils.safe_infer(left.func)
  2287. if not (
  2288. isinstance(left_func, nodes.ClassDef) and left_func.qname() == TYPE_QNAME
  2289. ):
  2290. return
  2291. if operator in {"is", "is not"} and _is_one_arg_pos_call(right):
  2292. right_func = utils.safe_infer(right.func)
  2293. if (
  2294. isinstance(right_func, nodes.ClassDef)
  2295. and right_func.qname() == TYPE_QNAME
  2296. ):
  2297. # type(x) == type(a)
  2298. right_arg = utils.safe_infer(right.args[0])
  2299. if not isinstance(right_arg, LITERAL_NODE_TYPES):
  2300. # not e.g. type(x) == type([])
  2301. return
  2302. self.add_message("unidiomatic-typecheck", node=node)
  2303. def register(linter):
  2304. """required method to auto register this checker"""
  2305. linter.register_checker(BasicErrorChecker(linter))
  2306. linter.register_checker(BasicChecker(linter))
  2307. linter.register_checker(NameChecker(linter))
  2308. linter.register_checker(DocStringChecker(linter))
  2309. linter.register_checker(PassChecker(linter))
  2310. linter.register_checker(ComparisonChecker(linter))