scoped_nodes.py 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267
  1. # Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2010 Daniel Harding <dharding@gmail.com>
  3. # Copyright (c) 2011, 2013-2015 Google, Inc.
  4. # Copyright (c) 2013-2020 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2013 Phil Schaf <flying-sheep@web.de>
  6. # Copyright (c) 2014 Eevee (Alex Munroe) <amunroe@yelp.com>
  7. # Copyright (c) 2015-2016 Florian Bruhin <me@the-compiler.org>
  8. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
  9. # Copyright (c) 2015 Rene Zhang <rz99@cornell.edu>
  10. # Copyright (c) 2015 Philip Lorenz <philip@bithub.de>
  11. # Copyright (c) 2016-2017 Derek Gustafson <degustaf@gmail.com>
  12. # Copyright (c) 2017-2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  13. # Copyright (c) 2017-2018 Ashley Whetter <ashley@awhetter.co.uk>
  14. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  15. # Copyright (c) 2017 David Euresti <david@dropbox.com>
  16. # Copyright (c) 2018-2019, 2021 Nick Drozd <nicholasdrozd@gmail.com>
  17. # Copyright (c) 2018 Ville Skyttä <ville.skytta@iki.fi>
  18. # Copyright (c) 2018 Anthony Sottile <asottile@umich.edu>
  19. # Copyright (c) 2018 HoverHell <hoverhell@gmail.com>
  20. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  21. # Copyright (c) 2019 Peter de Blanc <peter@standard.ai>
  22. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  23. # Copyright (c) 2020 Peter Kolbus <peter.kolbus@gmail.com>
  24. # Copyright (c) 2020 Tim Martin <tim@asymptotic.co.uk>
  25. # Copyright (c) 2020 Ram Rachum <ram@rachum.com>
  26. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  27. # Copyright (c) 2021 Tushar Sadhwani <86737547+tushar-deepsource@users.noreply.github.com>
  28. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  29. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  30. # Copyright (c) 2021 Kian Meng, Ang <kianmeng.ang@gmail.com>
  31. # Copyright (c) 2021 Dmitry Shachnev <mitya57@users.noreply.github.com>
  32. # Copyright (c) 2021 David Liu <david@cs.toronto.edu>
  33. # Copyright (c) 2021 pre-commit-ci[bot] <bot@noreply.github.com>
  34. # Copyright (c) 2021 doranid <ddandd@gmail.com>
  35. # Copyright (c) 2021 Andrew Haigh <hello@nelf.in>
  36. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  37. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  38. """
  39. This module contains the classes for "scoped" node, i.e. which are opening a
  40. new local scope in the language definition : Module, ClassDef, FunctionDef (and
  41. Lambda, GeneratorExp, DictComp and SetComp to some extent).
  42. """
  43. import builtins
  44. import io
  45. import itertools
  46. import os
  47. import sys
  48. import typing
  49. import warnings
  50. from typing import List, Optional, TypeVar, Union, overload
  51. from astroid import bases
  52. from astroid import decorators as decorators_mod
  53. from astroid import mixins, util
  54. from astroid.const import PY39_PLUS
  55. from astroid.context import (
  56. CallContext,
  57. InferenceContext,
  58. bind_context_to_node,
  59. copy_context,
  60. )
  61. from astroid.exceptions import (
  62. AstroidBuildingError,
  63. AstroidTypeError,
  64. AttributeInferenceError,
  65. DuplicateBasesError,
  66. InconsistentMroError,
  67. InferenceError,
  68. MroError,
  69. StatementMissing,
  70. TooManyLevelsError,
  71. )
  72. from astroid.filter_statements import _filter_stmts
  73. from astroid.interpreter.dunder_lookup import lookup
  74. from astroid.interpreter.objectmodel import ClassModel, FunctionModel, ModuleModel
  75. from astroid.manager import AstroidManager
  76. from astroid.nodes import Arguments, Const, node_classes
  77. if sys.version_info >= (3, 6, 2):
  78. from typing import NoReturn
  79. else:
  80. from typing_extensions import NoReturn
  81. if sys.version_info >= (3, 8):
  82. from typing import Literal
  83. else:
  84. from typing_extensions import Literal
  85. ITER_METHODS = ("__iter__", "__getitem__")
  86. EXCEPTION_BASE_CLASSES = frozenset({"Exception", "BaseException"})
  87. objects = util.lazy_import("objects")
  88. BUILTIN_DESCRIPTORS = frozenset(
  89. {"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"}
  90. )
  91. T = TypeVar("T")
  92. def _c3_merge(sequences, cls, context):
  93. """Merges MROs in *sequences* to a single MRO using the C3 algorithm.
  94. Adapted from http://www.python.org/download/releases/2.3/mro/.
  95. """
  96. result = []
  97. while True:
  98. sequences = [s for s in sequences if s] # purge empty sequences
  99. if not sequences:
  100. return result
  101. for s1 in sequences: # find merge candidates among seq heads
  102. candidate = s1[0]
  103. for s2 in sequences:
  104. if candidate in s2[1:]:
  105. candidate = None
  106. break # reject the current head, it appears later
  107. else:
  108. break
  109. if not candidate:
  110. # Show all the remaining bases, which were considered as
  111. # candidates for the next mro sequence.
  112. raise InconsistentMroError(
  113. message="Cannot create a consistent method resolution order "
  114. "for MROs {mros} of class {cls!r}.",
  115. mros=sequences,
  116. cls=cls,
  117. context=context,
  118. )
  119. result.append(candidate)
  120. # remove the chosen candidate
  121. for seq in sequences:
  122. if seq[0] == candidate:
  123. del seq[0]
  124. return None
  125. def clean_typing_generic_mro(sequences: List[List["ClassDef"]]) -> None:
  126. """A class can inherit from typing.Generic directly, as base,
  127. and as base of bases. The merged MRO must however only contain the last entry.
  128. To prepare for _c3_merge, remove some typing.Generic entries from
  129. sequences if multiple are present.
  130. This method will check if Generic is in inferred_bases and also
  131. part of bases_mro. If true, remove it from inferred_bases
  132. as well as its entry the bases_mro.
  133. Format sequences: [[self]] + bases_mro + [inferred_bases]
  134. """
  135. bases_mro = sequences[1:-1]
  136. inferred_bases = sequences[-1]
  137. # Check if Generic is part of inferred_bases
  138. for i, base in enumerate(inferred_bases):
  139. if base.qname() == "typing.Generic":
  140. position_in_inferred_bases = i
  141. break
  142. else:
  143. return
  144. # Check if also part of bases_mro
  145. # Ignore entry for typing.Generic
  146. for i, seq in enumerate(bases_mro):
  147. if i == position_in_inferred_bases:
  148. continue
  149. if any(base.qname() == "typing.Generic" for base in seq):
  150. break
  151. else:
  152. return
  153. # Found multiple Generics in mro, remove entry from inferred_bases
  154. # and the corresponding one from bases_mro
  155. inferred_bases.pop(position_in_inferred_bases)
  156. bases_mro.pop(position_in_inferred_bases)
  157. def clean_duplicates_mro(sequences, cls, context):
  158. for sequence in sequences:
  159. names = [
  160. (node.lineno, node.qname()) if node.name else None for node in sequence
  161. ]
  162. last_index = dict(map(reversed, enumerate(names)))
  163. if names and names[0] is not None and last_index[names[0]] != 0:
  164. raise DuplicateBasesError(
  165. message="Duplicates found in MROs {mros} for {cls!r}.",
  166. mros=sequences,
  167. cls=cls,
  168. context=context,
  169. )
  170. yield [
  171. node
  172. for i, (node, name) in enumerate(zip(sequence, names))
  173. if name is None or last_index[name] == i
  174. ]
  175. def function_to_method(n, klass):
  176. if isinstance(n, FunctionDef):
  177. if n.type == "classmethod":
  178. return bases.BoundMethod(n, klass)
  179. if n.type == "property":
  180. return n
  181. if n.type != "staticmethod":
  182. return bases.UnboundMethod(n)
  183. return n
  184. def builtin_lookup(name):
  185. """lookup a name into the builtin module
  186. return the list of matching statements and the astroid for the builtin
  187. module
  188. """
  189. builtin_astroid = AstroidManager().ast_from_module(builtins)
  190. if name == "__dict__":
  191. return builtin_astroid, ()
  192. try:
  193. stmts = builtin_astroid.locals[name]
  194. except KeyError:
  195. stmts = ()
  196. return builtin_astroid, stmts
  197. # TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup
  198. class LocalsDictNodeNG(node_classes.LookupMixIn, node_classes.NodeNG):
  199. """this class provides locals handling common to Module, FunctionDef
  200. and ClassDef nodes, including a dict like interface for direct access
  201. to locals information
  202. """
  203. # attributes below are set by the builder module or by raw factories
  204. locals = {}
  205. """A map of the name of a local variable to the node defining the local.
  206. :type: dict(str, NodeNG)
  207. """
  208. def qname(self):
  209. """Get the 'qualified' name of the node.
  210. For example: module.name, module.class.name ...
  211. :returns: The qualified name.
  212. :rtype: str
  213. """
  214. # pylint: disable=no-member; github.com/pycqa/astroid/issues/278
  215. if self.parent is None:
  216. return self.name
  217. return f"{self.parent.frame(future=True).qname()}.{self.name}"
  218. def scope(self: T) -> T:
  219. """The first parent node defining a new scope.
  220. :returns: The first parent scope node.
  221. :rtype: Module or FunctionDef or ClassDef or Lambda or GenExpr
  222. """
  223. return self
  224. def _scope_lookup(self, node, name, offset=0):
  225. """XXX method for interfacing the scope lookup"""
  226. try:
  227. stmts = _filter_stmts(node, self.locals[name], self, offset)
  228. except KeyError:
  229. stmts = ()
  230. if stmts:
  231. return self, stmts
  232. # Handle nested scopes: since class names do not extend to nested
  233. # scopes (e.g., methods), we find the next enclosing non-class scope
  234. pscope = self.parent and self.parent.scope()
  235. while pscope is not None:
  236. if not isinstance(pscope, ClassDef):
  237. return pscope.scope_lookup(node, name)
  238. pscope = pscope.parent and pscope.parent.scope()
  239. # self is at the top level of a module, or is enclosed only by ClassDefs
  240. return builtin_lookup(name)
  241. def set_local(self, name, stmt):
  242. """Define that the given name is declared in the given statement node.
  243. .. seealso:: :meth:`scope`
  244. :param name: The name that is being defined.
  245. :type name: str
  246. :param stmt: The statement that defines the given name.
  247. :type stmt: NodeNG
  248. """
  249. # assert not stmt in self.locals.get(name, ()), (self, stmt)
  250. self.locals.setdefault(name, []).append(stmt)
  251. __setitem__ = set_local
  252. def _append_node(self, child):
  253. """append a child, linking it in the tree"""
  254. # pylint: disable=no-member; depending by the class
  255. # which uses the current class as a mixin or base class.
  256. # It's rewritten in 2.0, so it makes no sense for now
  257. # to spend development time on it.
  258. self.body.append(child)
  259. child.parent = self
  260. def add_local_node(self, child_node, name=None):
  261. """Append a child that should alter the locals of this scope node.
  262. :param child_node: The child node that will alter locals.
  263. :type child_node: NodeNG
  264. :param name: The name of the local that will be altered by
  265. the given child node.
  266. :type name: str or None
  267. """
  268. if name != "__class__":
  269. # add __class__ node as a child will cause infinite recursion later!
  270. self._append_node(child_node)
  271. self.set_local(name or child_node.name, child_node)
  272. def __getitem__(self, item):
  273. """The first node the defines the given local.
  274. :param item: The name of the locally defined object.
  275. :type item: str
  276. :raises KeyError: If the name is not defined.
  277. """
  278. return self.locals[item][0]
  279. def __iter__(self):
  280. """Iterate over the names of locals defined in this scoped node.
  281. :returns: The names of the defined locals.
  282. :rtype: iterable(str)
  283. """
  284. return iter(self.keys())
  285. def keys(self):
  286. """The names of locals defined in this scoped node.
  287. :returns: The names of the defined locals.
  288. :rtype: list(str)
  289. """
  290. return list(self.locals.keys())
  291. def values(self):
  292. """The nodes that define the locals in this scoped node.
  293. :returns: The nodes that define locals.
  294. :rtype: list(NodeNG)
  295. """
  296. # pylint: disable=consider-using-dict-items
  297. # It look like this class override items/keys/values,
  298. # probably not worth the headache
  299. return [self[key] for key in self.keys()]
  300. def items(self):
  301. """Get the names of the locals and the node that defines the local.
  302. :returns: The names of locals and their associated node.
  303. :rtype: list(tuple(str, NodeNG))
  304. """
  305. return list(zip(self.keys(), self.values()))
  306. def __contains__(self, name):
  307. """Check if a local is defined in this scope.
  308. :param name: The name of the local to check for.
  309. :type name: str
  310. :returns: True if this node has a local of the given name,
  311. False otherwise.
  312. :rtype: bool
  313. """
  314. return name in self.locals
  315. class Module(LocalsDictNodeNG):
  316. """Class representing an :class:`ast.Module` node.
  317. >>> import astroid
  318. >>> node = astroid.extract_node('import astroid')
  319. >>> node
  320. <Import l.1 at 0x7f23b2e4e5c0>
  321. >>> node.parent
  322. <Module l.0 at 0x7f23b2e4eda0>
  323. """
  324. _astroid_fields = ("body",)
  325. fromlineno = 0
  326. """The first line that this node appears on in the source code.
  327. :type: int or None
  328. """
  329. lineno: Literal[0] = 0
  330. """The line that this node appears on in the source code.
  331. """
  332. # attributes below are set by the builder module or by raw factories
  333. file = None
  334. """The path to the file that this ast has been extracted from.
  335. This will be ``None`` when the representation has been built from a
  336. built-in module.
  337. :type: str or None
  338. """
  339. file_bytes = None
  340. """The string/bytes that this ast was built from.
  341. :type: str or bytes or None
  342. """
  343. file_encoding = None
  344. """The encoding of the source file.
  345. This is used to get unicode out of a source file.
  346. Python 2 only.
  347. :type: str or None
  348. """
  349. name = None
  350. """The name of the module.
  351. :type: str or None
  352. """
  353. pure_python = None
  354. """Whether the ast was built from source.
  355. :type: bool or None
  356. """
  357. package = None
  358. """Whether the node represents a package or a module.
  359. :type: bool or None
  360. """
  361. globals = None
  362. """A map of the name of a global variable to the node defining the global.
  363. :type: dict(str, NodeNG)
  364. """
  365. # Future imports
  366. future_imports = None
  367. """The imports from ``__future__``.
  368. :type: set(str) or None
  369. """
  370. special_attributes = ModuleModel()
  371. """The names of special attributes that this module has.
  372. :type: objectmodel.ModuleModel
  373. """
  374. # names of module attributes available through the global scope
  375. scope_attrs = {"__name__", "__doc__", "__file__", "__path__", "__package__"}
  376. """The names of module attributes available through the global scope.
  377. :type: str(str)
  378. """
  379. _other_fields = (
  380. "name",
  381. "doc",
  382. "file",
  383. "path",
  384. "package",
  385. "pure_python",
  386. "future_imports",
  387. )
  388. _other_other_fields = ("locals", "globals")
  389. col_offset: None
  390. end_lineno: None
  391. end_col_offset: None
  392. parent: None
  393. def __init__(
  394. self,
  395. name,
  396. doc,
  397. file=None,
  398. path: Optional[List[str]] = None,
  399. package=None,
  400. parent=None,
  401. pure_python=True,
  402. ):
  403. """
  404. :param name: The name of the module.
  405. :type name: str
  406. :param doc: The module docstring.
  407. :type doc: str
  408. :param file: The path to the file that this ast has been extracted from.
  409. :type file: str or None
  410. :param path:
  411. :type path: Optional[List[str]]
  412. :param package: Whether the node represents a package or a module.
  413. :type package: bool or None
  414. :param parent: The parent node in the syntax tree.
  415. :type parent: NodeNG or None
  416. :param pure_python: Whether the ast was built from source.
  417. :type pure_python: bool or None
  418. """
  419. self.name = name
  420. self.doc = doc
  421. self.file = file
  422. self.path = path
  423. self.package = package
  424. self.pure_python = pure_python
  425. self.locals = self.globals = {}
  426. """A map of the name of a local variable to the node defining the local.
  427. :type: dict(str, NodeNG)
  428. """
  429. self.body = []
  430. """The contents of the module.
  431. :type: list(NodeNG) or None
  432. """
  433. self.future_imports = set()
  434. super().__init__(lineno=0, parent=parent)
  435. # pylint: enable=redefined-builtin
  436. def postinit(self, body=None):
  437. """Do some setup after initialisation.
  438. :param body: The contents of the module.
  439. :type body: list(NodeNG) or None
  440. """
  441. self.body = body
  442. def _get_stream(self):
  443. if self.file_bytes is not None:
  444. return io.BytesIO(self.file_bytes)
  445. if self.file is not None:
  446. # pylint: disable=consider-using-with
  447. stream = open(self.file, "rb")
  448. return stream
  449. return None
  450. def stream(self):
  451. """Get a stream to the underlying file or bytes.
  452. :type: file or io.BytesIO or None
  453. """
  454. return self._get_stream()
  455. def block_range(self, lineno):
  456. """Get a range from where this node starts to where this node ends.
  457. :param lineno: Unused.
  458. :type lineno: int
  459. :returns: The range of line numbers that this node belongs to.
  460. :rtype: tuple(int, int)
  461. """
  462. return self.fromlineno, self.tolineno
  463. def scope_lookup(self, node, name, offset=0):
  464. """Lookup where the given variable is assigned.
  465. :param node: The node to look for assignments up to.
  466. Any assignments after the given node are ignored.
  467. :type node: NodeNG
  468. :param name: The name of the variable to find assignments for.
  469. :type name: str
  470. :param offset: The line offset to filter statements up to.
  471. :type offset: int
  472. :returns: This scope node and the list of assignments associated to the
  473. given name according to the scope where it has been found (locals,
  474. globals or builtin).
  475. :rtype: tuple(str, list(NodeNG))
  476. """
  477. if name in self.scope_attrs and name not in self.locals:
  478. try:
  479. return self, self.getattr(name)
  480. except AttributeInferenceError:
  481. return self, ()
  482. return self._scope_lookup(node, name, offset)
  483. def pytype(self):
  484. """Get the name of the type that this node represents.
  485. :returns: The name of the type.
  486. :rtype: str
  487. """
  488. return "builtins.module"
  489. def display_type(self):
  490. """A human readable type of this node.
  491. :returns: The type of this node.
  492. :rtype: str
  493. """
  494. return "Module"
  495. def getattr(self, name, context=None, ignore_locals=False):
  496. if not name:
  497. raise AttributeInferenceError(target=self, attribute=name, context=context)
  498. result = []
  499. name_in_locals = name in self.locals
  500. if name in self.special_attributes and not ignore_locals and not name_in_locals:
  501. result = [self.special_attributes.lookup(name)]
  502. elif not ignore_locals and name_in_locals:
  503. result = self.locals[name]
  504. elif self.package:
  505. try:
  506. result = [self.import_module(name, relative_only=True)]
  507. except (AstroidBuildingError, SyntaxError) as exc:
  508. raise AttributeInferenceError(
  509. target=self, attribute=name, context=context
  510. ) from exc
  511. result = [n for n in result if not isinstance(n, node_classes.DelName)]
  512. if result:
  513. return result
  514. raise AttributeInferenceError(target=self, attribute=name, context=context)
  515. def igetattr(self, name, context=None):
  516. """Infer the possible values of the given variable.
  517. :param name: The name of the variable to infer.
  518. :type name: str
  519. :returns: The inferred possible values.
  520. :rtype: iterable(NodeNG) or None
  521. """
  522. # set lookup name since this is necessary to infer on import nodes for
  523. # instance
  524. context = copy_context(context)
  525. context.lookupname = name
  526. try:
  527. return bases._infer_stmts(self.getattr(name, context), context, frame=self)
  528. except AttributeInferenceError as error:
  529. raise InferenceError(
  530. str(error), target=self, attribute=name, context=context
  531. ) from error
  532. def fully_defined(self):
  533. """Check if this module has been build from a .py file.
  534. If so, the module contains a complete representation,
  535. including the code.
  536. :returns: True if the module has been built from a .py file.
  537. :rtype: bool
  538. """
  539. return self.file is not None and self.file.endswith(".py")
  540. @overload
  541. def statement(self, *, future: Literal[None] = ...) -> "Module":
  542. ...
  543. # pylint: disable-next=arguments-differ
  544. # https://github.com/PyCQA/pylint/issues/5264
  545. @overload
  546. def statement(self, *, future: Literal[True]) -> NoReturn:
  547. ...
  548. def statement(
  549. self, *, future: Literal[None, True] = None
  550. ) -> Union["NoReturn", "Module"]:
  551. """The first parent node, including self, marked as statement node.
  552. When called on a :class:`Module` with the future parameter this raises an error.
  553. TODO: Deprecate the future parameter and only raise StatementMissing
  554. :raises StatementMissing: If no self has no parent attribute and future is True
  555. """
  556. if future:
  557. raise StatementMissing(target=self)
  558. warnings.warn(
  559. "In astroid 3.0.0 NodeNG.statement() will return either a nodes.Statement "
  560. "or raise a StatementMissing exception. nodes.Module will no longer be "
  561. "considered a statement. This behaviour can already be triggered "
  562. "by passing 'future=True' to a statement() call.",
  563. DeprecationWarning,
  564. )
  565. return self
  566. def previous_sibling(self):
  567. """The previous sibling statement.
  568. :returns: The previous sibling statement node.
  569. :rtype: NodeNG or None
  570. """
  571. def next_sibling(self):
  572. """The next sibling statement node.
  573. :returns: The next sibling statement node.
  574. :rtype: NodeNG or None
  575. """
  576. _absolute_import_activated = True
  577. def absolute_import_activated(self):
  578. """Whether :pep:`328` absolute import behaviour has been enabled.
  579. :returns: True if :pep:`328` has been enabled, False otherwise.
  580. :rtype: bool
  581. """
  582. return self._absolute_import_activated
  583. def import_module(self, modname, relative_only=False, level=None):
  584. """Get the ast for a given module as if imported from this module.
  585. :param modname: The name of the module to "import".
  586. :type modname: str
  587. :param relative_only: Whether to only consider relative imports.
  588. :type relative_only: bool
  589. :param level: The level of relative import.
  590. :type level: int or None
  591. :returns: The imported module ast.
  592. :rtype: NodeNG
  593. """
  594. if relative_only and level is None:
  595. level = 0
  596. absmodname = self.relative_to_absolute_name(modname, level)
  597. try:
  598. return AstroidManager().ast_from_module_name(absmodname)
  599. except AstroidBuildingError:
  600. # we only want to import a sub module or package of this module,
  601. # skip here
  602. if relative_only:
  603. raise
  604. return AstroidManager().ast_from_module_name(modname)
  605. def relative_to_absolute_name(self, modname: str, level: int) -> str:
  606. """Get the absolute module name for a relative import.
  607. The relative import can be implicit or explicit.
  608. :param modname: The module name to convert.
  609. :param level: The level of relative import.
  610. :returns: The absolute module name.
  611. :raises TooManyLevelsError: When the relative import refers to a
  612. module too far above this one.
  613. """
  614. # XXX this returns non sens when called on an absolute import
  615. # like 'pylint.checkers.astroid.utils'
  616. # XXX doesn't return absolute name if self.name isn't absolute name
  617. if self.absolute_import_activated() and level is None:
  618. return modname
  619. if level:
  620. if self.package:
  621. level = level - 1
  622. package_name = self.name.rsplit(".", level)[0]
  623. elif (
  624. self.path
  625. and not os.path.exists(os.path.dirname(self.path[0]) + "/__init__.py")
  626. and os.path.exists(
  627. os.path.dirname(self.path[0]) + "/" + modname.split(".")[0]
  628. )
  629. ):
  630. level = level - 1
  631. package_name = ""
  632. else:
  633. package_name = self.name.rsplit(".", level)[0]
  634. if level and self.name.count(".") < level:
  635. raise TooManyLevelsError(level=level, name=self.name)
  636. elif self.package:
  637. package_name = self.name
  638. else:
  639. package_name = self.name.rsplit(".", 1)[0]
  640. if package_name:
  641. if not modname:
  642. return package_name
  643. return f"{package_name}.{modname}"
  644. return modname
  645. def wildcard_import_names(self):
  646. """The list of imported names when this module is 'wildcard imported'.
  647. It doesn't include the '__builtins__' name which is added by the
  648. current CPython implementation of wildcard imports.
  649. :returns: The list of imported names.
  650. :rtype: list(str)
  651. """
  652. # We separate the different steps of lookup in try/excepts
  653. # to avoid catching too many Exceptions
  654. default = [name for name in self.keys() if not name.startswith("_")]
  655. try:
  656. all_values = self["__all__"]
  657. except KeyError:
  658. return default
  659. try:
  660. explicit = next(all_values.assigned_stmts())
  661. except (InferenceError, StopIteration):
  662. return default
  663. except AttributeError:
  664. # not an assignment node
  665. # XXX infer?
  666. return default
  667. # Try our best to detect the exported name.
  668. inferred = []
  669. try:
  670. explicit = next(explicit.infer())
  671. except (InferenceError, StopIteration):
  672. return default
  673. if not isinstance(explicit, (node_classes.Tuple, node_classes.List)):
  674. return default
  675. def str_const(node):
  676. return isinstance(node, node_classes.Const) and isinstance(node.value, str)
  677. for node in explicit.elts:
  678. if str_const(node):
  679. inferred.append(node.value)
  680. else:
  681. try:
  682. inferred_node = next(node.infer())
  683. except (InferenceError, StopIteration):
  684. continue
  685. if str_const(inferred_node):
  686. inferred.append(inferred_node.value)
  687. return inferred
  688. def public_names(self):
  689. """The list of the names that are publicly available in this module.
  690. :returns: The list of public names.
  691. :rtype: list(str)
  692. """
  693. return [name for name in self.keys() if not name.startswith("_")]
  694. def bool_value(self, context=None):
  695. """Determine the boolean value of this node.
  696. :returns: The boolean value of this node.
  697. For a :class:`Module` this is always ``True``.
  698. :rtype: bool
  699. """
  700. return True
  701. def get_children(self):
  702. yield from self.body
  703. def frame(self: T, *, future: Literal[None, True] = None) -> T:
  704. """The node's frame node.
  705. A frame node is a :class:`Module`, :class:`FunctionDef`,
  706. :class:`ClassDef` or :class:`Lambda`.
  707. :returns: The node itself.
  708. """
  709. return self
  710. class ComprehensionScope(LocalsDictNodeNG):
  711. """Scoping for different types of comprehensions."""
  712. scope_lookup = LocalsDictNodeNG._scope_lookup
  713. class GeneratorExp(ComprehensionScope):
  714. """Class representing an :class:`ast.GeneratorExp` node.
  715. >>> import astroid
  716. >>> node = astroid.extract_node('(thing for thing in things if thing)')
  717. >>> node
  718. <GeneratorExp l.1 at 0x7f23b2e4e400>
  719. """
  720. _astroid_fields = ("elt", "generators")
  721. _other_other_fields = ("locals",)
  722. elt = None
  723. """The element that forms the output of the expression.
  724. :type: NodeNG or None
  725. """
  726. generators = None
  727. """The generators that are looped through.
  728. :type: list(Comprehension) or None
  729. """
  730. def __init__(
  731. self,
  732. lineno=None,
  733. col_offset=None,
  734. parent=None,
  735. *,
  736. end_lineno=None,
  737. end_col_offset=None,
  738. ):
  739. """
  740. :param lineno: The line that this node appears on in the source code.
  741. :type lineno: int or None
  742. :param col_offset: The column that this node appears on in the
  743. source code.
  744. :type col_offset: int or None
  745. :param parent: The parent node in the syntax tree.
  746. :type parent: NodeNG or None
  747. :param end_lineno: The last line this node appears on in the source code.
  748. :type end_lineno: Optional[int]
  749. :param end_col_offset: The end column this node appears on in the
  750. source code. Note: This is after the last symbol.
  751. :type end_col_offset: Optional[int]
  752. """
  753. self.locals = {}
  754. """A map of the name of a local variable to the node defining the local.
  755. :type: dict(str, NodeNG)
  756. """
  757. super().__init__(
  758. lineno=lineno,
  759. col_offset=col_offset,
  760. end_lineno=end_lineno,
  761. end_col_offset=end_col_offset,
  762. parent=parent,
  763. )
  764. def postinit(self, elt=None, generators=None):
  765. """Do some setup after initialisation.
  766. :param elt: The element that forms the output of the expression.
  767. :type elt: NodeNG or None
  768. :param generators: The generators that are looped through.
  769. :type generators: list(Comprehension) or None
  770. """
  771. self.elt = elt
  772. if generators is None:
  773. self.generators = []
  774. else:
  775. self.generators = generators
  776. def bool_value(self, context=None):
  777. """Determine the boolean value of this node.
  778. :returns: The boolean value of this node.
  779. For a :class:`GeneratorExp` this is always ``True``.
  780. :rtype: bool
  781. """
  782. return True
  783. def get_children(self):
  784. yield self.elt
  785. yield from self.generators
  786. class DictComp(ComprehensionScope):
  787. """Class representing an :class:`ast.DictComp` node.
  788. >>> import astroid
  789. >>> node = astroid.extract_node('{k:v for k, v in things if k > v}')
  790. >>> node
  791. <DictComp l.1 at 0x7f23b2e41d68>
  792. """
  793. _astroid_fields = ("key", "value", "generators")
  794. _other_other_fields = ("locals",)
  795. key = None
  796. """What produces the keys.
  797. :type: NodeNG or None
  798. """
  799. value = None
  800. """What produces the values.
  801. :type: NodeNG or None
  802. """
  803. generators = None
  804. """The generators that are looped through.
  805. :type: list(Comprehension) or None
  806. """
  807. def __init__(
  808. self,
  809. lineno=None,
  810. col_offset=None,
  811. parent=None,
  812. *,
  813. end_lineno=None,
  814. end_col_offset=None,
  815. ):
  816. """
  817. :param lineno: The line that this node appears on in the source code.
  818. :type lineno: int or None
  819. :param col_offset: The column that this node appears on in the
  820. source code.
  821. :type col_offset: int or None
  822. :param parent: The parent node in the syntax tree.
  823. :type parent: NodeNG or None
  824. :param end_lineno: The last line this node appears on in the source code.
  825. :type end_lineno: Optional[int]
  826. :param end_col_offset: The end column this node appears on in the
  827. source code. Note: This is after the last symbol.
  828. :type end_col_offset: Optional[int]
  829. """
  830. self.locals = {}
  831. """A map of the name of a local variable to the node defining the local.
  832. :type: dict(str, NodeNG)
  833. """
  834. super().__init__(
  835. lineno=lineno,
  836. col_offset=col_offset,
  837. end_lineno=end_lineno,
  838. end_col_offset=end_col_offset,
  839. parent=parent,
  840. )
  841. def postinit(self, key=None, value=None, generators=None):
  842. """Do some setup after initialisation.
  843. :param key: What produces the keys.
  844. :type key: NodeNG or None
  845. :param value: What produces the values.
  846. :type value: NodeNG or None
  847. :param generators: The generators that are looped through.
  848. :type generators: list(Comprehension) or None
  849. """
  850. self.key = key
  851. self.value = value
  852. if generators is None:
  853. self.generators = []
  854. else:
  855. self.generators = generators
  856. def bool_value(self, context=None):
  857. """Determine the boolean value of this node.
  858. :returns: The boolean value of this node.
  859. For a :class:`DictComp` this is always :class:`Uninferable`.
  860. :rtype: Uninferable
  861. """
  862. return util.Uninferable
  863. def get_children(self):
  864. yield self.key
  865. yield self.value
  866. yield from self.generators
  867. class SetComp(ComprehensionScope):
  868. """Class representing an :class:`ast.SetComp` node.
  869. >>> import astroid
  870. >>> node = astroid.extract_node('{thing for thing in things if thing}')
  871. >>> node
  872. <SetComp l.1 at 0x7f23b2e41898>
  873. """
  874. _astroid_fields = ("elt", "generators")
  875. _other_other_fields = ("locals",)
  876. elt = None
  877. """The element that forms the output of the expression.
  878. :type: NodeNG or None
  879. """
  880. generators = None
  881. """The generators that are looped through.
  882. :type: list(Comprehension) or None
  883. """
  884. def __init__(
  885. self,
  886. lineno=None,
  887. col_offset=None,
  888. parent=None,
  889. *,
  890. end_lineno=None,
  891. end_col_offset=None,
  892. ):
  893. """
  894. :param lineno: The line that this node appears on in the source code.
  895. :type lineno: int or None
  896. :param col_offset: The column that this node appears on in the
  897. source code.
  898. :type col_offset: int or None
  899. :param parent: The parent node in the syntax tree.
  900. :type parent: NodeNG or None
  901. :param end_lineno: The last line this node appears on in the source code.
  902. :type end_lineno: Optional[int]
  903. :param end_col_offset: The end column this node appears on in the
  904. source code. Note: This is after the last symbol.
  905. :type end_col_offset: Optional[int]
  906. """
  907. self.locals = {}
  908. """A map of the name of a local variable to the node defining the local.
  909. :type: dict(str, NodeNG)
  910. """
  911. super().__init__(
  912. lineno=lineno,
  913. col_offset=col_offset,
  914. end_lineno=end_lineno,
  915. end_col_offset=end_col_offset,
  916. parent=parent,
  917. )
  918. def postinit(self, elt=None, generators=None):
  919. """Do some setup after initialisation.
  920. :param elt: The element that forms the output of the expression.
  921. :type elt: NodeNG or None
  922. :param generators: The generators that are looped through.
  923. :type generators: list(Comprehension) or None
  924. """
  925. self.elt = elt
  926. if generators is None:
  927. self.generators = []
  928. else:
  929. self.generators = generators
  930. def bool_value(self, context=None):
  931. """Determine the boolean value of this node.
  932. :returns: The boolean value of this node.
  933. For a :class:`SetComp` this is always :class:`Uninferable`.
  934. :rtype: Uninferable
  935. """
  936. return util.Uninferable
  937. def get_children(self):
  938. yield self.elt
  939. yield from self.generators
  940. class _ListComp(node_classes.NodeNG):
  941. """Class representing an :class:`ast.ListComp` node.
  942. >>> import astroid
  943. >>> node = astroid.extract_node('[thing for thing in things if thing]')
  944. >>> node
  945. <ListComp l.1 at 0x7f23b2e418d0>
  946. """
  947. _astroid_fields = ("elt", "generators")
  948. elt = None
  949. """The element that forms the output of the expression.
  950. :type: NodeNG or None
  951. """
  952. generators = None
  953. """The generators that are looped through.
  954. :type: list(Comprehension) or None
  955. """
  956. def postinit(self, elt=None, generators=None):
  957. """Do some setup after initialisation.
  958. :param elt: The element that forms the output of the expression.
  959. :type elt: NodeNG or None
  960. :param generators: The generators that are looped through.
  961. :type generators: list(Comprehension) or None
  962. """
  963. self.elt = elt
  964. self.generators = generators
  965. def bool_value(self, context=None):
  966. """Determine the boolean value of this node.
  967. :returns: The boolean value of this node.
  968. For a :class:`ListComp` this is always :class:`Uninferable`.
  969. :rtype: Uninferable
  970. """
  971. return util.Uninferable
  972. def get_children(self):
  973. yield self.elt
  974. yield from self.generators
  975. class ListComp(_ListComp, ComprehensionScope):
  976. """Class representing an :class:`ast.ListComp` node.
  977. >>> import astroid
  978. >>> node = astroid.extract_node('[thing for thing in things if thing]')
  979. >>> node
  980. <ListComp l.1 at 0x7f23b2e418d0>
  981. """
  982. _other_other_fields = ("locals",)
  983. def __init__(
  984. self,
  985. lineno=None,
  986. col_offset=None,
  987. parent=None,
  988. *,
  989. end_lineno=None,
  990. end_col_offset=None,
  991. ):
  992. self.locals = {}
  993. """A map of the name of a local variable to the node defining it.
  994. :type: dict(str, NodeNG)
  995. """
  996. super().__init__(
  997. lineno=lineno,
  998. col_offset=col_offset,
  999. end_lineno=end_lineno,
  1000. end_col_offset=end_col_offset,
  1001. parent=parent,
  1002. )
  1003. def _infer_decorator_callchain(node):
  1004. """Detect decorator call chaining and see if the end result is a
  1005. static or a classmethod.
  1006. """
  1007. if not isinstance(node, FunctionDef):
  1008. return None
  1009. if not node.parent:
  1010. return None
  1011. try:
  1012. result = next(node.infer_call_result(node.parent), None)
  1013. except InferenceError:
  1014. return None
  1015. if isinstance(result, bases.Instance):
  1016. result = result._proxied
  1017. if isinstance(result, ClassDef):
  1018. if result.is_subtype_of("builtins.classmethod"):
  1019. return "classmethod"
  1020. if result.is_subtype_of("builtins.staticmethod"):
  1021. return "staticmethod"
  1022. if isinstance(result, FunctionDef):
  1023. if not result.decorators:
  1024. return None
  1025. # Determine if this function is decorated with one of the builtin descriptors we want.
  1026. for decorator in result.decorators.nodes:
  1027. if isinstance(decorator, node_classes.Name):
  1028. if decorator.name in BUILTIN_DESCRIPTORS:
  1029. return decorator.name
  1030. if (
  1031. isinstance(decorator, node_classes.Attribute)
  1032. and isinstance(decorator.expr, node_classes.Name)
  1033. and decorator.expr.name == "builtins"
  1034. and decorator.attrname in BUILTIN_DESCRIPTORS
  1035. ):
  1036. return decorator.attrname
  1037. return None
  1038. class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG):
  1039. """Class representing an :class:`ast.Lambda` node.
  1040. >>> import astroid
  1041. >>> node = astroid.extract_node('lambda arg: arg + 1')
  1042. >>> node
  1043. <Lambda.<lambda> l.1 at 0x7f23b2e41518>
  1044. """
  1045. _astroid_fields = ("args", "body")
  1046. _other_other_fields = ("locals",)
  1047. name = "<lambda>"
  1048. is_lambda = True
  1049. def implicit_parameters(self):
  1050. return 0
  1051. # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod'
  1052. @property
  1053. def type(self):
  1054. """Whether this is a method or function.
  1055. :returns: 'method' if this is a method, 'function' otherwise.
  1056. :rtype: str
  1057. """
  1058. if self.args.arguments and self.args.arguments[0].name == "self":
  1059. if isinstance(self.parent.scope(), ClassDef):
  1060. return "method"
  1061. return "function"
  1062. def __init__(
  1063. self,
  1064. lineno=None,
  1065. col_offset=None,
  1066. parent=None,
  1067. *,
  1068. end_lineno=None,
  1069. end_col_offset=None,
  1070. ):
  1071. """
  1072. :param lineno: The line that this node appears on in the source code.
  1073. :type lineno: int or None
  1074. :param col_offset: The column that this node appears on in the
  1075. source code.
  1076. :type col_offset: int or None
  1077. :param parent: The parent node in the syntax tree.
  1078. :type parent: NodeNG or None
  1079. :param end_lineno: The last line this node appears on in the source code.
  1080. :type end_lineno: Optional[int]
  1081. :param end_col_offset: The end column this node appears on in the
  1082. source code. Note: This is after the last symbol.
  1083. :type end_col_offset: Optional[int]
  1084. """
  1085. self.locals = {}
  1086. """A map of the name of a local variable to the node defining it.
  1087. :type: dict(str, NodeNG)
  1088. """
  1089. self.args: Arguments
  1090. """The arguments that the function takes."""
  1091. self.body = []
  1092. """The contents of the function body.
  1093. :type: list(NodeNG)
  1094. """
  1095. super().__init__(
  1096. lineno=lineno,
  1097. col_offset=col_offset,
  1098. end_lineno=end_lineno,
  1099. end_col_offset=end_col_offset,
  1100. parent=parent,
  1101. )
  1102. def postinit(self, args: Arguments, body):
  1103. """Do some setup after initialisation.
  1104. :param args: The arguments that the function takes.
  1105. :param body: The contents of the function body.
  1106. :type body: list(NodeNG)
  1107. """
  1108. self.args = args
  1109. self.body = body
  1110. def pytype(self):
  1111. """Get the name of the type that this node represents.
  1112. :returns: The name of the type.
  1113. :rtype: str
  1114. """
  1115. if "method" in self.type:
  1116. return "builtins.instancemethod"
  1117. return "builtins.function"
  1118. def display_type(self):
  1119. """A human readable type of this node.
  1120. :returns: The type of this node.
  1121. :rtype: str
  1122. """
  1123. if "method" in self.type:
  1124. return "Method"
  1125. return "Function"
  1126. def callable(self):
  1127. """Whether this node defines something that is callable.
  1128. :returns: True if this defines something that is callable,
  1129. False otherwise.
  1130. For a :class:`Lambda` this is always ``True``.
  1131. :rtype: bool
  1132. """
  1133. return True
  1134. def argnames(self):
  1135. """Get the names of each of the arguments.
  1136. :returns: The names of the arguments.
  1137. :rtype: list(str)
  1138. """
  1139. if self.args.arguments: # maybe None with builtin functions
  1140. names = _rec_get_names(self.args.arguments)
  1141. else:
  1142. names = []
  1143. if self.args.vararg:
  1144. names.append(self.args.vararg)
  1145. if self.args.kwarg:
  1146. names.append(self.args.kwarg)
  1147. return names
  1148. def infer_call_result(self, caller, context=None):
  1149. """Infer what the function returns when called.
  1150. :param caller: Unused
  1151. :type caller: object
  1152. """
  1153. # pylint: disable=no-member; github.com/pycqa/astroid/issues/291
  1154. # args is in fact redefined later on by postinit. Can't be changed
  1155. # to None due to a strong interaction between Lambda and FunctionDef.
  1156. return self.body.infer(context)
  1157. def scope_lookup(self, node, name, offset=0):
  1158. """Lookup where the given names is assigned.
  1159. :param node: The node to look for assignments up to.
  1160. Any assignments after the given node are ignored.
  1161. :type node: NodeNG
  1162. :param name: The name to find assignments for.
  1163. :type name: str
  1164. :param offset: The line offset to filter statements up to.
  1165. :type offset: int
  1166. :returns: This scope node and the list of assignments associated to the
  1167. given name according to the scope where it has been found (locals,
  1168. globals or builtin).
  1169. :rtype: tuple(str, list(NodeNG))
  1170. """
  1171. if node in self.args.defaults or node in self.args.kw_defaults:
  1172. frame = self.parent.frame(future=True)
  1173. # line offset to avoid that def func(f=func) resolve the default
  1174. # value to the defined function
  1175. offset = -1
  1176. else:
  1177. # check this is not used in function decorators
  1178. frame = self
  1179. return frame._scope_lookup(node, name, offset)
  1180. def bool_value(self, context=None):
  1181. """Determine the boolean value of this node.
  1182. :returns: The boolean value of this node.
  1183. For a :class:`Lambda` this is always ``True``.
  1184. :rtype: bool
  1185. """
  1186. return True
  1187. def get_children(self):
  1188. yield self.args
  1189. yield self.body
  1190. def frame(self: T, *, future: Literal[None, True] = None) -> T:
  1191. """The node's frame node.
  1192. A frame node is a :class:`Module`, :class:`FunctionDef`,
  1193. :class:`ClassDef` or :class:`Lambda`.
  1194. :returns: The node itself.
  1195. """
  1196. return self
  1197. class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda):
  1198. """Class representing an :class:`ast.FunctionDef`.
  1199. >>> import astroid
  1200. >>> node = astroid.extract_node('''
  1201. ... def my_func(arg):
  1202. ... return arg + 1
  1203. ... ''')
  1204. >>> node
  1205. <FunctionDef.my_func l.2 at 0x7f23b2e71e10>
  1206. """
  1207. _astroid_fields = ("decorators", "args", "returns", "body")
  1208. _multi_line_block_fields = ("body",)
  1209. returns = None
  1210. decorators = None
  1211. """The decorators that are applied to this method or function.
  1212. :type: Decorators or None
  1213. """
  1214. special_attributes = FunctionModel()
  1215. """The names of special attributes that this function has.
  1216. :type: objectmodel.FunctionModel
  1217. """
  1218. is_function = True
  1219. """Whether this node indicates a function.
  1220. For a :class:`FunctionDef` this is always ``True``.
  1221. :type: bool
  1222. """
  1223. type_annotation = None
  1224. """If present, this will contain the type annotation passed by a type comment
  1225. :type: NodeNG or None
  1226. """
  1227. type_comment_args = None
  1228. """
  1229. If present, this will contain the type annotation for arguments
  1230. passed by a type comment
  1231. """
  1232. type_comment_returns = None
  1233. """If present, this will contain the return type annotation, passed by a type comment"""
  1234. # attributes below are set by the builder module or by raw factories
  1235. _other_fields = ("name", "doc")
  1236. _other_other_fields = (
  1237. "locals",
  1238. "_type",
  1239. "type_comment_returns",
  1240. "type_comment_args",
  1241. )
  1242. _type = None
  1243. def __init__(
  1244. self,
  1245. name=None,
  1246. doc=None,
  1247. lineno=None,
  1248. col_offset=None,
  1249. parent=None,
  1250. *,
  1251. end_lineno=None,
  1252. end_col_offset=None,
  1253. ):
  1254. """
  1255. :param name: The name of the function.
  1256. :type name: str or None
  1257. :param doc: The function's docstring.
  1258. :type doc: str or None
  1259. :param lineno: The line that this node appears on in the source code.
  1260. :type lineno: int or None
  1261. :param col_offset: The column that this node appears on in the
  1262. source code.
  1263. :type col_offset: int or None
  1264. :param parent: The parent node in the syntax tree.
  1265. :type parent: NodeNG or None
  1266. :param end_lineno: The last line this node appears on in the source code.
  1267. :type end_lineno: Optional[int]
  1268. :param end_col_offset: The end column this node appears on in the
  1269. source code. Note: This is after the last symbol.
  1270. :type end_col_offset: Optional[int]
  1271. """
  1272. self.name = name
  1273. """The name of the function.
  1274. :type name: str or None
  1275. """
  1276. self.doc = doc
  1277. """The function's docstring.
  1278. :type doc: str or None
  1279. """
  1280. self.instance_attrs = {}
  1281. super().__init__(
  1282. lineno=lineno,
  1283. col_offset=col_offset,
  1284. end_lineno=end_lineno,
  1285. end_col_offset=end_col_offset,
  1286. parent=parent,
  1287. )
  1288. if parent:
  1289. frame = parent.frame(future=True)
  1290. frame.set_local(name, self)
  1291. # pylint: disable=arguments-differ; different than Lambdas
  1292. def postinit(
  1293. self,
  1294. args: Arguments,
  1295. body,
  1296. decorators=None,
  1297. returns=None,
  1298. type_comment_returns=None,
  1299. type_comment_args=None,
  1300. ):
  1301. """Do some setup after initialisation.
  1302. :param args: The arguments that the function takes.
  1303. :param body: The contents of the function body.
  1304. :type body: list(NodeNG)
  1305. :param decorators: The decorators that are applied to this
  1306. method or function.
  1307. :type decorators: Decorators or None
  1308. :params type_comment_returns:
  1309. The return type annotation passed via a type comment.
  1310. :params type_comment_args:
  1311. The args type annotation passed via a type comment.
  1312. """
  1313. self.args = args
  1314. self.body = body
  1315. self.decorators = decorators
  1316. self.returns = returns
  1317. self.type_comment_returns = type_comment_returns
  1318. self.type_comment_args = type_comment_args
  1319. @decorators_mod.cachedproperty
  1320. def extra_decorators(self):
  1321. """The extra decorators that this function can have.
  1322. Additional decorators are considered when they are used as
  1323. assignments, as in ``method = staticmethod(method)``.
  1324. The property will return all the callables that are used for
  1325. decoration.
  1326. :type: list(NodeNG)
  1327. """
  1328. frame = self.parent.frame(future=True)
  1329. if not isinstance(frame, ClassDef):
  1330. return []
  1331. decorators = []
  1332. for assign in frame._get_assign_nodes():
  1333. if isinstance(assign.value, node_classes.Call) and isinstance(
  1334. assign.value.func, node_classes.Name
  1335. ):
  1336. for assign_node in assign.targets:
  1337. if not isinstance(assign_node, node_classes.AssignName):
  1338. # Support only `name = callable(name)`
  1339. continue
  1340. if assign_node.name != self.name:
  1341. # Interested only in the assignment nodes that
  1342. # decorates the current method.
  1343. continue
  1344. try:
  1345. meth = frame[self.name]
  1346. except KeyError:
  1347. continue
  1348. else:
  1349. # Must be a function and in the same frame as the
  1350. # original method.
  1351. if (
  1352. isinstance(meth, FunctionDef)
  1353. and assign_node.frame(future=True) == frame
  1354. ):
  1355. decorators.append(assign.value)
  1356. return decorators
  1357. @decorators_mod.cachedproperty
  1358. def type(
  1359. self,
  1360. ): # pylint: disable=invalid-overridden-method,too-many-return-statements
  1361. """The function type for this node.
  1362. Possible values are: method, function, staticmethod, classmethod.
  1363. :type: str
  1364. """
  1365. for decorator in self.extra_decorators:
  1366. if decorator.func.name in BUILTIN_DESCRIPTORS:
  1367. return decorator.func.name
  1368. frame = self.parent.frame(future=True)
  1369. type_name = "function"
  1370. if isinstance(frame, ClassDef):
  1371. if self.name == "__new__":
  1372. return "classmethod"
  1373. if self.name == "__init_subclass__":
  1374. return "classmethod"
  1375. if self.name == "__class_getitem__":
  1376. return "classmethod"
  1377. type_name = "method"
  1378. if not self.decorators:
  1379. return type_name
  1380. for node in self.decorators.nodes:
  1381. if isinstance(node, node_classes.Name):
  1382. if node.name in BUILTIN_DESCRIPTORS:
  1383. return node.name
  1384. if (
  1385. isinstance(node, node_classes.Attribute)
  1386. and isinstance(node.expr, node_classes.Name)
  1387. and node.expr.name == "builtins"
  1388. and node.attrname in BUILTIN_DESCRIPTORS
  1389. ):
  1390. return node.attrname
  1391. if isinstance(node, node_classes.Call):
  1392. # Handle the following case:
  1393. # @some_decorator(arg1, arg2)
  1394. # def func(...)
  1395. #
  1396. try:
  1397. current = next(node.func.infer())
  1398. except (InferenceError, StopIteration):
  1399. continue
  1400. _type = _infer_decorator_callchain(current)
  1401. if _type is not None:
  1402. return _type
  1403. try:
  1404. for inferred in node.infer():
  1405. # Check to see if this returns a static or a class method.
  1406. _type = _infer_decorator_callchain(inferred)
  1407. if _type is not None:
  1408. return _type
  1409. if not isinstance(inferred, ClassDef):
  1410. continue
  1411. for ancestor in inferred.ancestors():
  1412. if not isinstance(ancestor, ClassDef):
  1413. continue
  1414. if ancestor.is_subtype_of("builtins.classmethod"):
  1415. return "classmethod"
  1416. if ancestor.is_subtype_of("builtins.staticmethod"):
  1417. return "staticmethod"
  1418. except InferenceError:
  1419. pass
  1420. return type_name
  1421. @decorators_mod.cachedproperty
  1422. def fromlineno(self):
  1423. """The first line that this node appears on in the source code.
  1424. :type: int or None
  1425. """
  1426. # lineno is the line number of the first decorator, we want the def
  1427. # statement lineno
  1428. lineno = self.lineno
  1429. if self.decorators is not None:
  1430. lineno += sum(
  1431. node.tolineno - node.lineno + 1 for node in self.decorators.nodes
  1432. )
  1433. return lineno
  1434. @decorators_mod.cachedproperty
  1435. def blockstart_tolineno(self):
  1436. """The line on which the beginning of this block ends.
  1437. :type: int
  1438. """
  1439. return self.args.tolineno
  1440. def block_range(self, lineno):
  1441. """Get a range from the given line number to where this node ends.
  1442. :param lineno: Unused.
  1443. :type lineno: int
  1444. :returns: The range of line numbers that this node belongs to,
  1445. :rtype: tuple(int, int)
  1446. """
  1447. return self.fromlineno, self.tolineno
  1448. def getattr(self, name, context=None):
  1449. """this method doesn't look in the instance_attrs dictionary since it's
  1450. done by an Instance proxy at inference time.
  1451. """
  1452. if not name:
  1453. raise AttributeInferenceError(target=self, attribute=name, context=context)
  1454. found_attrs = []
  1455. if name in self.instance_attrs:
  1456. found_attrs = self.instance_attrs[name]
  1457. if name in self.special_attributes:
  1458. found_attrs.append(self.special_attributes.lookup(name))
  1459. if found_attrs:
  1460. return found_attrs
  1461. raise AttributeInferenceError(target=self, attribute=name)
  1462. def igetattr(self, name, context=None):
  1463. """Inferred getattr, which returns an iterator of inferred statements."""
  1464. try:
  1465. return bases._infer_stmts(self.getattr(name, context), context, frame=self)
  1466. except AttributeInferenceError as error:
  1467. raise InferenceError(
  1468. str(error), target=self, attribute=name, context=context
  1469. ) from error
  1470. def is_method(self):
  1471. """Check if this function node represents a method.
  1472. :returns: True if this is a method, False otherwise.
  1473. :rtype: bool
  1474. """
  1475. # check we are defined in a ClassDef, because this is usually expected
  1476. # (e.g. pylint...) when is_method() return True
  1477. return self.type != "function" and isinstance(
  1478. self.parent.frame(future=True), ClassDef
  1479. )
  1480. @decorators_mod.cached
  1481. def decoratornames(self, context=None):
  1482. """Get the qualified names of each of the decorators on this function.
  1483. :param context:
  1484. An inference context that can be passed to inference functions
  1485. :returns: The names of the decorators.
  1486. :rtype: set(str)
  1487. """
  1488. result = set()
  1489. decoratornodes = []
  1490. if self.decorators is not None:
  1491. decoratornodes += self.decorators.nodes
  1492. decoratornodes += self.extra_decorators
  1493. for decnode in decoratornodes:
  1494. try:
  1495. for infnode in decnode.infer(context=context):
  1496. result.add(infnode.qname())
  1497. except InferenceError:
  1498. continue
  1499. return result
  1500. def is_bound(self):
  1501. """Check if the function is bound to an instance or class.
  1502. :returns: True if the function is bound to an instance or class,
  1503. False otherwise.
  1504. :rtype: bool
  1505. """
  1506. return self.type == "classmethod"
  1507. def is_abstract(self, pass_is_abstract=True, any_raise_is_abstract=False):
  1508. """Check if the method is abstract.
  1509. A method is considered abstract if any of the following is true:
  1510. * The only statement is 'raise NotImplementedError'
  1511. * The only statement is 'raise <SomeException>' and any_raise_is_abstract is True
  1512. * The only statement is 'pass' and pass_is_abstract is True
  1513. * The method is annotated with abc.astractproperty/abc.abstractmethod
  1514. :returns: True if the method is abstract, False otherwise.
  1515. :rtype: bool
  1516. """
  1517. if self.decorators:
  1518. for node in self.decorators.nodes:
  1519. try:
  1520. inferred = next(node.infer())
  1521. except (InferenceError, StopIteration):
  1522. continue
  1523. if inferred and inferred.qname() in {
  1524. "abc.abstractproperty",
  1525. "abc.abstractmethod",
  1526. }:
  1527. return True
  1528. for child_node in self.body:
  1529. if isinstance(child_node, node_classes.Raise):
  1530. if any_raise_is_abstract:
  1531. return True
  1532. if child_node.raises_not_implemented():
  1533. return True
  1534. return pass_is_abstract and isinstance(child_node, node_classes.Pass)
  1535. # empty function is the same as function with a single "pass" statement
  1536. if pass_is_abstract:
  1537. return True
  1538. def is_generator(self):
  1539. """Check if this is a generator function.
  1540. :returns: True is this is a generator function, False otherwise.
  1541. :rtype: bool
  1542. """
  1543. return bool(next(self._get_yield_nodes_skip_lambdas(), False))
  1544. def infer_yield_result(self, context=None):
  1545. """Infer what the function yields when called
  1546. :returns: What the function yields
  1547. :rtype: iterable(NodeNG or Uninferable) or None
  1548. """
  1549. # pylint: disable=not-an-iterable
  1550. # https://github.com/PyCQA/astroid/issues/1015
  1551. for yield_ in self.nodes_of_class(node_classes.Yield):
  1552. if yield_.value is None:
  1553. const = node_classes.Const(None)
  1554. const.parent = yield_
  1555. const.lineno = yield_.lineno
  1556. yield const
  1557. elif yield_.scope() == self:
  1558. yield from yield_.value.infer(context=context)
  1559. def infer_call_result(self, caller=None, context=None):
  1560. """Infer what the function returns when called.
  1561. :returns: What the function returns.
  1562. :rtype: iterable(NodeNG or Uninferable) or None
  1563. """
  1564. if self.is_generator():
  1565. if isinstance(self, AsyncFunctionDef):
  1566. generator_cls = bases.AsyncGenerator
  1567. else:
  1568. generator_cls = bases.Generator
  1569. result = generator_cls(self, generator_initial_context=context)
  1570. yield result
  1571. return
  1572. # This is really a gigantic hack to work around metaclass generators
  1573. # that return transient class-generating functions. Pylint's AST structure
  1574. # cannot handle a base class object that is only used for calling __new__,
  1575. # but does not contribute to the inheritance structure itself. We inject
  1576. # a fake class into the hierarchy here for several well-known metaclass
  1577. # generators, and filter it out later.
  1578. if (
  1579. self.name == "with_metaclass"
  1580. and len(self.args.args) == 1
  1581. and self.args.vararg is not None
  1582. ):
  1583. metaclass = next(caller.args[0].infer(context), None)
  1584. if isinstance(metaclass, ClassDef):
  1585. try:
  1586. class_bases = [next(arg.infer(context)) for arg in caller.args[1:]]
  1587. except StopIteration as e:
  1588. raise InferenceError(node=caller.args[1:], context=context) from e
  1589. new_class = ClassDef(name="temporary_class")
  1590. new_class.hide = True
  1591. new_class.parent = self
  1592. new_class.postinit(
  1593. bases=[base for base in class_bases if base != util.Uninferable],
  1594. body=[],
  1595. decorators=[],
  1596. metaclass=metaclass,
  1597. )
  1598. yield new_class
  1599. return
  1600. returns = self._get_return_nodes_skip_functions()
  1601. first_return = next(returns, None)
  1602. if not first_return:
  1603. if self.body:
  1604. if self.is_abstract(pass_is_abstract=True, any_raise_is_abstract=True):
  1605. yield util.Uninferable
  1606. else:
  1607. yield node_classes.Const(None)
  1608. return
  1609. raise InferenceError("The function does not have any return statements")
  1610. for returnnode in itertools.chain((first_return,), returns):
  1611. if returnnode.value is None:
  1612. yield node_classes.Const(None)
  1613. else:
  1614. try:
  1615. yield from returnnode.value.infer(context)
  1616. except InferenceError:
  1617. yield util.Uninferable
  1618. def bool_value(self, context=None):
  1619. """Determine the boolean value of this node.
  1620. :returns: The boolean value of this node.
  1621. For a :class:`FunctionDef` this is always ``True``.
  1622. :rtype: bool
  1623. """
  1624. return True
  1625. def get_children(self):
  1626. if self.decorators is not None:
  1627. yield self.decorators
  1628. yield self.args
  1629. if self.returns is not None:
  1630. yield self.returns
  1631. yield from self.body
  1632. def scope_lookup(self, node, name, offset=0):
  1633. """Lookup where the given name is assigned."""
  1634. if name == "__class__":
  1635. # __class__ is an implicit closure reference created by the compiler
  1636. # if any methods in a class body refer to either __class__ or super.
  1637. # In our case, we want to be able to look it up in the current scope
  1638. # when `__class__` is being used.
  1639. frame = self.parent.frame(future=True)
  1640. if isinstance(frame, ClassDef):
  1641. return self, [frame]
  1642. return super().scope_lookup(node, name, offset)
  1643. def frame(self: T, *, future: Literal[None, True] = None) -> T:
  1644. """The node's frame node.
  1645. A frame node is a :class:`Module`, :class:`FunctionDef`,
  1646. :class:`ClassDef` or :class:`Lambda`.
  1647. :returns: The node itself.
  1648. """
  1649. return self
  1650. class AsyncFunctionDef(FunctionDef):
  1651. """Class representing an :class:`ast.FunctionDef` node.
  1652. A :class:`AsyncFunctionDef` is an asynchronous function
  1653. created with the `async` keyword.
  1654. >>> import astroid
  1655. >>> node = astroid.extract_node('''
  1656. async def func(things):
  1657. async for thing in things:
  1658. print(thing)
  1659. ''')
  1660. >>> node
  1661. <AsyncFunctionDef.func l.2 at 0x7f23b2e416d8>
  1662. >>> node.body[0]
  1663. <AsyncFor l.3 at 0x7f23b2e417b8>
  1664. """
  1665. def _rec_get_names(args, names=None):
  1666. """return a list of all argument names"""
  1667. if names is None:
  1668. names = []
  1669. for arg in args:
  1670. if isinstance(arg, node_classes.Tuple):
  1671. _rec_get_names(arg.elts, names)
  1672. else:
  1673. names.append(arg.name)
  1674. return names
  1675. def _is_metaclass(klass, seen=None):
  1676. """Return if the given class can be
  1677. used as a metaclass.
  1678. """
  1679. if klass.name == "type":
  1680. return True
  1681. if seen is None:
  1682. seen = set()
  1683. for base in klass.bases:
  1684. try:
  1685. for baseobj in base.infer():
  1686. baseobj_name = baseobj.qname()
  1687. if baseobj_name in seen:
  1688. continue
  1689. seen.add(baseobj_name)
  1690. if isinstance(baseobj, bases.Instance):
  1691. # not abstract
  1692. return False
  1693. if baseobj is util.Uninferable:
  1694. continue
  1695. if baseobj is klass:
  1696. continue
  1697. if not isinstance(baseobj, ClassDef):
  1698. continue
  1699. if baseobj._type == "metaclass":
  1700. return True
  1701. if _is_metaclass(baseobj, seen):
  1702. return True
  1703. except InferenceError:
  1704. continue
  1705. return False
  1706. def _class_type(klass, ancestors=None):
  1707. """return a ClassDef node type to differ metaclass and exception
  1708. from 'regular' classes
  1709. """
  1710. # XXX we have to store ancestors in case we have an ancestor loop
  1711. if klass._type is not None:
  1712. return klass._type
  1713. if _is_metaclass(klass):
  1714. klass._type = "metaclass"
  1715. elif klass.name.endswith("Exception"):
  1716. klass._type = "exception"
  1717. else:
  1718. if ancestors is None:
  1719. ancestors = set()
  1720. klass_name = klass.qname()
  1721. if klass_name in ancestors:
  1722. # XXX we are in loop ancestors, and have found no type
  1723. klass._type = "class"
  1724. return "class"
  1725. ancestors.add(klass_name)
  1726. for base in klass.ancestors(recurs=False):
  1727. name = _class_type(base, ancestors)
  1728. if name != "class":
  1729. if name == "metaclass" and not _is_metaclass(klass):
  1730. # don't propagate it if the current class
  1731. # can't be a metaclass
  1732. continue
  1733. klass._type = base.type
  1734. break
  1735. if klass._type is None:
  1736. klass._type = "class"
  1737. return klass._type
  1738. def get_wrapping_class(node):
  1739. """Get the class that wraps the given node.
  1740. We consider that a class wraps a node if the class
  1741. is a parent for the said node.
  1742. :returns: The class that wraps the given node
  1743. :rtype: ClassDef or None
  1744. """
  1745. klass = node.frame(future=True)
  1746. while klass is not None and not isinstance(klass, ClassDef):
  1747. if klass.parent is None:
  1748. klass = None
  1749. else:
  1750. klass = klass.parent.frame(future=True)
  1751. return klass
  1752. class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement):
  1753. """Class representing an :class:`ast.ClassDef` node.
  1754. >>> import astroid
  1755. >>> node = astroid.extract_node('''
  1756. class Thing:
  1757. def my_meth(self, arg):
  1758. return arg + self.offset
  1759. ''')
  1760. >>> node
  1761. <ClassDef.Thing l.2 at 0x7f23b2e9e748>
  1762. """
  1763. # some of the attributes below are set by the builder module or
  1764. # by a raw factories
  1765. # a dictionary of class instances attributes
  1766. _astroid_fields = ("decorators", "bases", "keywords", "body") # name
  1767. decorators = None
  1768. """The decorators that are applied to this class.
  1769. :type: Decorators or None
  1770. """
  1771. special_attributes = ClassModel()
  1772. """The names of special attributes that this class has.
  1773. :type: objectmodel.ClassModel
  1774. """
  1775. _type = None
  1776. _metaclass_hack = False
  1777. hide = False
  1778. type = property(
  1779. _class_type,
  1780. doc=(
  1781. "The class type for this node.\n\n"
  1782. "Possible values are: class, metaclass, exception.\n\n"
  1783. ":type: str"
  1784. ),
  1785. )
  1786. _other_fields = ("name", "doc")
  1787. _other_other_fields = ("locals", "_newstyle")
  1788. _newstyle = None
  1789. def __init__(
  1790. self,
  1791. name=None,
  1792. doc=None,
  1793. lineno=None,
  1794. col_offset=None,
  1795. parent=None,
  1796. *,
  1797. end_lineno=None,
  1798. end_col_offset=None,
  1799. ):
  1800. """
  1801. :param name: The name of the class.
  1802. :type name: str or None
  1803. :param doc: The function's docstring.
  1804. :type doc: str or None
  1805. :param lineno: The line that this node appears on in the source code.
  1806. :type lineno: int or None
  1807. :param col_offset: The column that this node appears on in the
  1808. source code.
  1809. :type col_offset: int or None
  1810. :param parent: The parent node in the syntax tree.
  1811. :type parent: NodeNG or None
  1812. :param end_lineno: The last line this node appears on in the source code.
  1813. :type end_lineno: Optional[int]
  1814. :param end_col_offset: The end column this node appears on in the
  1815. source code. Note: This is after the last symbol.
  1816. :type end_col_offset: Optional[int]
  1817. """
  1818. self.instance_attrs = {}
  1819. self.locals = {}
  1820. """A map of the name of a local variable to the node defining it.
  1821. :type: dict(str, NodeNG)
  1822. """
  1823. self.keywords = []
  1824. """The keywords given to the class definition.
  1825. This is usually for :pep:`3115` style metaclass declaration.
  1826. :type: list(Keyword) or None
  1827. """
  1828. self.bases = []
  1829. """What the class inherits from.
  1830. :type: list(NodeNG)
  1831. """
  1832. self.body = []
  1833. """The contents of the class body.
  1834. :type: list(NodeNG)
  1835. """
  1836. self.name = name
  1837. """The name of the class.
  1838. :type name: str or None
  1839. """
  1840. self.doc = doc
  1841. """The class' docstring.
  1842. :type doc: str or None
  1843. """
  1844. super().__init__(
  1845. lineno=lineno,
  1846. col_offset=col_offset,
  1847. end_lineno=end_lineno,
  1848. end_col_offset=end_col_offset,
  1849. parent=parent,
  1850. )
  1851. if parent is not None:
  1852. parent.frame(future=True).set_local(name, self)
  1853. for local_name, node in self.implicit_locals():
  1854. self.add_local_node(node, local_name)
  1855. def implicit_parameters(self):
  1856. return 1
  1857. def implicit_locals(self):
  1858. """Get implicitly defined class definition locals.
  1859. :returns: the the name and Const pair for each local
  1860. :rtype: tuple(tuple(str, node_classes.Const), ...)
  1861. """
  1862. locals_ = (("__module__", self.special_attributes.attr___module__),)
  1863. # __qualname__ is defined in PEP3155
  1864. locals_ += (("__qualname__", self.special_attributes.attr___qualname__),)
  1865. return locals_
  1866. # pylint: disable=redefined-outer-name
  1867. def postinit(
  1868. self, bases, body, decorators, newstyle=None, metaclass=None, keywords=None
  1869. ):
  1870. """Do some setup after initialisation.
  1871. :param bases: What the class inherits from.
  1872. :type bases: list(NodeNG)
  1873. :param body: The contents of the class body.
  1874. :type body: list(NodeNG)
  1875. :param decorators: The decorators that are applied to this class.
  1876. :type decorators: Decorators or None
  1877. :param newstyle: Whether this is a new style class or not.
  1878. :type newstyle: bool or None
  1879. :param metaclass: The metaclass of this class.
  1880. :type metaclass: NodeNG or None
  1881. :param keywords: The keywords given to the class definition.
  1882. :type keywords: list(Keyword) or None
  1883. """
  1884. if keywords is not None:
  1885. self.keywords = keywords
  1886. self.bases = bases
  1887. self.body = body
  1888. self.decorators = decorators
  1889. if newstyle is not None:
  1890. self._newstyle = newstyle
  1891. if metaclass is not None:
  1892. self._metaclass = metaclass
  1893. def _newstyle_impl(self, context=None):
  1894. if context is None:
  1895. context = InferenceContext()
  1896. if self._newstyle is not None:
  1897. return self._newstyle
  1898. for base in self.ancestors(recurs=False, context=context):
  1899. if base._newstyle_impl(context):
  1900. self._newstyle = True
  1901. break
  1902. klass = self.declared_metaclass()
  1903. # could be any callable, we'd need to infer the result of klass(name,
  1904. # bases, dict). punt if it's not a class node.
  1905. if klass is not None and isinstance(klass, ClassDef):
  1906. self._newstyle = klass._newstyle_impl(context)
  1907. if self._newstyle is None:
  1908. self._newstyle = False
  1909. return self._newstyle
  1910. _newstyle = None
  1911. newstyle = property(
  1912. _newstyle_impl,
  1913. doc=("Whether this is a new style class or not\n\n" ":type: bool or None"),
  1914. )
  1915. @decorators_mod.cachedproperty
  1916. def blockstart_tolineno(self):
  1917. """The line on which the beginning of this block ends.
  1918. :type: int
  1919. """
  1920. if self.bases:
  1921. return self.bases[-1].tolineno
  1922. return self.fromlineno
  1923. def block_range(self, lineno):
  1924. """Get a range from the given line number to where this node ends.
  1925. :param lineno: Unused.
  1926. :type lineno: int
  1927. :returns: The range of line numbers that this node belongs to,
  1928. :rtype: tuple(int, int)
  1929. """
  1930. return self.fromlineno, self.tolineno
  1931. def pytype(self):
  1932. """Get the name of the type that this node represents.
  1933. :returns: The name of the type.
  1934. :rtype: str
  1935. """
  1936. if self.newstyle:
  1937. return "builtins.type"
  1938. return "builtins.classobj"
  1939. def display_type(self):
  1940. """A human readable type of this node.
  1941. :returns: The type of this node.
  1942. :rtype: str
  1943. """
  1944. return "Class"
  1945. def callable(self):
  1946. """Whether this node defines something that is callable.
  1947. :returns: True if this defines something that is callable,
  1948. False otherwise.
  1949. For a :class:`ClassDef` this is always ``True``.
  1950. :rtype: bool
  1951. """
  1952. return True
  1953. def is_subtype_of(self, type_name, context=None):
  1954. """Whether this class is a subtype of the given type.
  1955. :param type_name: The name of the type of check against.
  1956. :type type_name: str
  1957. :returns: True if this class is a subtype of the given type,
  1958. False otherwise.
  1959. :rtype: bool
  1960. """
  1961. if self.qname() == type_name:
  1962. return True
  1963. return any(anc.qname() == type_name for anc in self.ancestors(context=context))
  1964. def _infer_type_call(self, caller, context):
  1965. try:
  1966. name_node = next(caller.args[0].infer(context))
  1967. except StopIteration as e:
  1968. raise InferenceError(node=caller.args[0], context=context) from e
  1969. if isinstance(name_node, node_classes.Const) and isinstance(
  1970. name_node.value, str
  1971. ):
  1972. name = name_node.value
  1973. else:
  1974. return util.Uninferable
  1975. result = ClassDef(name, None)
  1976. # Get the bases of the class.
  1977. try:
  1978. class_bases = next(caller.args[1].infer(context))
  1979. except StopIteration as e:
  1980. raise InferenceError(node=caller.args[1], context=context) from e
  1981. if isinstance(class_bases, (node_classes.Tuple, node_classes.List)):
  1982. bases = []
  1983. for base in class_bases.itered():
  1984. inferred = next(base.infer(context=context), None)
  1985. if inferred:
  1986. bases.append(
  1987. node_classes.EvaluatedObject(original=base, value=inferred)
  1988. )
  1989. result.bases = bases
  1990. else:
  1991. # There is currently no AST node that can represent an 'unknown'
  1992. # node (Uninferable is not an AST node), therefore we simply return Uninferable here
  1993. # although we know at least the name of the class.
  1994. return util.Uninferable
  1995. # Get the members of the class
  1996. try:
  1997. members = next(caller.args[2].infer(context))
  1998. except (InferenceError, StopIteration):
  1999. members = None
  2000. if members and isinstance(members, node_classes.Dict):
  2001. for attr, value in members.items:
  2002. if isinstance(attr, node_classes.Const) and isinstance(attr.value, str):
  2003. result.locals[attr.value] = [value]
  2004. result.parent = caller.parent
  2005. return result
  2006. def infer_call_result(self, caller, context=None):
  2007. """infer what a class is returning when called"""
  2008. if self.is_subtype_of("builtins.type", context) and len(caller.args) == 3:
  2009. result = self._infer_type_call(caller, context)
  2010. yield result
  2011. return
  2012. dunder_call = None
  2013. try:
  2014. metaclass = self.metaclass(context=context)
  2015. if metaclass is not None:
  2016. dunder_call = next(metaclass.igetattr("__call__", context))
  2017. except (AttributeInferenceError, StopIteration):
  2018. pass
  2019. if dunder_call and dunder_call.qname() != "builtins.type.__call__":
  2020. # Call type.__call__ if not set metaclass
  2021. # (since type is the default metaclass)
  2022. context = bind_context_to_node(context, self)
  2023. context.callcontext.callee = dunder_call
  2024. yield from dunder_call.infer_call_result(caller, context)
  2025. else:
  2026. yield self.instantiate_class()
  2027. def scope_lookup(self, node, name, offset=0):
  2028. """Lookup where the given name is assigned.
  2029. :param node: The node to look for assignments up to.
  2030. Any assignments after the given node are ignored.
  2031. :type node: NodeNG
  2032. :param name: The name to find assignments for.
  2033. :type name: str
  2034. :param offset: The line offset to filter statements up to.
  2035. :type offset: int
  2036. :returns: This scope node and the list of assignments associated to the
  2037. given name according to the scope where it has been found (locals,
  2038. globals or builtin).
  2039. :rtype: tuple(str, list(NodeNG))
  2040. """
  2041. # If the name looks like a builtin name, just try to look
  2042. # into the upper scope of this class. We might have a
  2043. # decorator that it's poorly named after a builtin object
  2044. # inside this class.
  2045. lookup_upper_frame = (
  2046. isinstance(node.parent, node_classes.Decorators)
  2047. and name in AstroidManager().builtins_module
  2048. )
  2049. if (
  2050. any(node == base or base.parent_of(node) for base in self.bases)
  2051. or lookup_upper_frame
  2052. ):
  2053. # Handle the case where we have either a name
  2054. # in the bases of a class, which exists before
  2055. # the actual definition or the case where we have
  2056. # a Getattr node, with that name.
  2057. #
  2058. # name = ...
  2059. # class A(name):
  2060. # def name(self): ...
  2061. #
  2062. # import name
  2063. # class A(name.Name):
  2064. # def name(self): ...
  2065. frame = self.parent.frame(future=True)
  2066. # line offset to avoid that class A(A) resolve the ancestor to
  2067. # the defined class
  2068. offset = -1
  2069. else:
  2070. frame = self
  2071. return frame._scope_lookup(node, name, offset)
  2072. @property
  2073. def basenames(self):
  2074. """The names of the parent classes
  2075. Names are given in the order they appear in the class definition.
  2076. :type: list(str)
  2077. """
  2078. return [bnode.as_string() for bnode in self.bases]
  2079. def ancestors(self, recurs=True, context=None):
  2080. """Iterate over the base classes in prefixed depth first order.
  2081. :param recurs: Whether to recurse or return direct ancestors only.
  2082. :type recurs: bool
  2083. :returns: The base classes
  2084. :rtype: iterable(NodeNG)
  2085. """
  2086. # FIXME: should be possible to choose the resolution order
  2087. # FIXME: inference make infinite loops possible here
  2088. yielded = {self}
  2089. if context is None:
  2090. context = InferenceContext()
  2091. if not self.bases and self.qname() != "builtins.object":
  2092. yield builtin_lookup("object")[1][0]
  2093. return
  2094. for stmt in self.bases:
  2095. with context.restore_path():
  2096. try:
  2097. for baseobj in stmt.infer(context):
  2098. if not isinstance(baseobj, ClassDef):
  2099. if isinstance(baseobj, bases.Instance):
  2100. baseobj = baseobj._proxied
  2101. else:
  2102. continue
  2103. if not baseobj.hide:
  2104. if baseobj in yielded:
  2105. continue
  2106. yielded.add(baseobj)
  2107. yield baseobj
  2108. if not recurs:
  2109. continue
  2110. for grandpa in baseobj.ancestors(recurs=True, context=context):
  2111. if grandpa is self:
  2112. # This class is the ancestor of itself.
  2113. break
  2114. if grandpa in yielded:
  2115. continue
  2116. yielded.add(grandpa)
  2117. yield grandpa
  2118. except InferenceError:
  2119. continue
  2120. def local_attr_ancestors(self, name, context=None):
  2121. """Iterate over the parents that define the given name.
  2122. :param name: The name to find definitions for.
  2123. :type name: str
  2124. :returns: The parents that define the given name.
  2125. :rtype: iterable(NodeNG)
  2126. """
  2127. # Look up in the mro if we can. This will result in the
  2128. # attribute being looked up just as Python does it.
  2129. try:
  2130. ancestors = self.mro(context)[1:]
  2131. except MroError:
  2132. # Fallback to use ancestors, we can't determine
  2133. # a sane MRO.
  2134. ancestors = self.ancestors(context=context)
  2135. for astroid in ancestors:
  2136. if name in astroid:
  2137. yield astroid
  2138. def instance_attr_ancestors(self, name, context=None):
  2139. """Iterate over the parents that define the given name as an attribute.
  2140. :param name: The name to find definitions for.
  2141. :type name: str
  2142. :returns: The parents that define the given name as
  2143. an instance attribute.
  2144. :rtype: iterable(NodeNG)
  2145. """
  2146. for astroid in self.ancestors(context=context):
  2147. if name in astroid.instance_attrs:
  2148. yield astroid
  2149. def has_base(self, node):
  2150. """Whether this class directly inherits from the given node.
  2151. :param node: The node to check for.
  2152. :type node: NodeNG
  2153. :returns: True if this class directly inherits from the given node.
  2154. :rtype: bool
  2155. """
  2156. return node in self.bases
  2157. def local_attr(self, name, context=None):
  2158. """Get the list of assign nodes associated to the given name.
  2159. Assignments are looked for in both this class and in parents.
  2160. :returns: The list of assignments to the given name.
  2161. :rtype: list(NodeNG)
  2162. :raises AttributeInferenceError: If no attribute with this name
  2163. can be found in this class or parent classes.
  2164. """
  2165. result = []
  2166. if name in self.locals:
  2167. result = self.locals[name]
  2168. else:
  2169. class_node = next(self.local_attr_ancestors(name, context), None)
  2170. if class_node:
  2171. result = class_node.locals[name]
  2172. result = [n for n in result if not isinstance(n, node_classes.DelAttr)]
  2173. if result:
  2174. return result
  2175. raise AttributeInferenceError(target=self, attribute=name, context=context)
  2176. def instance_attr(self, name, context=None):
  2177. """Get the list of nodes associated to the given attribute name.
  2178. Assignments are looked for in both this class and in parents.
  2179. :returns: The list of assignments to the given name.
  2180. :rtype: list(NodeNG)
  2181. :raises AttributeInferenceError: If no attribute with this name
  2182. can be found in this class or parent classes.
  2183. """
  2184. # Return a copy, so we don't modify self.instance_attrs,
  2185. # which could lead to infinite loop.
  2186. values = list(self.instance_attrs.get(name, []))
  2187. # get all values from parents
  2188. for class_node in self.instance_attr_ancestors(name, context):
  2189. values += class_node.instance_attrs[name]
  2190. values = [n for n in values if not isinstance(n, node_classes.DelAttr)]
  2191. if values:
  2192. return values
  2193. raise AttributeInferenceError(target=self, attribute=name, context=context)
  2194. def instantiate_class(self):
  2195. """Get an :class:`Instance` of the :class:`ClassDef` node.
  2196. :returns: An :class:`Instance` of the :class:`ClassDef` node,
  2197. or self if this is not possible.
  2198. :rtype: Instance or ClassDef
  2199. """
  2200. try:
  2201. if any(cls.name in EXCEPTION_BASE_CLASSES for cls in self.mro()):
  2202. # Subclasses of exceptions can be exception instances
  2203. return objects.ExceptionInstance(self)
  2204. except MroError:
  2205. pass
  2206. return bases.Instance(self)
  2207. def getattr(self, name, context=None, class_context=True):
  2208. """Get an attribute from this class, using Python's attribute semantic.
  2209. This method doesn't look in the :attr:`instance_attrs` dictionary
  2210. since it is done by an :class:`Instance` proxy at inference time.
  2211. It may return an :class:`Uninferable` object if
  2212. the attribute has not been
  2213. found, but a ``__getattr__`` or ``__getattribute__`` method is defined.
  2214. If ``class_context`` is given, then it is considered that the
  2215. attribute is accessed from a class context,
  2216. e.g. ClassDef.attribute, otherwise it might have been accessed
  2217. from an instance as well. If ``class_context`` is used in that
  2218. case, then a lookup in the implicit metaclass and the explicit
  2219. metaclass will be done.
  2220. :param name: The attribute to look for.
  2221. :type name: str
  2222. :param class_context: Whether the attribute can be accessed statically.
  2223. :type class_context: bool
  2224. :returns: The attribute.
  2225. :rtype: list(NodeNG)
  2226. :raises AttributeInferenceError: If the attribute cannot be inferred.
  2227. """
  2228. if not name:
  2229. raise AttributeInferenceError(target=self, attribute=name, context=context)
  2230. values = self.locals.get(name, [])
  2231. if name in self.special_attributes and class_context and not values:
  2232. result = [self.special_attributes.lookup(name)]
  2233. if name == "__bases__":
  2234. # Need special treatment, since they are mutable
  2235. # and we need to return all the values.
  2236. result += values
  2237. return result
  2238. # don't modify the list in self.locals!
  2239. values = list(values)
  2240. for classnode in self.ancestors(recurs=True, context=context):
  2241. values += classnode.locals.get(name, [])
  2242. if class_context:
  2243. values += self._metaclass_lookup_attribute(name, context)
  2244. if not values:
  2245. raise AttributeInferenceError(target=self, attribute=name, context=context)
  2246. # Look for AnnAssigns, which are not attributes in the purest sense.
  2247. for value in values:
  2248. if isinstance(value, node_classes.AssignName):
  2249. stmt = value.statement(future=True)
  2250. if isinstance(stmt, node_classes.AnnAssign) and stmt.value is None:
  2251. raise AttributeInferenceError(
  2252. target=self, attribute=name, context=context
  2253. )
  2254. return values
  2255. def _metaclass_lookup_attribute(self, name, context):
  2256. """Search the given name in the implicit and the explicit metaclass."""
  2257. attrs = set()
  2258. implicit_meta = self.implicit_metaclass()
  2259. context = copy_context(context)
  2260. metaclass = self.metaclass(context=context)
  2261. for cls in (implicit_meta, metaclass):
  2262. if cls and cls != self and isinstance(cls, ClassDef):
  2263. cls_attributes = self._get_attribute_from_metaclass(cls, name, context)
  2264. attrs.update(set(cls_attributes))
  2265. return attrs
  2266. def _get_attribute_from_metaclass(self, cls, name, context):
  2267. try:
  2268. attrs = cls.getattr(name, context=context, class_context=True)
  2269. except AttributeInferenceError:
  2270. return
  2271. for attr in bases._infer_stmts(attrs, context, frame=cls):
  2272. if not isinstance(attr, FunctionDef):
  2273. yield attr
  2274. continue
  2275. if isinstance(attr, objects.Property):
  2276. yield attr
  2277. continue
  2278. if attr.type == "classmethod":
  2279. # If the method is a classmethod, then it will
  2280. # be bound to the metaclass, not to the class
  2281. # from where the attribute is retrieved.
  2282. # get_wrapping_class could return None, so just
  2283. # default to the current class.
  2284. frame = get_wrapping_class(attr) or self
  2285. yield bases.BoundMethod(attr, frame)
  2286. elif attr.type == "staticmethod":
  2287. yield attr
  2288. else:
  2289. yield bases.BoundMethod(attr, self)
  2290. def igetattr(self, name, context=None, class_context=True):
  2291. """Infer the possible values of the given variable.
  2292. :param name: The name of the variable to infer.
  2293. :type name: str
  2294. :returns: The inferred possible values.
  2295. :rtype: iterable(NodeNG or Uninferable)
  2296. """
  2297. # set lookup name since this is necessary to infer on import nodes for
  2298. # instance
  2299. context = copy_context(context)
  2300. context.lookupname = name
  2301. metaclass = self.metaclass(context=context)
  2302. try:
  2303. attributes = self.getattr(name, context, class_context=class_context)
  2304. # If we have more than one attribute, make sure that those starting from
  2305. # the second one are from the same scope. This is to account for modifications
  2306. # to the attribute happening *after* the attribute's definition (e.g. AugAssigns on lists)
  2307. if len(attributes) > 1:
  2308. first_attr, attributes = attributes[0], attributes[1:]
  2309. first_scope = first_attr.scope()
  2310. attributes = [first_attr] + [
  2311. attr
  2312. for attr in attributes
  2313. if attr.parent and attr.parent.scope() == first_scope
  2314. ]
  2315. for inferred in bases._infer_stmts(attributes, context, frame=self):
  2316. # yield Uninferable object instead of descriptors when necessary
  2317. if not isinstance(inferred, node_classes.Const) and isinstance(
  2318. inferred, bases.Instance
  2319. ):
  2320. try:
  2321. inferred._proxied.getattr("__get__", context)
  2322. except AttributeInferenceError:
  2323. yield inferred
  2324. else:
  2325. yield util.Uninferable
  2326. elif isinstance(inferred, objects.Property):
  2327. function = inferred.function
  2328. if not class_context:
  2329. # Through an instance so we can solve the property
  2330. yield from function.infer_call_result(
  2331. caller=self, context=context
  2332. )
  2333. # If we're in a class context, we need to determine if the property
  2334. # was defined in the metaclass (a derived class must be a subclass of
  2335. # the metaclass of all its bases), in which case we can resolve the
  2336. # property. If not, i.e. the property is defined in some base class
  2337. # instead, then we return the property object
  2338. elif metaclass and function.parent.scope() is metaclass:
  2339. # Resolve a property as long as it is not accessed through
  2340. # the class itself.
  2341. yield from function.infer_call_result(
  2342. caller=self, context=context
  2343. )
  2344. else:
  2345. yield inferred
  2346. else:
  2347. yield function_to_method(inferred, self)
  2348. except AttributeInferenceError as error:
  2349. if not name.startswith("__") and self.has_dynamic_getattr(context):
  2350. # class handle some dynamic attributes, return a Uninferable object
  2351. yield util.Uninferable
  2352. else:
  2353. raise InferenceError(
  2354. str(error), target=self, attribute=name, context=context
  2355. ) from error
  2356. def has_dynamic_getattr(self, context=None):
  2357. """Check if the class has a custom __getattr__ or __getattribute__.
  2358. If any such method is found and it is not from
  2359. builtins, nor from an extension module, then the function
  2360. will return True.
  2361. :returns: True if the class has a custom
  2362. __getattr__ or __getattribute__, False otherwise.
  2363. :rtype: bool
  2364. """
  2365. def _valid_getattr(node):
  2366. root = node.root()
  2367. return root.name != "builtins" and getattr(root, "pure_python", None)
  2368. try:
  2369. return _valid_getattr(self.getattr("__getattr__", context)[0])
  2370. except AttributeInferenceError:
  2371. # if self.newstyle: XXX cause an infinite recursion error
  2372. try:
  2373. getattribute = self.getattr("__getattribute__", context)[0]
  2374. return _valid_getattr(getattribute)
  2375. except AttributeInferenceError:
  2376. pass
  2377. return False
  2378. def getitem(self, index, context=None):
  2379. """Return the inference of a subscript.
  2380. This is basically looking up the method in the metaclass and calling it.
  2381. :returns: The inferred value of a subscript to this class.
  2382. :rtype: NodeNG
  2383. :raises AstroidTypeError: If this class does not define a
  2384. ``__getitem__`` method.
  2385. """
  2386. try:
  2387. methods = lookup(self, "__getitem__")
  2388. except AttributeInferenceError as exc:
  2389. if isinstance(self, ClassDef):
  2390. # subscripting a class definition may be
  2391. # achieved thanks to __class_getitem__ method
  2392. # which is a classmethod defined in the class
  2393. # that supports subscript and not in the metaclass
  2394. try:
  2395. methods = self.getattr("__class_getitem__")
  2396. # Here it is assumed that the __class_getitem__ node is
  2397. # a FunctionDef. One possible improvement would be to deal
  2398. # with more generic inference.
  2399. except AttributeInferenceError:
  2400. raise AstroidTypeError(node=self, context=context) from exc
  2401. else:
  2402. raise AstroidTypeError(node=self, context=context) from exc
  2403. method = methods[0]
  2404. # Create a new callcontext for providing index as an argument.
  2405. new_context = bind_context_to_node(context, self)
  2406. new_context.callcontext = CallContext(args=[index], callee=method)
  2407. try:
  2408. return next(method.infer_call_result(self, new_context), util.Uninferable)
  2409. except AttributeError:
  2410. # Starting with python3.9, builtin types list, dict etc...
  2411. # are subscriptable thanks to __class_getitem___ classmethod.
  2412. # However in such case the method is bound to an EmptyNode and
  2413. # EmptyNode doesn't have infer_call_result method yielding to
  2414. # AttributeError
  2415. if (
  2416. isinstance(method, node_classes.EmptyNode)
  2417. and self.name in {"list", "dict", "set", "tuple", "frozenset"}
  2418. and PY39_PLUS
  2419. ):
  2420. return self
  2421. raise
  2422. except InferenceError:
  2423. return util.Uninferable
  2424. def methods(self):
  2425. """Iterate over all of the method defined in this class and its parents.
  2426. :returns: The methods defined on the class.
  2427. :rtype: iterable(FunctionDef)
  2428. """
  2429. done = {}
  2430. for astroid in itertools.chain(iter((self,)), self.ancestors()):
  2431. for meth in astroid.mymethods():
  2432. if meth.name in done:
  2433. continue
  2434. done[meth.name] = None
  2435. yield meth
  2436. def mymethods(self):
  2437. """Iterate over all of the method defined in this class only.
  2438. :returns: The methods defined on the class.
  2439. :rtype: iterable(FunctionDef)
  2440. """
  2441. for member in self.values():
  2442. if isinstance(member, FunctionDef):
  2443. yield member
  2444. def implicit_metaclass(self):
  2445. """Get the implicit metaclass of the current class.
  2446. For newstyle classes, this will return an instance of builtins.type.
  2447. For oldstyle classes, it will simply return None, since there's
  2448. no implicit metaclass there.
  2449. :returns: The metaclass.
  2450. :rtype: builtins.type or None
  2451. """
  2452. if self.newstyle:
  2453. return builtin_lookup("type")[1][0]
  2454. return None
  2455. _metaclass = None
  2456. def declared_metaclass(self, context=None):
  2457. """Return the explicit declared metaclass for the current class.
  2458. An explicit declared metaclass is defined
  2459. either by passing the ``metaclass`` keyword argument
  2460. in the class definition line (Python 3) or (Python 2) by
  2461. having a ``__metaclass__`` class attribute, or if there are
  2462. no explicit bases but there is a global ``__metaclass__`` variable.
  2463. :returns: The metaclass of this class,
  2464. or None if one could not be found.
  2465. :rtype: NodeNG or None
  2466. """
  2467. for base in self.bases:
  2468. try:
  2469. for baseobj in base.infer(context=context):
  2470. if isinstance(baseobj, ClassDef) and baseobj.hide:
  2471. self._metaclass = baseobj._metaclass
  2472. self._metaclass_hack = True
  2473. break
  2474. except InferenceError:
  2475. pass
  2476. if self._metaclass:
  2477. # Expects this from Py3k TreeRebuilder
  2478. try:
  2479. return next(
  2480. node
  2481. for node in self._metaclass.infer(context=context)
  2482. if node is not util.Uninferable
  2483. )
  2484. except (InferenceError, StopIteration):
  2485. return None
  2486. return None
  2487. def _find_metaclass(self, seen=None, context=None):
  2488. if seen is None:
  2489. seen = set()
  2490. seen.add(self)
  2491. klass = self.declared_metaclass(context=context)
  2492. if klass is None:
  2493. for parent in self.ancestors(context=context):
  2494. if parent not in seen:
  2495. klass = parent._find_metaclass(seen)
  2496. if klass is not None:
  2497. break
  2498. return klass
  2499. def metaclass(self, context=None):
  2500. """Get the metaclass of this class.
  2501. If this class does not define explicitly a metaclass,
  2502. then the first defined metaclass in ancestors will be used
  2503. instead.
  2504. :returns: The metaclass of this class.
  2505. :rtype: NodeNG or None
  2506. """
  2507. return self._find_metaclass(context=context)
  2508. def has_metaclass_hack(self):
  2509. return self._metaclass_hack
  2510. def _islots(self):
  2511. """Return an iterator with the inferred slots."""
  2512. if "__slots__" not in self.locals:
  2513. return None
  2514. for slots in self.igetattr("__slots__"):
  2515. # check if __slots__ is a valid type
  2516. for meth in ITER_METHODS:
  2517. try:
  2518. slots.getattr(meth)
  2519. break
  2520. except AttributeInferenceError:
  2521. continue
  2522. else:
  2523. continue
  2524. if isinstance(slots, node_classes.Const):
  2525. # a string. Ignore the following checks,
  2526. # but yield the node, only if it has a value
  2527. if slots.value:
  2528. yield slots
  2529. continue
  2530. if not hasattr(slots, "itered"):
  2531. # we can't obtain the values, maybe a .deque?
  2532. continue
  2533. if isinstance(slots, node_classes.Dict):
  2534. values = [item[0] for item in slots.items]
  2535. else:
  2536. values = slots.itered()
  2537. if values is util.Uninferable:
  2538. continue
  2539. if not values:
  2540. # Stop the iteration, because the class
  2541. # has an empty list of slots.
  2542. return values
  2543. for elt in values:
  2544. try:
  2545. for inferred in elt.infer():
  2546. if inferred is util.Uninferable:
  2547. continue
  2548. if not isinstance(
  2549. inferred, node_classes.Const
  2550. ) or not isinstance(inferred.value, str):
  2551. continue
  2552. if not inferred.value:
  2553. continue
  2554. yield inferred
  2555. except InferenceError:
  2556. continue
  2557. return None
  2558. def _slots(self):
  2559. if not self.newstyle:
  2560. raise NotImplementedError(
  2561. "The concept of slots is undefined for old-style classes."
  2562. )
  2563. slots = self._islots()
  2564. try:
  2565. first = next(slots)
  2566. except StopIteration as exc:
  2567. # The class doesn't have a __slots__ definition or empty slots.
  2568. if exc.args and exc.args[0] not in ("", None):
  2569. return exc.args[0]
  2570. return None
  2571. return [first] + list(slots)
  2572. # Cached, because inferring them all the time is expensive
  2573. @decorators_mod.cached
  2574. def slots(self):
  2575. """Get all the slots for this node.
  2576. :returns: The names of slots for this class.
  2577. If the class doesn't define any slot, through the ``__slots__``
  2578. variable, then this function will return a None.
  2579. Also, it will return None in the case the slots were not inferred.
  2580. :rtype: list(str) or None
  2581. """
  2582. def grouped_slots(
  2583. mro: List["ClassDef"],
  2584. ) -> typing.Iterator[Optional[node_classes.NodeNG]]:
  2585. # Not interested in object, since it can't have slots.
  2586. for cls in mro[:-1]:
  2587. try:
  2588. cls_slots = cls._slots()
  2589. except NotImplementedError:
  2590. continue
  2591. if cls_slots is not None:
  2592. yield from cls_slots
  2593. else:
  2594. yield None
  2595. if not self.newstyle:
  2596. raise NotImplementedError(
  2597. "The concept of slots is undefined for old-style classes."
  2598. )
  2599. try:
  2600. mro = self.mro()
  2601. except MroError as e:
  2602. raise NotImplementedError(
  2603. "Cannot get slots while parsing mro fails."
  2604. ) from e
  2605. slots = list(grouped_slots(mro))
  2606. if not all(slot is not None for slot in slots):
  2607. return None
  2608. return sorted(set(slots), key=lambda item: item.value)
  2609. def _inferred_bases(self, context=None):
  2610. # Similar with .ancestors, but the difference is when one base is inferred,
  2611. # only the first object is wanted. That's because
  2612. # we aren't interested in superclasses, as in the following
  2613. # example:
  2614. #
  2615. # class SomeSuperClass(object): pass
  2616. # class SomeClass(SomeSuperClass): pass
  2617. # class Test(SomeClass): pass
  2618. #
  2619. # Inferring SomeClass from the Test's bases will give
  2620. # us both SomeClass and SomeSuperClass, but we are interested
  2621. # only in SomeClass.
  2622. if context is None:
  2623. context = InferenceContext()
  2624. if not self.bases and self.qname() != "builtins.object":
  2625. yield builtin_lookup("object")[1][0]
  2626. return
  2627. for stmt in self.bases:
  2628. try:
  2629. # Find the first non-None inferred base value
  2630. baseobj = next(
  2631. b
  2632. for b in stmt.infer(context=context.clone())
  2633. if not (isinstance(b, Const) and b.value is None)
  2634. )
  2635. except (InferenceError, StopIteration):
  2636. continue
  2637. if isinstance(baseobj, bases.Instance):
  2638. baseobj = baseobj._proxied
  2639. if not isinstance(baseobj, ClassDef):
  2640. continue
  2641. if not baseobj.hide:
  2642. yield baseobj
  2643. else:
  2644. yield from baseobj.bases
  2645. def _compute_mro(self, context=None):
  2646. inferred_bases = list(self._inferred_bases(context=context))
  2647. bases_mro = []
  2648. for base in inferred_bases:
  2649. if base is self:
  2650. continue
  2651. try:
  2652. mro = base._compute_mro(context=context)
  2653. bases_mro.append(mro)
  2654. except NotImplementedError:
  2655. # Some classes have in their ancestors both newstyle and
  2656. # old style classes. For these we can't retrieve the .mro,
  2657. # although in Python it's possible, since the class we are
  2658. # currently working is in fact new style.
  2659. # So, we fallback to ancestors here.
  2660. ancestors = list(base.ancestors(context=context))
  2661. bases_mro.append(ancestors)
  2662. unmerged_mro = [[self]] + bases_mro + [inferred_bases]
  2663. unmerged_mro = list(clean_duplicates_mro(unmerged_mro, self, context))
  2664. clean_typing_generic_mro(unmerged_mro)
  2665. return _c3_merge(unmerged_mro, self, context)
  2666. def mro(self, context=None) -> List["ClassDef"]:
  2667. """Get the method resolution order, using C3 linearization.
  2668. :returns: The list of ancestors, sorted by the mro.
  2669. :rtype: list(NodeNG)
  2670. :raises DuplicateBasesError: Duplicate bases in the same class base
  2671. :raises InconsistentMroError: A class' MRO is inconsistent
  2672. """
  2673. return self._compute_mro(context=context)
  2674. def bool_value(self, context=None):
  2675. """Determine the boolean value of this node.
  2676. :returns: The boolean value of this node.
  2677. For a :class:`ClassDef` this is always ``True``.
  2678. :rtype: bool
  2679. """
  2680. return True
  2681. def get_children(self):
  2682. if self.decorators is not None:
  2683. yield self.decorators
  2684. yield from self.bases
  2685. if self.keywords is not None:
  2686. yield from self.keywords
  2687. yield from self.body
  2688. @decorators_mod.cached
  2689. def _get_assign_nodes(self):
  2690. children_assign_nodes = (
  2691. child_node._get_assign_nodes() for child_node in self.body
  2692. )
  2693. return list(itertools.chain.from_iterable(children_assign_nodes))
  2694. def frame(self: T, *, future: Literal[None, True] = None) -> T:
  2695. """The node's frame node.
  2696. A frame node is a :class:`Module`, :class:`FunctionDef`,
  2697. :class:`ClassDef` or :class:`Lambda`.
  2698. :returns: The node itself.
  2699. """
  2700. return self