protocols.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. # Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2014-2020 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2014 Google, Inc.
  4. # Copyright (c) 2014 Eevee (Alex Munroe) <amunroe@yelp.com>
  5. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
  6. # Copyright (c) 2015 Dmitry Pribysh <dmand@yandex.ru>
  7. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  8. # Copyright (c) 2017-2018 Ashley Whetter <ashley@awhetter.co.uk>
  9. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  10. # Copyright (c) 2017 rr- <rr-@sakuya.pl>
  11. # Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
  12. # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
  13. # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  14. # Copyright (c) 2018 HoverHell <hoverhell@gmail.com>
  15. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  16. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  17. # Copyright (c) 2020 Vilnis Termanis <vilnis.termanis@iotics.com>
  18. # Copyright (c) 2020 Ram Rachum <ram@rachum.com>
  19. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  20. # Copyright (c) 2021 Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com>
  21. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  22. # Copyright (c) 2021 David Liu <david@cs.toronto.edu>
  23. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  24. # Copyright (c) 2021 doranid <ddandd@gmail.com>
  25. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  26. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  27. """this module contains a set of functions to handle python protocols for nodes
  28. where it makes sense.
  29. """
  30. import collections
  31. import itertools
  32. import operator as operator_mod
  33. import sys
  34. from typing import Any, Generator, List, Optional, Union
  35. from astroid import arguments, bases, decorators, helpers, nodes, util
  36. from astroid.const import Context
  37. from astroid.context import InferenceContext, copy_context
  38. from astroid.exceptions import (
  39. AstroidIndexError,
  40. AstroidTypeError,
  41. AttributeInferenceError,
  42. InferenceError,
  43. NoDefault,
  44. )
  45. from astroid.nodes import node_classes
  46. if sys.version_info >= (3, 8):
  47. from typing import Literal
  48. else:
  49. from typing_extensions import Literal
  50. raw_building = util.lazy_import("raw_building")
  51. objects = util.lazy_import("objects")
  52. def _reflected_name(name):
  53. return "__r" + name[2:]
  54. def _augmented_name(name):
  55. return "__i" + name[2:]
  56. _CONTEXTLIB_MGR = "contextlib.contextmanager"
  57. BIN_OP_METHOD = {
  58. "+": "__add__",
  59. "-": "__sub__",
  60. "/": "__truediv__",
  61. "//": "__floordiv__",
  62. "*": "__mul__",
  63. "**": "__pow__",
  64. "%": "__mod__",
  65. "&": "__and__",
  66. "|": "__or__",
  67. "^": "__xor__",
  68. "<<": "__lshift__",
  69. ">>": "__rshift__",
  70. "@": "__matmul__",
  71. }
  72. REFLECTED_BIN_OP_METHOD = {
  73. key: _reflected_name(value) for (key, value) in BIN_OP_METHOD.items()
  74. }
  75. AUGMENTED_OP_METHOD = {
  76. key + "=": _augmented_name(value) for (key, value) in BIN_OP_METHOD.items()
  77. }
  78. UNARY_OP_METHOD = {
  79. "+": "__pos__",
  80. "-": "__neg__",
  81. "~": "__invert__",
  82. "not": None, # XXX not '__nonzero__'
  83. }
  84. _UNARY_OPERATORS = {
  85. "+": operator_mod.pos,
  86. "-": operator_mod.neg,
  87. "~": operator_mod.invert,
  88. "not": operator_mod.not_,
  89. }
  90. def _infer_unary_op(obj, op):
  91. func = _UNARY_OPERATORS[op]
  92. value = func(obj)
  93. return nodes.const_factory(value)
  94. nodes.Tuple.infer_unary_op = lambda self, op: _infer_unary_op(tuple(self.elts), op)
  95. nodes.List.infer_unary_op = lambda self, op: _infer_unary_op(self.elts, op)
  96. nodes.Set.infer_unary_op = lambda self, op: _infer_unary_op(set(self.elts), op)
  97. nodes.Const.infer_unary_op = lambda self, op: _infer_unary_op(self.value, op)
  98. nodes.Dict.infer_unary_op = lambda self, op: _infer_unary_op(dict(self.items), op)
  99. # Binary operations
  100. BIN_OP_IMPL = {
  101. "+": lambda a, b: a + b,
  102. "-": lambda a, b: a - b,
  103. "/": lambda a, b: a / b,
  104. "//": lambda a, b: a // b,
  105. "*": lambda a, b: a * b,
  106. "**": lambda a, b: a ** b,
  107. "%": lambda a, b: a % b,
  108. "&": lambda a, b: a & b,
  109. "|": lambda a, b: a | b,
  110. "^": lambda a, b: a ^ b,
  111. "<<": lambda a, b: a << b,
  112. ">>": lambda a, b: a >> b,
  113. "@": operator_mod.matmul,
  114. }
  115. for _KEY, _IMPL in list(BIN_OP_IMPL.items()):
  116. BIN_OP_IMPL[_KEY + "="] = _IMPL
  117. @decorators.yes_if_nothing_inferred
  118. def const_infer_binary_op(self, opnode, operator, other, context, _):
  119. not_implemented = nodes.Const(NotImplemented)
  120. if isinstance(other, nodes.Const):
  121. try:
  122. impl = BIN_OP_IMPL[operator]
  123. try:
  124. yield nodes.const_factory(impl(self.value, other.value))
  125. except TypeError:
  126. # ArithmeticError is not enough: float >> float is a TypeError
  127. yield not_implemented
  128. except Exception: # pylint: disable=broad-except
  129. yield util.Uninferable
  130. except TypeError:
  131. yield not_implemented
  132. elif isinstance(self.value, str) and operator == "%":
  133. # TODO(cpopa): implement string interpolation later on.
  134. yield util.Uninferable
  135. else:
  136. yield not_implemented
  137. nodes.Const.infer_binary_op = const_infer_binary_op
  138. def _multiply_seq_by_int(self, opnode, other, context):
  139. node = self.__class__(parent=opnode)
  140. filtered_elts = (
  141. helpers.safe_infer(elt, context) or util.Uninferable
  142. for elt in self.elts
  143. if elt is not util.Uninferable
  144. )
  145. node.elts = list(filtered_elts) * other.value
  146. return node
  147. def _filter_uninferable_nodes(elts, context):
  148. for elt in elts:
  149. if elt is util.Uninferable:
  150. yield nodes.Unknown()
  151. else:
  152. for inferred in elt.infer(context):
  153. if inferred is not util.Uninferable:
  154. yield inferred
  155. else:
  156. yield nodes.Unknown()
  157. @decorators.yes_if_nothing_inferred
  158. def tl_infer_binary_op(self, opnode, operator, other, context, method):
  159. not_implemented = nodes.Const(NotImplemented)
  160. if isinstance(other, self.__class__) and operator == "+":
  161. node = self.__class__(parent=opnode)
  162. node.elts = list(
  163. itertools.chain(
  164. _filter_uninferable_nodes(self.elts, context),
  165. _filter_uninferable_nodes(other.elts, context),
  166. )
  167. )
  168. yield node
  169. elif isinstance(other, nodes.Const) and operator == "*":
  170. if not isinstance(other.value, int):
  171. yield not_implemented
  172. return
  173. yield _multiply_seq_by_int(self, opnode, other, context)
  174. elif isinstance(other, bases.Instance) and operator == "*":
  175. # Verify if the instance supports __index__.
  176. as_index = helpers.class_instance_as_index(other)
  177. if not as_index:
  178. yield util.Uninferable
  179. else:
  180. yield _multiply_seq_by_int(self, opnode, as_index, context)
  181. else:
  182. yield not_implemented
  183. nodes.Tuple.infer_binary_op = tl_infer_binary_op
  184. nodes.List.infer_binary_op = tl_infer_binary_op
  185. @decorators.yes_if_nothing_inferred
  186. def instance_class_infer_binary_op(self, opnode, operator, other, context, method):
  187. return method.infer_call_result(self, context)
  188. bases.Instance.infer_binary_op = instance_class_infer_binary_op
  189. nodes.ClassDef.infer_binary_op = instance_class_infer_binary_op
  190. # assignment ##################################################################
  191. """the assigned_stmts method is responsible to return the assigned statement
  192. (e.g. not inferred) according to the assignment type.
  193. The `assign_path` argument is used to record the lhs path of the original node.
  194. For instance if we want assigned statements for 'c' in 'a, (b,c)', assign_path
  195. will be [1, 1] once arrived to the Assign node.
  196. The `context` argument is the current inference context which should be given
  197. to any intermediary inference necessary.
  198. """
  199. def _resolve_looppart(parts, assign_path, context):
  200. """recursive function to resolve multiple assignments on loops"""
  201. assign_path = assign_path[:]
  202. index = assign_path.pop(0)
  203. for part in parts:
  204. if part is util.Uninferable:
  205. continue
  206. if not hasattr(part, "itered"):
  207. continue
  208. try:
  209. itered = part.itered()
  210. except TypeError:
  211. continue
  212. for stmt in itered:
  213. index_node = nodes.Const(index)
  214. try:
  215. assigned = stmt.getitem(index_node, context)
  216. except (AttributeError, AstroidTypeError, AstroidIndexError):
  217. continue
  218. if not assign_path:
  219. # we achieved to resolved the assignment path,
  220. # don't infer the last part
  221. yield assigned
  222. elif assigned is util.Uninferable:
  223. break
  224. else:
  225. # we are not yet on the last part of the path
  226. # search on each possibly inferred value
  227. try:
  228. yield from _resolve_looppart(
  229. assigned.infer(context), assign_path, context
  230. )
  231. except InferenceError:
  232. break
  233. @decorators.raise_if_nothing_inferred
  234. def for_assigned_stmts(
  235. self: Union[nodes.For, nodes.Comprehension],
  236. node: node_classes.AssignedStmtsPossibleNode = None,
  237. context: Optional[InferenceContext] = None,
  238. assign_path: Optional[List[int]] = None,
  239. ) -> Any:
  240. if isinstance(self, nodes.AsyncFor) or getattr(self, "is_async", False):
  241. # Skip inferring of async code for now
  242. return dict(node=self, unknown=node, assign_path=assign_path, context=context)
  243. if assign_path is None:
  244. for lst in self.iter.infer(context):
  245. if isinstance(lst, (nodes.Tuple, nodes.List)):
  246. yield from lst.elts
  247. else:
  248. yield from _resolve_looppart(self.iter.infer(context), assign_path, context)
  249. return dict(node=self, unknown=node, assign_path=assign_path, context=context)
  250. nodes.For.assigned_stmts = for_assigned_stmts
  251. nodes.Comprehension.assigned_stmts = for_assigned_stmts
  252. def sequence_assigned_stmts(
  253. self: Union[nodes.Tuple, nodes.List],
  254. node: node_classes.AssignedStmtsPossibleNode = None,
  255. context: Optional[InferenceContext] = None,
  256. assign_path: Optional[List[int]] = None,
  257. ) -> Any:
  258. if assign_path is None:
  259. assign_path = []
  260. try:
  261. index = self.elts.index(node)
  262. except ValueError as exc:
  263. raise InferenceError(
  264. "Tried to retrieve a node {node!r} which does not exist",
  265. node=self,
  266. assign_path=assign_path,
  267. context=context,
  268. ) from exc
  269. assign_path.insert(0, index)
  270. return self.parent.assigned_stmts(
  271. node=self, context=context, assign_path=assign_path
  272. )
  273. nodes.Tuple.assigned_stmts = sequence_assigned_stmts
  274. nodes.List.assigned_stmts = sequence_assigned_stmts
  275. def assend_assigned_stmts(
  276. self: Union[nodes.AssignName, nodes.AssignAttr],
  277. node: node_classes.AssignedStmtsPossibleNode = None,
  278. context: Optional[InferenceContext] = None,
  279. assign_path: Optional[List[int]] = None,
  280. ) -> Any:
  281. return self.parent.assigned_stmts(node=self, context=context)
  282. nodes.AssignName.assigned_stmts = assend_assigned_stmts
  283. nodes.AssignAttr.assigned_stmts = assend_assigned_stmts
  284. def _arguments_infer_argname(self, name, context):
  285. # arguments information may be missing, in which case we can't do anything
  286. # more
  287. if not (self.arguments or self.vararg or self.kwarg):
  288. yield util.Uninferable
  289. return
  290. functype = self.parent.type
  291. # first argument of instance/class method
  292. if (
  293. self.arguments
  294. and getattr(self.arguments[0], "name", None) == name
  295. and functype != "staticmethod"
  296. ):
  297. cls = self.parent.parent.scope()
  298. is_metaclass = isinstance(cls, nodes.ClassDef) and cls.type == "metaclass"
  299. # If this is a metaclass, then the first argument will always
  300. # be the class, not an instance.
  301. if context.boundnode and isinstance(context.boundnode, bases.Instance):
  302. cls = context.boundnode._proxied
  303. if is_metaclass or functype == "classmethod":
  304. yield cls
  305. return
  306. if functype == "method":
  307. yield cls.instantiate_class()
  308. return
  309. if context and context.callcontext:
  310. callee = context.callcontext.callee
  311. while hasattr(callee, "_proxied"):
  312. callee = callee._proxied
  313. if getattr(callee, "name", None) == self.parent.name:
  314. call_site = arguments.CallSite(context.callcontext, context.extra_context)
  315. yield from call_site.infer_argument(self.parent, name, context)
  316. return
  317. if name == self.vararg:
  318. vararg = nodes.const_factory(())
  319. vararg.parent = self
  320. if not self.arguments and self.parent.name == "__init__":
  321. cls = self.parent.parent.scope()
  322. vararg.elts = [cls.instantiate_class()]
  323. yield vararg
  324. return
  325. if name == self.kwarg:
  326. kwarg = nodes.const_factory({})
  327. kwarg.parent = self
  328. yield kwarg
  329. return
  330. # if there is a default value, yield it. And then yield Uninferable to reflect
  331. # we can't guess given argument value
  332. try:
  333. context = copy_context(context)
  334. yield from self.default_value(name).infer(context)
  335. yield util.Uninferable
  336. except NoDefault:
  337. yield util.Uninferable
  338. def arguments_assigned_stmts(
  339. self: nodes.Arguments,
  340. node: node_classes.AssignedStmtsPossibleNode = None,
  341. context: Optional[InferenceContext] = None,
  342. assign_path: Optional[List[int]] = None,
  343. ) -> Any:
  344. if context.callcontext:
  345. callee = context.callcontext.callee
  346. while hasattr(callee, "_proxied"):
  347. callee = callee._proxied
  348. else:
  349. callee = None
  350. if (
  351. context.callcontext
  352. and node
  353. and getattr(callee, "name", None) == node.frame(future=True).name
  354. ):
  355. # reset call context/name
  356. callcontext = context.callcontext
  357. context = copy_context(context)
  358. context.callcontext = None
  359. args = arguments.CallSite(callcontext, context=context)
  360. return args.infer_argument(self.parent, node.name, context)
  361. return _arguments_infer_argname(self, node.name, context)
  362. nodes.Arguments.assigned_stmts = arguments_assigned_stmts
  363. @decorators.raise_if_nothing_inferred
  364. def assign_assigned_stmts(
  365. self: Union[nodes.AugAssign, nodes.Assign, nodes.AnnAssign],
  366. node: node_classes.AssignedStmtsPossibleNode = None,
  367. context: Optional[InferenceContext] = None,
  368. assign_path: Optional[List[int]] = None,
  369. ) -> Any:
  370. if not assign_path:
  371. yield self.value
  372. return None
  373. yield from _resolve_assignment_parts(
  374. self.value.infer(context), assign_path, context
  375. )
  376. return dict(node=self, unknown=node, assign_path=assign_path, context=context)
  377. def assign_annassigned_stmts(
  378. self: nodes.AnnAssign,
  379. node: node_classes.AssignedStmtsPossibleNode = None,
  380. context: Optional[InferenceContext] = None,
  381. assign_path: Optional[List[int]] = None,
  382. ) -> Any:
  383. for inferred in assign_assigned_stmts(self, node, context, assign_path):
  384. if inferred is None:
  385. yield util.Uninferable
  386. else:
  387. yield inferred
  388. nodes.Assign.assigned_stmts = assign_assigned_stmts
  389. nodes.AnnAssign.assigned_stmts = assign_annassigned_stmts
  390. nodes.AugAssign.assigned_stmts = assign_assigned_stmts
  391. def _resolve_assignment_parts(parts, assign_path, context):
  392. """recursive function to resolve multiple assignments"""
  393. assign_path = assign_path[:]
  394. index = assign_path.pop(0)
  395. for part in parts:
  396. assigned = None
  397. if isinstance(part, nodes.Dict):
  398. # A dictionary in an iterating context
  399. try:
  400. assigned, _ = part.items[index]
  401. except IndexError:
  402. return
  403. elif hasattr(part, "getitem"):
  404. index_node = nodes.Const(index)
  405. try:
  406. assigned = part.getitem(index_node, context)
  407. except (AstroidTypeError, AstroidIndexError):
  408. return
  409. if not assigned:
  410. return
  411. if not assign_path:
  412. # we achieved to resolved the assignment path, don't infer the
  413. # last part
  414. yield assigned
  415. elif assigned is util.Uninferable:
  416. return
  417. else:
  418. # we are not yet on the last part of the path search on each
  419. # possibly inferred value
  420. try:
  421. yield from _resolve_assignment_parts(
  422. assigned.infer(context), assign_path, context
  423. )
  424. except InferenceError:
  425. return
  426. @decorators.raise_if_nothing_inferred
  427. def excepthandler_assigned_stmts(
  428. self: nodes.ExceptHandler,
  429. node: node_classes.AssignedStmtsPossibleNode = None,
  430. context: Optional[InferenceContext] = None,
  431. assign_path: Optional[List[int]] = None,
  432. ) -> Any:
  433. for assigned in node_classes.unpack_infer(self.type):
  434. if isinstance(assigned, nodes.ClassDef):
  435. assigned = objects.ExceptionInstance(assigned)
  436. yield assigned
  437. return dict(node=self, unknown=node, assign_path=assign_path, context=context)
  438. nodes.ExceptHandler.assigned_stmts = excepthandler_assigned_stmts
  439. def _infer_context_manager(self, mgr, context):
  440. try:
  441. inferred = next(mgr.infer(context=context))
  442. except StopIteration as e:
  443. raise InferenceError(node=mgr) from e
  444. if isinstance(inferred, bases.Generator):
  445. # Check if it is decorated with contextlib.contextmanager.
  446. func = inferred.parent
  447. if not func.decorators:
  448. raise InferenceError(
  449. "No decorators found on inferred generator %s", node=func
  450. )
  451. for decorator_node in func.decorators.nodes:
  452. decorator = next(decorator_node.infer(context=context), None)
  453. if isinstance(decorator, nodes.FunctionDef):
  454. if decorator.qname() == _CONTEXTLIB_MGR:
  455. break
  456. else:
  457. # It doesn't interest us.
  458. raise InferenceError(node=func)
  459. try:
  460. yield next(inferred.infer_yield_types())
  461. except StopIteration as e:
  462. raise InferenceError(node=func) from e
  463. elif isinstance(inferred, bases.Instance):
  464. try:
  465. enter = next(inferred.igetattr("__enter__", context=context))
  466. except (InferenceError, AttributeInferenceError, StopIteration) as exc:
  467. raise InferenceError(node=inferred) from exc
  468. if not isinstance(enter, bases.BoundMethod):
  469. raise InferenceError(node=enter)
  470. yield from enter.infer_call_result(self, context)
  471. else:
  472. raise InferenceError(node=mgr)
  473. @decorators.raise_if_nothing_inferred
  474. def with_assigned_stmts(
  475. self: nodes.With,
  476. node: node_classes.AssignedStmtsPossibleNode = None,
  477. context: Optional[InferenceContext] = None,
  478. assign_path: Optional[List[int]] = None,
  479. ) -> Any:
  480. """Infer names and other nodes from a *with* statement.
  481. This enables only inference for name binding in a *with* statement.
  482. For instance, in the following code, inferring `func` will return
  483. the `ContextManager` class, not whatever ``__enter__`` returns.
  484. We are doing this intentionally, because we consider that the context
  485. manager result is whatever __enter__ returns and what it is binded
  486. using the ``as`` keyword.
  487. class ContextManager(object):
  488. def __enter__(self):
  489. return 42
  490. with ContextManager() as f:
  491. pass
  492. # ContextManager().infer() will return ContextManager
  493. # f.infer() will return 42.
  494. Arguments:
  495. self: nodes.With
  496. node: The target of the assignment, `as (a, b)` in `with foo as (a, b)`.
  497. context: Inference context used for caching already inferred objects
  498. assign_path:
  499. A list of indices, where each index specifies what item to fetch from
  500. the inference results.
  501. """
  502. try:
  503. mgr = next(mgr for (mgr, vars) in self.items if vars == node)
  504. except StopIteration:
  505. return None
  506. if assign_path is None:
  507. yield from _infer_context_manager(self, mgr, context)
  508. else:
  509. for result in _infer_context_manager(self, mgr, context):
  510. # Walk the assign_path and get the item at the final index.
  511. obj = result
  512. for index in assign_path:
  513. if not hasattr(obj, "elts"):
  514. raise InferenceError(
  515. "Wrong type ({targets!r}) for {node!r} assignment",
  516. node=self,
  517. targets=node,
  518. assign_path=assign_path,
  519. context=context,
  520. )
  521. try:
  522. obj = obj.elts[index]
  523. except IndexError as exc:
  524. raise InferenceError(
  525. "Tried to infer a nonexistent target with index {index} "
  526. "in {node!r}.",
  527. node=self,
  528. targets=node,
  529. assign_path=assign_path,
  530. context=context,
  531. ) from exc
  532. except TypeError as exc:
  533. raise InferenceError(
  534. "Tried to unpack a non-iterable value " "in {node!r}.",
  535. node=self,
  536. targets=node,
  537. assign_path=assign_path,
  538. context=context,
  539. ) from exc
  540. yield obj
  541. return dict(node=self, unknown=node, assign_path=assign_path, context=context)
  542. nodes.With.assigned_stmts = with_assigned_stmts
  543. @decorators.raise_if_nothing_inferred
  544. def named_expr_assigned_stmts(
  545. self: nodes.NamedExpr,
  546. node: node_classes.AssignedStmtsPossibleNode,
  547. context: Optional[InferenceContext] = None,
  548. assign_path: Optional[List[int]] = None,
  549. ) -> Any:
  550. """Infer names and other nodes from an assignment expression"""
  551. if self.target == node:
  552. yield from self.value.infer(context=context)
  553. else:
  554. raise InferenceError(
  555. "Cannot infer NamedExpr node {node!r}",
  556. node=self,
  557. assign_path=assign_path,
  558. context=context,
  559. )
  560. nodes.NamedExpr.assigned_stmts = named_expr_assigned_stmts
  561. @decorators.yes_if_nothing_inferred
  562. def starred_assigned_stmts(
  563. self: nodes.Starred,
  564. node: node_classes.AssignedStmtsPossibleNode = None,
  565. context: Optional[InferenceContext] = None,
  566. assign_path: Optional[List[int]] = None,
  567. ) -> Any:
  568. """
  569. Arguments:
  570. self: nodes.Starred
  571. node: a node related to the current underlying Node.
  572. context: Inference context used for caching already inferred objects
  573. assign_path:
  574. A list of indices, where each index specifies what item to fetch from
  575. the inference results.
  576. """
  577. # pylint: disable=too-many-locals,too-many-statements
  578. def _determine_starred_iteration_lookups(starred, target, lookups):
  579. # Determine the lookups for the rhs of the iteration
  580. itered = target.itered()
  581. for index, element in enumerate(itered):
  582. if (
  583. isinstance(element, nodes.Starred)
  584. and element.value.name == starred.value.name
  585. ):
  586. lookups.append((index, len(itered)))
  587. break
  588. if isinstance(element, nodes.Tuple):
  589. lookups.append((index, len(element.itered())))
  590. _determine_starred_iteration_lookups(starred, element, lookups)
  591. stmt = self.statement(future=True)
  592. if not isinstance(stmt, (nodes.Assign, nodes.For)):
  593. raise InferenceError(
  594. "Statement {stmt!r} enclosing {node!r} " "must be an Assign or For node.",
  595. node=self,
  596. stmt=stmt,
  597. unknown=node,
  598. context=context,
  599. )
  600. if context is None:
  601. context = InferenceContext()
  602. if isinstance(stmt, nodes.Assign):
  603. value = stmt.value
  604. lhs = stmt.targets[0]
  605. if sum(1 for _ in lhs.nodes_of_class(nodes.Starred)) > 1:
  606. raise InferenceError(
  607. "Too many starred arguments in the " " assignment targets {lhs!r}.",
  608. node=self,
  609. targets=lhs,
  610. unknown=node,
  611. context=context,
  612. )
  613. try:
  614. rhs = next(value.infer(context))
  615. except (InferenceError, StopIteration):
  616. yield util.Uninferable
  617. return
  618. if rhs is util.Uninferable or not hasattr(rhs, "itered"):
  619. yield util.Uninferable
  620. return
  621. try:
  622. elts = collections.deque(rhs.itered())
  623. except TypeError:
  624. yield util.Uninferable
  625. return
  626. # Unpack iteratively the values from the rhs of the assignment,
  627. # until the find the starred node. What will remain will
  628. # be the list of values which the Starred node will represent
  629. # This is done in two steps, from left to right to remove
  630. # anything before the starred node and from right to left
  631. # to remove anything after the starred node.
  632. for index, left_node in enumerate(lhs.elts):
  633. if not isinstance(left_node, nodes.Starred):
  634. if not elts:
  635. break
  636. elts.popleft()
  637. continue
  638. lhs_elts = collections.deque(reversed(lhs.elts[index:]))
  639. for right_node in lhs_elts:
  640. if not isinstance(right_node, nodes.Starred):
  641. if not elts:
  642. break
  643. elts.pop()
  644. continue
  645. # We're done unpacking.
  646. packed = nodes.List(
  647. ctx=Context.Store,
  648. parent=self,
  649. lineno=lhs.lineno,
  650. col_offset=lhs.col_offset,
  651. )
  652. packed.postinit(elts=list(elts))
  653. yield packed
  654. break
  655. if isinstance(stmt, nodes.For):
  656. try:
  657. inferred_iterable = next(stmt.iter.infer(context=context))
  658. except (InferenceError, StopIteration):
  659. yield util.Uninferable
  660. return
  661. if inferred_iterable is util.Uninferable or not hasattr(
  662. inferred_iterable, "itered"
  663. ):
  664. yield util.Uninferable
  665. return
  666. try:
  667. itered = inferred_iterable.itered()
  668. except TypeError:
  669. yield util.Uninferable
  670. return
  671. target = stmt.target
  672. if not isinstance(target, nodes.Tuple):
  673. raise InferenceError(
  674. "Could not make sense of this, the target must be a tuple",
  675. context=context,
  676. )
  677. lookups = []
  678. _determine_starred_iteration_lookups(self, target, lookups)
  679. if not lookups:
  680. raise InferenceError(
  681. "Could not make sense of this, needs at least a lookup", context=context
  682. )
  683. # Make the last lookup a slice, since that what we want for a Starred node
  684. last_element_index, last_element_length = lookups[-1]
  685. is_starred_last = last_element_index == (last_element_length - 1)
  686. lookup_slice = slice(
  687. last_element_index,
  688. None if is_starred_last else (last_element_length - last_element_index),
  689. )
  690. lookups[-1] = lookup_slice
  691. for element in itered:
  692. # We probably want to infer the potential values *for each* element in an
  693. # iterable, but we can't infer a list of all values, when only a list of
  694. # step values are expected:
  695. #
  696. # for a, *b in [...]:
  697. # b
  698. #
  699. # *b* should now point to just the elements at that particular iteration step,
  700. # which astroid can't know about.
  701. found_element = None
  702. for lookup in lookups:
  703. if not hasattr(element, "itered"):
  704. break
  705. if not isinstance(lookup, slice):
  706. # Grab just the index, not the whole length
  707. lookup = lookup[0]
  708. try:
  709. itered_inner_element = element.itered()
  710. element = itered_inner_element[lookup]
  711. except IndexError:
  712. break
  713. except TypeError:
  714. # Most likely the itered() call failed, cannot make sense of this
  715. yield util.Uninferable
  716. return
  717. else:
  718. found_element = element
  719. unpacked = nodes.List(
  720. ctx=Context.Store,
  721. parent=self,
  722. lineno=self.lineno,
  723. col_offset=self.col_offset,
  724. )
  725. unpacked.postinit(elts=found_element or [])
  726. yield unpacked
  727. return
  728. yield util.Uninferable
  729. nodes.Starred.assigned_stmts = starred_assigned_stmts
  730. @decorators.yes_if_nothing_inferred
  731. def match_mapping_assigned_stmts(
  732. self: nodes.MatchMapping,
  733. node: nodes.AssignName,
  734. context: Optional[InferenceContext] = None,
  735. assign_path: Literal[None] = None,
  736. ) -> Generator[nodes.NodeNG, None, None]:
  737. """Return empty generator (return -> raises StopIteration) so inferred value
  738. is Uninferable.
  739. """
  740. return
  741. yield
  742. nodes.MatchMapping.assigned_stmts = match_mapping_assigned_stmts
  743. @decorators.yes_if_nothing_inferred
  744. def match_star_assigned_stmts(
  745. self: nodes.MatchStar,
  746. node: nodes.AssignName,
  747. context: Optional[InferenceContext] = None,
  748. assign_path: Literal[None] = None,
  749. ) -> Generator[nodes.NodeNG, None, None]:
  750. """Return empty generator (return -> raises StopIteration) so inferred value
  751. is Uninferable.
  752. """
  753. return
  754. yield
  755. nodes.MatchStar.assigned_stmts = match_star_assigned_stmts
  756. @decorators.yes_if_nothing_inferred
  757. def match_as_assigned_stmts(
  758. self: nodes.MatchAs,
  759. node: nodes.AssignName,
  760. context: Optional[InferenceContext] = None,
  761. assign_path: Literal[None] = None,
  762. ) -> Generator[nodes.NodeNG, None, None]:
  763. """Infer MatchAs as the Match subject if it's the only MatchCase pattern
  764. else raise StopIteration to yield Uninferable.
  765. """
  766. if (
  767. isinstance(self.parent, nodes.MatchCase)
  768. and isinstance(self.parent.parent, nodes.Match)
  769. and self.pattern is None
  770. ):
  771. yield self.parent.parent.subject
  772. nodes.MatchAs.assigned_stmts = match_as_assigned_stmts