objects.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. # Copyright (c) 2015-2016, 2018-2020 Claudiu Popa <pcmanticore@gmail.com>
  2. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
  3. # Copyright (c) 2015 Florian Bruhin <me@the-compiler.org>
  4. # Copyright (c) 2016 Derek Gustafson <degustaf@gmail.com>
  5. # Copyright (c) 2018 hippo91 <guillaume.peillex@gmail.com>
  6. # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  7. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  8. # Copyright (c) 2021 Craig Franklin <craigjfranklin@gmail.com>
  9. # Copyright (c) 2021 Alphadelta14 <alpha@alphaservcomputing.solutions>
  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. """
  14. Inference objects are a way to represent composite AST nodes,
  15. which are used only as inference results, so they can't be found in the
  16. original AST tree. For instance, inferring the following frozenset use,
  17. leads to an inferred FrozenSet:
  18. Call(func=Name('frozenset'), args=Tuple(...))
  19. """
  20. from astroid import bases, decorators, util
  21. from astroid.exceptions import (
  22. AttributeInferenceError,
  23. InferenceError,
  24. MroError,
  25. SuperError,
  26. )
  27. from astroid.manager import AstroidManager
  28. from astroid.nodes import node_classes, scoped_nodes
  29. objectmodel = util.lazy_import("interpreter.objectmodel")
  30. class FrozenSet(node_classes.BaseContainer):
  31. """class representing a FrozenSet composite node"""
  32. def pytype(self):
  33. return "builtins.frozenset"
  34. def _infer(self, context=None):
  35. yield self
  36. @decorators.cachedproperty
  37. def _proxied(self): # pylint: disable=method-hidden
  38. ast_builtins = AstroidManager().builtins_module
  39. return ast_builtins.getattr("frozenset")[0]
  40. class Super(node_classes.NodeNG):
  41. """Proxy class over a super call.
  42. This class offers almost the same behaviour as Python's super,
  43. which is MRO lookups for retrieving attributes from the parents.
  44. The *mro_pointer* is the place in the MRO from where we should
  45. start looking, not counting it. *mro_type* is the object which
  46. provides the MRO, it can be both a type or an instance.
  47. *self_class* is the class where the super call is, while
  48. *scope* is the function where the super call is.
  49. """
  50. # pylint: disable=unnecessary-lambda
  51. special_attributes = util.lazy_descriptor(lambda: objectmodel.SuperModel())
  52. def __init__(self, mro_pointer, mro_type, self_class, scope):
  53. self.type = mro_type
  54. self.mro_pointer = mro_pointer
  55. self._class_based = False
  56. self._self_class = self_class
  57. self._scope = scope
  58. super().__init__()
  59. def _infer(self, context=None):
  60. yield self
  61. def super_mro(self):
  62. """Get the MRO which will be used to lookup attributes in this super."""
  63. if not isinstance(self.mro_pointer, scoped_nodes.ClassDef):
  64. raise SuperError(
  65. "The first argument to super must be a subtype of "
  66. "type, not {mro_pointer}.",
  67. super_=self,
  68. )
  69. if isinstance(self.type, scoped_nodes.ClassDef):
  70. # `super(type, type)`, most likely in a class method.
  71. self._class_based = True
  72. mro_type = self.type
  73. else:
  74. mro_type = getattr(self.type, "_proxied", None)
  75. if not isinstance(mro_type, (bases.Instance, scoped_nodes.ClassDef)):
  76. raise SuperError(
  77. "The second argument to super must be an "
  78. "instance or subtype of type, not {type}.",
  79. super_=self,
  80. )
  81. if not mro_type.newstyle:
  82. raise SuperError("Unable to call super on old-style classes.", super_=self)
  83. mro = mro_type.mro()
  84. if self.mro_pointer not in mro:
  85. raise SuperError(
  86. "The second argument to super must be an "
  87. "instance or subtype of type, not {type}.",
  88. super_=self,
  89. )
  90. index = mro.index(self.mro_pointer)
  91. return mro[index + 1 :]
  92. @decorators.cachedproperty
  93. def _proxied(self):
  94. ast_builtins = AstroidManager().builtins_module
  95. return ast_builtins.getattr("super")[0]
  96. def pytype(self):
  97. return "builtins.super"
  98. def display_type(self):
  99. return "Super of"
  100. @property
  101. def name(self):
  102. """Get the name of the MRO pointer."""
  103. return self.mro_pointer.name
  104. def qname(self):
  105. return "super"
  106. def igetattr(self, name, context=None):
  107. """Retrieve the inferred values of the given attribute name."""
  108. if name in self.special_attributes:
  109. yield self.special_attributes.lookup(name)
  110. return
  111. try:
  112. mro = self.super_mro()
  113. # Don't let invalid MROs or invalid super calls
  114. # leak out as is from this function.
  115. except SuperError as exc:
  116. raise AttributeInferenceError(
  117. (
  118. "Lookup for {name} on {target!r} because super call {super!r} "
  119. "is invalid."
  120. ),
  121. target=self,
  122. attribute=name,
  123. context=context,
  124. super_=exc.super_,
  125. ) from exc
  126. except MroError as exc:
  127. raise AttributeInferenceError(
  128. (
  129. "Lookup for {name} on {target!r} failed because {cls!r} has an "
  130. "invalid MRO."
  131. ),
  132. target=self,
  133. attribute=name,
  134. context=context,
  135. mros=exc.mros,
  136. cls=exc.cls,
  137. ) from exc
  138. found = False
  139. for cls in mro:
  140. if name not in cls.locals:
  141. continue
  142. found = True
  143. for inferred in bases._infer_stmts([cls[name]], context, frame=self):
  144. if not isinstance(inferred, scoped_nodes.FunctionDef):
  145. yield inferred
  146. continue
  147. # We can obtain different descriptors from a super depending
  148. # on what we are accessing and where the super call is.
  149. if inferred.type == "classmethod":
  150. yield bases.BoundMethod(inferred, cls)
  151. elif self._scope.type == "classmethod" and inferred.type == "method":
  152. yield inferred
  153. elif self._class_based or inferred.type == "staticmethod":
  154. yield inferred
  155. elif isinstance(inferred, Property):
  156. function = inferred.function
  157. try:
  158. yield from function.infer_call_result(
  159. caller=self, context=context
  160. )
  161. except InferenceError:
  162. yield util.Uninferable
  163. elif bases._is_property(inferred):
  164. # TODO: support other descriptors as well.
  165. try:
  166. yield from inferred.infer_call_result(self, context)
  167. except InferenceError:
  168. yield util.Uninferable
  169. else:
  170. yield bases.BoundMethod(inferred, cls)
  171. if not found:
  172. raise AttributeInferenceError(target=self, attribute=name, context=context)
  173. def getattr(self, name, context=None):
  174. return list(self.igetattr(name, context=context))
  175. class ExceptionInstance(bases.Instance):
  176. """Class for instances of exceptions
  177. It has special treatment for some of the exceptions's attributes,
  178. which are transformed at runtime into certain concrete objects, such as
  179. the case of .args.
  180. """
  181. @decorators.cachedproperty
  182. def special_attributes(self):
  183. qname = self.qname()
  184. instance = objectmodel.BUILTIN_EXCEPTIONS.get(
  185. qname, objectmodel.ExceptionInstanceModel
  186. )
  187. return instance()(self)
  188. class DictInstance(bases.Instance):
  189. """Special kind of instances for dictionaries
  190. This instance knows the underlying object model of the dictionaries, which means
  191. that methods such as .values or .items can be properly inferred.
  192. """
  193. # pylint: disable=unnecessary-lambda
  194. special_attributes = util.lazy_descriptor(lambda: objectmodel.DictModel())
  195. # Custom objects tailored for dictionaries, which are used to
  196. # disambiguate between the types of Python 2 dict's method returns
  197. # and Python 3 (where they return set like objects).
  198. class DictItems(bases.Proxy):
  199. __str__ = node_classes.NodeNG.__str__
  200. __repr__ = node_classes.NodeNG.__repr__
  201. class DictKeys(bases.Proxy):
  202. __str__ = node_classes.NodeNG.__str__
  203. __repr__ = node_classes.NodeNG.__repr__
  204. class DictValues(bases.Proxy):
  205. __str__ = node_classes.NodeNG.__str__
  206. __repr__ = node_classes.NodeNG.__repr__
  207. class PartialFunction(scoped_nodes.FunctionDef):
  208. """A class representing partial function obtained via functools.partial"""
  209. def __init__(
  210. self, call, name=None, doc=None, lineno=None, col_offset=None, parent=None
  211. ):
  212. super().__init__(name, doc, lineno, col_offset, parent=None)
  213. # A typical FunctionDef automatically adds its name to the parent scope,
  214. # but a partial should not, so defer setting parent until after init
  215. self.parent = parent
  216. self.filled_args = call.positional_arguments[1:]
  217. self.filled_keywords = call.keyword_arguments
  218. wrapped_function = call.positional_arguments[0]
  219. inferred_wrapped_function = next(wrapped_function.infer())
  220. if isinstance(inferred_wrapped_function, PartialFunction):
  221. self.filled_args = inferred_wrapped_function.filled_args + self.filled_args
  222. self.filled_keywords = {
  223. **inferred_wrapped_function.filled_keywords,
  224. **self.filled_keywords,
  225. }
  226. self.filled_positionals = len(self.filled_args)
  227. def infer_call_result(self, caller=None, context=None):
  228. if context:
  229. current_passed_keywords = {
  230. keyword for (keyword, _) in context.callcontext.keywords
  231. }
  232. for keyword, value in self.filled_keywords.items():
  233. if keyword not in current_passed_keywords:
  234. context.callcontext.keywords.append((keyword, value))
  235. call_context_args = context.callcontext.args or []
  236. context.callcontext.args = self.filled_args + call_context_args
  237. return super().infer_call_result(caller=caller, context=context)
  238. def qname(self):
  239. return self.__class__.__name__
  240. # TODO: Hack to solve the circular import problem between node_classes and objects
  241. # This is not needed in 2.0, which has a cleaner design overall
  242. node_classes.Dict.__bases__ = (node_classes.NodeNG, DictInstance)
  243. class Property(scoped_nodes.FunctionDef):
  244. """Class representing a Python property"""
  245. def __init__(
  246. self, function, name=None, doc=None, lineno=None, col_offset=None, parent=None
  247. ):
  248. self.function = function
  249. super().__init__(name, doc, lineno, col_offset, parent)
  250. # pylint: disable=unnecessary-lambda
  251. special_attributes = util.lazy_descriptor(lambda: objectmodel.PropertyModel())
  252. type = "property"
  253. def pytype(self):
  254. return "builtins.property"
  255. def infer_call_result(self, caller=None, context=None):
  256. raise InferenceError("Properties are not callable")
  257. def infer(self, context=None, **kwargs):
  258. return iter((self,))