utils.py 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """Some functions that may be useful for various checkers."""
  5. from __future__ import annotations
  6. import _string
  7. import builtins
  8. import fnmatch
  9. import itertools
  10. import numbers
  11. import re
  12. import string
  13. from collections.abc import Callable, Iterable, Iterator
  14. from functools import lru_cache, partial
  15. from re import Match
  16. from typing import TYPE_CHECKING, TypeVar
  17. import astroid
  18. import astroid.exceptions
  19. import astroid.helpers
  20. from astroid import TooManyLevelsError, bases, nodes, objects, util
  21. from astroid.context import InferenceContext
  22. from astroid.exceptions import AstroidError
  23. from astroid.nodes._base_nodes import ImportNode, Statement
  24. from astroid.typing import InferenceResult, SuccessfulInferenceResult
  25. from pylint.constants import TYPING_NEVER, TYPING_NORETURN
  26. if TYPE_CHECKING:
  27. from pylint.checkers import BaseChecker
  28. _NodeT = TypeVar("_NodeT", bound=nodes.NodeNG)
  29. _CheckerT = TypeVar("_CheckerT", bound="BaseChecker")
  30. AstCallbackMethod = Callable[[_CheckerT, _NodeT], None]
  31. COMP_NODE_TYPES = (
  32. nodes.ListComp,
  33. nodes.SetComp,
  34. nodes.DictComp,
  35. nodes.GeneratorExp,
  36. )
  37. EXCEPTIONS_MODULE = "builtins"
  38. ABC_MODULES = {"abc", "_py_abc"}
  39. ABC_METHODS = {
  40. "abc.abstractproperty",
  41. "abc.abstractmethod",
  42. "abc.abstractclassmethod",
  43. "abc.abstractstaticmethod",
  44. }
  45. TYPING_PROTOCOLS = frozenset(
  46. {"typing.Protocol", "typing_extensions.Protocol", ".Protocol"}
  47. )
  48. COMMUTATIVE_OPERATORS = frozenset({"*", "+", "^", "&", "|"})
  49. ITER_METHOD = "__iter__"
  50. AITER_METHOD = "__aiter__"
  51. NEXT_METHOD = "__next__"
  52. GETITEM_METHOD = "__getitem__"
  53. CLASS_GETITEM_METHOD = "__class_getitem__"
  54. SETITEM_METHOD = "__setitem__"
  55. DELITEM_METHOD = "__delitem__"
  56. CONTAINS_METHOD = "__contains__"
  57. KEYS_METHOD = "keys"
  58. # Dictionary which maps the number of expected parameters a
  59. # special method can have to a set of special methods.
  60. # The following keys are used to denote the parameters restrictions:
  61. #
  62. # * None: variable number of parameters
  63. # * number: exactly that number of parameters
  64. # * tuple: these are the odd ones. Basically it means that the function
  65. # can work with any number of arguments from that tuple,
  66. # although it's best to implement it in order to accept
  67. # all of them.
  68. _SPECIAL_METHODS_PARAMS = {
  69. None: ("__new__", "__init__", "__call__", "__init_subclass__"),
  70. 0: (
  71. "__del__",
  72. "__repr__",
  73. "__str__",
  74. "__bytes__",
  75. "__hash__",
  76. "__bool__",
  77. "__dir__",
  78. "__len__",
  79. "__length_hint__",
  80. "__iter__",
  81. "__reversed__",
  82. "__neg__",
  83. "__pos__",
  84. "__abs__",
  85. "__invert__",
  86. "__complex__",
  87. "__int__",
  88. "__float__",
  89. "__index__",
  90. "__trunc__",
  91. "__floor__",
  92. "__ceil__",
  93. "__enter__",
  94. "__aenter__",
  95. "__getnewargs_ex__",
  96. "__getnewargs__",
  97. "__getstate__",
  98. "__reduce__",
  99. "__copy__",
  100. "__unicode__",
  101. "__nonzero__",
  102. "__await__",
  103. "__aiter__",
  104. "__anext__",
  105. "__fspath__",
  106. "__subclasses__",
  107. ),
  108. 1: (
  109. "__format__",
  110. "__lt__",
  111. "__le__",
  112. "__eq__",
  113. "__ne__",
  114. "__gt__",
  115. "__ge__",
  116. "__getattr__",
  117. "__getattribute__",
  118. "__delattr__",
  119. "__delete__",
  120. "__instancecheck__",
  121. "__subclasscheck__",
  122. "__getitem__",
  123. "__missing__",
  124. "__delitem__",
  125. "__contains__",
  126. "__add__",
  127. "__sub__",
  128. "__mul__",
  129. "__truediv__",
  130. "__floordiv__",
  131. "__rfloordiv__",
  132. "__mod__",
  133. "__divmod__",
  134. "__lshift__",
  135. "__rshift__",
  136. "__and__",
  137. "__xor__",
  138. "__or__",
  139. "__radd__",
  140. "__rsub__",
  141. "__rmul__",
  142. "__rtruediv__",
  143. "__rmod__",
  144. "__rdivmod__",
  145. "__rpow__",
  146. "__rlshift__",
  147. "__rrshift__",
  148. "__rand__",
  149. "__rxor__",
  150. "__ror__",
  151. "__iadd__",
  152. "__isub__",
  153. "__imul__",
  154. "__itruediv__",
  155. "__ifloordiv__",
  156. "__imod__",
  157. "__ilshift__",
  158. "__irshift__",
  159. "__iand__",
  160. "__ixor__",
  161. "__ior__",
  162. "__ipow__",
  163. "__setstate__",
  164. "__reduce_ex__",
  165. "__deepcopy__",
  166. "__cmp__",
  167. "__matmul__",
  168. "__rmatmul__",
  169. "__imatmul__",
  170. "__div__",
  171. ),
  172. 2: ("__setattr__", "__get__", "__set__", "__setitem__", "__set_name__"),
  173. 3: ("__exit__", "__aexit__"),
  174. (0, 1): ("__round__",),
  175. (1, 2): ("__pow__",),
  176. }
  177. SPECIAL_METHODS_PARAMS = {
  178. name: params
  179. for params, methods in _SPECIAL_METHODS_PARAMS.items()
  180. for name in methods
  181. }
  182. PYMETHODS = set(SPECIAL_METHODS_PARAMS)
  183. SUBSCRIPTABLE_CLASSES_PEP585 = frozenset(
  184. (
  185. "builtins.tuple",
  186. "builtins.list",
  187. "builtins.dict",
  188. "builtins.set",
  189. "builtins.frozenset",
  190. "builtins.type",
  191. "collections.deque",
  192. "collections.defaultdict",
  193. "collections.OrderedDict",
  194. "collections.Counter",
  195. "collections.ChainMap",
  196. "_collections_abc.Awaitable",
  197. "_collections_abc.Coroutine",
  198. "_collections_abc.AsyncIterable",
  199. "_collections_abc.AsyncIterator",
  200. "_collections_abc.AsyncGenerator",
  201. "_collections_abc.Iterable",
  202. "_collections_abc.Iterator",
  203. "_collections_abc.Generator",
  204. "_collections_abc.Reversible",
  205. "_collections_abc.Container",
  206. "_collections_abc.Collection",
  207. "_collections_abc.Callable",
  208. "_collections_abc.Set",
  209. "_collections_abc.MutableSet",
  210. "_collections_abc.Mapping",
  211. "_collections_abc.MutableMapping",
  212. "_collections_abc.Sequence",
  213. "_collections_abc.MutableSequence",
  214. "_collections_abc.ByteString",
  215. "_collections_abc.MappingView",
  216. "_collections_abc.KeysView",
  217. "_collections_abc.ItemsView",
  218. "_collections_abc.ValuesView",
  219. "contextlib.AbstractContextManager",
  220. "contextlib.AbstractAsyncContextManager",
  221. "re.Pattern",
  222. "re.Match",
  223. )
  224. )
  225. SINGLETON_VALUES = {True, False, None}
  226. TERMINATING_FUNCS_QNAMES = frozenset(
  227. {
  228. "_sitebuiltins.Quitter",
  229. "sys.exit",
  230. "posix._exit",
  231. "nt._exit",
  232. "unittest.case.TestCase.fail",
  233. }
  234. )
  235. class NoSuchArgumentError(Exception):
  236. pass
  237. class InferredTypeError(Exception):
  238. pass
  239. def get_all_elements(
  240. node: nodes.NodeNG,
  241. ) -> Iterable[nodes.NodeNG]:
  242. """Recursively returns all atoms in nested lists and tuples."""
  243. if isinstance(node, (nodes.Tuple, nodes.List)):
  244. for child in node.elts:
  245. yield from get_all_elements(child)
  246. else:
  247. yield node
  248. def is_super(node: nodes.NodeNG) -> bool:
  249. """Return True if the node is referencing the "super" builtin function."""
  250. if getattr(node, "name", None) == "super" and node.root().name == "builtins":
  251. return True
  252. return False
  253. def is_error(node: nodes.FunctionDef) -> bool:
  254. """Return true if the given function node only raises an exception."""
  255. return len(node.body) == 1 and isinstance(node.body[0], nodes.Raise)
  256. builtins = builtins.__dict__.copy() # type: ignore[assignment]
  257. SPECIAL_BUILTINS = ("__builtins__",) # '__path__', '__file__')
  258. def is_builtin_object(node: nodes.NodeNG) -> bool:
  259. """Returns True if the given node is an object from the __builtin__ module."""
  260. return node and node.root().name == "builtins" # type: ignore[no-any-return]
  261. def is_builtin(name: str) -> bool:
  262. """Return true if <name> could be considered as a builtin defined by python."""
  263. return name in builtins or name in SPECIAL_BUILTINS # type: ignore[operator]
  264. def is_defined_in_scope(
  265. var_node: nodes.NodeNG,
  266. varname: str,
  267. scope: nodes.NodeNG,
  268. ) -> bool:
  269. return defnode_in_scope(var_node, varname, scope) is not None
  270. # pylint: disable = too-many-branches
  271. def defnode_in_scope(
  272. var_node: nodes.NodeNG,
  273. varname: str,
  274. scope: nodes.NodeNG,
  275. ) -> nodes.NodeNG | None:
  276. if isinstance(scope, nodes.If):
  277. for node in scope.body:
  278. if isinstance(node, nodes.Nonlocal) and varname in node.names:
  279. return node
  280. if isinstance(node, nodes.Assign):
  281. for target in node.targets:
  282. if isinstance(target, nodes.AssignName) and target.name == varname:
  283. return target
  284. elif isinstance(scope, (COMP_NODE_TYPES, nodes.For)):
  285. for ass_node in scope.nodes_of_class(nodes.AssignName):
  286. if ass_node.name == varname:
  287. return ass_node
  288. elif isinstance(scope, nodes.With):
  289. for expr, ids in scope.items:
  290. if expr.parent_of(var_node):
  291. break
  292. if ids and isinstance(ids, nodes.AssignName) and ids.name == varname:
  293. return ids
  294. elif isinstance(scope, (nodes.Lambda, nodes.FunctionDef)):
  295. if scope.args.is_argument(varname):
  296. # If the name is found inside a default value
  297. # of a function, then let the search continue
  298. # in the parent's tree.
  299. if scope.args.parent_of(var_node):
  300. try:
  301. scope.args.default_value(varname)
  302. scope = scope.parent
  303. defnode = defnode_in_scope(var_node, varname, scope)
  304. except astroid.NoDefault:
  305. pass
  306. else:
  307. return defnode
  308. return scope
  309. if getattr(scope, "name", None) == varname:
  310. return scope
  311. elif isinstance(scope, nodes.ExceptHandler):
  312. if isinstance(scope.name, nodes.AssignName):
  313. ass_node = scope.name
  314. if ass_node.name == varname:
  315. return ass_node
  316. return None
  317. def is_defined_before(var_node: nodes.Name) -> bool:
  318. """Check if the given variable node is defined before.
  319. Verify that the variable node is defined by a parent node
  320. (e.g. if or with) earlier than `var_node`, or is defined by a
  321. (list, set, dict, or generator comprehension, lambda)
  322. or in a previous sibling node on the same line
  323. (statement_defining ; statement_using).
  324. """
  325. varname = var_node.name
  326. for parent in var_node.node_ancestors():
  327. defnode = defnode_in_scope(var_node, varname, parent)
  328. if defnode is None:
  329. continue
  330. defnode_scope = defnode.scope()
  331. if isinstance(
  332. defnode_scope, (*COMP_NODE_TYPES, nodes.Lambda, nodes.FunctionDef)
  333. ):
  334. # Avoid the case where var_node_scope is a nested function
  335. if isinstance(defnode_scope, nodes.FunctionDef):
  336. var_node_scope = var_node.scope()
  337. if var_node_scope is not defnode_scope and isinstance(
  338. var_node_scope, nodes.FunctionDef
  339. ):
  340. return False
  341. return True
  342. if defnode.lineno < var_node.lineno:
  343. return True
  344. # `defnode` and `var_node` on the same line
  345. for defnode_anc in defnode.node_ancestors():
  346. if defnode_anc.lineno != var_node.lineno:
  347. continue
  348. if isinstance(
  349. defnode_anc,
  350. (
  351. nodes.For,
  352. nodes.While,
  353. nodes.With,
  354. nodes.Try,
  355. nodes.ExceptHandler,
  356. ),
  357. ):
  358. return True
  359. # possibly multiple statements on the same line using semicolon separator
  360. stmt = var_node.statement()
  361. _node = stmt.previous_sibling()
  362. lineno = stmt.fromlineno
  363. while _node and _node.fromlineno == lineno:
  364. for assign_node in _node.nodes_of_class(nodes.AssignName):
  365. if assign_node.name == varname:
  366. return True
  367. for imp_node in _node.nodes_of_class((nodes.ImportFrom, nodes.Import)):
  368. if varname in [name[1] or name[0] for name in imp_node.names]:
  369. return True
  370. _node = _node.previous_sibling()
  371. return False
  372. def is_default_argument(node: nodes.NodeNG, scope: nodes.NodeNG | None = None) -> bool:
  373. """Return true if the given Name node is used in function or lambda
  374. default argument's value.
  375. """
  376. if not scope:
  377. scope = node.scope()
  378. if isinstance(scope, (nodes.FunctionDef, nodes.Lambda)):
  379. all_defaults = itertools.chain(
  380. scope.args.defaults, (d for d in scope.args.kw_defaults if d is not None)
  381. )
  382. return any(
  383. default_name_node is node
  384. for default_node in all_defaults
  385. for default_name_node in default_node.nodes_of_class(nodes.Name)
  386. )
  387. return False
  388. def is_func_decorator(node: nodes.NodeNG) -> bool:
  389. """Return true if the name is used in function decorator."""
  390. for parent in node.node_ancestors():
  391. if isinstance(parent, nodes.Decorators):
  392. return True
  393. if parent.is_statement or isinstance(
  394. parent,
  395. (
  396. nodes.Lambda,
  397. nodes.ComprehensionScope,
  398. nodes.ListComp,
  399. ),
  400. ):
  401. break
  402. return False
  403. def is_ancestor_name(frame: nodes.ClassDef, node: nodes.NodeNG) -> bool:
  404. """Return whether `frame` is an nodes.Class node with `node` in the
  405. subtree of its bases attribute.
  406. """
  407. if not isinstance(frame, nodes.ClassDef):
  408. return False
  409. return any(node in base.nodes_of_class(nodes.Name) for base in frame.bases)
  410. def is_being_called(node: nodes.NodeNG) -> bool:
  411. """Return True if node is the function being called in a Call node."""
  412. return isinstance(node.parent, nodes.Call) and node.parent.func is node
  413. def assign_parent(node: nodes.NodeNG) -> nodes.NodeNG:
  414. """Return the higher parent which is not an AssignName, Tuple or List node."""
  415. while node and isinstance(node, (nodes.AssignName, nodes.Tuple, nodes.List)):
  416. node = node.parent
  417. return node
  418. def overrides_a_method(class_node: nodes.ClassDef, name: str) -> bool:
  419. """Return True if <name> is a method overridden from an ancestor
  420. which is not the base object class.
  421. """
  422. for ancestor in class_node.ancestors():
  423. if ancestor.name == "object":
  424. continue
  425. if name in ancestor and isinstance(ancestor[name], nodes.FunctionDef):
  426. return True
  427. return False
  428. def only_required_for_messages(
  429. *messages: str,
  430. ) -> Callable[
  431. [AstCallbackMethod[_CheckerT, _NodeT]], AstCallbackMethod[_CheckerT, _NodeT]
  432. ]:
  433. """Decorator to store messages that are handled by a checker method as an
  434. attribute of the function object.
  435. This information is used by ``ASTWalker`` to decide whether to call the decorated
  436. method or not. If none of the messages is enabled, the method will be skipped.
  437. Therefore, the list of messages must be well maintained at all times!
  438. This decorator only has an effect on ``visit_*`` and ``leave_*`` methods
  439. of a class inheriting from ``BaseChecker``.
  440. """
  441. def store_messages(
  442. func: AstCallbackMethod[_CheckerT, _NodeT],
  443. ) -> AstCallbackMethod[_CheckerT, _NodeT]:
  444. func.checks_msgs = messages # type: ignore[attr-defined]
  445. return func
  446. return store_messages
  447. class IncompleteFormatString(Exception):
  448. """A format string ended in the middle of a format specifier."""
  449. class UnsupportedFormatCharacter(Exception):
  450. """A format character in a format string is not one of the supported
  451. format characters.
  452. """
  453. def __init__(self, index: int) -> None:
  454. super().__init__(index)
  455. self.index = index
  456. def parse_format_string(
  457. format_string: str,
  458. ) -> tuple[set[str], int, dict[str, str], list[str]]:
  459. """Parses a format string, returning a tuple (keys, num_args).
  460. Where 'keys' is the set of mapping keys in the format string, and 'num_args' is the number
  461. of arguments required by the format string. Raises IncompleteFormatString or
  462. UnsupportedFormatCharacter if a parse error occurs.
  463. """
  464. keys = set()
  465. key_types = {}
  466. pos_types = []
  467. num_args = 0
  468. def next_char(i: int) -> tuple[int, str]:
  469. i += 1
  470. if i == len(format_string):
  471. raise IncompleteFormatString
  472. return (i, format_string[i])
  473. i = 0
  474. while i < len(format_string):
  475. char = format_string[i]
  476. if char == "%":
  477. i, char = next_char(i)
  478. # Parse the mapping key (optional).
  479. key = None
  480. if char == "(":
  481. depth = 1
  482. i, char = next_char(i)
  483. key_start = i
  484. while depth != 0:
  485. if char == "(":
  486. depth += 1
  487. elif char == ")":
  488. depth -= 1
  489. i, char = next_char(i)
  490. key_end = i - 1
  491. key = format_string[key_start:key_end]
  492. # Parse the conversion flags (optional).
  493. while char in "#0- +":
  494. i, char = next_char(i)
  495. # Parse the minimum field width (optional).
  496. if char == "*":
  497. num_args += 1
  498. i, char = next_char(i)
  499. else:
  500. while char in string.digits:
  501. i, char = next_char(i)
  502. # Parse the precision (optional).
  503. if char == ".":
  504. i, char = next_char(i)
  505. if char == "*":
  506. num_args += 1
  507. i, char = next_char(i)
  508. else:
  509. while char in string.digits:
  510. i, char = next_char(i)
  511. # Parse the length modifier (optional).
  512. if char in "hlL":
  513. i, char = next_char(i)
  514. # Parse the conversion type (mandatory).
  515. flags = "diouxXeEfFgGcrs%a"
  516. if char not in flags:
  517. raise UnsupportedFormatCharacter(i)
  518. if key:
  519. keys.add(key)
  520. key_types[key] = char
  521. elif char != "%":
  522. num_args += 1
  523. pos_types.append(char)
  524. i += 1
  525. return keys, num_args, key_types, pos_types
  526. def split_format_field_names(
  527. format_string: str,
  528. ) -> tuple[str, Iterable[tuple[bool, str]]]:
  529. try:
  530. return _string.formatter_field_name_split(format_string) # type: ignore[no-any-return]
  531. except ValueError as e:
  532. raise IncompleteFormatString() from e
  533. def collect_string_fields(format_string: str) -> Iterable[str | None]:
  534. """Given a format string, return an iterator
  535. of all the valid format fields.
  536. It handles nested fields as well.
  537. """
  538. formatter = string.Formatter()
  539. # pylint: disable = too-many-try-statements
  540. try:
  541. parseiterator = formatter.parse(format_string)
  542. for result in parseiterator:
  543. if all(item is None for item in result[1:]):
  544. # not a replacement format
  545. continue
  546. name = result[1]
  547. nested = result[2]
  548. yield name
  549. if nested:
  550. yield from collect_string_fields(nested)
  551. except ValueError as exc:
  552. # Probably the format string is invalid.
  553. if exc.args[0].startswith("cannot switch from manual"):
  554. # On Jython, parsing a string with both manual
  555. # and automatic positions will fail with a ValueError,
  556. # while on CPython it will simply return the fields,
  557. # the validation being done in the interpreter (?).
  558. # We're just returning two mixed fields in order
  559. # to trigger the format-combined-specification check.
  560. yield ""
  561. yield "1"
  562. return
  563. raise IncompleteFormatString(format_string) from exc
  564. def parse_format_method_string(
  565. format_string: str,
  566. ) -> tuple[list[tuple[str, list[tuple[bool, str]]]], int, int]:
  567. """Parses a PEP 3101 format string, returning a tuple of
  568. (keyword_arguments, implicit_pos_args_cnt, explicit_pos_args).
  569. keyword_arguments is the set of mapping keys in the format string, implicit_pos_args_cnt
  570. is the number of arguments required by the format string and
  571. explicit_pos_args is the number of arguments passed with the position.
  572. """
  573. keyword_arguments = []
  574. implicit_pos_args_cnt = 0
  575. explicit_pos_args = set()
  576. for name in collect_string_fields(format_string):
  577. if name and str(name).isdigit():
  578. explicit_pos_args.add(str(name))
  579. elif name:
  580. keyname, fielditerator = split_format_field_names(name)
  581. if isinstance(keyname, numbers.Number):
  582. explicit_pos_args.add(str(keyname))
  583. try:
  584. keyword_arguments.append((keyname, list(fielditerator)))
  585. except ValueError as e:
  586. raise IncompleteFormatString() from e
  587. else:
  588. implicit_pos_args_cnt += 1
  589. return keyword_arguments, implicit_pos_args_cnt, len(explicit_pos_args)
  590. def is_attr_protected(attrname: str) -> bool:
  591. """Return True if attribute name is protected (start with _ and some other
  592. details), False otherwise.
  593. """
  594. return (
  595. attrname[0] == "_"
  596. and attrname != "_"
  597. and not (attrname.startswith("__") and attrname.endswith("__"))
  598. )
  599. def node_frame_class(node: nodes.NodeNG) -> nodes.ClassDef | None:
  600. """Return the class that is wrapping the given node.
  601. The function returns a class for a method node (or a staticmethod or a
  602. classmethod), otherwise it returns `None`.
  603. """
  604. klass = node.frame()
  605. nodes_to_check = (
  606. nodes.NodeNG,
  607. astroid.UnboundMethod,
  608. astroid.BaseInstance,
  609. )
  610. while (
  611. klass
  612. and isinstance(klass, nodes_to_check)
  613. and not isinstance(klass, nodes.ClassDef)
  614. ):
  615. if klass.parent is None:
  616. return None
  617. klass = klass.parent.frame()
  618. return klass
  619. def get_outer_class(class_node: nodes.ClassDef) -> nodes.ClassDef | None:
  620. """Return the class that is the outer class of given (nested) class_node."""
  621. parent_klass = class_node.parent.frame()
  622. return parent_klass if isinstance(parent_klass, nodes.ClassDef) else None
  623. def is_attr_private(attrname: str) -> Match[str] | None:
  624. """Check that attribute name is private (at least two leading underscores,
  625. at most one trailing underscore).
  626. """
  627. regex = re.compile("^_{2,10}.*[^_]+_?$")
  628. return regex.match(attrname)
  629. def get_argument_from_call(
  630. call_node: nodes.Call, position: int | None = None, keyword: str | None = None
  631. ) -> nodes.Name:
  632. """Returns the specified argument from a function call.
  633. :param nodes.Call call_node: Node representing a function call to check.
  634. :param int position: position of the argument.
  635. :param str keyword: the keyword of the argument.
  636. :returns: The node representing the argument, None if the argument is not found.
  637. :rtype: nodes.Name
  638. :raises ValueError: if both position and keyword are None.
  639. :raises NoSuchArgumentError: if no argument at the provided position or with
  640. the provided keyword.
  641. """
  642. if position is None and keyword is None:
  643. raise ValueError("Must specify at least one of: position or keyword.")
  644. if position is not None:
  645. try:
  646. return call_node.args[position]
  647. except IndexError:
  648. pass
  649. if keyword and call_node.keywords:
  650. for arg in call_node.keywords:
  651. if arg.arg == keyword:
  652. return arg.value
  653. raise NoSuchArgumentError
  654. def infer_kwarg_from_call(call_node: nodes.Call, keyword: str) -> nodes.Name | None:
  655. """Returns the specified argument from a function's kwargs.
  656. :param nodes.Call call_node: Node representing a function call to check.
  657. :param str keyword: Name of the argument to be extracted.
  658. :returns: The node representing the argument, None if the argument is not found.
  659. :rtype: nodes.Name
  660. """
  661. for arg in call_node.kwargs:
  662. inferred = safe_infer(arg.value)
  663. if isinstance(inferred, nodes.Dict):
  664. for item in inferred.items:
  665. if item[0].value == keyword:
  666. return item[1]
  667. return None
  668. def inherit_from_std_ex(node: nodes.NodeNG | astroid.Instance) -> bool:
  669. """Return whether the given class node is subclass of
  670. exceptions.Exception.
  671. """
  672. ancestors = node.ancestors() if hasattr(node, "ancestors") else []
  673. return any(
  674. ancestor.name in {"Exception", "BaseException"}
  675. and ancestor.root().name == EXCEPTIONS_MODULE
  676. for ancestor in itertools.chain([node], ancestors)
  677. )
  678. def error_of_type(
  679. handler: nodes.ExceptHandler,
  680. error_type: str | type[Exception] | tuple[str | type[Exception], ...],
  681. ) -> bool:
  682. """Check if the given exception handler catches
  683. the given error_type.
  684. The *handler* parameter is a node, representing an ExceptHandler node.
  685. The *error_type* can be an exception, such as AttributeError,
  686. the name of an exception, or it can be a tuple of errors.
  687. The function will return True if the handler catches any of the
  688. given errors.
  689. """
  690. def stringify_error(error: str | type[Exception]) -> str:
  691. if not isinstance(error, str):
  692. return error.__name__
  693. return error
  694. if not isinstance(error_type, tuple):
  695. error_type = (error_type,)
  696. expected_errors = {stringify_error(error) for error in error_type}
  697. if not handler.type:
  698. return False
  699. return handler.catch(expected_errors) # type: ignore[no-any-return]
  700. def decorated_with_property(node: nodes.FunctionDef) -> bool:
  701. """Detect if the given function node is decorated with a property."""
  702. if not node.decorators:
  703. return False
  704. for decorator in node.decorators.nodes:
  705. try:
  706. if _is_property_decorator(decorator):
  707. return True
  708. except astroid.InferenceError:
  709. pass
  710. return False
  711. def _is_property_kind(node: nodes.NodeNG, *kinds: str) -> bool:
  712. if not isinstance(node, (astroid.UnboundMethod, nodes.FunctionDef)):
  713. return False
  714. if node.decorators:
  715. for decorator in node.decorators.nodes:
  716. if isinstance(decorator, nodes.Attribute) and decorator.attrname in kinds:
  717. return True
  718. return False
  719. def is_property_setter(node: nodes.NodeNG) -> bool:
  720. """Check if the given node is a property setter."""
  721. return _is_property_kind(node, "setter")
  722. def is_property_deleter(node: nodes.NodeNG) -> bool:
  723. """Check if the given node is a property deleter."""
  724. return _is_property_kind(node, "deleter")
  725. def is_property_setter_or_deleter(node: nodes.NodeNG) -> bool:
  726. """Check if the given node is either a property setter or a deleter."""
  727. return _is_property_kind(node, "setter", "deleter")
  728. def _is_property_decorator(decorator: nodes.Name) -> bool:
  729. for inferred in decorator.infer():
  730. if isinstance(inferred, nodes.ClassDef):
  731. if inferred.qname() in {"builtins.property", "functools.cached_property"}:
  732. return True
  733. for ancestor in inferred.ancestors():
  734. if ancestor.name == "property" and ancestor.root().name == "builtins":
  735. return True
  736. elif isinstance(inferred, nodes.FunctionDef):
  737. # If decorator is function, check if it has exactly one return
  738. # and the return is itself a function decorated with property
  739. returns: list[nodes.Return] = list(
  740. inferred._get_return_nodes_skip_functions()
  741. )
  742. if len(returns) == 1 and isinstance(
  743. returns[0].value, (nodes.Name, nodes.Attribute)
  744. ):
  745. inferred = safe_infer(returns[0].value)
  746. if (
  747. inferred
  748. and isinstance(inferred, objects.Property)
  749. and isinstance(inferred.function, nodes.FunctionDef)
  750. ):
  751. return decorated_with_property(inferred.function)
  752. return False
  753. def decorated_with(
  754. func: (
  755. nodes.ClassDef | nodes.FunctionDef | astroid.BoundMethod | astroid.UnboundMethod
  756. ),
  757. qnames: Iterable[str],
  758. ) -> bool:
  759. """Determine if the `func` node has a decorator with the qualified name `qname`."""
  760. decorators = func.decorators.nodes if func.decorators else []
  761. for decorator_node in decorators:
  762. if isinstance(decorator_node, nodes.Call):
  763. # We only want to infer the function name
  764. decorator_node = decorator_node.func
  765. try:
  766. if any(
  767. i.name in qnames or i.qname() in qnames
  768. for i in decorator_node.infer()
  769. if isinstance(i, (nodes.ClassDef, nodes.FunctionDef))
  770. ):
  771. return True
  772. except astroid.InferenceError:
  773. continue
  774. return False
  775. def uninferable_final_decorators(
  776. node: nodes.Decorators,
  777. ) -> list[nodes.Attribute | nodes.Name | None]:
  778. """Return a list of uninferable `typing.final` decorators in `node`.
  779. This function is used to determine if the `typing.final` decorator is used
  780. with an unsupported Python version; the decorator cannot be inferred when
  781. using a Python version lower than 3.8.
  782. """
  783. decorators = []
  784. for decorator in getattr(node, "nodes", []):
  785. import_nodes: tuple[nodes.Import | nodes.ImportFrom] | None = None
  786. # Get the `Import` node. The decorator is of the form: @module.name
  787. if isinstance(decorator, nodes.Attribute):
  788. inferred = safe_infer(decorator.expr)
  789. if isinstance(inferred, nodes.Module) and inferred.qname() == "typing":
  790. _, import_nodes = decorator.expr.lookup(decorator.expr.name)
  791. # Get the `ImportFrom` node. The decorator is of the form: @name
  792. elif isinstance(decorator, nodes.Name):
  793. _, import_nodes = decorator.lookup(decorator.name)
  794. # The `final` decorator is expected to be found in the
  795. # import_nodes. Continue if we don't find any `Import` or `ImportFrom`
  796. # nodes for this decorator.
  797. if not import_nodes:
  798. continue
  799. import_node = import_nodes[0]
  800. if not isinstance(import_node, (nodes.Import, nodes.ImportFrom)):
  801. continue
  802. import_names = dict(import_node.names)
  803. # Check if the import is of the form: `from typing import final`
  804. is_from_import = ("final" in import_names) and import_node.modname == "typing"
  805. # Check if the import is of the form: `import typing`
  806. is_import = ("typing" in import_names) and getattr(
  807. decorator, "attrname", None
  808. ) == "final"
  809. if is_from_import or is_import:
  810. inferred = safe_infer(decorator)
  811. if inferred is None or isinstance(inferred, util.UninferableBase):
  812. decorators.append(decorator)
  813. return decorators
  814. @lru_cache(maxsize=1024)
  815. def unimplemented_abstract_methods(
  816. node: nodes.ClassDef, is_abstract_cb: nodes.FunctionDef | None = None
  817. ) -> dict[str, nodes.FunctionDef]:
  818. """Get the unimplemented abstract methods for the given *node*.
  819. A method can be considered abstract if the callback *is_abstract_cb*
  820. returns a ``True`` value. The check defaults to verifying that
  821. a method is decorated with abstract methods.
  822. It will return a dictionary of abstract method
  823. names and their inferred objects.
  824. """
  825. if is_abstract_cb is None:
  826. is_abstract_cb = partial(decorated_with, qnames=ABC_METHODS)
  827. visited: dict[str, nodes.FunctionDef] = {}
  828. try:
  829. mro = reversed(node.mro())
  830. except astroid.ResolveError:
  831. # Probably inconsistent hierarchy, don't try to figure this out here.
  832. return {}
  833. for ancestor in mro:
  834. for obj in ancestor.values():
  835. inferred = obj
  836. if isinstance(obj, nodes.AssignName):
  837. inferred = safe_infer(obj)
  838. if not inferred:
  839. # Might be an abstract function,
  840. # but since we don't have enough information
  841. # in order to take this decision, we're taking
  842. # the *safe* decision instead.
  843. if obj.name in visited:
  844. del visited[obj.name]
  845. continue
  846. if not isinstance(inferred, nodes.FunctionDef):
  847. if obj.name in visited:
  848. del visited[obj.name]
  849. if isinstance(inferred, nodes.FunctionDef):
  850. # It's critical to use the original name,
  851. # since after inferring, an object can be something
  852. # else than expected, as in the case of the
  853. # following assignment.
  854. #
  855. # class A:
  856. # def keys(self): pass
  857. # __iter__ = keys
  858. abstract = is_abstract_cb(inferred)
  859. if abstract:
  860. visited[obj.name] = inferred
  861. elif not abstract and obj.name in visited:
  862. del visited[obj.name]
  863. return visited
  864. def find_try_except_wrapper_node(
  865. node: nodes.NodeNG,
  866. ) -> nodes.ExceptHandler | nodes.Try | None:
  867. """Return the ExceptHandler or the Try node in which the node is."""
  868. current = node
  869. ignores = (nodes.ExceptHandler, nodes.Try)
  870. while current and not isinstance(current.parent, ignores):
  871. current = current.parent
  872. if current and isinstance(current.parent, ignores):
  873. return current.parent
  874. return None
  875. def find_except_wrapper_node_in_scope(
  876. node: nodes.NodeNG,
  877. ) -> nodes.ExceptHandler | None:
  878. """Return the ExceptHandler in which the node is, without going out of scope."""
  879. for current in node.node_ancestors():
  880. match current:
  881. case nodes.LocalsDictNodeNG():
  882. # If we're inside a function/class definition, we don't want to keep checking
  883. # higher ancestors for `except` clauses, because if these exist, it means our
  884. # function/class was defined in an `except` clause, rather than the current code
  885. # actually running in an `except` clause.
  886. return None
  887. case nodes.ExceptHandler():
  888. return current
  889. return None
  890. def is_from_fallback_block(node: nodes.NodeNG) -> bool:
  891. """Check if the given node is from a fallback import block."""
  892. context = find_try_except_wrapper_node(node)
  893. if not context:
  894. return False
  895. if isinstance(context, nodes.ExceptHandler):
  896. other_body = context.parent.body
  897. handlers = context.parent.handlers
  898. else:
  899. other_body = itertools.chain.from_iterable(
  900. handler.body for handler in context.handlers
  901. )
  902. handlers = context.handlers
  903. has_fallback_imports = any(
  904. isinstance(import_node, (nodes.ImportFrom, nodes.Import))
  905. for import_node in other_body
  906. )
  907. ignores_import_error = _except_handlers_ignores_exceptions(
  908. handlers, (ImportError, ModuleNotFoundError)
  909. )
  910. return ignores_import_error or has_fallback_imports
  911. def _except_handlers_ignores_exceptions(
  912. handlers: nodes.ExceptHandler,
  913. exceptions: tuple[type[ImportError], type[ModuleNotFoundError]],
  914. ) -> bool:
  915. func = partial(error_of_type, error_type=exceptions)
  916. return any(func(handler) for handler in handlers)
  917. def get_exception_handlers(
  918. node: nodes.NodeNG, exception: type[Exception] | str = Exception
  919. ) -> list[nodes.ExceptHandler] | None:
  920. """Return the collections of handlers handling the exception in arguments.
  921. Args:
  922. node (nodes.NodeNG): A node that is potentially wrapped in a try except.
  923. exception (builtin.Exception or str): exception or name of the exception.
  924. Returns:
  925. list: the collection of handlers that are handling the exception or None.
  926. """
  927. context = find_try_except_wrapper_node(node)
  928. if isinstance(context, nodes.Try):
  929. return [
  930. handler for handler in context.handlers if error_of_type(handler, exception)
  931. ]
  932. return []
  933. def get_contextlib_with_statements(node: nodes.NodeNG) -> Iterator[nodes.With]:
  934. """Get all contextlib.with statements in the ancestors of the given node."""
  935. for with_node in node.node_ancestors():
  936. if isinstance(with_node, nodes.With):
  937. yield with_node
  938. def _suppresses_exception(
  939. call: nodes.Call, exception: type[Exception] | str = Exception
  940. ) -> bool:
  941. """Check if the given node suppresses the given exception."""
  942. if not isinstance(exception, str):
  943. exception = exception.__name__
  944. for arg in call.args:
  945. match inferred := safe_infer(arg):
  946. case nodes.ClassDef():
  947. if inferred.name == exception:
  948. return True
  949. case nodes.Tuple():
  950. for elt in inferred.elts:
  951. inferred_elt = safe_infer(elt)
  952. if (
  953. isinstance(inferred_elt, nodes.ClassDef)
  954. and inferred_elt.name == exception
  955. ):
  956. return True
  957. return False
  958. def get_contextlib_suppressors(
  959. node: nodes.NodeNG, exception: type[Exception] | str = Exception
  960. ) -> Iterator[nodes.With]:
  961. """Return the contextlib suppressors handling the exception.
  962. Args:
  963. node (nodes.NodeNG): A node that is potentially wrapped in a contextlib.suppress.
  964. exception (builtin.Exception): exception or name of the exception.
  965. Yields:
  966. nodes.With: A with node that is suppressing the exception.
  967. """
  968. for with_node in get_contextlib_with_statements(node):
  969. for item, _ in with_node.items:
  970. if isinstance(item, nodes.Call):
  971. inferred = safe_infer(item.func)
  972. if (
  973. isinstance(inferred, nodes.ClassDef)
  974. and inferred.qname() == "contextlib.suppress"
  975. ):
  976. if _suppresses_exception(item, exception):
  977. yield with_node
  978. def is_node_inside_try_except(node: nodes.Raise) -> bool:
  979. """Check if the node is directly under a Try/Except statement
  980. (but not under an ExceptHandler!).
  981. Args:
  982. node (nodes.Raise): the node raising the exception.
  983. Returns:
  984. bool: True if the node is inside a try/except statement, False otherwise.
  985. """
  986. context = find_try_except_wrapper_node(node)
  987. return isinstance(context, nodes.Try)
  988. def node_ignores_exception(
  989. node: nodes.NodeNG, exception: type[Exception] | str = Exception
  990. ) -> bool:
  991. """Check if the node is in a Try which handles the given exception.
  992. If the exception is not given, the function is going to look for bare
  993. excepts.
  994. """
  995. managing_handlers = get_exception_handlers(node, exception)
  996. if managing_handlers:
  997. return True
  998. return any(get_contextlib_suppressors(node, exception))
  999. @lru_cache(maxsize=1024)
  1000. def class_is_abstract(node: nodes.ClassDef) -> bool:
  1001. """Return true if the given class node should be considered as an abstract
  1002. class.
  1003. """
  1004. # Protocol classes are considered "abstract"
  1005. if is_protocol_class(node):
  1006. return True
  1007. # Only check for explicit metaclass=ABCMeta on this specific class
  1008. meta = node.declared_metaclass()
  1009. if meta is not None:
  1010. if meta.name == "ABCMeta" and meta.root().name in ABC_MODULES:
  1011. return True
  1012. for ancestor in node.ancestors():
  1013. if ancestor.name == "ABC" and ancestor.root().name in ABC_MODULES:
  1014. # abc.ABC inheritance
  1015. return True
  1016. for method in node.methods():
  1017. if method.parent.frame() is node:
  1018. if method.is_abstract(pass_is_abstract=False):
  1019. return True
  1020. return False
  1021. def _supports_protocol_method(value: nodes.NodeNG, attr: str) -> bool:
  1022. try:
  1023. attributes = value.getattr(attr)
  1024. except astroid.NotFoundError:
  1025. return False
  1026. first = attributes[0]
  1027. # Return False if a constant is assigned
  1028. if isinstance(first, nodes.AssignName):
  1029. this_assign_parent = get_node_first_ancestor_of_type(
  1030. first, (nodes.Assign, nodes.NamedExpr)
  1031. )
  1032. if this_assign_parent is None: # pragma: no cover
  1033. # Cannot imagine this being None, but return True to avoid false positives
  1034. return True
  1035. if isinstance(this_assign_parent.value, nodes.BaseContainer):
  1036. if all(isinstance(n, nodes.Const) for n in this_assign_parent.value.elts):
  1037. return False
  1038. if isinstance(this_assign_parent.value, nodes.Const):
  1039. return False
  1040. return True
  1041. def is_comprehension(node: nodes.NodeNG) -> bool:
  1042. comprehensions = (
  1043. nodes.ListComp,
  1044. nodes.SetComp,
  1045. nodes.DictComp,
  1046. nodes.GeneratorExp,
  1047. )
  1048. return isinstance(node, comprehensions)
  1049. def _supports_mapping_protocol(value: nodes.NodeNG) -> bool:
  1050. return _supports_protocol_method(
  1051. value, GETITEM_METHOD
  1052. ) and _supports_protocol_method(value, KEYS_METHOD)
  1053. def _supports_membership_test_protocol(value: nodes.NodeNG) -> bool:
  1054. return _supports_protocol_method(value, CONTAINS_METHOD)
  1055. def _supports_iteration_protocol(value: nodes.NodeNG) -> bool:
  1056. return _supports_protocol_method(value, ITER_METHOD) or _supports_protocol_method(
  1057. value, GETITEM_METHOD
  1058. )
  1059. def _supports_async_iteration_protocol(value: nodes.NodeNG) -> bool:
  1060. return _supports_protocol_method(value, AITER_METHOD)
  1061. def _supports_getitem_protocol(value: nodes.NodeNG) -> bool:
  1062. return _supports_protocol_method(value, GETITEM_METHOD)
  1063. def _supports_setitem_protocol(value: nodes.NodeNG) -> bool:
  1064. return _supports_protocol_method(value, SETITEM_METHOD)
  1065. def _supports_delitem_protocol(value: nodes.NodeNG) -> bool:
  1066. return _supports_protocol_method(value, DELITEM_METHOD)
  1067. def _is_abstract_class_name(name: str) -> bool:
  1068. lname = name.lower()
  1069. is_mixin = lname.endswith("mixin")
  1070. is_abstract = lname.startswith("abstract")
  1071. is_base = lname.startswith("base") or lname.endswith("base")
  1072. return is_mixin or is_abstract or is_base
  1073. def is_inside_abstract_class(node: nodes.NodeNG) -> bool:
  1074. while node is not None:
  1075. if isinstance(node, nodes.ClassDef):
  1076. if class_is_abstract(node):
  1077. return True
  1078. name = getattr(node, "name", None)
  1079. if name is not None and _is_abstract_class_name(name):
  1080. return True
  1081. node = node.parent
  1082. return False
  1083. def _supports_protocol(
  1084. value: nodes.NodeNG, protocol_callback: Callable[[nodes.NodeNG], bool]
  1085. ) -> bool:
  1086. match value:
  1087. case nodes.ClassDef():
  1088. if not has_known_bases(value):
  1089. return True
  1090. # classobj can only be iterable if it has an iterable metaclass
  1091. meta = value.metaclass()
  1092. if meta is not None:
  1093. if protocol_callback(meta):
  1094. return True
  1095. case astroid.BaseInstance():
  1096. if not has_known_bases(value):
  1097. return True
  1098. if value.has_dynamic_getattr():
  1099. return True
  1100. if protocol_callback(value):
  1101. return True
  1102. case nodes.ComprehensionScope():
  1103. return True
  1104. case bases.Proxy(_proxied=astroid.BaseInstance() as p) if has_known_bases(p):
  1105. return protocol_callback(p)
  1106. return False
  1107. def is_iterable(value: nodes.NodeNG, check_async: bool = False) -> bool:
  1108. if check_async:
  1109. protocol_check = _supports_async_iteration_protocol
  1110. else:
  1111. protocol_check = _supports_iteration_protocol
  1112. return _supports_protocol(value, protocol_check)
  1113. def is_mapping(value: nodes.NodeNG) -> bool:
  1114. return _supports_protocol(value, _supports_mapping_protocol)
  1115. def supports_membership_test(value: nodes.NodeNG) -> bool:
  1116. supported = _supports_protocol(value, _supports_membership_test_protocol)
  1117. return supported or is_iterable(value)
  1118. def supports_getitem(value: nodes.NodeNG, node: nodes.NodeNG) -> bool:
  1119. if isinstance(value, nodes.ClassDef):
  1120. if _supports_protocol_method(value, CLASS_GETITEM_METHOD):
  1121. return True
  1122. if is_postponed_evaluation_enabled(node) and is_node_in_type_annotation_context(
  1123. node
  1124. ):
  1125. return True
  1126. return _supports_protocol(value, _supports_getitem_protocol)
  1127. def supports_setitem(value: nodes.NodeNG, _: nodes.NodeNG) -> bool:
  1128. return _supports_protocol(value, _supports_setitem_protocol)
  1129. def supports_delitem(value: nodes.NodeNG, _: nodes.NodeNG) -> bool:
  1130. return _supports_protocol(value, _supports_delitem_protocol)
  1131. def _get_python_type_of_node(node: nodes.NodeNG) -> str | None:
  1132. pytype: Callable[[], str] | None = getattr(node, "pytype", None)
  1133. if callable(pytype):
  1134. return pytype()
  1135. return None
  1136. @lru_cache(maxsize=1024)
  1137. def safe_infer(
  1138. node: nodes.NodeNG,
  1139. context: InferenceContext | None = None,
  1140. *,
  1141. compare_constants: bool = False,
  1142. compare_constructors: bool = False,
  1143. ) -> InferenceResult | None:
  1144. """Return the inferred value for the given node.
  1145. Return None if inference failed or if there is some ambiguity (more than
  1146. one node has been inferred of different types).
  1147. If compare_constants is True and if multiple constants are inferred,
  1148. unequal inferred values are also considered ambiguous and return None.
  1149. If compare_constructors is True and if multiple classes are inferred,
  1150. constructors with different signatures are held ambiguous and return None.
  1151. """
  1152. inferred_types: set[str | None] = set()
  1153. try:
  1154. infer_gen = node.infer(context=context)
  1155. value = next(infer_gen)
  1156. except astroid.InferenceError:
  1157. return None
  1158. except Exception as e: # pragma: no cover
  1159. raise AstroidError from e
  1160. if not isinstance(value, util.UninferableBase):
  1161. inferred_types.add(_get_python_type_of_node(value))
  1162. # pylint: disable = too-many-try-statements
  1163. try:
  1164. for inferred in infer_gen:
  1165. inferred_type = _get_python_type_of_node(inferred)
  1166. if inferred_type not in inferred_types:
  1167. return None # If there is ambiguity on the inferred node.
  1168. if (
  1169. compare_constants
  1170. and isinstance(inferred, nodes.Const)
  1171. and isinstance(value, nodes.Const)
  1172. and inferred.value != value.value
  1173. ):
  1174. return None
  1175. if (
  1176. isinstance(inferred, nodes.FunctionDef)
  1177. and isinstance(value, nodes.FunctionDef)
  1178. and function_arguments_are_ambiguous(inferred, value)
  1179. ):
  1180. return None
  1181. if (
  1182. compare_constructors
  1183. and isinstance(inferred, nodes.ClassDef)
  1184. and isinstance(value, nodes.ClassDef)
  1185. and class_constructors_are_ambiguous(inferred, value)
  1186. ):
  1187. return None
  1188. except astroid.InferenceError:
  1189. return None # There is some kind of ambiguity
  1190. except StopIteration:
  1191. return value
  1192. except Exception as e: # pragma: no cover
  1193. raise AstroidError from e
  1194. return value if len(inferred_types) <= 1 else None
  1195. @lru_cache(maxsize=512)
  1196. def infer_all(
  1197. node: nodes.NodeNG, context: InferenceContext | None = None
  1198. ) -> list[InferenceResult]:
  1199. try:
  1200. return list(node.infer(context=context))
  1201. except astroid.InferenceError:
  1202. return []
  1203. except Exception as e: # pragma: no cover
  1204. raise AstroidError from e
  1205. def function_arguments_are_ambiguous(
  1206. func1: nodes.FunctionDef, func2: nodes.FunctionDef
  1207. ) -> bool:
  1208. if func1.argnames() != func2.argnames():
  1209. return True
  1210. # Check ambiguity among function default values
  1211. pairs_of_defaults = [
  1212. (func1.args.defaults, func2.args.defaults),
  1213. (func1.args.kw_defaults, func2.args.kw_defaults),
  1214. ]
  1215. for zippable_default in pairs_of_defaults:
  1216. if None in zippable_default:
  1217. continue
  1218. if len(zippable_default[0]) != len(zippable_default[1]):
  1219. return True
  1220. for default1, default2 in zip(*zippable_default):
  1221. match (default1, default2):
  1222. case [nodes.Const(), nodes.Const()]:
  1223. return default1.value != default2.value # type: ignore[no-any-return]
  1224. case [nodes.Name(), nodes.Name()]:
  1225. return default1.name != default2.name # type: ignore[no-any-return]
  1226. case _:
  1227. return True
  1228. return False
  1229. def class_constructors_are_ambiguous(
  1230. class1: nodes.ClassDef, class2: nodes.ClassDef
  1231. ) -> bool:
  1232. try:
  1233. constructor1 = class1.local_attr("__init__")[0]
  1234. constructor2 = class2.local_attr("__init__")[0]
  1235. except astroid.NotFoundError:
  1236. return False
  1237. if not isinstance(constructor1, nodes.FunctionDef):
  1238. return False
  1239. if not isinstance(constructor2, nodes.FunctionDef):
  1240. return False
  1241. return function_arguments_are_ambiguous(constructor1, constructor2)
  1242. def has_known_bases(
  1243. klass: nodes.ClassDef, context: InferenceContext | None = None
  1244. ) -> bool:
  1245. """Return true if all base classes of a class could be inferred."""
  1246. try:
  1247. return klass._all_bases_known # type: ignore[no-any-return]
  1248. except AttributeError:
  1249. pass
  1250. for base in klass.bases:
  1251. result = safe_infer(base, context=context)
  1252. if (
  1253. not isinstance(result, nodes.ClassDef)
  1254. or result is klass
  1255. or not has_known_bases(result, context=context)
  1256. ):
  1257. klass._all_bases_known = False
  1258. return False
  1259. klass._all_bases_known = True
  1260. return True
  1261. def is_none(node: nodes.NodeNG) -> bool:
  1262. match node:
  1263. case None | nodes.Const(value=None) | nodes.Name(value="None"):
  1264. return True
  1265. return False
  1266. def node_type(node: nodes.NodeNG) -> SuccessfulInferenceResult | None:
  1267. """Return the inferred type for `node`.
  1268. If there is more than one possible type, or if inferred type is Uninferable or None,
  1269. return None
  1270. """
  1271. # check there is only one possible type for the assign node. Else we
  1272. # don't handle it for now
  1273. types: set[SuccessfulInferenceResult] = set()
  1274. try:
  1275. for var_type in node.infer():
  1276. if isinstance(var_type, util.UninferableBase) or is_none(var_type):
  1277. continue
  1278. types.add(var_type)
  1279. if len(types) > 1:
  1280. return None
  1281. except astroid.InferenceError:
  1282. return None
  1283. return types.pop() if types else None
  1284. def is_registered_in_singledispatch_function(node: nodes.FunctionDef) -> bool:
  1285. """Check if the given function node is a singledispatch function."""
  1286. singledispatch_qnames = (
  1287. "functools.singledispatch",
  1288. "singledispatch.singledispatch",
  1289. )
  1290. if not isinstance(node, nodes.FunctionDef):
  1291. return False
  1292. decorators = node.decorators.nodes if node.decorators else []
  1293. for decorator in decorators:
  1294. # func.register are function calls or register attributes
  1295. # when the function is annotated with types
  1296. match decorator:
  1297. case nodes.Call(func=func) | (nodes.Attribute() as func):
  1298. pass
  1299. case _:
  1300. continue
  1301. if not (isinstance(func, nodes.Attribute) and func.attrname == "register"):
  1302. continue
  1303. try:
  1304. func_def = next(func.expr.infer())
  1305. except astroid.InferenceError:
  1306. continue
  1307. if isinstance(func_def, nodes.FunctionDef):
  1308. return decorated_with(func_def, singledispatch_qnames)
  1309. return False
  1310. def find_inferred_fn_from_register(node: nodes.NodeNG) -> nodes.FunctionDef | None:
  1311. # func.register are function calls or register attributes
  1312. # when the function is annotated with types
  1313. match node:
  1314. case nodes.Call(func=func) | (nodes.Attribute() as func):
  1315. pass
  1316. case _:
  1317. return None
  1318. if not (isinstance(func, nodes.Attribute) and func.attrname == "register"):
  1319. return None
  1320. func_def = safe_infer(func.expr)
  1321. if not isinstance(func_def, nodes.FunctionDef):
  1322. return None
  1323. return func_def
  1324. def is_registered_in_singledispatchmethod_function(node: nodes.FunctionDef) -> bool:
  1325. """Check if the given function node is a singledispatchmethod function."""
  1326. singledispatchmethod_qnames = (
  1327. "functools.singledispatchmethod",
  1328. "singledispatch.singledispatchmethod",
  1329. )
  1330. decorators = node.decorators.nodes if node.decorators else []
  1331. for decorator in decorators:
  1332. func_def = find_inferred_fn_from_register(decorator)
  1333. if func_def:
  1334. return decorated_with(func_def, singledispatchmethod_qnames)
  1335. return False
  1336. def get_node_last_lineno(node: nodes.NodeNG) -> int:
  1337. """Get the last lineno of the given node.
  1338. For a simple statement this will just be node.lineno,
  1339. but for a node that has child statements (e.g. a method) this will be the lineno of the last
  1340. child statement recursively.
  1341. """
  1342. # 'finalbody' is always the last clause in a try statement, if present
  1343. if getattr(node, "finalbody", False):
  1344. return get_node_last_lineno(node.finalbody[-1])
  1345. # For if, while, and for statements 'orelse' is always the last clause.
  1346. # For try statements 'orelse' is the last in the absence of a 'finalbody'
  1347. if getattr(node, "orelse", False):
  1348. return get_node_last_lineno(node.orelse[-1])
  1349. # try statements have the 'handlers' last if there is no 'orelse' or 'finalbody'
  1350. if getattr(node, "handlers", False):
  1351. return get_node_last_lineno(node.handlers[-1])
  1352. # All compound statements have a 'body'
  1353. if getattr(node, "body", False):
  1354. return get_node_last_lineno(node.body[-1])
  1355. # Not a compound statement
  1356. return node.lineno # type: ignore[no-any-return]
  1357. def is_postponed_evaluation_enabled(node: nodes.NodeNG) -> bool:
  1358. """Check if the postponed evaluation of annotations is enabled."""
  1359. module = node.root()
  1360. return "annotations" in module.future_imports
  1361. def is_node_in_type_annotation_context(node: nodes.NodeNG) -> bool:
  1362. """Check if node is in type annotation context.
  1363. Check for 'AnnAssign', function 'Arguments',
  1364. or part of function return type annotation.
  1365. """
  1366. current_node, parent_node = node, node.parent
  1367. while True:
  1368. match parent_node:
  1369. case nodes.AnnAssign(annotation=ann) if ann == current_node:
  1370. return True
  1371. case nodes.Arguments() if current_node in (
  1372. *parent_node.annotations,
  1373. *parent_node.posonlyargs_annotations,
  1374. *parent_node.kwonlyargs_annotations,
  1375. parent_node.varargannotation,
  1376. parent_node.kwargannotation,
  1377. ):
  1378. return True
  1379. case nodes.FunctionDef(returns=ret) if ret == current_node:
  1380. return True
  1381. current_node, parent_node = parent_node, parent_node.parent
  1382. if isinstance(parent_node, nodes.Module):
  1383. return False
  1384. def is_node_in_pep695_type_context(node: nodes.NodeNG) -> nodes.NodeNG | None:
  1385. """Check if node is used in a TypeAlias or as part of a type param."""
  1386. return get_node_first_ancestor_of_type(
  1387. node, (nodes.TypeAlias, nodes.TypeVar, nodes.ParamSpec, nodes.TypeVarTuple)
  1388. )
  1389. def is_subclass_of(child: nodes.ClassDef, parent: nodes.ClassDef) -> bool:
  1390. """Check if first node is a subclass of second node.
  1391. :param child: Node to check for subclass.
  1392. :param parent: Node to check for superclass.
  1393. :returns: True if child is derived from parent. False otherwise.
  1394. """
  1395. if not all(isinstance(node, nodes.ClassDef) for node in (child, parent)):
  1396. return False
  1397. for ancestor in child.ancestors():
  1398. try:
  1399. if astroid.helpers.is_subtype(ancestor, parent):
  1400. return True
  1401. except astroid.exceptions._NonDeducibleTypeHierarchy:
  1402. continue
  1403. return False
  1404. @lru_cache(maxsize=1024)
  1405. def is_overload_stub(node: nodes.NodeNG) -> bool:
  1406. """Check if a node is a function stub decorated with typing.overload.
  1407. :param node: Node to check.
  1408. :returns: True if node is an overload function stub. False otherwise.
  1409. """
  1410. decorators = getattr(node, "decorators", None)
  1411. return bool(decorators and decorated_with(node, ["typing.overload", "overload"]))
  1412. def is_protocol_class(cls: nodes.NodeNG) -> bool:
  1413. """Check if the given node represents a protocol class.
  1414. :param cls: The node to check
  1415. :returns: True if the node is or inherits from typing.Protocol directly, false otherwise.
  1416. """
  1417. if not isinstance(cls, nodes.ClassDef):
  1418. return False
  1419. # Return if klass is protocol
  1420. if cls.qname() in TYPING_PROTOCOLS:
  1421. return True
  1422. for base in cls.bases:
  1423. try:
  1424. for inf_base in base.infer():
  1425. if inf_base.qname() in TYPING_PROTOCOLS:
  1426. return True
  1427. except astroid.InferenceError:
  1428. continue
  1429. return False
  1430. def is_call_of_name(node: nodes.NodeNG, name: str) -> bool:
  1431. """Checks if node is a function call with the given name."""
  1432. match node:
  1433. case nodes.Call(func=nodes.Name(name=func_name)):
  1434. return func_name == name # type: ignore[no-any-return]
  1435. return False
  1436. def is_test_condition(
  1437. node: nodes.NodeNG,
  1438. parent: nodes.NodeNG | None = None,
  1439. ) -> bool:
  1440. """Returns true if the given node is being tested for truthiness."""
  1441. match parent := parent or node.parent:
  1442. case nodes.While() | nodes.If() | nodes.IfExp() | nodes.Assert():
  1443. return node is parent.test or parent.test.parent_of(node)
  1444. case nodes.Comprehension():
  1445. return node in parent.ifs
  1446. return is_call_of_name(parent, "bool") and parent.parent_of(node)
  1447. def is_classdef_type(node: nodes.ClassDef) -> bool:
  1448. """Test if ClassDef node is Type."""
  1449. if node.name == "type":
  1450. return True
  1451. return any(isinstance(b, nodes.Name) and b.name == "type" for b in node.bases)
  1452. def is_attribute_typed_annotation(
  1453. node: nodes.ClassDef | astroid.Instance, attr_name: str
  1454. ) -> bool:
  1455. """Test if attribute is typed annotation in current node
  1456. or any base nodes.
  1457. """
  1458. match node.locals.get(attr_name, [None])[0]:
  1459. case nodes.AssignName(parent=nodes.AnnAssign()):
  1460. return True
  1461. for base in node.bases:
  1462. match inferred := safe_infer(base):
  1463. case nodes.ClassDef() if is_attribute_typed_annotation(inferred, attr_name):
  1464. return True
  1465. return False
  1466. def is_enum(node: nodes.ClassDef) -> bool:
  1467. return node.name == "Enum" and node.root().name == "enum" # type: ignore[no-any-return]
  1468. def is_assign_name_annotated_with(node: nodes.AssignName, typing_name: str) -> bool:
  1469. """Test if AssignName node has `typing_name` annotation.
  1470. Especially useful to check for `typing._SpecialForm` instances
  1471. like: `Union`, `Optional`, `Literal`, `ClassVar`, `Final`.
  1472. """
  1473. if not isinstance(node.parent, nodes.AnnAssign):
  1474. return False
  1475. annotation = node.parent.annotation
  1476. if isinstance(annotation, nodes.Subscript):
  1477. annotation = annotation.value
  1478. match annotation:
  1479. case nodes.Name(name=n) | nodes.Attribute(attrname=n) if n == typing_name:
  1480. return True
  1481. return False
  1482. def is_assign_name_annotated_with_class_var_typing_name(
  1483. node: nodes.AssignName, typing_name: str
  1484. ) -> bool:
  1485. if not is_assign_name_annotated_with(node, "ClassVar"):
  1486. return False
  1487. annotation = node.parent.annotation
  1488. if isinstance(annotation, nodes.Subscript):
  1489. annotation = annotation.slice
  1490. if isinstance(annotation, nodes.Subscript):
  1491. annotation = annotation.value
  1492. match annotation:
  1493. case nodes.Name(name=n) | nodes.Attribute(attrname=n) if n == typing_name:
  1494. return True
  1495. return False
  1496. def get_iterating_dictionary_name(node: nodes.For | nodes.Comprehension) -> str | None:
  1497. """Get the name of the dictionary which keys are being iterated over on
  1498. a ``nodes.For`` or ``nodes.Comprehension`` node.
  1499. If the iterating object is not either the keys method of a dictionary
  1500. or a dictionary itself, this returns None.
  1501. """
  1502. # Is it a proper keys call?
  1503. match node.iter:
  1504. case nodes.Call(func=nodes.Attribute(attrname="keys")):
  1505. inferred = safe_infer(node.iter.func)
  1506. if not isinstance(inferred, astroid.BoundMethod):
  1507. return None
  1508. return node.iter.as_string().rpartition(".keys")[0] # type: ignore[no-any-return]
  1509. # Is it a dictionary?
  1510. if isinstance(node.iter, (nodes.Name, nodes.Attribute)):
  1511. inferred = safe_infer(node.iter)
  1512. if not isinstance(inferred, nodes.Dict):
  1513. return None
  1514. return node.iter.as_string() # type: ignore[no-any-return]
  1515. return None
  1516. def get_subscript_const_value(node: nodes.Subscript) -> nodes.Const:
  1517. """Returns the value 'subscript.slice' of a Subscript node.
  1518. :param node: Subscript Node to extract value from
  1519. :returns: Const Node containing subscript value
  1520. :raises InferredTypeError: if the subscript node cannot be inferred as a Const
  1521. """
  1522. inferred = safe_infer(node.slice)
  1523. if not isinstance(inferred, nodes.Const):
  1524. raise InferredTypeError("Subscript.slice cannot be inferred as a nodes.Const")
  1525. return inferred
  1526. def get_import_name(importnode: ImportNode, modname: str | None) -> str | None:
  1527. """Get a prepared module name from the given import node.
  1528. In the case of relative imports, this will return the
  1529. absolute qualified module name, which might be useful
  1530. for debugging. Otherwise, the initial module name
  1531. is returned unchanged.
  1532. :param importnode: node representing import statement.
  1533. :param modname: module name from import statement.
  1534. :returns: absolute qualified module name of the module
  1535. used in import.
  1536. """
  1537. if isinstance(importnode, nodes.ImportFrom) and importnode.level:
  1538. root = importnode.root()
  1539. if isinstance(root, nodes.Module):
  1540. try:
  1541. return root.relative_to_absolute_name( # type: ignore[no-any-return]
  1542. modname, level=importnode.level
  1543. )
  1544. except TooManyLevelsError:
  1545. return modname
  1546. return modname
  1547. def is_sys_guard(node: nodes.If) -> bool:
  1548. """Return True if IF stmt is a sys.version_info guard.
  1549. >>> import sys
  1550. >>> from typing import Literal
  1551. """
  1552. if isinstance(node.test, nodes.Compare):
  1553. value = node.test.left
  1554. if isinstance(value, nodes.Subscript):
  1555. value = value.value
  1556. if (
  1557. isinstance(value, nodes.Attribute)
  1558. and value.as_string() == "sys.version_info"
  1559. ):
  1560. return True
  1561. elif isinstance(node.test, nodes.Attribute) and node.test.as_string() in {
  1562. "six.PY2",
  1563. "six.PY3",
  1564. }:
  1565. return True
  1566. return False
  1567. def _is_node_in_same_scope(
  1568. candidate: nodes.NodeNG, node_scope: nodes.LocalsDictNodeNG
  1569. ) -> bool:
  1570. if isinstance(candidate, (nodes.ClassDef, nodes.FunctionDef)):
  1571. return candidate.parent is not None and candidate.parent.scope() is node_scope
  1572. return candidate.scope() is node_scope
  1573. def _is_reassigned_relative_to_current(
  1574. node: nodes.NodeNG, varname: str, before: bool
  1575. ) -> bool:
  1576. """Check if the given variable name is reassigned in the same scope relative to
  1577. the current node.
  1578. """
  1579. node_scope = node.scope()
  1580. node_lineno = node.lineno
  1581. if node_lineno is None:
  1582. return False
  1583. for a in node_scope.nodes_of_class(
  1584. (nodes.AssignName, nodes.ClassDef, nodes.FunctionDef)
  1585. ):
  1586. if a.name == varname and a.lineno is not None:
  1587. if before:
  1588. if a.lineno < node_lineno:
  1589. if _is_node_in_same_scope(a, node_scope):
  1590. return True
  1591. elif a.lineno > node_lineno:
  1592. if _is_node_in_same_scope(a, node_scope):
  1593. return True
  1594. return False
  1595. def is_reassigned_before_current(node: nodes.NodeNG, varname: str) -> bool:
  1596. """Check if the given variable name is reassigned in the same scope before the
  1597. current node.
  1598. """
  1599. return _is_reassigned_relative_to_current(node, varname, before=True)
  1600. def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool:
  1601. """Check if the given variable name is reassigned in the same scope after the
  1602. current node.
  1603. """
  1604. return _is_reassigned_relative_to_current(node, varname, before=False)
  1605. def is_deleted_after_current(node: nodes.NodeNG, varname: str) -> bool:
  1606. """Check if the given variable name is deleted in the same scope after the current
  1607. node.
  1608. """
  1609. return any(
  1610. getattr(target, "name", None) == varname and target.lineno > node.lineno
  1611. for del_node in node.scope().nodes_of_class(nodes.Delete)
  1612. for target in del_node.targets
  1613. )
  1614. def is_function_body_ellipsis(node: nodes.FunctionDef) -> bool:
  1615. """Checks whether a function body only consists of a single Ellipsis."""
  1616. match node.body:
  1617. case [nodes.Expr(value=nodes.Const(value=value))]:
  1618. return value is Ellipsis
  1619. return False
  1620. def is_base_container(node: nodes.NodeNG | None) -> bool:
  1621. return isinstance(node, nodes.BaseContainer) and not node.elts
  1622. def is_empty_dict_literal(node: nodes.NodeNG | None) -> bool:
  1623. return isinstance(node, nodes.Dict) and not node.items
  1624. def is_empty_str_literal(node: nodes.NodeNG | None) -> bool:
  1625. return (
  1626. isinstance(node, nodes.Const) and isinstance(node.value, str) and not node.value
  1627. )
  1628. def returns_bool(node: nodes.NodeNG) -> bool:
  1629. """Returns true if a node is a nodes.Return that returns a constant boolean."""
  1630. match node:
  1631. case nodes.Return(value=nodes.Const(value=bool())):
  1632. return True
  1633. return False
  1634. def assigned_bool(node: nodes.NodeNG) -> bool:
  1635. """Returns true if a node is a nodes.Assign that returns a constant boolean."""
  1636. match node:
  1637. case nodes.Assign(value=nodes.Const(value=bool())):
  1638. return True
  1639. return False
  1640. def get_node_first_ancestor_of_type(
  1641. node: nodes.NodeNG, ancestor_type: type[_NodeT] | tuple[type[_NodeT], ...]
  1642. ) -> _NodeT | None:
  1643. """Return the first parent node that is any of the provided types (or None)."""
  1644. for ancestor in node.node_ancestors():
  1645. if isinstance(ancestor, ancestor_type):
  1646. return ancestor # type: ignore[no-any-return]
  1647. return None
  1648. def get_node_first_ancestor_of_type_and_its_child(
  1649. node: nodes.NodeNG, ancestor_type: type[_NodeT] | tuple[type[_NodeT], ...]
  1650. ) -> tuple[None, None] | tuple[_NodeT, nodes.NodeNG]:
  1651. """Modified version of get_node_first_ancestor_of_type to also return the
  1652. descendant visited directly before reaching the sought ancestor.
  1653. Useful for extracting whether a statement is guarded by a try, except, or finally
  1654. when searching for a Try ancestor.
  1655. """
  1656. child = node
  1657. for ancestor in node.node_ancestors():
  1658. if isinstance(ancestor, ancestor_type):
  1659. return (ancestor, child)
  1660. child = ancestor
  1661. return None, None
  1662. def in_type_checking_block(node: nodes.NodeNG) -> bool:
  1663. """Check if a node is guarded by a TYPE_CHECKING guard."""
  1664. for ancestor in node.node_ancestors():
  1665. if not isinstance(ancestor, nodes.If):
  1666. continue
  1667. if isinstance(ancestor.test, nodes.Name):
  1668. if ancestor.test.name != "TYPE_CHECKING":
  1669. continue
  1670. lookup_result = ancestor.test.lookup(ancestor.test.name)[1]
  1671. if not lookup_result:
  1672. return False
  1673. maybe_import_from = lookup_result[0]
  1674. if (
  1675. isinstance(maybe_import_from, nodes.ImportFrom)
  1676. and maybe_import_from.modname == "typing"
  1677. ):
  1678. return True
  1679. match safe_infer(ancestor.test):
  1680. case nodes.Const(value=False):
  1681. return True
  1682. elif isinstance(ancestor.test, nodes.Attribute):
  1683. if ancestor.test.attrname != "TYPE_CHECKING":
  1684. continue
  1685. match safe_infer(ancestor.test.expr):
  1686. case nodes.Module(name="typing"):
  1687. return True
  1688. return False
  1689. def is_typing_member(node: nodes.NodeNG, names_to_check: tuple[str, ...]) -> bool:
  1690. """Check if `node` is a member of the `typing` module and has one of the names from
  1691. `names_to_check`.
  1692. """
  1693. match node:
  1694. case nodes.Name():
  1695. try:
  1696. import_from = node.lookup(node.name)[1][0]
  1697. except IndexError:
  1698. return False
  1699. match import_from:
  1700. case nodes.ImportFrom(modname="typing"):
  1701. return import_from.real_name(node.name) in names_to_check
  1702. return False
  1703. case nodes.Attribute():
  1704. match safe_infer(node.expr):
  1705. case nodes.Module(name="typing"):
  1706. return node.attrname in names_to_check
  1707. return False
  1708. return False
  1709. @lru_cache
  1710. def in_for_else_branch(parent: nodes.NodeNG, stmt: Statement) -> bool:
  1711. """Returns True if stmt is inside the else branch for a parent For stmt."""
  1712. return isinstance(parent, nodes.For) and any(
  1713. else_stmt.parent_of(stmt) or else_stmt == stmt for else_stmt in parent.orelse
  1714. )
  1715. def find_assigned_names_recursive(
  1716. target: nodes.AssignName | nodes.BaseContainer,
  1717. ) -> Iterator[str]:
  1718. """Yield the names of assignment targets, accounting for nested ones."""
  1719. match target:
  1720. case nodes.AssignName():
  1721. if target.name is not None:
  1722. yield target.name
  1723. case nodes.BaseContainer():
  1724. for elt in target.elts:
  1725. yield from find_assigned_names_recursive(elt)
  1726. def has_starred_node_recursive(
  1727. node: nodes.For | nodes.Comprehension | nodes.Set | nodes.Starred,
  1728. ) -> Iterator[bool]:
  1729. """Yield ``True`` if a Starred node is found recursively."""
  1730. match node:
  1731. case nodes.Starred():
  1732. yield True
  1733. case nodes.Set():
  1734. for elt in node.elts:
  1735. yield from has_starred_node_recursive(elt)
  1736. case nodes.For() | nodes.Comprehension():
  1737. for elt in node.iter.elts:
  1738. yield from has_starred_node_recursive(elt)
  1739. def is_hashable(node: nodes.NodeNG) -> bool:
  1740. """Return whether any inferred value of `node` is hashable.
  1741. When finding ambiguity, return True.
  1742. """
  1743. # pylint: disable = too-many-try-statements
  1744. try:
  1745. for inferred in node.infer():
  1746. if isinstance(inferred, (nodes.ClassDef, util.UninferableBase)):
  1747. return True
  1748. if not hasattr(inferred, "igetattr"):
  1749. return True
  1750. hash_fn = next(inferred.igetattr("__hash__"))
  1751. if hash_fn.parent is inferred:
  1752. return True
  1753. if getattr(hash_fn, "value", True) is not None:
  1754. return True
  1755. return False
  1756. except astroid.InferenceError:
  1757. return True
  1758. def subscript_chain_is_equal(left: nodes.Subscript, right: nodes.Subscript) -> bool:
  1759. while isinstance(left, nodes.Subscript) and isinstance(right, nodes.Subscript):
  1760. try:
  1761. if (
  1762. get_subscript_const_value(left).value
  1763. != get_subscript_const_value(right).value
  1764. ):
  1765. return False
  1766. left = left.value
  1767. right = right.value
  1768. except InferredTypeError:
  1769. return False
  1770. return left.as_string() == right.as_string() # type: ignore[no-any-return]
  1771. def _is_target_name_in_binop_side(
  1772. target: nodes.AssignName | nodes.AssignAttr, side: nodes.NodeNG | None
  1773. ) -> bool:
  1774. """Determine whether the target name-like node is referenced in the side node."""
  1775. match (side, target):
  1776. case [nodes.Name(), nodes.AssignName()]:
  1777. return target.name == side.name # type: ignore[no-any-return]
  1778. case [nodes.Attribute(), nodes.AssignAttr()]:
  1779. return target.as_string() == side.as_string() # type: ignore[no-any-return]
  1780. case [nodes.Subscript(), nodes.Subscript()]:
  1781. return subscript_chain_is_equal(target, side)
  1782. case _:
  1783. return False
  1784. def is_augmented_assign(node: nodes.Assign) -> tuple[bool, str]:
  1785. """Determine if the node is assigning itself (with modifications) to itself.
  1786. For example: x = 1 + x
  1787. """
  1788. if not isinstance(node.value, nodes.BinOp):
  1789. return False, ""
  1790. binop = node.value
  1791. target = node.targets[0]
  1792. if not isinstance(target, (nodes.AssignName, nodes.AssignAttr, nodes.Subscript)):
  1793. return False, ""
  1794. # We don't want to catch x = "1" + x or x = "%s" % x
  1795. if isinstance(binop.left, nodes.Const) and isinstance(
  1796. binop.left.value, (str, bytes)
  1797. ):
  1798. return False, ""
  1799. # This could probably be improved but for now we disregard all assignments from calls
  1800. if isinstance(binop.left, nodes.Call) or isinstance(binop.right, nodes.Call):
  1801. return False, ""
  1802. if _is_target_name_in_binop_side(target, binop.left):
  1803. return True, binop.op
  1804. if (
  1805. # Unless an operator is commutative, we should not raise (i.e. x = 3/x)
  1806. binop.op in COMMUTATIVE_OPERATORS
  1807. and _is_target_name_in_binop_side(target, binop.right)
  1808. ):
  1809. if isinstance(binop.left, nodes.Const):
  1810. # This bizarrely became necessary after an unrelated call to igetattr().
  1811. # Seems like a code smell uncovered in #10212.
  1812. # tuple(node.frame().igetattr(node.name))
  1813. inferred_left = binop.left
  1814. else:
  1815. inferred_left = safe_infer(binop.left)
  1816. match inferred_left:
  1817. case nodes.Const(value=int()):
  1818. return True, binop.op
  1819. return False, ""
  1820. return False, ""
  1821. def _qualified_name_parts(qualified_module_name: str) -> list[str]:
  1822. """Split the names of the given module into subparts.
  1823. For example,
  1824. _qualified_name_parts('pylint.checkers.ImportsChecker')
  1825. returns
  1826. ['pylint', 'pylint.checkers', 'pylint.checkers.ImportsChecker']
  1827. """
  1828. names = qualified_module_name.split(".")
  1829. return [".".join(names[0 : i + 1]) for i in range(len(names))]
  1830. def is_module_ignored(
  1831. qualified_module_name: str, ignored_modules: Iterable[str]
  1832. ) -> bool:
  1833. ignored_modules = set(ignored_modules)
  1834. for current_module in _qualified_name_parts(qualified_module_name):
  1835. # Try to match the module name directly
  1836. if current_module in ignored_modules:
  1837. return True
  1838. for ignore in ignored_modules:
  1839. # Try to see if the ignores pattern match against the module name.
  1840. if fnmatch.fnmatch(current_module, ignore):
  1841. return True
  1842. return False
  1843. def is_singleton_const(node: nodes.NodeNG) -> bool:
  1844. return isinstance(node, nodes.Const) and any(
  1845. node.value is value for value in SINGLETON_VALUES
  1846. )
  1847. def is_terminating_func(node: nodes.Call) -> bool:
  1848. """Detect call to exit(), quit(), os._exit(), sys.exit(), or
  1849. functions annotated with `typing.NoReturn` or `typing.Never`.
  1850. """
  1851. if not isinstance(node.func, (nodes.Attribute, nodes.Name)) or isinstance(
  1852. node.parent, nodes.Lambda
  1853. ):
  1854. return False
  1855. try:
  1856. for inferred in node.func.infer():
  1857. if (
  1858. hasattr(inferred, "qname")
  1859. and inferred.qname() in TERMINATING_FUNCS_QNAMES
  1860. ):
  1861. return True
  1862. match inferred:
  1863. case astroid.BoundMethod(_proxied=astroid.UnboundMethod(_proxied=p)):
  1864. # Unwrap to get the actual function node object
  1865. inferred = p
  1866. if ( # pylint: disable=too-many-boolean-expressions
  1867. isinstance(inferred, nodes.FunctionDef)
  1868. and (
  1869. not isinstance(inferred, nodes.AsyncFunctionDef)
  1870. or isinstance(node.parent, nodes.Await)
  1871. )
  1872. and isinstance(inferred.returns, nodes.Name)
  1873. and (inferred_func := safe_infer(inferred.returns))
  1874. and hasattr(inferred_func, "qname")
  1875. and inferred_func.qname()
  1876. in (
  1877. *TYPING_NEVER,
  1878. *TYPING_NORETURN,
  1879. # In Python 3.7 - 3.8, NoReturn is alias of '_SpecialForm'
  1880. # "typing._SpecialForm",
  1881. # But 'typing.Any' also inherits _SpecialForm
  1882. # See #9751
  1883. )
  1884. ):
  1885. return True
  1886. except (StopIteration, astroid.InferenceError):
  1887. pass
  1888. return False
  1889. def is_class_attr(name: str, klass: nodes.ClassDef) -> bool:
  1890. try:
  1891. klass.getattr(name)
  1892. return True
  1893. except astroid.NotFoundError:
  1894. return False
  1895. def get_inverse_comparator(op: str) -> str:
  1896. """Returns the inverse comparator given a comparator.
  1897. E.g. when given "==", returns "!="
  1898. :param str op: the comparator to look up.
  1899. :returns: The inverse of the comparator in string format
  1900. :raises KeyError: if input is not recognized as a comparator
  1901. """
  1902. return {
  1903. "==": "!=",
  1904. "!=": "==",
  1905. "<": ">=",
  1906. ">": "<=",
  1907. "<=": ">",
  1908. ">=": "<",
  1909. "in": "not in",
  1910. "not in": "in",
  1911. "is": "is not",
  1912. "is not": "is",
  1913. }[op]
  1914. def not_condition_as_string(
  1915. test_node: nodes.Compare | nodes.Name | nodes.UnaryOp | nodes.BoolOp | nodes.BinOp,
  1916. ) -> str:
  1917. match test_node:
  1918. case nodes.UnaryOp():
  1919. return test_node.operand.as_string() # type: ignore[no-any-return]
  1920. case nodes.BoolOp():
  1921. return f"not ({test_node.as_string()})"
  1922. case nodes.Compare():
  1923. lhs = test_node.left
  1924. ops, rhs = test_node.ops[0]
  1925. lower_priority_expressions = (
  1926. nodes.Lambda,
  1927. nodes.UnaryOp,
  1928. nodes.BoolOp,
  1929. nodes.IfExp,
  1930. nodes.NamedExpr,
  1931. )
  1932. lhs = (
  1933. f"({lhs.as_string()})"
  1934. if isinstance(lhs, lower_priority_expressions)
  1935. else lhs.as_string()
  1936. )
  1937. rhs = (
  1938. f"({rhs.as_string()})"
  1939. if isinstance(rhs, lower_priority_expressions)
  1940. else rhs.as_string()
  1941. )
  1942. return f"{lhs} {get_inverse_comparator(ops)} {rhs}"
  1943. case _:
  1944. return f"not {test_node.as_string()}"
  1945. @lru_cache(maxsize=1000)
  1946. def overridden_method(
  1947. klass: nodes.LocalsDictNodeNG, name: str | None
  1948. ) -> nodes.FunctionDef | None:
  1949. """Get overridden method if any."""
  1950. try:
  1951. parent = next(klass.local_attr_ancestors(name))
  1952. except (StopIteration, KeyError):
  1953. return None
  1954. try:
  1955. meth_node = parent[name]
  1956. except KeyError: # pragma: no cover
  1957. # We have found an ancestor defining <name> but it's not in the local
  1958. # dictionary. This may happen with astroid built from living objects.
  1959. return None
  1960. if isinstance(meth_node, nodes.FunctionDef):
  1961. return meth_node
  1962. return None # pragma: no cover
  1963. def is_enum_member(node: nodes.AssignName) -> bool:
  1964. """Return `True` if `node` is an Enum member (is an item of the
  1965. `__members__` container).
  1966. """
  1967. frame = node.frame()
  1968. if (
  1969. not isinstance(frame, nodes.ClassDef)
  1970. or not frame.is_subtype_of("enum.Enum")
  1971. or frame.root().qname() == "enum"
  1972. ):
  1973. return False
  1974. members = frame.locals.get("__members__")
  1975. # A dataclass is one known case for when `members` can be `None`
  1976. if members is None:
  1977. return False
  1978. return node.name in [name_obj.name for value, name_obj in members[0].items]