arguments.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. from astroid import nodes
  6. from astroid.bases import Instance
  7. from astroid.context import CallContext, InferenceContext
  8. from astroid.exceptions import InferenceError, NoDefault
  9. from astroid.typing import InferenceResult
  10. from astroid.util import Uninferable, UninferableBase, safe_infer
  11. class CallSite:
  12. """Class for understanding arguments passed into a call site.
  13. It needs a call context, which contains the arguments and the
  14. keyword arguments that were passed into a given call site.
  15. In order to infer what an argument represents, call :meth:`infer_argument`
  16. with the corresponding function node and the argument name.
  17. :param callcontext:
  18. An instance of :class:`astroid.context.CallContext`, that holds
  19. the arguments for the call site.
  20. :param argument_context_map:
  21. Additional contexts per node, passed in from :attr:`astroid.context.Context.extra_context`
  22. :param context:
  23. An instance of :class:`astroid.context.Context`.
  24. """
  25. def __init__(
  26. self,
  27. callcontext: CallContext,
  28. argument_context_map=None,
  29. context: InferenceContext | None = None,
  30. ):
  31. if argument_context_map is None:
  32. argument_context_map = {}
  33. self.argument_context_map = argument_context_map
  34. args = callcontext.args
  35. keywords = callcontext.keywords
  36. self.duplicated_keywords: set[str] = set()
  37. self._unpacked_args = self._unpack_args(args, context=context)
  38. self._unpacked_kwargs = self._unpack_keywords(keywords, context=context)
  39. self.positional_arguments = [
  40. arg for arg in self._unpacked_args if not isinstance(arg, UninferableBase)
  41. ]
  42. self.keyword_arguments = {
  43. key: value
  44. for key, value in self._unpacked_kwargs.items()
  45. if not isinstance(value, UninferableBase)
  46. }
  47. @classmethod
  48. def from_call(cls, call_node: nodes.Call, context: InferenceContext | None = None):
  49. """Get a CallSite object from the given Call node.
  50. context will be used to force a single inference path.
  51. """
  52. # Determine the callcontext from the given `context` object if any.
  53. context = context or InferenceContext()
  54. callcontext = CallContext(call_node.args, call_node.keywords)
  55. return cls(callcontext, context=context)
  56. def has_invalid_arguments(self) -> bool:
  57. """Check if in the current CallSite were passed *invalid* arguments.
  58. This can mean multiple things. For instance, if an unpacking
  59. of an invalid object was passed, then this method will return True.
  60. Other cases can be when the arguments can't be inferred by astroid,
  61. for example, by passing objects which aren't known statically.
  62. """
  63. return len(self.positional_arguments) != len(self._unpacked_args)
  64. def has_invalid_keywords(self) -> bool:
  65. """Check if in the current CallSite were passed *invalid* keyword arguments.
  66. For instance, unpacking a dictionary with integer keys is invalid
  67. (**{1:2}), because the keys must be strings, which will make this
  68. method to return True. Other cases where this might return True if
  69. objects which can't be inferred were passed.
  70. """
  71. return len(self.keyword_arguments) != len(self._unpacked_kwargs)
  72. def _unpack_keywords(
  73. self,
  74. keywords: list[tuple[str | None, nodes.NodeNG]],
  75. context: InferenceContext | None = None,
  76. ) -> dict[str | None, InferenceResult]:
  77. values: dict[str | None, InferenceResult] = {}
  78. context = context or InferenceContext()
  79. context.extra_context = self.argument_context_map
  80. for name, value in keywords:
  81. if name is None:
  82. # Then it's an unpacking operation (**)
  83. inferred = safe_infer(value, context=context)
  84. if not isinstance(inferred, nodes.Dict):
  85. # Not something we can work with.
  86. values[name] = Uninferable
  87. continue
  88. for dict_key, dict_value in inferred.items:
  89. dict_key = safe_infer(dict_key, context=context)
  90. if not isinstance(dict_key, nodes.Const):
  91. values[name] = Uninferable
  92. continue
  93. if not isinstance(dict_key.value, str):
  94. values[name] = Uninferable
  95. continue
  96. if dict_key.value in values:
  97. # The name is already in the dictionary
  98. values[dict_key.value] = Uninferable
  99. self.duplicated_keywords.add(dict_key.value)
  100. continue
  101. values[dict_key.value] = dict_value
  102. else:
  103. values[name] = value
  104. return values
  105. def _unpack_args(self, args, context: InferenceContext | None = None):
  106. values = []
  107. context = context or InferenceContext()
  108. context.extra_context = self.argument_context_map
  109. for arg in args:
  110. if isinstance(arg, nodes.Starred):
  111. inferred = safe_infer(arg.value, context=context)
  112. if isinstance(inferred, UninferableBase):
  113. values.append(Uninferable)
  114. continue
  115. if not hasattr(inferred, "elts"):
  116. values.append(Uninferable)
  117. continue
  118. values.extend(inferred.elts)
  119. else:
  120. values.append(arg)
  121. return values
  122. def infer_argument(
  123. self, funcnode: InferenceResult, name: str, context: InferenceContext
  124. ): # noqa: C901
  125. """Infer a function argument value according to the call context."""
  126. # pylint: disable = too-many-branches
  127. if not isinstance(funcnode, (nodes.FunctionDef, nodes.Lambda)):
  128. raise InferenceError(
  129. f"Can not infer function argument value for non-function node {funcnode!r}.",
  130. call_site=self,
  131. func=funcnode,
  132. arg=name,
  133. context=context,
  134. )
  135. if name in self.duplicated_keywords:
  136. raise InferenceError(
  137. "The arguments passed to {func!r} have duplicate keywords.",
  138. call_site=self,
  139. func=funcnode,
  140. arg=name,
  141. context=context,
  142. )
  143. # Look into the keywords first, maybe it's already there.
  144. try:
  145. return self.keyword_arguments[name].infer(context)
  146. except KeyError:
  147. pass
  148. # Too many arguments given and no variable arguments.
  149. if len(self.positional_arguments) > len(funcnode.args.args):
  150. if not funcnode.args.vararg and not funcnode.args.posonlyargs:
  151. raise InferenceError(
  152. "Too many positional arguments "
  153. "passed to {func!r} that does "
  154. "not have *args.",
  155. call_site=self,
  156. func=funcnode,
  157. arg=name,
  158. context=context,
  159. )
  160. positional = self.positional_arguments[: len(funcnode.args.args)]
  161. vararg = self.positional_arguments[len(funcnode.args.args) :]
  162. # preserving previous behavior, when vararg and kwarg were not included in find_argname results
  163. if name in [funcnode.args.vararg, funcnode.args.kwarg]:
  164. argindex = None
  165. else:
  166. argindex = funcnode.args.find_argname(name)[0]
  167. kwonlyargs = {arg.name for arg in funcnode.args.kwonlyargs}
  168. kwargs = {
  169. key: value
  170. for key, value in self.keyword_arguments.items()
  171. if key not in kwonlyargs
  172. }
  173. # If there are too few positionals compared to
  174. # what the function expects to receive, check to see
  175. # if the missing positional arguments were passed
  176. # as keyword arguments and if so, place them into the
  177. # positional args list.
  178. if len(positional) < len(funcnode.args.args):
  179. for func_arg in funcnode.args.args:
  180. if func_arg.name in kwargs:
  181. arg = kwargs.pop(func_arg.name)
  182. positional.append(arg)
  183. if argindex is not None:
  184. boundnode = context.boundnode
  185. # 2. first argument of instance/class method
  186. if argindex == 0 and funcnode.type in {"method", "classmethod"}:
  187. # context.boundnode is None when an instance method is called with
  188. # the class, e.g. MyClass.method(obj, ...). In this case, self
  189. # is the first argument.
  190. if boundnode is None and funcnode.type == "method" and positional:
  191. return positional[0].infer(context=context)
  192. if boundnode is None:
  193. # XXX can do better ?
  194. boundnode = funcnode.parent.frame()
  195. if isinstance(boundnode, nodes.ClassDef):
  196. # Verify that we're accessing a method
  197. # of the metaclass through a class, as in
  198. # `cls.metaclass_method`. In this case, the
  199. # first argument is always the class.
  200. method_scope = funcnode.parent.scope()
  201. if method_scope is boundnode.metaclass(context=context):
  202. return iter((boundnode,))
  203. if funcnode.type == "method":
  204. if not isinstance(boundnode, Instance):
  205. boundnode = boundnode.instantiate_class()
  206. return iter((boundnode,))
  207. if funcnode.type == "classmethod":
  208. return iter((boundnode,))
  209. # if we have a method, extract one position
  210. # from the index, so we'll take in account
  211. # the extra parameter represented by `self` or `cls`
  212. if funcnode.type in {"method", "classmethod"} and boundnode:
  213. argindex -= 1
  214. # 2. search arg index
  215. try:
  216. return self.positional_arguments[argindex].infer(context)
  217. except IndexError:
  218. pass
  219. if funcnode.args.kwarg == name:
  220. # It wants all the keywords that were passed into
  221. # the call site.
  222. if self.has_invalid_keywords():
  223. raise InferenceError(
  224. "Inference failed to find values for all keyword arguments "
  225. "to {func!r}: {unpacked_kwargs!r} doesn't correspond to "
  226. "{keyword_arguments!r}.",
  227. keyword_arguments=self.keyword_arguments,
  228. unpacked_kwargs=self._unpacked_kwargs,
  229. call_site=self,
  230. func=funcnode,
  231. arg=name,
  232. context=context,
  233. )
  234. kwarg = nodes.Dict(
  235. lineno=funcnode.args.lineno,
  236. col_offset=funcnode.args.col_offset,
  237. parent=funcnode.args,
  238. end_lineno=funcnode.args.end_lineno,
  239. end_col_offset=funcnode.args.end_col_offset,
  240. )
  241. kwarg.postinit(
  242. [(nodes.const_factory(key), value) for key, value in kwargs.items()]
  243. )
  244. return iter((kwarg,))
  245. if funcnode.args.vararg == name:
  246. # It wants all the args that were passed into
  247. # the call site.
  248. if self.has_invalid_arguments():
  249. raise InferenceError(
  250. "Inference failed to find values for all positional "
  251. "arguments to {func!r}: {unpacked_args!r} doesn't "
  252. "correspond to {positional_arguments!r}.",
  253. positional_arguments=self.positional_arguments,
  254. unpacked_args=self._unpacked_args,
  255. call_site=self,
  256. func=funcnode,
  257. arg=name,
  258. context=context,
  259. )
  260. args = nodes.Tuple(
  261. lineno=funcnode.args.lineno,
  262. col_offset=funcnode.args.col_offset,
  263. parent=funcnode.args,
  264. )
  265. args.postinit(vararg)
  266. return iter((args,))
  267. # Check if it's a default parameter.
  268. try:
  269. return funcnode.args.default_value(name).infer(context)
  270. except NoDefault:
  271. pass
  272. raise InferenceError(
  273. "No value found for argument {arg} to {func!r}",
  274. call_site=self,
  275. func=funcnode,
  276. arg=name,
  277. context=context,
  278. )