arguments.py 13 KB

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