Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

node_classes.py 127KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267
  1. # pylint: disable=too-many-lines; https://github.com/PyCQA/astroid/issues/465
  2. # Copyright (c) 2009-2011, 2013-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  3. # Copyright (c) 2013-2014, 2016 Google, Inc.
  4. # Copyright (c) 2014-2016 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2015-2016 Cara Vinson <ceridwenv@gmail.com>
  6. # Copyright (c) 2016 Jakub Wilk <jwilk@jwilk.net>
  7. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  8. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  9. """Module for some node classes. More nodes in scoped_nodes.py
  10. """
  11. import abc
  12. import pprint
  13. import warnings
  14. try:
  15. from functools import singledispatch as _singledispatch
  16. except ImportError:
  17. from singledispatch import singledispatch as _singledispatch
  18. import six
  19. from astroid import as_string
  20. from astroid import bases
  21. from astroid import context as contextmod
  22. from astroid import decorators
  23. from astroid import exceptions
  24. from astroid import manager
  25. from astroid import mixins
  26. from astroid import util
  27. BUILTINS = six.moves.builtins.__name__
  28. MANAGER = manager.AstroidManager()
  29. @decorators.raise_if_nothing_inferred
  30. def unpack_infer(stmt, context=None):
  31. """recursively generate nodes inferred by the given statement.
  32. If the inferred value is a list or a tuple, recurse on the elements
  33. """
  34. if isinstance(stmt, (List, Tuple)):
  35. for elt in stmt.elts:
  36. if elt is util.Uninferable:
  37. yield elt
  38. continue
  39. for inferred_elt in unpack_infer(elt, context):
  40. yield inferred_elt
  41. # Explicit StopIteration to return error information, see comment
  42. # in raise_if_nothing_inferred.
  43. raise StopIteration(dict(node=stmt, context=context))
  44. # if inferred is a final node, return it and stop
  45. inferred = next(stmt.infer(context))
  46. if inferred is stmt:
  47. yield inferred
  48. # Explicit StopIteration to return error information, see comment
  49. # in raise_if_nothing_inferred.
  50. raise StopIteration(dict(node=stmt, context=context))
  51. # else, infer recursively, except Uninferable object that should be returned as is
  52. for inferred in stmt.infer(context):
  53. if inferred is util.Uninferable:
  54. yield inferred
  55. else:
  56. for inf_inf in unpack_infer(inferred, context):
  57. yield inf_inf
  58. raise StopIteration(dict(node=stmt, context=context))
  59. def are_exclusive(stmt1, stmt2, exceptions=None): # pylint: disable=redefined-outer-name
  60. """return true if the two given statements are mutually exclusive
  61. `exceptions` may be a list of exception names. If specified, discard If
  62. branches and check one of the statement is in an exception handler catching
  63. one of the given exceptions.
  64. algorithm :
  65. 1) index stmt1's parents
  66. 2) climb among stmt2's parents until we find a common parent
  67. 3) if the common parent is a If or TryExcept statement, look if nodes are
  68. in exclusive branches
  69. """
  70. # index stmt1's parents
  71. stmt1_parents = {}
  72. children = {}
  73. node = stmt1.parent
  74. previous = stmt1
  75. while node:
  76. stmt1_parents[node] = 1
  77. children[node] = previous
  78. previous = node
  79. node = node.parent
  80. # climb among stmt2's parents until we find a common parent
  81. node = stmt2.parent
  82. previous = stmt2
  83. while node:
  84. if node in stmt1_parents:
  85. # if the common parent is a If or TryExcept statement, look if
  86. # nodes are in exclusive branches
  87. if isinstance(node, If) and exceptions is None:
  88. if (node.locate_child(previous)[1]
  89. is not node.locate_child(children[node])[1]):
  90. return True
  91. elif isinstance(node, TryExcept):
  92. c2attr, c2node = node.locate_child(previous)
  93. c1attr, c1node = node.locate_child(children[node])
  94. if c1node is not c2node:
  95. first_in_body_caught_by_handlers = (
  96. c2attr == 'handlers'
  97. and c1attr == 'body'
  98. and previous.catch(exceptions))
  99. second_in_body_caught_by_handlers = (
  100. c2attr == 'body'
  101. and c1attr == 'handlers'
  102. and children[node].catch(exceptions))
  103. first_in_else_other_in_handlers = (
  104. c2attr == 'handlers' and c1attr == 'orelse')
  105. second_in_else_other_in_handlers = (
  106. c2attr == 'orelse' and c1attr == 'handlers')
  107. if any((first_in_body_caught_by_handlers,
  108. second_in_body_caught_by_handlers,
  109. first_in_else_other_in_handlers,
  110. second_in_else_other_in_handlers)):
  111. return True
  112. elif c2attr == 'handlers' and c1attr == 'handlers':
  113. return previous is not children[node]
  114. return False
  115. previous = node
  116. node = node.parent
  117. return False
  118. # getitem() helpers.
  119. _SLICE_SENTINEL = object()
  120. def _slice_value(index, context=None):
  121. """Get the value of the given slice index."""
  122. if isinstance(index, Const):
  123. if isinstance(index.value, (int, type(None))):
  124. return index.value
  125. elif index is None:
  126. return None
  127. else:
  128. # Try to infer what the index actually is.
  129. # Since we can't return all the possible values,
  130. # we'll stop at the first possible value.
  131. try:
  132. inferred = next(index.infer(context=context))
  133. except exceptions.InferenceError:
  134. pass
  135. else:
  136. if isinstance(inferred, Const):
  137. if isinstance(inferred.value, (int, type(None))):
  138. return inferred.value
  139. # Use a sentinel, because None can be a valid
  140. # value that this function can return,
  141. # as it is the case for unspecified bounds.
  142. return _SLICE_SENTINEL
  143. def _infer_slice(node, context=None):
  144. lower = _slice_value(node.lower, context)
  145. upper = _slice_value(node.upper, context)
  146. step = _slice_value(node.step, context)
  147. if all(elem is not _SLICE_SENTINEL for elem in (lower, upper, step)):
  148. return slice(lower, upper, step)
  149. raise exceptions.AstroidTypeError(
  150. message='Could not infer slice used in subscript',
  151. node=node, index=node.parent, context=context)
  152. def _container_getitem(instance, elts, index, context=None):
  153. """Get a slice or an item, using the given *index*, for the given sequence."""
  154. try:
  155. if isinstance(index, Slice):
  156. index_slice = _infer_slice(index, context=context)
  157. new_cls = instance.__class__()
  158. new_cls.elts = elts[index_slice]
  159. new_cls.parent = instance.parent
  160. return new_cls
  161. elif isinstance(index, Const):
  162. return elts[index.value]
  163. except IndexError:
  164. util.reraise(exceptions.AstroidIndexError(
  165. message='Index {index!s} out of range',
  166. node=instance, index=index, context=context))
  167. except TypeError as exc:
  168. util.reraise(exceptions.AstroidTypeError(
  169. message='Type error {error!r}', error=exc,
  170. node=instance, index=index, context=context))
  171. raise exceptions.AstroidTypeError(
  172. 'Could not use %s as subscript index' % index
  173. )
  174. class NodeNG(object):
  175. """ A node of the new Abstract Syntax Tree (AST).
  176. This is the base class for all Astroid node classes.
  177. """
  178. is_statement = False
  179. """Whether this node indicates a statement.
  180. :type: bool
  181. """
  182. optional_assign = False # True for For (and for Comprehension if py <3.0)
  183. """Whether this node optionally assigns a variable.
  184. This is for loop assignments because loop won't necessarily perform an
  185. assignment if the loop has no iterations.
  186. This is also the case from comprehensions in Python 2.
  187. :type: bool
  188. """
  189. is_function = False # True for FunctionDef nodes
  190. """Whether this node indicates a function.
  191. :type: bool
  192. """
  193. # Attributes below are set by the builder module or by raw factories
  194. lineno = None
  195. """The line that this node appears on in the source code.
  196. :type: int or None
  197. """
  198. col_offset = None
  199. """The column that this node appears on in the source code.
  200. :type: int or None
  201. """
  202. parent = None
  203. """The parent node in the syntax tree.
  204. :type: NodeNG or None
  205. """
  206. _astroid_fields = ()
  207. """Node attributes that contain child nodes.
  208. This is redefined in most concrete classes.
  209. :type: tuple(str)
  210. """
  211. _other_fields = ()
  212. """Node attributes that do not contain child nodes.
  213. :type: tuple(str)
  214. """
  215. _other_other_fields = ()
  216. """Attributes that contain AST-dependent fields.
  217. :type: tuple(str)
  218. """
  219. # instance specific inference function infer(node, context)
  220. _explicit_inference = None
  221. def __init__(self, lineno=None, col_offset=None, parent=None):
  222. """
  223. :param lineno: The line that this node appears on in the source code.
  224. :type lineno: int or None
  225. :param col_offset: The column that this node appears on in the
  226. source code.
  227. :type col_offset: int or None
  228. :param parent: The parent node in the syntax tree.
  229. :type parent: NodeNG or None
  230. """
  231. self.lineno = lineno
  232. self.col_offset = col_offset
  233. self.parent = parent
  234. def infer(self, context=None, **kwargs):
  235. """Get a generator of the inferred values.
  236. This is the main entry point to the inference system.
  237. .. seealso:: :ref:`inference`
  238. If the instance has some explicit inference function set, it will be
  239. called instead of the default interface.
  240. :returns: The inferred values.
  241. :rtype: iterable
  242. """
  243. if self._explicit_inference is not None:
  244. # explicit_inference is not bound, give it self explicitly
  245. try:
  246. # pylint: disable=not-callable
  247. return self._explicit_inference(self, context, **kwargs)
  248. except exceptions.UseInferenceDefault:
  249. pass
  250. if not context:
  251. return self._infer(context, **kwargs)
  252. key = (self, context.lookupname,
  253. context.callcontext, context.boundnode)
  254. if key in context.inferred:
  255. return iter(context.inferred[key])
  256. return context.cache_generator(key, self._infer(context, **kwargs))
  257. def _repr_name(self):
  258. """Get a name for nice representation.
  259. This is either :attr:`name`, :attr:`attrname`, or the empty string.
  260. :returns: The nice name.
  261. :rtype: str
  262. """
  263. return getattr(self, 'name', getattr(self, 'attrname', ''))
  264. def __str__(self):
  265. rname = self._repr_name()
  266. cname = type(self).__name__
  267. if rname:
  268. string = '%(cname)s.%(rname)s(%(fields)s)'
  269. alignment = len(cname) + len(rname) + 2
  270. else:
  271. string = '%(cname)s(%(fields)s)'
  272. alignment = len(cname) + 1
  273. result = []
  274. for field in self._other_fields + self._astroid_fields:
  275. value = getattr(self, field)
  276. width = 80 - len(field) - alignment
  277. lines = pprint.pformat(value, indent=2,
  278. width=width).splitlines(True)
  279. inner = [lines[0]]
  280. for line in lines[1:]:
  281. inner.append(' ' * alignment + line)
  282. result.append('%s=%s' % (field, ''.join(inner)))
  283. return string % {'cname': cname,
  284. 'rname': rname,
  285. 'fields': (',\n' + ' ' * alignment).join(result)}
  286. def __repr__(self):
  287. rname = self._repr_name()
  288. if rname:
  289. string = '<%(cname)s.%(rname)s l.%(lineno)s at 0x%(id)x>'
  290. else:
  291. string = '<%(cname)s l.%(lineno)s at 0x%(id)x>'
  292. return string % {'cname': type(self).__name__,
  293. 'rname': rname,
  294. 'lineno': self.fromlineno,
  295. 'id': id(self)}
  296. def accept(self, visitor):
  297. """Visit this node using the given visitor."""
  298. func = getattr(visitor, "visit_" + self.__class__.__name__.lower())
  299. return func(self)
  300. def get_children(self):
  301. """Get the child nodes below this node.
  302. :returns: The children.
  303. :rtype: iterable(NodeNG)
  304. """
  305. for field in self._astroid_fields:
  306. attr = getattr(self, field)
  307. if attr is None:
  308. continue
  309. if isinstance(attr, (list, tuple)):
  310. for elt in attr:
  311. yield elt
  312. else:
  313. yield attr
  314. def last_child(self):
  315. """An optimized version of list(get_children())[-1]
  316. :returns: The last child, or None if no children exist.
  317. :rtype: NodeNG or None
  318. """
  319. for field in self._astroid_fields[::-1]:
  320. attr = getattr(self, field)
  321. if not attr: # None or empty listy / tuple
  322. continue
  323. if isinstance(attr, (list, tuple)):
  324. return attr[-1]
  325. return attr
  326. return None
  327. def parent_of(self, node):
  328. """Check if this node is the parent of the given node.
  329. :param node: The node to check if it is the child.
  330. :type node: NodeNG
  331. :returns: True if this node is the parent of the given node,
  332. False otherwise.
  333. :rtype: bool
  334. """
  335. parent = node.parent
  336. while parent is not None:
  337. if self is parent:
  338. return True
  339. parent = parent.parent
  340. return False
  341. def statement(self):
  342. """The first parent node, including self, marked as statement node.
  343. :returns: The first parent statement.
  344. :rtype: NodeNG
  345. """
  346. if self.is_statement:
  347. return self
  348. return self.parent.statement()
  349. def frame(self):
  350. """The first parent frame node.
  351. A frame node is a :class:`Module`, :class:`FunctionDef`,
  352. or :class:`ClassDef`.
  353. :returns: The first parent frame node.
  354. :rtype: Module or FunctionDef or ClassDef
  355. """
  356. return self.parent.frame()
  357. def scope(self):
  358. """The first parent node defining a new scope.
  359. :returns: The first parent scope node.
  360. :rtype: Module or FunctionDef or ClassDef or Lambda or GenExpr
  361. """
  362. return self.parent.scope()
  363. def root(self):
  364. """Return the root node of the syntax tree.
  365. :returns: The root node.
  366. :rtype: Module
  367. """
  368. if self.parent:
  369. return self.parent.root()
  370. return self
  371. def child_sequence(self, child):
  372. """Search for the sequence that contains this child.
  373. :param child: The child node to search sequences for.
  374. :type child: NodeNG
  375. :returns: The sequence containing the given child node.
  376. :rtype: iterable(NodeNG)
  377. :raises AstroidError: If no sequence could be found that contains
  378. the given child.
  379. """
  380. for field in self._astroid_fields:
  381. node_or_sequence = getattr(self, field)
  382. if node_or_sequence is child:
  383. return [node_or_sequence]
  384. # /!\ compiler.ast Nodes have an __iter__ walking over child nodes
  385. if (isinstance(node_or_sequence, (tuple, list))
  386. and child in node_or_sequence):
  387. return node_or_sequence
  388. msg = 'Could not find %s in %s\'s children'
  389. raise exceptions.AstroidError(msg % (repr(child), repr(self)))
  390. def locate_child(self, child):
  391. """Find the field of this node that contains the given child.
  392. :param child: The child node to search fields for.
  393. :type child: NodeNG
  394. :returns: A tuple of the name of the field that contains the child,
  395. and the sequence or node that contains the child node.
  396. :rtype: tuple(str, iterable(NodeNG) or NodeNG)
  397. :raises AstroidError: If no field could be found that contains
  398. the given child.
  399. """
  400. for field in self._astroid_fields:
  401. node_or_sequence = getattr(self, field)
  402. # /!\ compiler.ast Nodes have an __iter__ walking over child nodes
  403. if child is node_or_sequence:
  404. return field, child
  405. if isinstance(node_or_sequence, (tuple, list)) and child in node_or_sequence:
  406. return field, node_or_sequence
  407. msg = 'Could not find %s in %s\'s children'
  408. raise exceptions.AstroidError(msg % (repr(child), repr(self)))
  409. # FIXME : should we merge child_sequence and locate_child ? locate_child
  410. # is only used in are_exclusive, child_sequence one time in pylint.
  411. def next_sibling(self):
  412. """The next sibling statement node.
  413. :returns: The next sibling statement node.
  414. :rtype: NodeNG or None
  415. """
  416. return self.parent.next_sibling()
  417. def previous_sibling(self):
  418. """The previous sibling statement.
  419. :returns: The previous sibling statement node.
  420. :rtype: NodeNG or None
  421. """
  422. return self.parent.previous_sibling()
  423. def nearest(self, nodes):
  424. """Get the node closest to this one from the given list of nodes.
  425. :param nodes: The list of nodes to search. All of these nodes must
  426. belong to the same module as this one. The list should be
  427. sorted by the line number of the nodes, smallest first.
  428. :type nodes: iterable(NodeNG)
  429. :returns: The node closest to this one in the source code,
  430. or None if one could not be found.
  431. :rtype: NodeNG or None
  432. """
  433. myroot = self.root()
  434. mylineno = self.fromlineno
  435. nearest = None, 0
  436. for node in nodes:
  437. assert node.root() is myroot, \
  438. 'nodes %s and %s are not from the same module' % (self, node)
  439. lineno = node.fromlineno
  440. if node.fromlineno > mylineno:
  441. break
  442. if lineno > nearest[1]:
  443. nearest = node, lineno
  444. # FIXME: raise an exception if nearest is None ?
  445. return nearest[0]
  446. # these are lazy because they're relatively expensive to compute for every
  447. # single node, and they rarely get looked at
  448. @decorators.cachedproperty
  449. def fromlineno(self):
  450. """The first line that this node appears on in the source code.
  451. :type: int or None
  452. """
  453. if self.lineno is None:
  454. return self._fixed_source_line()
  455. return self.lineno
  456. @decorators.cachedproperty
  457. def tolineno(self):
  458. """The last line that this node appears on in the source code.
  459. :type: int or None
  460. """
  461. if not self._astroid_fields:
  462. # can't have children
  463. lastchild = None
  464. else:
  465. lastchild = self.last_child()
  466. if lastchild is None:
  467. return self.fromlineno
  468. return lastchild.tolineno
  469. def _fixed_source_line(self):
  470. """Attempt to find the line that this node appears on.
  471. We need this method since not all nodes have :attr:`lineno` set.
  472. :returns: The line number of this node,
  473. or None if this could not be determined.
  474. :rtype: int or None
  475. """
  476. line = self.lineno
  477. _node = self
  478. try:
  479. while line is None:
  480. _node = next(_node.get_children())
  481. line = _node.lineno
  482. except StopIteration:
  483. _node = self.parent
  484. while _node and line is None:
  485. line = _node.lineno
  486. _node = _node.parent
  487. return line
  488. def block_range(self, lineno):
  489. """Get a range from the given line number to where this node ends.
  490. :param lineno: The line number to start the range at.
  491. :type lineno: int
  492. :returns: The range of line numbers that this node belongs to,
  493. starting at the given line number.
  494. :rtype: tuple(int, int or None)
  495. """
  496. return lineno, self.tolineno
  497. def set_local(self, name, stmt):
  498. """Define that the given name is declared in the given statement node.
  499. This definition is stored on the parent scope node.
  500. .. seealso:: :meth:`scope`
  501. :param name: The name that is being defined.
  502. :type name: str
  503. :param stmt: The statement that defines the given name.
  504. :type stmt: NodeNG
  505. """
  506. self.parent.set_local(name, stmt)
  507. def nodes_of_class(self, klass, skip_klass=None):
  508. """Get the nodes (including this one or below) of the given type.
  509. :param klass: The type of node to search for.
  510. :type klass: builtins.type
  511. :param skip_klass: A type of node to ignore. This is useful to ignore
  512. subclasses of :attr:`klass`.
  513. :type skip_klass: builtins.type
  514. :returns: The node of the given type.
  515. :rtype: iterable(NodeNG)
  516. """
  517. if isinstance(self, klass):
  518. yield self
  519. for child_node in self.get_children():
  520. if skip_klass is not None and isinstance(child_node, skip_klass):
  521. continue
  522. for matching in child_node.nodes_of_class(klass, skip_klass):
  523. yield matching
  524. def _infer_name(self, frame, name): #pylint: disable=useless-return
  525. # overridden for ImportFrom, Import, Global, TryExcept and Arguments
  526. return None
  527. def _infer(self, context=None):
  528. """we don't know how to resolve a statement by default"""
  529. # this method is overridden by most concrete classes
  530. raise exceptions.InferenceError('No inference function for {node!r}.',
  531. node=self, context=context)
  532. def inferred(self):
  533. """Get a list of the inferred values.
  534. .. seealso:: :ref:`inference`
  535. :returns: The inferred values.
  536. :rtype: list
  537. """
  538. return list(self.infer())
  539. def infered(self):
  540. """A deprecated alias of :meth:`inferred`.
  541. .. deprecated:: 1.5
  542. :returns: The inferred values.
  543. :rtype: list
  544. """
  545. warnings.warn('%s.infered() is deprecated and slated for removal '
  546. 'in astroid 2.0, use %s.inferred() instead.'
  547. % (type(self).__name__, type(self).__name__),
  548. PendingDeprecationWarning, stacklevel=2)
  549. return self.inferred()
  550. def instantiate_class(self):
  551. """Instantiate a instance of the defined class.
  552. .. note::
  553. On anything other than a :class:`ClassDef` this will return self.
  554. :returns: An instance of the defined class.
  555. :rtype: object
  556. """
  557. return self
  558. def has_base(self, node):
  559. """Check if this node inherits from the given type.
  560. :param node: The node defining the base to look for.
  561. Usually this is a :class:`Name` node.
  562. :type node: NodeNG
  563. """
  564. return False
  565. def callable(self):
  566. """Whether this node defines something that is callable.
  567. :returns: True if this defines something that is callable,
  568. False otherwise.
  569. :rtype: bool
  570. """
  571. return False
  572. def eq(self, value):
  573. return False
  574. def as_string(self):
  575. """Get the source code that this node represents.
  576. :returns: The source code.
  577. :rtype: str
  578. """
  579. return as_string.to_code(self)
  580. def repr_tree(self, ids=False, include_linenos=False,
  581. ast_state=False, indent=' ', max_depth=0, max_width=80):
  582. """Get a string representation of the AST from this node.
  583. :param ids: If true, includes the ids with the node type names.
  584. :type ids: bool
  585. :param include_linenos: If true, includes the line numbers and
  586. column offsets.
  587. :type include_linenos: bool
  588. :param ast_state: If true, includes information derived from
  589. the whole AST like local and global variables.
  590. :type ast_state: bool
  591. :param indent: A string to use to indent the output string.
  592. :type indent: str
  593. :param max_depth: If set to a positive integer, won't return
  594. nodes deeper than max_depth in the string.
  595. :type max_depth: int
  596. :param max_width: Attempt to format the output string to stay
  597. within this number of characters, but can exceed it under some
  598. circumstances. Only positive integer values are valid, the default is 80.
  599. :type max_width: int
  600. :returns: The string representation of the AST.
  601. :rtype: str
  602. """
  603. @_singledispatch
  604. def _repr_tree(node, result, done, cur_indent='', depth=1):
  605. """Outputs a representation of a non-tuple/list, non-node that's
  606. contained within an AST, including strings.
  607. """
  608. lines = pprint.pformat(node,
  609. width=max(max_width - len(cur_indent),
  610. 1)).splitlines(True)
  611. result.append(lines[0])
  612. result.extend([cur_indent + line for line in lines[1:]])
  613. return len(lines) != 1
  614. # pylint: disable=unused-variable; doesn't understand singledispatch
  615. @_repr_tree.register(tuple)
  616. @_repr_tree.register(list)
  617. def _repr_seq(node, result, done, cur_indent='', depth=1):
  618. """Outputs a representation of a sequence that's contained within an AST."""
  619. cur_indent += indent
  620. result.append('[')
  621. if not node:
  622. broken = False
  623. elif len(node) == 1:
  624. broken = _repr_tree(node[0], result, done, cur_indent, depth)
  625. elif len(node) == 2:
  626. broken = _repr_tree(node[0], result, done, cur_indent, depth)
  627. if not broken:
  628. result.append(', ')
  629. else:
  630. result.append(',\n')
  631. result.append(cur_indent)
  632. broken = (_repr_tree(node[1], result, done, cur_indent, depth)
  633. or broken)
  634. else:
  635. result.append('\n')
  636. result.append(cur_indent)
  637. for child in node[:-1]:
  638. _repr_tree(child, result, done, cur_indent, depth)
  639. result.append(',\n')
  640. result.append(cur_indent)
  641. _repr_tree(node[-1], result, done, cur_indent, depth)
  642. broken = True
  643. result.append(']')
  644. return broken
  645. # pylint: disable=unused-variable; doesn't understand singledispatch
  646. @_repr_tree.register(NodeNG)
  647. def _repr_node(node, result, done, cur_indent='', depth=1):
  648. """Outputs a strings representation of an astroid node."""
  649. if node in done:
  650. result.append(indent + '<Recursion on %s with id=%s' %
  651. (type(node).__name__, id(node)))
  652. return False
  653. else:
  654. done.add(node)
  655. if max_depth and depth > max_depth:
  656. result.append('...')
  657. return False
  658. depth += 1
  659. cur_indent += indent
  660. if ids:
  661. result.append('%s<0x%x>(\n' % (type(node).__name__, id(node)))
  662. else:
  663. result.append('%s(' % type(node).__name__)
  664. fields = []
  665. if include_linenos:
  666. fields.extend(('lineno', 'col_offset'))
  667. fields.extend(node._other_fields)
  668. fields.extend(node._astroid_fields)
  669. if ast_state:
  670. fields.extend(node._other_other_fields)
  671. if not fields:
  672. broken = False
  673. elif len(fields) == 1:
  674. result.append('%s=' % fields[0])
  675. broken = _repr_tree(getattr(node, fields[0]), result, done,
  676. cur_indent, depth)
  677. else:
  678. result.append('\n')
  679. result.append(cur_indent)
  680. for field in fields[:-1]:
  681. result.append('%s=' % field)
  682. _repr_tree(getattr(node, field), result, done, cur_indent,
  683. depth)
  684. result.append(',\n')
  685. result.append(cur_indent)
  686. result.append('%s=' % fields[-1])
  687. _repr_tree(getattr(node, fields[-1]), result, done, cur_indent,
  688. depth)
  689. broken = True
  690. result.append(')')
  691. return broken
  692. result = []
  693. _repr_tree(self, result, set())
  694. return ''.join(result)
  695. def bool_value(self):
  696. """Determine the boolean value of this node.
  697. The boolean value of a node can have three
  698. possible values:
  699. * False: For instance, empty data structures,
  700. False, empty strings, instances which return
  701. explicitly False from the __nonzero__ / __bool__
  702. method.
  703. * True: Most of constructs are True by default:
  704. classes, functions, modules etc
  705. * Uninferable: The inference engine is uncertain of the
  706. node's value.
  707. :returns: The boolean value of this node.
  708. :rtype: bool or Uninferable
  709. """
  710. return util.Uninferable
  711. class Statement(NodeNG):
  712. """Statement node adding a few attributes"""
  713. is_statement = True
  714. """Whether this node indicates a statement.
  715. :type: bool
  716. """
  717. def next_sibling(self):
  718. """The next sibling statement node.
  719. :returns: The next sibling statement node.
  720. :rtype: NodeNG or None
  721. """
  722. stmts = self.parent.child_sequence(self)
  723. index = stmts.index(self)
  724. try:
  725. return stmts[index +1]
  726. except IndexError:
  727. pass
  728. def previous_sibling(self):
  729. """The previous sibling statement.
  730. :returns: The previous sibling statement node.
  731. :rtype: NodeNG or None
  732. """
  733. stmts = self.parent.child_sequence(self)
  734. index = stmts.index(self)
  735. if index >= 1:
  736. return stmts[index -1]
  737. return None
  738. @six.add_metaclass(abc.ABCMeta)
  739. class _BaseContainer(mixins.ParentAssignTypeMixin,
  740. NodeNG, bases.Instance):
  741. """Base class for Set, FrozenSet, Tuple and List."""
  742. _astroid_fields = ('elts',)
  743. def __init__(self, lineno=None, col_offset=None, parent=None):
  744. """
  745. :param lineno: The line that this node appears on in the source code.
  746. :type lineno: int or None
  747. :param col_offset: The column that this node appears on in the
  748. source code.
  749. :type col_offset: int or None
  750. :param parent: The parent node in the syntax tree.
  751. :type parent: NodeNG or None
  752. """
  753. self.elts = []
  754. """The elements in the node.
  755. :type: list(NodeNG)
  756. """
  757. super(_BaseContainer, self).__init__(lineno, col_offset, parent)
  758. def postinit(self, elts):
  759. """Do some setup after initialisation.
  760. :param elts: The list of elements the that node contains.
  761. :type elts: list(NodeNG)
  762. """
  763. self.elts = elts
  764. @classmethod
  765. def from_constants(cls, elts=None):
  766. """Create a node of this type from the given list of elements.
  767. :param elts: The list of elements that the node should contain.
  768. :type elts: list(NodeNG)
  769. :returns: A new node containing the given elements.
  770. :rtype: NodeNG
  771. """
  772. node = cls()
  773. if elts is None:
  774. node.elts = []
  775. else:
  776. node.elts = [const_factory(e) for e in elts]
  777. return node
  778. def itered(self):
  779. """An iterator over the elements this node contains.
  780. :returns: The contents of this node.
  781. :rtype: iterable(NodeNG)
  782. """
  783. return self.elts
  784. def bool_value(self):
  785. """Determine the boolean value of this node.
  786. :returns: The boolean value of this node.
  787. :rtype: bool or Uninferable
  788. """
  789. return bool(self.elts)
  790. @abc.abstractmethod
  791. def pytype(self):
  792. """Get the name of the type that this node represents.
  793. :returns: The name of the type.
  794. :rtype: str
  795. """
  796. class LookupMixIn(object):
  797. """Mixin to look up a name in the right scope."""
  798. def lookup(self, name):
  799. """Lookup where the given variable is assigned.
  800. The lookup starts from self's scope. If self is not a frame itself
  801. and the name is found in the inner frame locals, statements will be
  802. filtered to remove ignorable statements according to self's location.
  803. :param name: The name of the variable to find assignments for.
  804. :type name: str
  805. :returns: The scope node and the list of assignments associated to the
  806. given name according to the scope where it has been found (locals,
  807. globals or builtin).
  808. :rtype: tuple(str, list(NodeNG))
  809. """
  810. return self.scope().scope_lookup(self, name)
  811. def ilookup(self, name):
  812. """Lookup the inferred values of the given variable.
  813. :param name: The variable name to find values for.
  814. :type name: str
  815. :returns: The inferred values of the statements returned from
  816. :meth:`lookup`.
  817. :rtype: iterable
  818. """
  819. frame, stmts = self.lookup(name)
  820. context = contextmod.InferenceContext()
  821. return bases._infer_stmts(stmts, context, frame)
  822. def _filter_stmts(self, stmts, frame, offset):
  823. """Filter the given list of statements to remove ignorable statements.
  824. If self is not a frame itself and the name is found in the inner
  825. frame locals, statements will be filtered to remove ignorable
  826. statements according to self's location.
  827. :param stmts: The statements to filter.
  828. :type stmts: list(NodeNG)
  829. :param frame: The frame that all of the given statements belong to.
  830. :type frame: NodeNG
  831. :param offset: The line offset to filter statements up to.
  832. :type offset: int
  833. :returns: The filtered statements.
  834. :rtype: list(NodeNG)
  835. """
  836. # if offset == -1, my actual frame is not the inner frame but its parent
  837. #
  838. # class A(B): pass
  839. #
  840. # we need this to resolve B correctly
  841. if offset == -1:
  842. myframe = self.frame().parent.frame()
  843. else:
  844. myframe = self.frame()
  845. # If the frame of this node is the same as the statement
  846. # of this node, then the node is part of a class or
  847. # a function definition and the frame of this node should be the
  848. # the upper frame, not the frame of the definition.
  849. # For more information why this is important,
  850. # see Pylint issue #295.
  851. # For example, for 'b', the statement is the same
  852. # as the frame / scope:
  853. #
  854. # def test(b=1):
  855. # ...
  856. if self.statement() is myframe and myframe.parent:
  857. myframe = myframe.parent.frame()
  858. mystmt = self.statement()
  859. # line filtering if we are in the same frame
  860. #
  861. # take care node may be missing lineno information (this is the case for
  862. # nodes inserted for living objects)
  863. if myframe is frame and mystmt.fromlineno is not None:
  864. assert mystmt.fromlineno is not None, mystmt
  865. mylineno = mystmt.fromlineno + offset
  866. else:
  867. # disabling lineno filtering
  868. mylineno = 0
  869. _stmts = []
  870. _stmt_parents = []
  871. for node in stmts:
  872. stmt = node.statement()
  873. # line filtering is on and we have reached our location, break
  874. if mylineno > 0 and stmt.fromlineno > mylineno:
  875. break
  876. assert hasattr(node, 'assign_type'), (node, node.scope(),
  877. node.scope().locals)
  878. assign_type = node.assign_type()
  879. if node.has_base(self):
  880. break
  881. _stmts, done = assign_type._get_filtered_stmts(self, node, _stmts, mystmt)
  882. if done:
  883. break
  884. optional_assign = assign_type.optional_assign
  885. if optional_assign and assign_type.parent_of(self):
  886. # we are inside a loop, loop var assignment is hiding previous
  887. # assignment
  888. _stmts = [node]
  889. _stmt_parents = [stmt.parent]
  890. continue
  891. # XXX comment various branches below!!!
  892. try:
  893. pindex = _stmt_parents.index(stmt.parent)
  894. except ValueError:
  895. pass
  896. else:
  897. # we got a parent index, this means the currently visited node
  898. # is at the same block level as a previously visited node
  899. if _stmts[pindex].assign_type().parent_of(assign_type):
  900. # both statements are not at the same block level
  901. continue
  902. # if currently visited node is following previously considered
  903. # assignment and both are not exclusive, we can drop the
  904. # previous one. For instance in the following code ::
  905. #
  906. # if a:
  907. # x = 1
  908. # else:
  909. # x = 2
  910. # print x
  911. #
  912. # we can't remove neither x = 1 nor x = 2 when looking for 'x'
  913. # of 'print x'; while in the following ::
  914. #
  915. # x = 1
  916. # x = 2
  917. # print x
  918. #
  919. # we can remove x = 1 when we see x = 2
  920. #
  921. # moreover, on loop assignment types, assignment won't
  922. # necessarily be done if the loop has no iteration, so we don't
  923. # want to clear previous assignments if any (hence the test on
  924. # optional_assign)
  925. if not (optional_assign or are_exclusive(_stmts[pindex], node)):
  926. del _stmt_parents[pindex]
  927. del _stmts[pindex]
  928. if isinstance(node, AssignName):
  929. if not optional_assign and stmt.parent is mystmt.parent:
  930. _stmts = []
  931. _stmt_parents = []
  932. elif isinstance(node, DelName):
  933. _stmts = []
  934. _stmt_parents = []
  935. continue
  936. if not are_exclusive(self, node):
  937. _stmts.append(node)
  938. _stmt_parents.append(stmt.parent)
  939. return _stmts
  940. # Name classes
  941. class AssignName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG):
  942. """Variation of :class:`ast.Assign` representing assignment to a name.
  943. An :class:`AssignName` is the name of something that is assigned to.
  944. This includes variables defined in a function signature or in a loop.
  945. >>> node = astroid.extract_node('variable = range(10)')
  946. >>> node
  947. <Assign l.1 at 0x7effe1db8550>
  948. >>> list(node.get_children())
  949. [<AssignName.variable l.1 at 0x7effe1db8748>, <Call l.1 at 0x7effe1db8630>]
  950. >>> list(node.get_children())[0].as_string()
  951. 'variable'
  952. """
  953. _other_fields = ('name',)
  954. def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
  955. """
  956. :param name: The name that is assigned to.
  957. :type name: str or None
  958. :param lineno: The line that this node appears on in the source code.
  959. :type lineno: int or None
  960. :param col_offset: The column that this node appears on in the
  961. source code.
  962. :type col_offset: int or None
  963. :param parent: The parent node in the syntax tree.
  964. :type parent: NodeNG or None
  965. """
  966. self.name = name
  967. """The name that is assigned to.
  968. :type: str or None
  969. """
  970. super(AssignName, self).__init__(lineno, col_offset, parent)
  971. class DelName(LookupMixIn, mixins.ParentAssignTypeMixin, NodeNG):
  972. """Variation of :class:`ast.Delete` represention deletion of a name.
  973. A :class:`DelName` is the name of something that is deleted.
  974. >>> node = astroid.extract_node("del variable #@")
  975. >>> list(node.get_children())
  976. [<DelName.variable l.1 at 0x7effe1da4d30>]
  977. >>> list(node.get_children())[0].as_string()
  978. 'variable'
  979. """
  980. _other_fields = ('name',)
  981. def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
  982. """
  983. :param name: The name that is being deleted.
  984. :type name: str or None
  985. :param lineno: The line that this node appears on in the source code.
  986. :type lineno: int or None
  987. :param col_offset: The column that this node appears on in the
  988. source code.
  989. :type col_offset: int or None
  990. :param parent: The parent node in the syntax tree.
  991. :type parent: NodeNG or None
  992. """
  993. self.name = name
  994. """The name that is being deleted.
  995. :type: str or None
  996. """
  997. super(DelName, self).__init__(lineno, col_offset, parent)
  998. class Name(LookupMixIn, NodeNG):
  999. """Class representing an :class:`ast.Name` node.
  1000. A :class:`Name` node is something that is named, but not covered by
  1001. :class:`AssignName` or :class:`DelName`.
  1002. >>> node = astroid.extract_node('range(10)')
  1003. >>> node
  1004. <Call l.1 at 0x7effe1db8710>
  1005. >>> list(node.get_children())
  1006. [<Name.range l.1 at 0x7effe1db86a0>, <Const.int l.1 at 0x7effe1db8518>]
  1007. >>> list(node.get_children())[0].as_string()
  1008. 'range'
  1009. """
  1010. _other_fields = ('name',)
  1011. def __init__(self, name=None, lineno=None, col_offset=None, parent=None):
  1012. """
  1013. :param name: The name that this node refers to.
  1014. :type name: str or None
  1015. :param lineno: The line that this node appears on in the source code.
  1016. :type lineno: int or None
  1017. :param col_offset: The column that this node appears on in the
  1018. source code.
  1019. :type col_offset: int or None
  1020. :param parent: The parent node in the syntax tree.
  1021. :type parent: NodeNG or None
  1022. """
  1023. self.name = name
  1024. """The name that this node refers to.
  1025. :type: str or None
  1026. """
  1027. super(Name, self).__init__(lineno, col_offset, parent)
  1028. class Arguments(mixins.AssignTypeMixin, NodeNG):
  1029. """Class representing an :class:`ast.arguments` node.
  1030. An :class:`Arguments` node represents that arguments in a
  1031. function definition.
  1032. >>> node = astroid.extract_node('def foo(bar): pass')
  1033. >>> node
  1034. <FunctionDef.foo l.1 at 0x7effe1db8198>
  1035. >>> node.args
  1036. <Arguments l.1 at 0x7effe1db82e8>
  1037. """
  1038. if six.PY3:
  1039. # Python 3.4+ uses a different approach regarding annotations,
  1040. # each argument is a new class, _ast.arg, which exposes an
  1041. # 'annotation' attribute. In astroid though, arguments are exposed
  1042. # as is in the Arguments node and the only way to expose annotations
  1043. # is by using something similar with Python 3.3:
  1044. # - we expose 'varargannotation' and 'kwargannotation' of annotations
  1045. # of varargs and kwargs.
  1046. # - we expose 'annotation', a list with annotations for
  1047. # for each normal argument. If an argument doesn't have an
  1048. # annotation, its value will be None.
  1049. _astroid_fields = ('args', 'defaults', 'kwonlyargs',
  1050. 'kw_defaults', 'annotations', 'varargannotation',
  1051. 'kwargannotation', 'kwonlyargs_annotations')
  1052. varargannotation = None
  1053. """The type annotation for the variable length arguments.
  1054. :type: NodeNG
  1055. """
  1056. kwargannotation = None
  1057. """The type annotation for the variable length keyword arguments.
  1058. :type: NodeNG
  1059. """
  1060. else:
  1061. _astroid_fields = ('args', 'defaults', 'kwonlyargs', 'kw_defaults')
  1062. _other_fields = ('vararg', 'kwarg')
  1063. def __init__(self, vararg=None, kwarg=None, parent=None):
  1064. """
  1065. :param vararg: The name of the variable length arguments.
  1066. :type vararg: str or None
  1067. :param kwarg: The name of the variable length keyword arguments.
  1068. :type kwarg: str or None
  1069. :param parent: The parent node in the syntax tree.
  1070. :type parent: NodeNG or None
  1071. """
  1072. super(Arguments, self).__init__(parent=parent)
  1073. self.vararg = vararg
  1074. """The name of the variable length arguments.
  1075. :type: str or None
  1076. """
  1077. self.kwarg = kwarg
  1078. """The name of the variable length keyword arguments.
  1079. :type: str or None
  1080. """
  1081. self.args = []
  1082. """The names of the required arguments.
  1083. :type: list(AssignName)
  1084. """
  1085. self.defaults = []
  1086. """The default values for arguments that can be passed positionally.
  1087. :type: list(NodeNG)
  1088. """
  1089. self.kwonlyargs = []
  1090. """The keyword arguments that cannot be passed positionally.
  1091. :type: list(AssignName)
  1092. """
  1093. self.kw_defaults = []
  1094. """The default values for keyword arguments that cannot be passed positionally.
  1095. :type: list(NodeNG)
  1096. """
  1097. self.annotations = []
  1098. """The type annotations of arguments that can be passed positionally.
  1099. :type: list(NodeNG)
  1100. """
  1101. self.kwonlyargs_annotations = []
  1102. """The type annotations of arguments that cannot be passed positionally.
  1103. :type: list(NodeNG)
  1104. """
  1105. def postinit(self, args, defaults, kwonlyargs, kw_defaults,
  1106. annotations,
  1107. kwonlyargs_annotations=None,
  1108. varargannotation=None,
  1109. kwargannotation=None):
  1110. """Do some setup after initialisation.
  1111. :param args: The names of the required arguments.
  1112. :type args: list(AssignName)
  1113. :param defaults: The default values for arguments that can be passed
  1114. positionally.
  1115. :type defaults: list(NodeNG)
  1116. :param kwonlyargs: The keyword arguments that cannot be passed
  1117. positionally.
  1118. :type kwonlyargs: list(AssignName)
  1119. :param kw_defaults: The default values for keyword arguments that
  1120. cannot be passed positionally.
  1121. :type kw_defaults: list(NodeNG)
  1122. :param annotations: The type annotations of arguments that can be
  1123. passed positionally.
  1124. :type annotations: list(NodeNG)
  1125. :param kwonlyargs_annotations: The type annotations of arguments that
  1126. cannot be passed positionally. This should always be passed in
  1127. Python 3.
  1128. :type kwonlyargs_annotations: list(NodeNG)
  1129. :param varargannotation: The type annotation for the variable length
  1130. arguments.
  1131. :type varargannotation: NodeNG
  1132. :param kwargannotation: The type annotation for the variable length
  1133. keyword arguments.
  1134. :type kwargannotation: NodeNG
  1135. """
  1136. self.args = args
  1137. self.defaults = defaults
  1138. self.kwonlyargs = kwonlyargs
  1139. self.kw_defaults = kw_defaults
  1140. self.annotations = annotations
  1141. self.kwonlyargs_annotations = kwonlyargs_annotations
  1142. self.varargannotation = varargannotation
  1143. self.kwargannotation = kwargannotation
  1144. def _infer_name(self, frame, name):
  1145. if self.parent is frame:
  1146. return name
  1147. return None
  1148. @decorators.cachedproperty
  1149. def fromlineno(self):
  1150. """The first line that this node appears on in the source code.
  1151. :type: int or None
  1152. """
  1153. lineno = super(Arguments, self).fromlineno
  1154. return max(lineno, self.parent.fromlineno or 0)
  1155. def format_args(self):
  1156. """Get the arguments formatted as string.
  1157. :returns: The formatted arguments.
  1158. :rtype: str
  1159. """
  1160. result = []
  1161. if self.args:
  1162. result.append(
  1163. _format_args(self.args, self.defaults,
  1164. getattr(self, 'annotations', None))
  1165. )
  1166. if self.vararg:
  1167. result.append('*%s' % self.vararg)
  1168. if self.kwonlyargs:
  1169. if not self.vararg:
  1170. result.append('*')
  1171. result.append(_format_args(
  1172. self.kwonlyargs,
  1173. self.kw_defaults,
  1174. self.kwonlyargs_annotations
  1175. ))
  1176. if self.kwarg:
  1177. result.append('**%s' % self.kwarg)
  1178. return ', '.join(result)
  1179. def default_value(self, argname):
  1180. """Get the default value for an argument.
  1181. :param argname: The name of the argument to get the default value for.
  1182. :type argname: str
  1183. :raises NoDefault: If there is no default value defined for the
  1184. given argument.
  1185. """
  1186. i = _find_arg(argname, self.args)[0]
  1187. if i is not None:
  1188. idx = i - (len(self.args) - len(self.defaults))
  1189. if idx >= 0:
  1190. return self.defaults[idx]
  1191. i = _find_arg(argname, self.kwonlyargs)[0]
  1192. if i is not None and self.kw_defaults[i] is not None:
  1193. return self.kw_defaults[i]
  1194. raise exceptions.NoDefault(func=self.parent, name=argname)
  1195. def is_argument(self, name):
  1196. """Check if the given name is defined in the arguments.
  1197. :param name: The name to check for.
  1198. :type name: str
  1199. :returns: True if the given name is defined in the arguments,
  1200. False otherwise.
  1201. :rtype: bool
  1202. """
  1203. if name == self.vararg:
  1204. return True
  1205. if name == self.kwarg:
  1206. return True
  1207. return (self.find_argname(name, True)[1] is not None or
  1208. self.kwonlyargs and _find_arg(name, self.kwonlyargs, True)[1] is not None)
  1209. def find_argname(self, argname, rec=False):
  1210. """Get the index and :class:`AssignName` node for given name.
  1211. :param argname: The name of the argument to search for.
  1212. :type argname: str
  1213. :param rec: Whether or not to include arguments in unpacked tuples
  1214. in the search.
  1215. :type rec: bool
  1216. :returns: The index and node for the argument.
  1217. :rtype: tuple(str or None, AssignName or None)
  1218. """
  1219. if self.args: # self.args may be None in some cases (builtin function)
  1220. return _find_arg(argname, self.args, rec)
  1221. return None, None
  1222. def get_children(self):
  1223. """Get the child nodes below this node.
  1224. This skips over `None` elements in :attr:`kw_defaults`.
  1225. :returns: The children.
  1226. :rtype: iterable(NodeNG)
  1227. """
  1228. for child in super(Arguments, self).get_children():
  1229. if child is not None:
  1230. yield child
  1231. def _find_arg(argname, args, rec=False):
  1232. for i, arg in enumerate(args):
  1233. if isinstance(arg, Tuple):
  1234. if rec:
  1235. found = _find_arg(argname, arg.elts)
  1236. if found[0] is not None:
  1237. return found
  1238. elif arg.name == argname:
  1239. return i, arg
  1240. return None, None
  1241. def _format_args(args, defaults=None, annotations=None):
  1242. values = []
  1243. if args is None:
  1244. return ''
  1245. if annotations is None:
  1246. annotations = []
  1247. if defaults is not None:
  1248. default_offset = len(args) - len(defaults)
  1249. packed = six.moves.zip_longest(args, annotations)
  1250. for i, (arg, annotation) in enumerate(packed):
  1251. if isinstance(arg, Tuple):
  1252. values.append('(%s)' % _format_args(arg.elts))
  1253. else:
  1254. argname = arg.name
  1255. if annotation is not None:
  1256. argname += ':' + annotation.as_string()
  1257. values.append(argname)
  1258. if defaults is not None and i >= default_offset:
  1259. if defaults[i-default_offset] is not None:
  1260. values[-1] += '=' + defaults[i-default_offset].as_string()
  1261. return ', '.join(values)
  1262. class AssignAttr(mixins.ParentAssignTypeMixin, NodeNG):
  1263. """Variation of :class:`ast.Assign` representing assignment to an attribute.
  1264. >>> node = astroid.extract_node('self.attribute = range(10)')
  1265. >>> node
  1266. <Assign l.1 at 0x7effe1d521d0>
  1267. >>> list(node.get_children())
  1268. [<AssignAttr.attribute l.1 at 0x7effe1d52320>, <Call l.1 at 0x7effe1d522e8>]
  1269. >>> list(node.get_children())[0].as_string()
  1270. 'self.attribute'
  1271. """
  1272. _astroid_fields = ('expr',)
  1273. _other_fields = ('attrname',)
  1274. expr = None
  1275. """What has the attribute that is being assigned to.
  1276. :type: NodeNG or None
  1277. """
  1278. def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
  1279. """
  1280. :param attrname: The name of the attribute being assigned to.
  1281. :type attrname: str or None
  1282. :param lineno: The line that this node appears on in the source code.
  1283. :type lineno: int or None
  1284. :param col_offset: The column that this node appears on in the
  1285. source code.
  1286. :type col_offset: int or None
  1287. :param parent: The parent node in the syntax tree.
  1288. :type parent: NodeNG or None
  1289. """
  1290. self.attrname = attrname
  1291. """The name of the attribute being assigned to.
  1292. :type: str or None
  1293. """
  1294. super(AssignAttr, self).__init__(lineno, col_offset, parent)
  1295. def postinit(self, expr=None):
  1296. """Do some setup after initialisation.
  1297. :param expr: What has the attribute that is being assigned to.
  1298. :type expr: NodeNG or None
  1299. """
  1300. self.expr = expr
  1301. class Assert(Statement):
  1302. """Class representing an :class:`ast.Assert` node.
  1303. An :class:`Assert` node represents an assert statement.
  1304. >>> node = astroid.extract_node('assert len(things) == 10, "Not enough things"')
  1305. >>> node
  1306. <Assert l.1 at 0x7effe1d527b8>
  1307. """
  1308. _astroid_fields = ('test', 'fail',)
  1309. test = None
  1310. """The test that passes or fails the assertion.
  1311. :type: NodeNG or None
  1312. """
  1313. fail = None
  1314. """The message shown when the assertion fails.
  1315. :type: NodeNG or None
  1316. """
  1317. def postinit(self, test=None, fail=None):
  1318. """Do some setup after initialisation.
  1319. :param test: The test that passes or fails the assertion.
  1320. :type test: NodeNG or None
  1321. :param fail: The message shown when the assertion fails.
  1322. :type fail: NodeNG or None
  1323. """
  1324. self.fail = fail
  1325. self.test = test
  1326. class Assign(mixins.AssignTypeMixin, Statement):
  1327. """Class representing an :class:`ast.Assign` node.
  1328. An :class:`Assign` is a statement where something is explicitly
  1329. asssigned to.
  1330. >>> node = astroid.extract_node('variable = range(10)')
  1331. >>> node
  1332. <Assign l.1 at 0x7effe1db8550>
  1333. """
  1334. _astroid_fields = ('targets', 'value',)
  1335. targets = None
  1336. """What is being assigned to.
  1337. :type: list(NodeNG) or None
  1338. """
  1339. value = None
  1340. """The value being assigned to the variables.
  1341. :type: NodeNG or None
  1342. """
  1343. def postinit(self, targets=None, value=None):
  1344. """Do some setup after initialisation.
  1345. :param targets: What is being assigned to.
  1346. :type targets: list(NodeNG) or None
  1347. :param value: The value being assigned to the variables.
  1348. :type: NodeNG or None
  1349. """
  1350. self.targets = targets
  1351. self.value = value
  1352. class AnnAssign(mixins.AssignTypeMixin, Statement):
  1353. """Class representing an :class:`ast.AnnAssign` node.
  1354. An :class:`AnnAssign` is an assignment with a type annotation.
  1355. >>> node = astroid.extract_node('variable: List[int] = range(10)')
  1356. >>> node
  1357. <AnnAssign l.1 at 0x7effe1d4c630>
  1358. """
  1359. _astroid_fields = ('target', 'annotation', 'value',)
  1360. _other_fields = ('simple',)
  1361. target = None
  1362. """What is being assigned to.
  1363. :type: NodeNG or None
  1364. """
  1365. annotation = None
  1366. """The type annotation of what is being assigned to.
  1367. :type: NodeNG
  1368. """
  1369. value = None
  1370. """The value being assigned to the variables.
  1371. :type: NodeNG or None
  1372. """
  1373. simple = None
  1374. """Whether :attr:`target` is a pure name or a complex statement.
  1375. :type: int
  1376. """
  1377. def postinit(self, target, annotation, simple, value=None):
  1378. """Do some setup after initialisation.
  1379. :param target: What is being assigned to.
  1380. :type target: NodeNG
  1381. :param annotation: The type annotation of what is being assigned to.
  1382. :type: NodeNG
  1383. :param simple: Whether :attr:`target` is a pure name
  1384. or a complex statement.
  1385. :type simple: int
  1386. :param value: The value being assigned to the variables.
  1387. :type: NodeNG or None
  1388. """
  1389. self.target = target
  1390. self.annotation = annotation
  1391. self.value = value
  1392. self.simple = simple
  1393. class AugAssign(mixins.AssignTypeMixin, Statement):
  1394. """Class representing an :class:`ast.AugAssign` node.
  1395. An :class:`AugAssign` is an assignment paired with an operator.
  1396. >>> node = astroid.extract_node('variable += 1')
  1397. >>> node
  1398. <AugAssign l.1 at 0x7effe1db4d68>
  1399. """
  1400. _astroid_fields = ('target', 'value')
  1401. _other_fields = ('op',)
  1402. target = None
  1403. """What is being assigned to.
  1404. :type: NodeNG or None
  1405. """
  1406. value = None
  1407. """The value being assigned to the variable.
  1408. :type: NodeNG or None
  1409. """
  1410. def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
  1411. """
  1412. :param op: The operator that is being combined with the assignment.
  1413. This includes the equals sign.
  1414. :type op: str or None
  1415. :param lineno: The line that this node appears on in the source code.
  1416. :type lineno: int or None
  1417. :param col_offset: The column that this node appears on in the
  1418. source code.
  1419. :type col_offset: int or None
  1420. :param parent: The parent node in the syntax tree.
  1421. :type parent: NodeNG or None
  1422. """
  1423. self.op = op
  1424. """The operator that is being combined with the assignment.
  1425. This includes the equals sign.
  1426. :type: str or None
  1427. """
  1428. super(AugAssign, self).__init__(lineno, col_offset, parent)
  1429. def postinit(self, target=None, value=None):
  1430. """Do some setup after initialisation.
  1431. :param target: What is being assigned to.
  1432. :type target: NodeNG or None
  1433. :param value: The value being assigned to the variable.
  1434. :type: NodeNG or None
  1435. """
  1436. self.target = target
  1437. self.value = value
  1438. # This is set by inference.py
  1439. def _infer_augassign(self, context=None):
  1440. raise NotImplementedError
  1441. def type_errors(self, context=None):
  1442. """Get a list of type errors which can occur during inference.
  1443. Each TypeError is represented by a :class:`BadBinaryOperationMessage` ,
  1444. which holds the original exception.
  1445. :returns: The list of possible type errors.
  1446. :rtype: list(BadBinaryOperationMessage)
  1447. """
  1448. try:
  1449. results = self._infer_augassign(context=context)
  1450. return [result for result in results
  1451. if isinstance(result, util.BadBinaryOperationMessage)]
  1452. except exceptions.InferenceError:
  1453. return []
  1454. class Repr(NodeNG):
  1455. """Class representing an :class:`ast.Repr` node.
  1456. A :class:`Repr` node represents the backtick syntax,
  1457. which is a deprecated alias for :func:`repr` removed in Python 3.
  1458. >>> node = astroid.extract_node('`variable`')
  1459. >>> node
  1460. <Repr l.1 at 0x7fa0951d75d0>
  1461. """
  1462. _astroid_fields = ('value',)
  1463. value = None
  1464. """What is having :func:`repr` called on it.
  1465. :type: NodeNG or None
  1466. """
  1467. def postinit(self, value=None):
  1468. """Do some setup after initialisation.
  1469. :param value: What is having :func:`repr` called on it.
  1470. :type value: NodeNG or None
  1471. """
  1472. self.value = value
  1473. class BinOp(NodeNG):
  1474. """Class representing an :class:`ast.BinOp` node.
  1475. A :class:`BinOp` node is an application of a binary operator.
  1476. >>> node = astroid.extract_node('a + b')
  1477. >>> node
  1478. <BinOp l.1 at 0x7f23b2e8cfd0>
  1479. """
  1480. _astroid_fields = ('left', 'right')
  1481. _other_fields = ('op',)
  1482. left = None
  1483. """What is being applied to the operator on the left side.
  1484. :type: NodeNG or None
  1485. """
  1486. right = None
  1487. """What is being applied to the operator on the right side.
  1488. :type: NodeNG or None
  1489. """
  1490. def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
  1491. """
  1492. :param op: The operator.
  1493. :type: str or None
  1494. :param lineno: The line that this node appears on in the source code.
  1495. :type lineno: int or None
  1496. :param col_offset: The column that this node appears on in the
  1497. source code.
  1498. :type col_offset: int or None
  1499. :param parent: The parent node in the syntax tree.
  1500. :type parent: NodeNG or None
  1501. """
  1502. self.op = op
  1503. """The operator.
  1504. :type: str or None
  1505. """
  1506. super(BinOp, self).__init__(lineno, col_offset, parent)
  1507. def postinit(self, left=None, right=None):
  1508. """Do some setup after initialisation.
  1509. :param left: What is being applied to the operator on the left side.
  1510. :type left: NodeNG or None
  1511. :param right: What is being applied to the operator on the right side.
  1512. :type right: NodeNG or None
  1513. """
  1514. self.left = left
  1515. self.right = right
  1516. # This is set by inference.py
  1517. def _infer_binop(self, context=None):
  1518. raise NotImplementedError
  1519. def type_errors(self, context=None):
  1520. """Get a list of type errors which can occur during inference.
  1521. Each TypeError is represented by a :class:`BadBinaryOperationMessage`,
  1522. which holds the original exception.
  1523. :returns: The list of possible type errors.
  1524. :rtype: list(BadBinaryOperationMessage)
  1525. """
  1526. try:
  1527. results = self._infer_binop(context=context)
  1528. return [result for result in results
  1529. if isinstance(result, util.BadBinaryOperationMessage)]
  1530. except exceptions.InferenceError:
  1531. return []
  1532. class BoolOp(NodeNG):
  1533. """Class representing an :class:`ast.BoolOp` node.
  1534. A :class:`BoolOp` is an application of a boolean operator.
  1535. >>> node = astroid.extract_node('a and b')
  1536. >>> node
  1537. <BinOp l.1 at 0x7f23b2e71c50>
  1538. """
  1539. _astroid_fields = ('values',)
  1540. _other_fields = ('op',)
  1541. values = None
  1542. """The values being applied to the operator.
  1543. :type: list(NodeNG) or None
  1544. """
  1545. def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
  1546. """
  1547. :param op: The operator.
  1548. :type: str or None
  1549. :param lineno: The line that this node appears on in the source code.
  1550. :type lineno: int or None
  1551. :param col_offset: The column that this node appears on in the
  1552. source code.
  1553. :type col_offset: int or None
  1554. :param parent: The parent node in the syntax tree.
  1555. :type parent: NodeNG or None
  1556. """
  1557. self.op = op
  1558. """The operator.
  1559. :type: str or None
  1560. """
  1561. super(BoolOp, self).__init__(lineno, col_offset, parent)
  1562. def postinit(self, values=None):
  1563. """Do some setup after initialisation.
  1564. :param values: The values being applied to the operator.
  1565. :type values: list(NodeNG) or None
  1566. """
  1567. self.values = values
  1568. class Break(Statement):
  1569. """Class representing an :class:`ast.Break` node.
  1570. >>> node = astroid.extract_node('break')
  1571. >>> node
  1572. <Break l.1 at 0x7f23b2e9e5c0>
  1573. """
  1574. class Call(NodeNG):
  1575. """Class representing an :class:`ast.Call` node.
  1576. A :class:`Call` node is a call to a function, method, etc.
  1577. >>> node = astroid.extract_node('function()')
  1578. >>> node
  1579. <Call l.1 at 0x7f23b2e71eb8>
  1580. """
  1581. _astroid_fields = ('func', 'args', 'keywords')
  1582. func = None
  1583. """What is being called.
  1584. :type: NodeNG or None
  1585. """
  1586. args = None
  1587. """The positional arguments being given to the call.
  1588. :type: list(NodeNG) or None
  1589. """
  1590. keywords = None
  1591. """The keyword arguments being given to the call.
  1592. :type: list(NodeNG) or None
  1593. """
  1594. def postinit(self, func=None, args=None, keywords=None):
  1595. """Do some setup after initialisation.
  1596. :param func: What is being called.
  1597. :type func: NodeNG or None
  1598. :param args: The positional arguments being given to the call.
  1599. :type args: list(NodeNG) or None
  1600. :param keywords: The keyword arguments being given to the call.
  1601. :type keywords: list(NodeNG) or None
  1602. """
  1603. self.func = func
  1604. self.args = args
  1605. self.keywords = keywords
  1606. @property
  1607. def starargs(self):
  1608. """The positional arguments that unpack something.
  1609. :type: list(Starred)
  1610. """
  1611. args = self.args or []
  1612. return [arg for arg in args if isinstance(arg, Starred)]
  1613. @property
  1614. def kwargs(self):
  1615. """The keyword arguments that unpack something.
  1616. :type: list(Keyword)
  1617. """
  1618. keywords = self.keywords or []
  1619. return [keyword for keyword in keywords if keyword.arg is None]
  1620. class Compare(NodeNG):
  1621. """Class representing an :class:`ast.Compare` node.
  1622. A :class:`Compare` node indicates a comparison.
  1623. >>> node = astroid.extract_node('a <= b <= c')
  1624. >>> node
  1625. <Compare l.1 at 0x7f23b2e9e6d8>
  1626. >>> node.ops
  1627. [('<=', <Name.b l.1 at 0x7f23b2e9e2b0>), ('<=', <Name.c l.1 at 0x7f23b2e9e390>)]
  1628. """
  1629. _astroid_fields = ('left', 'ops',)
  1630. left = None
  1631. """The value at the left being applied to a comparison operator.
  1632. :type: NodeNG or None
  1633. """
  1634. ops = None
  1635. """The remainder of the operators and their relevant right hand value.
  1636. :type: list(tuple(str, NodeNG)) or None
  1637. """
  1638. def postinit(self, left=None, ops=None):
  1639. """Do some setup after initialisation.
  1640. :param left: The value at the left being applied to a comparison
  1641. operator.
  1642. :type left: NodeNG or None
  1643. :param ops: The remainder of the operators
  1644. and their relevant right hand value.
  1645. :type ops: list(tuple(str, NodeNG)) or None
  1646. """
  1647. self.left = left
  1648. self.ops = ops
  1649. def get_children(self):
  1650. """Get the child nodes below this node.
  1651. Overridden to handle the tuple fields and skip returning the operator
  1652. strings.
  1653. :returns: The children.
  1654. :rtype: iterable(NodeNG)
  1655. """
  1656. yield self.left
  1657. for _, comparator in self.ops:
  1658. yield comparator # we don't want the 'op'
  1659. def last_child(self):
  1660. """An optimized version of list(get_children())[-1]
  1661. :returns: The last child.
  1662. :rtype: NodeNG
  1663. """
  1664. # XXX maybe if self.ops:
  1665. return self.ops[-1][1]
  1666. #return self.left
  1667. class Comprehension(NodeNG):
  1668. """Class representing an :class:`ast.comprehension` node.
  1669. A :class:`Comprehension` indicates the loop inside any type of
  1670. comprehension including generator expressions.
  1671. >>> node = astroid.extract_node('[x for x in some_values]')
  1672. >>> list(node.get_children())
  1673. [<Name.x l.1 at 0x7f23b2e352b0>, <Comprehension l.1 at 0x7f23b2e35320>]
  1674. >>> list(node.get_children())[1].as_string()
  1675. 'for x in some_values'
  1676. """
  1677. _astroid_fields = ('target', 'iter', 'ifs')
  1678. _other_fields = ('is_async',)
  1679. target = None
  1680. """What is assigned to by the comprehension.
  1681. :type: NodeNG or None
  1682. """
  1683. iter = None
  1684. """What is iterated over by the comprehension.
  1685. :type: NodeNG or None
  1686. """
  1687. ifs = None
  1688. """The contents of any if statements that filter the comprehension.
  1689. :type: list(NodeNG) or None
  1690. """
  1691. is_async = None
  1692. """Whether this is an asynchronous comprehension or not.
  1693. :type: bool or None
  1694. """
  1695. def __init__(self, parent=None):
  1696. """
  1697. :param parent: The parent node in the syntax tree.
  1698. :type parent: NodeNG or None
  1699. """
  1700. super(Comprehension, self).__init__()
  1701. self.parent = parent
  1702. # pylint: disable=redefined-builtin; same name as builtin ast module.
  1703. def postinit(self, target=None, iter=None, ifs=None, is_async=None):
  1704. """Do some setup after initialisation.
  1705. :param target: What is assigned to by the comprehension.
  1706. :type target: NodeNG or None
  1707. :param iter: What is iterated over by the comprehension.
  1708. :type iter: NodeNG or None
  1709. :param ifs: The contents of any if statements that filter
  1710. the comprehension.
  1711. :type ifs: list(NodeNG) or None
  1712. :param is_async: Whether this is an asynchronous comprehension or not.
  1713. :type: bool or None
  1714. """
  1715. self.target = target
  1716. self.iter = iter
  1717. self.ifs = ifs
  1718. self.is_async = is_async
  1719. optional_assign = True
  1720. """Whether this node optionally assigns a variable.
  1721. :type: bool
  1722. """
  1723. def assign_type(self):
  1724. """The type of assignment that this node performs.
  1725. :returns: The assignment type.
  1726. :rtype: NodeNG
  1727. """
  1728. return self
  1729. def ass_type(self):
  1730. """A deprecated alias of :meth:`assign_type`.
  1731. .. deprecated:: 1.5
  1732. :returns: The assignment type.
  1733. :rtype: NodeNG
  1734. """
  1735. warnings.warn('%s.ass_type() is deprecated and slated for removal'
  1736. 'in astroid 2.0, use %s.assign_type() instead.'
  1737. % (type(self).__name__, type(self).__name__),
  1738. PendingDeprecationWarning, stacklevel=2)
  1739. return self.assign_type()
  1740. def _get_filtered_stmts(self, lookup_node, node, stmts, mystmt):
  1741. """method used in filter_stmts"""
  1742. if self is mystmt:
  1743. if isinstance(lookup_node, (Const, Name)):
  1744. return [lookup_node], True
  1745. elif self.statement() is mystmt:
  1746. # original node's statement is the assignment, only keeps
  1747. # current node (gen exp, list comp)
  1748. return [node], True
  1749. return stmts, False
  1750. class Const(NodeNG, bases.Instance):
  1751. """Class representing any constant including num, str, bool, None, bytes.
  1752. >>> node = astroid.extract_node('(5, "This is a string.", True, None, b"bytes")')
  1753. >>> node
  1754. <Tuple.tuple l.1 at 0x7f23b2e358d0>
  1755. >>> list(node.get_children())
  1756. [<Const.int l.1 at 0x7f23b2e35940>,
  1757. <Const.str l.1 at 0x7f23b2e35978>,
  1758. <Const.bool l.1 at 0x7f23b2e359b0>,
  1759. <Const.NoneType l.1 at 0x7f23b2e359e8>,
  1760. <Const.bytes l.1 at 0x7f23b2e35a20>]
  1761. """
  1762. _other_fields = ('value',)
  1763. def __init__(self, value, lineno=None, col_offset=None, parent=None):
  1764. """
  1765. :param value: The value that the constant represents.
  1766. :type value: object
  1767. :param lineno: The line that this node appears on in the source code.
  1768. :type lineno: int or None
  1769. :param col_offset: The column that this node appears on in the
  1770. source code.
  1771. :type col_offset: int or None
  1772. :param parent: The parent node in the syntax tree.
  1773. :type parent: NodeNG or None
  1774. """
  1775. self.value = value
  1776. """The value that the constant represents.
  1777. :type: object
  1778. """
  1779. super(Const, self).__init__(lineno, col_offset, parent)
  1780. def getitem(self, index, context=None):
  1781. """Get an item from this node if subscriptable.
  1782. :param index: The node to use as a subscript index.
  1783. :type index: Const or Slice
  1784. :raises AstroidTypeError: When the given index cannot be used as a
  1785. subscript index, or if this node is not subscriptable.
  1786. """
  1787. if isinstance(index, Const):
  1788. index_value = index.value
  1789. elif isinstance(index, Slice):
  1790. index_value = _infer_slice(index, context=context)
  1791. else:
  1792. raise exceptions.AstroidTypeError(
  1793. 'Could not use type {} as subscript index'.format(type(index))
  1794. )
  1795. try:
  1796. if isinstance(self.value, six.string_types):
  1797. return Const(self.value[index_value])
  1798. if isinstance(self.value, bytes) and six.PY3:
  1799. # Bytes aren't instances of six.string_types
  1800. # on Python 3. Also, indexing them should return
  1801. # integers.
  1802. return Const(self.value[index_value])
  1803. except IndexError as exc:
  1804. util.reraise(exceptions.AstroidIndexError(
  1805. message='Index {index!r} out of range', error=exc,
  1806. node=self, index=index, context=context))
  1807. except TypeError as exc:
  1808. util.reraise(exceptions.AstroidTypeError(
  1809. message='Type error {error!r}', error=exc,
  1810. node=self, index=index, context=context))
  1811. raise exceptions.AstroidTypeError(
  1812. '%r (value=%s)' % (self, self.value)
  1813. )
  1814. def has_dynamic_getattr(self):
  1815. """Check if the node has a custom __getattr__ or __getattribute__.
  1816. :returns: True if the class has a custom
  1817. __getattr__ or __getattribute__, False otherwise.
  1818. For a :class:`Const` this is always ``False``.
  1819. :rtype: bool
  1820. """
  1821. return False
  1822. def itered(self):
  1823. """An iterator over the elements this node contains.
  1824. :returns: The contents of this node.
  1825. :rtype: iterable(str)
  1826. :raises TypeError: If this node does not represent something that is iterable.
  1827. """
  1828. if isinstance(self.value, six.string_types):
  1829. return self.value
  1830. raise TypeError()
  1831. def pytype(self):
  1832. """Get the name of the type that this node represents.
  1833. :returns: The name of the type.
  1834. :rtype: str
  1835. """
  1836. return self._proxied.qname()
  1837. def bool_value(self):
  1838. """Determine the boolean value of this node.
  1839. :returns: The boolean value of this node.
  1840. :rtype: bool
  1841. """
  1842. return bool(self.value)
  1843. class Continue(Statement):
  1844. """Class representing an :class:`ast.Continue` node.
  1845. >>> node = astroid.extract_node('continue')
  1846. >>> node
  1847. <Continue l.1 at 0x7f23b2e35588>
  1848. """
  1849. class Decorators(NodeNG):
  1850. """A node representing a list of decorators.
  1851. A :class:`Decorators` is the decorators that are applied to
  1852. a method or function.
  1853. >>> node = astroid.extract_node('''
  1854. @property
  1855. def my_property(self):
  1856. return 3
  1857. ''')
  1858. >>> node
  1859. <FunctionDef.my_property l.2 at 0x7f23b2e35d30>
  1860. >>> list(node.get_children())[0]
  1861. <Decorators l.1 at 0x7f23b2e35d68>
  1862. """
  1863. _astroid_fields = ('nodes',)
  1864. nodes = None
  1865. """The decorators that this node contains.
  1866. :type: list(Name or Call) or None
  1867. """
  1868. def postinit(self, nodes):
  1869. """Do some setup after initialisation.
  1870. :param nodes: The decorators that this node contains.
  1871. :type nodes: list(Name or Call)
  1872. """
  1873. self.nodes = nodes
  1874. def scope(self):
  1875. """The first parent node defining a new scope.
  1876. :returns: The first parent scope node.
  1877. :rtype: Module or FunctionDef or ClassDef or Lambda or GenExpr
  1878. """
  1879. # skip the function node to go directly to the upper level scope
  1880. return self.parent.parent.scope()
  1881. class DelAttr(mixins.ParentAssignTypeMixin, NodeNG):
  1882. """Variation of :class:`ast.Delete` representing deletion of an attribute.
  1883. >>> node = astroid.extract_node('del self.attr')
  1884. >>> node
  1885. <Delete l.1 at 0x7f23b2e35f60>
  1886. >>> list(node.get_children())[0]
  1887. <DelAttr.attr l.1 at 0x7f23b2e411d0>
  1888. """
  1889. _astroid_fields = ('expr',)
  1890. _other_fields = ('attrname',)
  1891. expr = None
  1892. """The name that this node represents.
  1893. :type: Name or None
  1894. """
  1895. def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
  1896. """
  1897. :param attrname: The name of the attribute that is being deleted.
  1898. :type attrname: str or None
  1899. :param lineno: The line that this node appears on in the source code.
  1900. :type lineno: int or None
  1901. :param col_offset: The column that this node appears on in the
  1902. source code.
  1903. :type col_offset: int or None
  1904. :param parent: The parent node in the syntax tree.
  1905. :type parent: NodeNG or None
  1906. """
  1907. self.attrname = attrname
  1908. """The name of the attribute that is being deleted.
  1909. :type: str or None
  1910. """
  1911. super(DelAttr, self).__init__(lineno, col_offset, parent)
  1912. def postinit(self, expr=None):
  1913. """Do some setup after initialisation.
  1914. :param expr: The name that this node represents.
  1915. :type expr: Name or None
  1916. """
  1917. self.expr = expr
  1918. class Delete(mixins.AssignTypeMixin, Statement):
  1919. """Class representing an :class:`ast.Delete` node.
  1920. A :class:`Delete` is a ``del`` statement this is deleting something.
  1921. >>> node = astroid.extract_node('del self.attr')
  1922. >>> node
  1923. <Delete l.1 at 0x7f23b2e35f60>
  1924. """
  1925. _astroid_fields = ('targets',)
  1926. targets = None
  1927. """What is being deleted.
  1928. :type: list(NodeNG) or None
  1929. """
  1930. def postinit(self, targets=None):
  1931. """Do some setup after initialisation.
  1932. :param targets: What is being deleted.
  1933. :type targets: list(NodeNG) or None
  1934. """
  1935. self.targets = targets
  1936. class Dict(NodeNG, bases.Instance):
  1937. """Class representing an :class:`ast.Dict` node.
  1938. A :class:`Dict` is a dictionary that is created with ``{}`` syntax.
  1939. >>> node = astroid.extract_node('{1: "1"}')
  1940. >>> node
  1941. <Dict.dict l.1 at 0x7f23b2e35cc0>
  1942. """
  1943. _astroid_fields = ('items',)
  1944. def __init__(self, lineno=None, col_offset=None, parent=None):
  1945. """
  1946. :param lineno: The line that this node appears on in the source code.
  1947. :type lineno: int or None
  1948. :param col_offset: The column that this node appears on in the
  1949. source code.
  1950. :type col_offset: int or None
  1951. :param parent: The parent node in the syntax tree.
  1952. :type parent: NodeNG or None
  1953. """
  1954. self.items = []
  1955. """The key-value pairs contained in the dictionary.
  1956. :type: list(tuple(NodeNG, NodeNG))
  1957. """
  1958. super(Dict, self).__init__(lineno, col_offset, parent)
  1959. def postinit(self, items):
  1960. """Do some setup after initialisation.
  1961. :param items: The ley-value pairs contained in the dictionary.
  1962. :type items: list(tuple(NodeNG, NodeNG))
  1963. """
  1964. self.items = items
  1965. @classmethod
  1966. def from_constants(cls, items=None):
  1967. """Create a :class:`Dict` of constants from a live dictionary.
  1968. :param items: The items to store in the node.
  1969. :type items: dict
  1970. :returns: The created dictionary node.
  1971. :rtype: Dict
  1972. """
  1973. node = cls()
  1974. if items is None:
  1975. node.items = []
  1976. else:
  1977. node.items = [(const_factory(k), const_factory(v))
  1978. for k, v in items.items()]
  1979. return node
  1980. def pytype(self):
  1981. """Get the name of the type that this node represents.
  1982. :returns: The name of the type.
  1983. :rtype: str
  1984. """
  1985. return '%s.dict' % BUILTINS
  1986. def get_children(self):
  1987. """Get the key and value nodes below this node.
  1988. Children are returned in the order that they are defined in the source
  1989. code, key first then the value.
  1990. :returns: The children.
  1991. :rtype: iterable(NodeNG)
  1992. """
  1993. for key, value in self.items:
  1994. yield key
  1995. yield value
  1996. def last_child(self):
  1997. """An optimized version of list(get_children())[-1]
  1998. :returns: The last child, or None if no children exist.
  1999. :rtype: NodeNG or None
  2000. """
  2001. if self.items:
  2002. return self.items[-1][1]
  2003. return None
  2004. def itered(self):
  2005. """An iterator over the keys this node contains.
  2006. :returns: The keys of this node.
  2007. :rtype: iterable(NodeNG)
  2008. """
  2009. return self.items[::2]
  2010. def getitem(self, index, context=None):
  2011. """Get an item from this node.
  2012. :param index: The node to use as a subscript index.
  2013. :type index: Const or Slice
  2014. :raises AstroidTypeError: When the given index cannot be used as a
  2015. subscript index, or if this node is not subscriptable.
  2016. :raises AstroidIndexError: If the given index does not exist in the
  2017. dictionary.
  2018. """
  2019. for key, value in self.items:
  2020. # TODO(cpopa): no support for overriding yet, {1:2, **{1: 3}}.
  2021. if isinstance(key, DictUnpack):
  2022. try:
  2023. return value.getitem(index, context)
  2024. except (exceptions.AstroidTypeError, exceptions.AstroidIndexError):
  2025. continue
  2026. for inferredkey in key.infer(context):
  2027. if inferredkey is util.Uninferable:
  2028. continue
  2029. if isinstance(inferredkey, Const) and isinstance(index, Const):
  2030. if inferredkey.value == index.value:
  2031. return value
  2032. raise exceptions.AstroidIndexError(index)
  2033. def bool_value(self):
  2034. """Determine the boolean value of this node.
  2035. :returns: The boolean value of this node.
  2036. :rtype: bool
  2037. """
  2038. return bool(self.items)
  2039. class Expr(Statement):
  2040. """Class representing an :class:`ast.Expr` node.
  2041. An :class:`Expr` is any expression that does not have its value used or
  2042. stored.
  2043. >>> node = astroid.extract_node('method()')
  2044. >>> node
  2045. <Call l.1 at 0x7f23b2e352b0>
  2046. >>> node.parent
  2047. <Expr l.1 at 0x7f23b2e35278>
  2048. """
  2049. _astroid_fields = ('value',)
  2050. value = None
  2051. """What the expression does.
  2052. :type: NodeNG or None
  2053. """
  2054. def postinit(self, value=None):
  2055. """Do some setup after initialisation.
  2056. :param value: What the expression does.
  2057. :type value: NodeNG or None
  2058. """
  2059. self.value = value
  2060. class Ellipsis(NodeNG): # pylint: disable=redefined-builtin
  2061. """Class representing an :class:`ast.Ellipsis` node.
  2062. An :class:`Ellipsis` is the ``...`` syntax.
  2063. >>> node = astroid.extract_node('...')
  2064. >>> node
  2065. <Ellipsis l.1 at 0x7f23b2e35160>
  2066. """
  2067. def bool_value(self):
  2068. """Determine the boolean value of this node.
  2069. :returns: The boolean value of this node.
  2070. For an :class:`Ellipsis` this is always ``True``.
  2071. :rtype: bool
  2072. """
  2073. return True
  2074. class EmptyNode(NodeNG):
  2075. """Holds an arbitrary object in the :attr:`LocalsDictNodeNG.locals`."""
  2076. object = None
  2077. class ExceptHandler(mixins.AssignTypeMixin, Statement):
  2078. """Class representing an :class:`ast.ExceptHandler`. node.
  2079. An :class:`ExceptHandler` is an ``except`` block on a try-except.
  2080. >>> node = astroid.extract_node('''
  2081. try:
  2082. do_something()
  2083. except Exception as error:
  2084. print("Error!")
  2085. ''')
  2086. >>> node
  2087. <TryExcept l.2 at 0x7f23b2e9d908>
  2088. >>> >>> node.handlers
  2089. [<ExceptHandler l.4 at 0x7f23b2e9e860>]
  2090. """
  2091. _astroid_fields = ('type', 'name', 'body',)
  2092. type = None
  2093. """The types that the block handles.
  2094. :type: Tuple or NodeNG or None
  2095. """
  2096. name = None
  2097. """The name that the caught exception is assigned to.
  2098. :type: AssignName or None
  2099. """
  2100. body = None
  2101. """The contents of the block.
  2102. :type: list(NodeNG) or None
  2103. """
  2104. # pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
  2105. def postinit(self, type=None, name=None, body=None):
  2106. """Do some setup after initialisation.
  2107. :param type: The types that the block handles.
  2108. :type type: Tuple or NodeNG or None
  2109. :param name: The name that the caught exception is assigned to.
  2110. :type name: AssignName or None
  2111. :param body:The contents of the block.
  2112. :type body: list(NodeNG) or None
  2113. """
  2114. self.type = type
  2115. self.name = name
  2116. self.body = body
  2117. @decorators.cachedproperty
  2118. def blockstart_tolineno(self):
  2119. """The line on which the beginning of this block ends.
  2120. :type: int
  2121. """
  2122. if self.name:
  2123. return self.name.tolineno
  2124. elif self.type:
  2125. return self.type.tolineno
  2126. return self.lineno
  2127. def catch(self, exceptions): # pylint: disable=redefined-outer-name
  2128. """Check if this node handles any of the given exceptions.
  2129. If ``exceptions`` is empty, this will default to ``True``.
  2130. :param exceptions: The name of the exceptions to check for.
  2131. :type exceptions: list(str)
  2132. """
  2133. if self.type is None or exceptions is None:
  2134. return True
  2135. for node in self.type.nodes_of_class(Name):
  2136. if node.name in exceptions:
  2137. return True
  2138. return False
  2139. class Exec(Statement):
  2140. """Class representing the ``exec`` statement.
  2141. >>> node = astroid.extract_node('exec "True"')
  2142. >>> node
  2143. <Exec l.1 at 0x7f0e8106c6d0>
  2144. """
  2145. _astroid_fields = ('expr', 'globals', 'locals',)
  2146. expr = None
  2147. """The expression to be executed.
  2148. :type: NodeNG or None
  2149. """
  2150. globals = None
  2151. """The globals dictionary to execute with.
  2152. :type: NodeNG or None
  2153. """
  2154. locals = None
  2155. """The locals dictionary to execute with.
  2156. :type: NodeNG or None
  2157. """
  2158. # pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
  2159. def postinit(self, expr=None, globals=None, locals=None):
  2160. """Do some setup after initialisation.
  2161. :param expr: The expression to be executed.
  2162. :type expr: NodeNG or None
  2163. :param globals:The globals dictionary to execute with.
  2164. :type globals: NodeNG or None
  2165. :param locals: The locals dictionary to execute with.
  2166. :type locals: NodeNG or None
  2167. """
  2168. self.expr = expr
  2169. self.globals = globals
  2170. self.locals = locals
  2171. class ExtSlice(NodeNG):
  2172. """Class representing an :class:`ast.ExtSlice` node.
  2173. An :class:`ExtSlice` is a complex slice expression.
  2174. >>> node = astroid.extract_node('l[1:3, 5]')
  2175. >>> node
  2176. <Subscript l.1 at 0x7f23b2e9e550>
  2177. >>> node.slice
  2178. <ExtSlice l.1 at 0x7f23b7b05ef0>
  2179. """
  2180. _astroid_fields = ('dims',)
  2181. dims = None
  2182. """The simple dimensions that form the complete slice.
  2183. :type: list(NodeNG) or None
  2184. """
  2185. def postinit(self, dims=None):
  2186. """Do some setup after initialisation.
  2187. :param dims: The simple dimensions that form the complete slice.
  2188. :type dims: list(NodeNG) or None
  2189. """
  2190. self.dims = dims
  2191. class For(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
  2192. """Class representing an :class:`ast.For` node.
  2193. >>> node = astroid.extract_node('for thing in things: print(thing)')
  2194. >>> node
  2195. <For l.1 at 0x7f23b2e8cf28>
  2196. """
  2197. _astroid_fields = ('target', 'iter', 'body', 'orelse',)
  2198. target = None
  2199. """What the loop assigns to.
  2200. :type: NodeNG or None
  2201. """
  2202. iter = None
  2203. """What the loop iterates over.
  2204. :type: NodeNG or None
  2205. """
  2206. body = None
  2207. """The contents of the body of the loop.
  2208. :type: list(NodeNG) or None
  2209. """
  2210. orelse = None
  2211. """The contents of the ``else`` block of the loop.
  2212. :type: list(NodeNG) or None
  2213. """
  2214. # pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
  2215. def postinit(self, target=None, iter=None, body=None, orelse=None):
  2216. """Do some setup after initialisation.
  2217. :param target: What the loop assigns to.
  2218. :type target: NodeNG or None
  2219. :param iter: What the loop iterates over.
  2220. :type iter: NodeNG or None
  2221. :param body: The contents of the body of the loop.
  2222. :type body: list(NodeNG) or None
  2223. :param orelse: The contents of the ``else`` block of the loop.
  2224. :type orelse: list(NodeNG) or None
  2225. """
  2226. self.target = target
  2227. self.iter = iter
  2228. self.body = body
  2229. self.orelse = orelse
  2230. optional_assign = True
  2231. """Whether this node optionally assigns a variable.
  2232. This is always ``True`` for :class:`For` nodes.
  2233. :type: bool
  2234. """
  2235. @decorators.cachedproperty
  2236. def blockstart_tolineno(self):
  2237. """The line on which the beginning of this block ends.
  2238. :type: int
  2239. """
  2240. return self.iter.tolineno
  2241. class AsyncFor(For):
  2242. """Class representing an :class:`ast.AsyncFor` node.
  2243. An :class:`AsyncFor` is an asynchronous :class:`For` built with
  2244. the ``async`` keyword.
  2245. >>> node = astroid.extract_node('''
  2246. async def func(things):
  2247. async for thing in things:
  2248. print(thing)
  2249. ''')
  2250. >>> node
  2251. <AsyncFunctionDef.func l.2 at 0x7f23b2e416d8>
  2252. >>> node.body[0]
  2253. <AsyncFor l.3 at 0x7f23b2e417b8>
  2254. """
  2255. class Await(NodeNG):
  2256. """Class representing an :class:`ast.Await` node.
  2257. An :class:`Await` is the ``await`` keyword.
  2258. >>> node = astroid.extract_node('''
  2259. async def func(things):
  2260. await other_func()
  2261. ''')
  2262. >>> node
  2263. <AsyncFunctionDef.func l.2 at 0x7f23b2e41748>
  2264. >>> node.body[0]
  2265. <Expr l.3 at 0x7f23b2e419e8>
  2266. >>> list(node.body[0].get_children())[0]
  2267. <Await l.3 at 0x7f23b2e41a20>
  2268. """
  2269. _astroid_fields = ('value', )
  2270. value = None
  2271. """What to wait for.
  2272. :type: NodeNG or None
  2273. """
  2274. def postinit(self, value=None):
  2275. """Do some setup after initialisation.
  2276. :param value: What to wait for.
  2277. :type value: NodeNG or None
  2278. """
  2279. self.value = value
  2280. class ImportFrom(mixins.ImportFromMixin, Statement):
  2281. """Class representing an :class:`ast.ImportFrom` node.
  2282. >>> node = astroid.extract_node('from my_package import my_module')
  2283. >>> node
  2284. <ImportFrom l.1 at 0x7f23b2e415c0>
  2285. """
  2286. _other_fields = ('modname', 'names', 'level')
  2287. def __init__(self, fromname, names, level=0, lineno=None,
  2288. col_offset=None, parent=None):
  2289. """
  2290. :param fromname: The module that is being imported from.
  2291. :type fromname: str or None
  2292. :param names: What is being imported from the module.
  2293. :type names: list(tuple(str, str or None))
  2294. :param level: The level of relative import.
  2295. :type level: int
  2296. :param lineno: The line that this node appears on in the source code.
  2297. :type lineno: int or None
  2298. :param col_offset: The column that this node appears on in the
  2299. source code.
  2300. :type col_offset: int or None
  2301. :param parent: The parent node in the syntax tree.
  2302. :type parent: NodeNG or None
  2303. """
  2304. self.modname = fromname
  2305. """The module that is being imported from.
  2306. This is ``None`` for relative imports.
  2307. :type: str or None
  2308. """
  2309. self.names = names
  2310. """What is being imported from the module.
  2311. Each entry is a :class:`tuple` of the name being imported,
  2312. and the alias that the name is assigned to (if any).
  2313. :type: list(tuple(str, str or None))
  2314. """
  2315. self.level = level
  2316. """The level of relative import.
  2317. Essentially this is the number of dots in the import.
  2318. This is always 0 for absolute imports.
  2319. :type: int
  2320. """
  2321. super(ImportFrom, self).__init__(lineno, col_offset, parent)
  2322. class Attribute(NodeNG):
  2323. """Class representing an :class:`ast.Attribute` node."""
  2324. _astroid_fields = ('expr',)
  2325. _other_fields = ('attrname',)
  2326. expr = None
  2327. """The name that this node represents.
  2328. :type: Name or None
  2329. """
  2330. def __init__(self, attrname=None, lineno=None, col_offset=None, parent=None):
  2331. """
  2332. :param attrname: The name of the attribute.
  2333. :type attrname: str or None
  2334. :param lineno: The line that this node appears on in the source code.
  2335. :type lineno: int or None
  2336. :param col_offset: The column that this node appears on in the
  2337. source code.
  2338. :type col_offset: int or None
  2339. :param parent: The parent node in the syntax tree.
  2340. :type parent: NodeNG or None
  2341. """
  2342. self.attrname = attrname
  2343. """The name of the attribute.
  2344. :type: str or None
  2345. """
  2346. super(Attribute, self).__init__(lineno, col_offset, parent)
  2347. def postinit(self, expr=None):
  2348. """Do some setup after initialisation.
  2349. :param expr: The name that this node represents.
  2350. :type expr: Name or None
  2351. """
  2352. self.expr = expr
  2353. class Global(Statement):
  2354. """Class representing an :class:`ast.Global` node.
  2355. >>> node = astroid.extract_node('global a_global')
  2356. >>> node
  2357. <Global l.1 at 0x7f23b2e9de10>
  2358. """
  2359. _other_fields = ('names',)
  2360. def __init__(self, names, lineno=None, col_offset=None, parent=None):
  2361. """
  2362. :param names: The names being declared as global.
  2363. :type names: list(str)
  2364. :param lineno: The line that this node appears on in the source code.
  2365. :type lineno: int or None
  2366. :param col_offset: The column that this node appears on in the
  2367. source code.
  2368. :type col_offset: int or None
  2369. :param parent: The parent node in the syntax tree.
  2370. :type parent: NodeNG or None
  2371. """
  2372. self.names = names
  2373. """The names being declared as global.
  2374. :type: list(str)
  2375. """
  2376. super(Global, self).__init__(lineno, col_offset, parent)
  2377. def _infer_name(self, frame, name):
  2378. return name
  2379. class If(mixins.BlockRangeMixIn, Statement):
  2380. """Class representing an :class:`ast.If` node.
  2381. >>> node = astroid.extract_node('if condition: print(True)')
  2382. >>> node
  2383. <If l.1 at 0x7f23b2e9dd30>
  2384. """
  2385. _astroid_fields = ('test', 'body', 'orelse')
  2386. test = None
  2387. """The condition that the statement tests.
  2388. :type: NodeNG or None
  2389. """
  2390. body = None
  2391. """The contents of the block.
  2392. :type: list(NodeNG) or None
  2393. """
  2394. orelse = None
  2395. """The contents of the ``else`` block.
  2396. :type: list(NodeNG) or None
  2397. """
  2398. def postinit(self, test=None, body=None, orelse=None):
  2399. """Do some setup after initialisation.
  2400. :param test: The condition that the statement tests.
  2401. :type test: NodeNG or None
  2402. :param body: The contents of the block.
  2403. :type body: list(NodeNG) or None
  2404. :param orelse: The contents of the ``else`` block.
  2405. :type orelse: list(NodeNG) or None
  2406. """
  2407. self.test = test
  2408. self.body = body
  2409. self.orelse = orelse
  2410. @decorators.cachedproperty
  2411. def blockstart_tolineno(self):
  2412. """The line on which the beginning of this block ends.
  2413. :type: int
  2414. """
  2415. return self.test.tolineno
  2416. def block_range(self, lineno):
  2417. """Get a range from the given line number to where this node ends.
  2418. :param lineno: The line number to start the range at.
  2419. :type lineno: int
  2420. :returns: The range of line numbers that this node belongs to,
  2421. starting at the given line number.
  2422. :rtype: tuple(int, int)
  2423. """
  2424. if lineno == self.body[0].fromlineno:
  2425. return lineno, lineno
  2426. if lineno <= self.body[-1].tolineno:
  2427. return lineno, self.body[-1].tolineno
  2428. return self._elsed_block_range(lineno, self.orelse,
  2429. self.body[0].fromlineno - 1)
  2430. class IfExp(NodeNG):
  2431. """Class representing an :class:`ast.IfExp` node.
  2432. >>> node = astroid.extract_node('value if condition else other')
  2433. >>> node
  2434. <IfExp l.1 at 0x7f23b2e9dbe0>
  2435. """
  2436. _astroid_fields = ('test', 'body', 'orelse')
  2437. test = None
  2438. """The condition that the statement tests.
  2439. :type: NodeNG or None
  2440. """
  2441. body = None
  2442. """The contents of the block.
  2443. :type: list(NodeNG) or None
  2444. """
  2445. orelse = None
  2446. """The contents of the ``else`` block.
  2447. :type: list(NodeNG) or None
  2448. """
  2449. def postinit(self, test=None, body=None, orelse=None):
  2450. """Do some setup after initialisation.
  2451. :param test: The condition that the statement tests.
  2452. :type test: NodeNG or None
  2453. :param body: The contents of the block.
  2454. :type body: list(NodeNG) or None
  2455. :param orelse: The contents of the ``else`` block.
  2456. :type orelse: list(NodeNG) or None
  2457. """
  2458. self.test = test
  2459. self.body = body
  2460. self.orelse = orelse
  2461. class Import(mixins.ImportFromMixin, Statement):
  2462. """Class representing an :class:`ast.Import` node.
  2463. >>> node = astroid.extract_node('import astroid')
  2464. >>> node
  2465. <Import l.1 at 0x7f23b2e4e5c0>
  2466. """
  2467. _other_fields = ('names',)
  2468. def __init__(self, names=None, lineno=None, col_offset=None, parent=None):
  2469. """
  2470. :param names: The names being imported.
  2471. :type names: list(tuple(str, str or None)) or None
  2472. :param lineno: The line that this node appears on in the source code.
  2473. :type lineno: int or None
  2474. :param col_offset: The column that this node appears on in the
  2475. source code.
  2476. :type col_offset: int or None
  2477. :param parent: The parent node in the syntax tree.
  2478. :type parent: NodeNG or None
  2479. """
  2480. self.names = names
  2481. """The names being imported.
  2482. Each entry is a :class:`tuple` of the name being imported,
  2483. and the alias that the name is assigned to (if any).
  2484. :type: list(tuple(str, str or None)) or None
  2485. """
  2486. super(Import, self).__init__(lineno, col_offset, parent)
  2487. class Index(NodeNG):
  2488. """Class representing an :class:`ast.Index` node.
  2489. An :class:`Index` is a simple subscript.
  2490. >>> node = astroid.extract_node('things[1]')
  2491. >>> node
  2492. <Subscript l.1 at 0x7f23b2e9e2b0>
  2493. >>> node.slice
  2494. <Index l.1 at 0x7f23b2e9e6a0>
  2495. """
  2496. _astroid_fields = ('value',)
  2497. value = None
  2498. """The value to subscript with.
  2499. :type: NodeNG or None
  2500. """
  2501. def postinit(self, value=None):
  2502. """Do some setup after initialisation.
  2503. :param value: The value to subscript with.
  2504. :type value: NodeNG or None
  2505. """
  2506. self.value = value
  2507. class Keyword(NodeNG):
  2508. """Class representing an :class:`ast.keyword` node.
  2509. >>> node = astroid.extract_node('function(a_kwarg=True)')
  2510. >>> node
  2511. <Call l.1 at 0x7f23b2e9e320>
  2512. >>> node.keywords
  2513. [<Keyword l.1 at 0x7f23b2e9e9b0>]
  2514. """
  2515. _astroid_fields = ('value',)
  2516. _other_fields = ('arg',)
  2517. value = None
  2518. """The value being assigned to the keyword argument.
  2519. :type: NodeNG or None
  2520. """
  2521. def __init__(self, arg=None, lineno=None, col_offset=None, parent=None):
  2522. """
  2523. :param arg: The argument being assigned to.
  2524. :type arg: Name or None
  2525. :param lineno: The line that this node appears on in the source code.
  2526. :type lineno: int or None
  2527. :param col_offset: The column that this node appears on in the
  2528. source code.
  2529. :type col_offset: int or None
  2530. :param parent: The parent node in the syntax tree.
  2531. :type parent: NodeNG or None
  2532. """
  2533. self.arg = arg
  2534. """The argument being assigned to.
  2535. :type: Name or None
  2536. """
  2537. super(Keyword, self).__init__(lineno, col_offset, parent)
  2538. def postinit(self, value=None):
  2539. """Do some setup after initialisation.
  2540. :param value: The value being assigned to the ketword argument.
  2541. :type value: NodeNG or None
  2542. """
  2543. self.value = value
  2544. class List(_BaseContainer):
  2545. """Class representing an :class:`ast.List` node.
  2546. >>> node = astroid.extract_node('[1, 2, 3]')
  2547. >>> node
  2548. <List.list l.1 at 0x7f23b2e9e128>
  2549. """
  2550. _other_fields = ('ctx',)
  2551. def __init__(self, ctx=None, lineno=None,
  2552. col_offset=None, parent=None):
  2553. """
  2554. :param ctx: Whether the list is assigned to or loaded from.
  2555. :type ctx: Context or None
  2556. :param lineno: The line that this node appears on in the source code.
  2557. :type lineno: int or None
  2558. :param col_offset: The column that this node appears on in the
  2559. source code.
  2560. :type col_offset: int or None
  2561. :param parent: The parent node in the syntax tree.
  2562. :type parent: NodeNG or None
  2563. """
  2564. self.ctx = ctx
  2565. """Whether the list is assigned to or loaded from.
  2566. :type: Context or None
  2567. """
  2568. super(List, self).__init__(lineno, col_offset, parent)
  2569. def pytype(self):
  2570. """Get the name of the type that this node represents.
  2571. :returns: The name of the type.
  2572. :rtype: str
  2573. """
  2574. return '%s.list' % BUILTINS
  2575. def getitem(self, index, context=None):
  2576. """Get an item from this node.
  2577. :param index: The node to use as a subscript index.
  2578. :type index: Const or Slice
  2579. """
  2580. return _container_getitem(self, self.elts, index, context=context)
  2581. class Nonlocal(Statement):
  2582. """Class representing an :class:`ast.Nonlocal` node.
  2583. >>> node = astroid.extract_node('''
  2584. def function():
  2585. nonlocal var
  2586. ''')
  2587. >>> node
  2588. <FunctionDef.function l.2 at 0x7f23b2e9e208>
  2589. >>> node.body[0]
  2590. <Nonlocal l.3 at 0x7f23b2e9e908>
  2591. """
  2592. _other_fields = ('names',)
  2593. def __init__(self, names, lineno=None, col_offset=None, parent=None):
  2594. """
  2595. :param names: The names being decalred as not local.
  2596. :type names: list(str)
  2597. :param lineno: The line that this node appears on in the source code.
  2598. :type lineno: int or None
  2599. :param col_offset: The column that this node appears on in the
  2600. source code.
  2601. :type col_offset: int or None
  2602. :param parent: The parent node in the syntax tree.
  2603. :type parent: NodeNG or None
  2604. """
  2605. self.names = names
  2606. """The names being declared as not local.
  2607. :type: list(str)
  2608. """
  2609. super(Nonlocal, self).__init__(lineno, col_offset, parent)
  2610. def _infer_name(self, frame, name):
  2611. return name
  2612. class Pass(Statement):
  2613. """Class representing an :class:`ast.Pass` node.
  2614. >>> node = astroid.extract_node('pass')
  2615. >>> node
  2616. <Pass l.1 at 0x7f23b2e9e748>
  2617. """
  2618. class Print(Statement):
  2619. """Class representing an :class:`ast.Print` node.
  2620. >>> node = astroid.extract_node('print "A message"')
  2621. >>> node
  2622. <Print l.1 at 0x7f0e8101d290>
  2623. """
  2624. _astroid_fields = ('dest', 'values',)
  2625. dest = None
  2626. """Where to print to.
  2627. :type: NodeNG or None
  2628. """
  2629. values = None
  2630. """What to print.
  2631. :type: list(NodeNG) or None
  2632. """
  2633. def __init__(self, nl=None, lineno=None, col_offset=None, parent=None):
  2634. """
  2635. :param nl: Whether to print a new line.
  2636. :type nl: bool or None
  2637. :param lineno: The line that this node appears on in the source code.
  2638. :type lineno: int or None
  2639. :param col_offset: The column that this node appears on in the
  2640. source code.
  2641. :type col_offset: int or None
  2642. :param parent: The parent node in the syntax tree.
  2643. :type parent: NodeNG or None
  2644. """
  2645. self.nl = nl
  2646. """Whether to print a new line.
  2647. :type: bool or None
  2648. """
  2649. super(Print, self).__init__(lineno, col_offset, parent)
  2650. def postinit(self, dest=None, values=None):
  2651. """Do some setup after initialisation.
  2652. :param dest: Where to print to.
  2653. :type dest: NodeNG or None
  2654. :param values: What to print.
  2655. :type values: list(NodeNG) or None
  2656. """
  2657. self.dest = dest
  2658. self.values = values
  2659. class Raise(Statement):
  2660. """Class representing an :class:`ast.Raise` node.
  2661. >>> node = astroid.extract_node('raise RuntimeError("Something bad happened!")')
  2662. >>> node
  2663. <Raise l.1 at 0x7f23b2e9e828>
  2664. """
  2665. exc = None
  2666. """What is being raised.
  2667. :type: NodeNG or None
  2668. """
  2669. if six.PY2:
  2670. _astroid_fields = ('exc', 'inst', 'tback')
  2671. inst = None
  2672. """The "value" of the exception being raised.
  2673. :type: NodeNG or None
  2674. """
  2675. tback = None
  2676. """The traceback object to raise with.
  2677. :type: NodeNG or None
  2678. """
  2679. def postinit(self, exc=None, inst=None, tback=None):
  2680. """Do some setup after initialisation.
  2681. :param exc: What is being raised.
  2682. :type exc: NodeNG or None
  2683. :param inst: The "value" of the exception being raised.
  2684. :type inst: NodeNG or None
  2685. :param tback: The traceback object to raise with.
  2686. :type tback: NodeNG or None
  2687. """
  2688. self.exc = exc
  2689. self.inst = inst
  2690. self.tback = tback
  2691. else:
  2692. _astroid_fields = ('exc', 'cause')
  2693. cause = None
  2694. """The exception being used to raise this one.
  2695. :type: NodeNG or None
  2696. """
  2697. def postinit(self, exc=None, cause=None):
  2698. """Do some setup after initialisation.
  2699. :param exc: What is being raised.
  2700. :type exc: NodeNG or None
  2701. :param cause: The exception being used to raise this one.
  2702. :type cause: NodeNG or None
  2703. """
  2704. self.exc = exc
  2705. self.cause = cause
  2706. def raises_not_implemented(self):
  2707. """Check if this node raises a :class:`NotImplementedError`.
  2708. :returns: True if this node raises a :class:`NotImplementedError`,
  2709. False otherwise.
  2710. :rtype: bool
  2711. """
  2712. if not self.exc:
  2713. return False
  2714. for name in self.exc.nodes_of_class(Name):
  2715. if name.name == 'NotImplementedError':
  2716. return True
  2717. return False
  2718. class Return(Statement):
  2719. """Class representing an :class:`ast.Return` node.
  2720. >>> node = astroid.extract_node('return True')
  2721. >>> node
  2722. <Return l.1 at 0x7f23b8211908>
  2723. """
  2724. _astroid_fields = ('value',)
  2725. value = None
  2726. """The value being returned.
  2727. :type: NodeNG or None
  2728. """
  2729. def postinit(self, value=None):
  2730. """Do some setup after initialisation.
  2731. :param value: The value being returned.
  2732. :type value: NodeNG or None
  2733. """
  2734. self.value = value
  2735. class Set(_BaseContainer):
  2736. """Class representing an :class:`ast.Set` node.
  2737. >>> node = astroid.extract_node('{1, 2, 3}')
  2738. >>> node
  2739. <Set.set l.1 at 0x7f23b2e71d68>
  2740. """
  2741. def pytype(self):
  2742. """Get the name of the type that this node represents.
  2743. :returns: The name of the type.
  2744. :rtype: str
  2745. """
  2746. return '%s.set' % BUILTINS
  2747. class Slice(NodeNG):
  2748. """Class representing an :class:`ast.Slice` node.
  2749. >>> node = astroid.extract_node('things[1:3]')
  2750. >>> node
  2751. <Subscript l.1 at 0x7f23b2e71f60>
  2752. >>> node.slice
  2753. <Slice l.1 at 0x7f23b2e71e80>
  2754. """
  2755. _astroid_fields = ('lower', 'upper', 'step')
  2756. lower = None
  2757. """The lower index in the slice.
  2758. :type: NodeNG or None
  2759. """
  2760. upper = None
  2761. """The upper index in the slice.
  2762. :type: NodeNG or None
  2763. """
  2764. step = None
  2765. """The step to take between indexes.
  2766. :type: NodeNG or None
  2767. """
  2768. def postinit(self, lower=None, upper=None, step=None):
  2769. """Do some setup after initialisation.
  2770. :param lower: The lower index in the slice.
  2771. :value lower: NodeNG or None
  2772. :param upper: The upper index in the slice.
  2773. :value upper: NodeNG or None
  2774. :param step: The step to take between index.
  2775. :param step: NodeNG or None
  2776. """
  2777. self.lower = lower
  2778. self.upper = upper
  2779. self.step = step
  2780. def _wrap_attribute(self, attr):
  2781. """Wrap the empty attributes of the Slice in a Const node."""
  2782. if not attr:
  2783. const = const_factory(attr)
  2784. const.parent = self
  2785. return const
  2786. return attr
  2787. @decorators.cachedproperty
  2788. def _proxied(self):
  2789. builtins = MANAGER.astroid_cache[BUILTINS]
  2790. return builtins.getattr('slice')[0]
  2791. def pytype(self):
  2792. """Get the name of the type that this node represents.
  2793. :returns: The name of the type.
  2794. :rtype: str
  2795. """
  2796. return '%s.slice' % BUILTINS
  2797. def igetattr(self, attrname, context=None):
  2798. """Infer the possible values of the given attribute on the slice.
  2799. :param attrname: The name of the attribute to infer.
  2800. :type attrname: str
  2801. :returns: The inferred possible values.
  2802. :rtype: iterable(NodeNG)
  2803. """
  2804. if attrname == 'start':
  2805. yield self._wrap_attribute(self.lower)
  2806. elif attrname == 'stop':
  2807. yield self._wrap_attribute(self.upper)
  2808. elif attrname == 'step':
  2809. yield self._wrap_attribute(self.step)
  2810. else:
  2811. for value in self.getattr(attrname, context=context):
  2812. yield value
  2813. def getattr(self, attrname, context=None):
  2814. return self._proxied.getattr(attrname, context)
  2815. class Starred(mixins.ParentAssignTypeMixin, NodeNG):
  2816. """Class representing an :class:`ast.Starred` node.
  2817. >>> node = astroid.extract_node('*args')
  2818. >>> node
  2819. <Starred l.1 at 0x7f23b2e41978>
  2820. """
  2821. _astroid_fields = ('value',)
  2822. _other_fields = ('ctx', )
  2823. value = None
  2824. """What is being unpacked.
  2825. :type: NodeNG or None
  2826. """
  2827. def __init__(self, ctx=None, lineno=None, col_offset=None, parent=None):
  2828. """
  2829. :param ctx: Whether the list is assigned to or loaded from.
  2830. :type ctx: Context or None
  2831. :param lineno: The line that this node appears on in the source code.
  2832. :type lineno: int or None
  2833. :param col_offset: The column that this node appears on in the
  2834. source code.
  2835. :type col_offset: int or None
  2836. :param parent: The parent node in the syntax tree.
  2837. :type parent: NodeNG or None
  2838. """
  2839. self.ctx = ctx
  2840. """Whether the starred item is assigned to or loaded from.
  2841. :type: Context or None
  2842. """
  2843. super(Starred, self).__init__(lineno=lineno,
  2844. col_offset=col_offset, parent=parent)
  2845. def postinit(self, value=None):
  2846. """Do some setup after initialisation.
  2847. :param value: What is being unpacked.
  2848. :type value: NodeNG or None
  2849. """
  2850. self.value = value
  2851. class Subscript(NodeNG):
  2852. """Class representing an :class:`ast.Subscript` node.
  2853. >>> node = astroid.extract_node('things[1:3]')
  2854. >>> node
  2855. <Subscript l.1 at 0x7f23b2e71f60>
  2856. """
  2857. _astroid_fields = ('value', 'slice')
  2858. _other_fields = ('ctx', )
  2859. value = None
  2860. """What is being indexed.
  2861. :type: NodeNG or None
  2862. """
  2863. slice = None
  2864. """The slice being used to lookup.
  2865. :type: NodeNG or None
  2866. """
  2867. def __init__(self, ctx=None, lineno=None, col_offset=None, parent=None):
  2868. """
  2869. :param ctx: Whether the subscripted item is assigned to or loaded from.
  2870. :type ctx: Context or None
  2871. :param lineno: The line that this node appears on in the source code.
  2872. :type lineno: int or None
  2873. :param col_offset: The column that this node appears on in the
  2874. source code.
  2875. :type col_offset: int or None
  2876. :param parent: The parent node in the syntax tree.
  2877. :type parent: NodeNG or None
  2878. """
  2879. self.ctx = ctx
  2880. """Whether the subscripted item is assigned to or loaded from.
  2881. :type: Context or None
  2882. """
  2883. super(Subscript, self).__init__(lineno=lineno,
  2884. col_offset=col_offset, parent=parent)
  2885. # pylint: disable=redefined-builtin; had to use the same name as builtin ast module.
  2886. def postinit(self, value=None, slice=None):
  2887. """Do some setup after initialisation.
  2888. :param value: What is being indexed.
  2889. :type value: NodeNG or None
  2890. :param slice: The slice being used to lookup.
  2891. :type slice: NodeNG or None
  2892. """
  2893. self.value = value
  2894. self.slice = slice
  2895. class TryExcept(mixins.BlockRangeMixIn, Statement):
  2896. """Class representing an :class:`ast.TryExcept` node.
  2897. >>> node = astroid.extract_node('''
  2898. try:
  2899. do_something()
  2900. except Exception as error:
  2901. print("Error!")
  2902. ''')
  2903. >>> node
  2904. <TryExcept l.2 at 0x7f23b2e9d908>
  2905. """
  2906. _astroid_fields = ('body', 'handlers', 'orelse',)
  2907. body = None
  2908. """The contents of the block to catch exceptions from.
  2909. :type: list(NodeNG) or None
  2910. """
  2911. handlers = None
  2912. """The exception handlers.
  2913. :type: list(ExceptHandler) or None
  2914. """
  2915. orelse = None
  2916. """The contents of the ``else`` block.
  2917. :type: list(NodeNG) or None
  2918. """
  2919. def postinit(self, body=None, handlers=None, orelse=None):
  2920. """Do some setup after initialisation.
  2921. :param body: The contents of the block to catch exceptions from.
  2922. :type body: list(NodeNG) or None
  2923. :param handlers: The exception handlers.
  2924. :type handlers: list(ExceptHandler) or None
  2925. :param orelse: The contents of the ``else`` block.
  2926. :type orelse: list(NodeNG) or None
  2927. """
  2928. self.body = body
  2929. self.handlers = handlers
  2930. self.orelse = orelse
  2931. def _infer_name(self, frame, name):
  2932. return name
  2933. def block_range(self, lineno):
  2934. """Get a range from the given line number to where this node ends.
  2935. :param lineno: The line number to start the range at.
  2936. :type lineno: int
  2937. :returns: The range of line numbers that this node belongs to,
  2938. starting at the given line number.
  2939. :rtype: tuple(int, int)
  2940. """
  2941. last = None
  2942. for exhandler in self.handlers:
  2943. if exhandler.type and lineno == exhandler.type.fromlineno:
  2944. return lineno, lineno
  2945. if exhandler.body[0].fromlineno <= lineno <= exhandler.body[-1].tolineno:
  2946. return lineno, exhandler.body[-1].tolineno
  2947. if last is None:
  2948. last = exhandler.body[0].fromlineno - 1
  2949. return self._elsed_block_range(lineno, self.orelse, last)
  2950. class TryFinally(mixins.BlockRangeMixIn, Statement):
  2951. """Class representing an :class:`ast.TryFinally` node.
  2952. >>> node = astroid.extract_node('''
  2953. try:
  2954. do_something()
  2955. except Exception as error:
  2956. print("Error!")
  2957. finally:
  2958. print("Cleanup!")
  2959. ''')
  2960. >>> node
  2961. <TryFinally l.2 at 0x7f23b2e41d68>
  2962. """
  2963. _astroid_fields = ('body', 'finalbody',)
  2964. body = None
  2965. """The try-except that the finally is attached to.
  2966. :type: list(TryExcept) or None
  2967. """
  2968. finalbody = None
  2969. """The contents of the ``finally`` block.
  2970. :type: list(NodeNG) or None
  2971. """
  2972. def postinit(self, body=None, finalbody=None):
  2973. """Do some setup after initialisation.
  2974. :param body: The try-except that the finally is attached to.
  2975. :type body: list(TryExcept) or None
  2976. :param finalbody: The contents of the ``finally`` block.
  2977. :type finalbody: list(NodeNG) or None
  2978. """
  2979. self.body = body
  2980. self.finalbody = finalbody
  2981. def block_range(self, lineno):
  2982. """Get a range from the given line number to where this node ends.
  2983. :param lineno: The line number to start the range at.
  2984. :type lineno: int
  2985. :returns: The range of line numbers that this node belongs to,
  2986. starting at the given line number.
  2987. :rtype: tuple(int, int)
  2988. """
  2989. child = self.body[0]
  2990. # py2.5 try: except: finally:
  2991. if (isinstance(child, TryExcept) and child.fromlineno == self.fromlineno
  2992. and lineno > self.fromlineno and lineno <= child.tolineno):
  2993. return child.block_range(lineno)
  2994. return self._elsed_block_range(lineno, self.finalbody)
  2995. class Tuple(_BaseContainer):
  2996. """Class representing an :class:`ast.Tuple` node.
  2997. >>> node = astroid.extract_node('(1, 2, 3)')
  2998. >>> node
  2999. <Tuple.tuple l.1 at 0x7f23b2e41780>
  3000. """
  3001. _other_fields = ('ctx',)
  3002. def __init__(self, ctx=None, lineno=None,
  3003. col_offset=None, parent=None):
  3004. """
  3005. :param ctx: Whether the tuple is assigned to or loaded from.
  3006. :type ctx: Context or None
  3007. :param lineno: The line that this node appears on in the source code.
  3008. :type lineno: int or None
  3009. :param col_offset: The column that this node appears on in the
  3010. source code.
  3011. :type col_offset: int or None
  3012. :param parent: The parent node in the syntax tree.
  3013. :type parent: NodeNG or None
  3014. """
  3015. self.ctx = ctx
  3016. """Whether the tuple is assigned to or loaded from.
  3017. :type: Context or None
  3018. """
  3019. super(Tuple, self).__init__(lineno, col_offset, parent)
  3020. def pytype(self):
  3021. """Get the name of the type that this node represents.
  3022. :returns: The name of the type.
  3023. :rtype: str
  3024. """
  3025. return '%s.tuple' % BUILTINS
  3026. def getitem(self, index, context=None):
  3027. """Get an item from this node.
  3028. :param index: The node to use as a subscript index.
  3029. :type index: Const or Slice
  3030. """
  3031. return _container_getitem(self, self.elts, index, context=context)
  3032. class UnaryOp(NodeNG):
  3033. """Class representing an :class:`ast.UnaryOp` node.
  3034. >>> node = astroid.extract_node('-5')
  3035. >>> node
  3036. <UnaryOp l.1 at 0x7f23b2e4e198>
  3037. """
  3038. _astroid_fields = ('operand',)
  3039. _other_fields = ('op',)
  3040. operand = None
  3041. """What the unary operator is applied to.
  3042. :type: NodeNG or None
  3043. """
  3044. def __init__(self, op=None, lineno=None, col_offset=None, parent=None):
  3045. """
  3046. :param op: The operator.
  3047. :type: str or None
  3048. :param lineno: The line that this node appears on in the source code.
  3049. :type lineno: int or None
  3050. :param col_offset: The column that this node appears on in the
  3051. source code.
  3052. :type col_offset: int or None
  3053. :param parent: The parent node in the syntax tree.
  3054. :type parent: NodeNG or None
  3055. """
  3056. self.op = op
  3057. """The operator.
  3058. :type: str or None
  3059. """
  3060. super(UnaryOp, self).__init__(lineno, col_offset, parent)
  3061. def postinit(self, operand=None):
  3062. """Do some setup after initialisation.
  3063. :param operand: What the unary operator is applied to.
  3064. :type operand: NodeNG or None
  3065. """
  3066. self.operand = operand
  3067. # This is set by inference.py
  3068. def _infer_unaryop(self, context=None):
  3069. raise NotImplementedError
  3070. def type_errors(self, context=None):
  3071. """Get a list of type errors which can occur during inference.
  3072. Each TypeError is represented by a :class:`BadBinaryOperationMessage`,
  3073. which holds the original exception.
  3074. :returns: The list of possible type errors.
  3075. :rtype: list(BadBinaryOperationMessage)
  3076. """
  3077. try:
  3078. results = self._infer_unaryop(context=context)
  3079. return [result for result in results
  3080. if isinstance(result, util.BadUnaryOperationMessage)]
  3081. except exceptions.InferenceError:
  3082. return []
  3083. class While(mixins.BlockRangeMixIn, Statement):
  3084. """Class representing an :class:`ast.While` node.
  3085. >>> node = astroid.extract_node('''
  3086. while condition():
  3087. print("True")
  3088. ''')
  3089. >>> node
  3090. <While l.2 at 0x7f23b2e4e390>
  3091. """
  3092. _astroid_fields = ('test', 'body', 'orelse',)
  3093. test = None
  3094. """The condition that the loop tests.
  3095. :type: NodeNG or None
  3096. """
  3097. body = None
  3098. """The contents of the loop.
  3099. :type: list(NodeNG) or None
  3100. """
  3101. orelse = None
  3102. """The contents of the ``else`` block.
  3103. :type: list(NodeNG) or None
  3104. """
  3105. def postinit(self, test=None, body=None, orelse=None):
  3106. """Do some setup after initialisation.
  3107. :param test: The condition that the loop tests.
  3108. :type test: NodeNG or None
  3109. :param body: The contents of the loop.
  3110. :type body: list(NodeNG) or None
  3111. :param orelse: The contents of the ``else`` block.
  3112. :type orelse: list(NodeNG) or None
  3113. """
  3114. self.test = test
  3115. self.body = body
  3116. self.orelse = orelse
  3117. @decorators.cachedproperty
  3118. def blockstart_tolineno(self):
  3119. """The line on which the beginning of this block ends.
  3120. :type: int
  3121. """
  3122. return self.test.tolineno
  3123. def block_range(self, lineno):
  3124. """Get a range from the given line number to where this node ends.
  3125. :param lineno: The line number to start the range at.
  3126. :type lineno: int
  3127. :returns: The range of line numbers that this node belongs to,
  3128. starting at the given line number.
  3129. :rtype: tuple(int, int)
  3130. """
  3131. return self. _elsed_block_range(lineno, self.orelse)
  3132. class With(mixins.BlockRangeMixIn, mixins.AssignTypeMixin, Statement):
  3133. """Class representing an :class:`ast.With` node.
  3134. >>> node = astroid.extract_node('''
  3135. with open(file_path) as file_:
  3136. print(file_.read())
  3137. ''')
  3138. >>> node
  3139. <With l.2 at 0x7f23b2e4e710>
  3140. """
  3141. _astroid_fields = ('items', 'body')
  3142. items = None
  3143. """The pairs of context managers and the names they are assigned to.
  3144. :type: list(tuple(NodeNG, AssignName or None)) or None
  3145. """
  3146. body = None
  3147. """The contents of the ``with`` block.
  3148. :type: list(NodeNG) or None
  3149. """
  3150. def postinit(self, items=None, body=None):
  3151. """Do some setup after initialisation.
  3152. :param items: The pairs of context managers and the names
  3153. they are assigned to.
  3154. :type items: list(tuple(NodeNG, AssignName or None)) or None
  3155. :param body: The contents of the ``with`` block.
  3156. :type body: list(NodeNG) or None
  3157. """
  3158. self.items = items
  3159. self.body = body
  3160. @decorators.cachedproperty
  3161. def blockstart_tolineno(self):
  3162. """The line on which the beginning of this block ends.
  3163. :type: int
  3164. """
  3165. return self.items[-1][0].tolineno
  3166. def get_children(self):
  3167. """Get the child nodes below this node.
  3168. :returns: The children.
  3169. :rtype: iterable(NodeNG)
  3170. """
  3171. for expr, var in self.items:
  3172. yield expr
  3173. if var:
  3174. yield var
  3175. for elt in self.body:
  3176. yield elt
  3177. class AsyncWith(With):
  3178. """Asynchronous ``with`` built with the ``async`` keyword."""
  3179. class Yield(NodeNG):
  3180. """Class representing an :class:`ast.Yield` node.
  3181. >>> node = astroid.extract_node('yield True')
  3182. >>> node
  3183. <Yield l.1 at 0x7f23b2e4e5f8>
  3184. """
  3185. _astroid_fields = ('value',)
  3186. value = None
  3187. """The value to yield.
  3188. :type: NodeNG or None
  3189. """
  3190. def postinit(self, value=None):
  3191. """Do some setup after initialisation.
  3192. :param value: The value to yield.
  3193. :type value: NodeNG or None
  3194. """
  3195. self.value = value
  3196. class YieldFrom(Yield):
  3197. """Class representing an :class:`ast.YieldFrom` node."""
  3198. class DictUnpack(NodeNG):
  3199. """Represents the unpacking of dicts into dicts using :pep:`448`."""
  3200. class FormattedValue(NodeNG):
  3201. """Class representing an :class:`ast.FormattedValue` node.
  3202. Represents a :pep:`498` format string.
  3203. >>> node = astroid.extract_node('f"Format {type_}"')
  3204. >>> node
  3205. <JoinedStr l.1 at 0x7f23b2e4ed30>
  3206. >>> node.values
  3207. [<Const.str l.1 at 0x7f23b2e4eda0>, <FormattedValue l.1 at 0x7f23b2e4edd8>]
  3208. """
  3209. _astroid_fields = ('value', 'format_spec')
  3210. value = None
  3211. """The value to be formatted into the string.
  3212. :type: NodeNG or None
  3213. """
  3214. conversion = None
  3215. """The type of formatting to be applied to the value.
  3216. .. seealso::
  3217. :class:`ast.FormattedValue`
  3218. :type: int or None
  3219. """
  3220. format_spec = None
  3221. """The formatting to be applied to the value.
  3222. .. seealso::
  3223. :class:`ast.FormattedValue`
  3224. :type: JoinedStr or None
  3225. """
  3226. def postinit(self, value, conversion=None, format_spec=None):
  3227. """Do some setup after initialisation.
  3228. :param value: The value to be formatted into the string.
  3229. :type value: NodeNG
  3230. :param conversion: The type of formatting to be applied to the value.
  3231. :type conversion: int or None
  3232. :param format_spec: The formatting to be applied to the value.
  3233. :type format_spec: JoinedStr or None
  3234. """
  3235. self.value = value
  3236. self.conversion = conversion
  3237. self.format_spec = format_spec
  3238. class JoinedStr(NodeNG):
  3239. """Represents a list of string expressions to be joined.
  3240. >>> node = astroid.extract_node('f"Format {type_}"')
  3241. >>> node
  3242. <JoinedStr l.1 at 0x7f23b2e4ed30>
  3243. """
  3244. _astroid_fields = ('values',)
  3245. values = None
  3246. """The string expressions to be joined.
  3247. :type: list(FormattedValue or Const) or None
  3248. """
  3249. def postinit(self, values=None):
  3250. """Do some setup after initialisation.
  3251. :param value: The string expressions to be joined.
  3252. :type: list(FormattedValue or Const) or None
  3253. """
  3254. self.values = values
  3255. class Unknown(mixins.AssignTypeMixin, NodeNG):
  3256. """This node represents a node in a constructed AST where
  3257. introspection is not possible. At the moment, it's only used in
  3258. the args attribute of FunctionDef nodes where function signature
  3259. introspection failed.
  3260. """
  3261. def infer(self, context=None, **kwargs):
  3262. """Inference on an Unknown node immediately terminates."""
  3263. yield util.Uninferable
  3264. # constants ##############################################################
  3265. CONST_CLS = {
  3266. list: List,
  3267. tuple: Tuple,
  3268. dict: Dict,
  3269. set: Set,
  3270. type(None): Const,
  3271. type(NotImplemented): Const,
  3272. }
  3273. def _update_const_classes():
  3274. """update constant classes, so the keys of CONST_CLS can be reused"""
  3275. klasses = (bool, int, float, complex, str)
  3276. if six.PY2:
  3277. # pylint: disable=undefined-variable
  3278. klasses += (unicode, long)
  3279. klasses += (bytes,)
  3280. for kls in klasses:
  3281. CONST_CLS[kls] = Const
  3282. _update_const_classes()
  3283. def _two_step_initialization(cls, value):
  3284. instance = cls()
  3285. instance.postinit(value)
  3286. return instance
  3287. def _dict_initialization(cls, value):
  3288. if isinstance(value, dict):
  3289. value = tuple(value.items())
  3290. return _two_step_initialization(cls, value)
  3291. _CONST_CLS_CONSTRUCTORS = {
  3292. List: _two_step_initialization,
  3293. Tuple: _two_step_initialization,
  3294. Dict: _dict_initialization,
  3295. Set: _two_step_initialization,
  3296. Const: lambda cls, value: cls(value)
  3297. }
  3298. def const_factory(value):
  3299. """return an astroid node for a python value"""
  3300. # XXX we should probably be stricter here and only consider stuff in
  3301. # CONST_CLS or do better treatment: in case where value is not in CONST_CLS,
  3302. # we should rather recall the builder on this value than returning an empty
  3303. # node (another option being that const_factory shouldn't be called with something
  3304. # not in CONST_CLS)
  3305. assert not isinstance(value, NodeNG)
  3306. # Hack for ignoring elements of a sequence
  3307. # or a mapping, in order to avoid transforming
  3308. # each element to an AST. This is fixed in 2.0
  3309. # and this approach is a temporary hack.
  3310. if isinstance(value, (list, set, tuple, dict)):
  3311. elts = []
  3312. else:
  3313. elts = value
  3314. try:
  3315. initializer_cls = CONST_CLS[value.__class__]
  3316. initializer = _CONST_CLS_CONSTRUCTORS[initializer_cls]
  3317. return initializer(initializer_cls, elts)
  3318. except (KeyError, AttributeError):
  3319. node = EmptyNode()
  3320. node.object = value
  3321. return node
  3322. # Backward-compatibility aliases
  3323. Backquote = util.proxy_alias('Backquote', Repr)
  3324. Discard = util.proxy_alias('Discard', Expr)
  3325. AssName = util.proxy_alias('AssName', AssignName)
  3326. AssAttr = util.proxy_alias('AssAttr', AssignAttr)
  3327. Getattr = util.proxy_alias('Getattr', Attribute)
  3328. CallFunc = util.proxy_alias('CallFunc', Call)
  3329. From = util.proxy_alias('From', ImportFrom)