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.

scoped_nodes.py 89KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686
  1. # Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2011, 2013-2015 Google, Inc.
  3. # Copyright (c) 2013-2016 Claudiu Popa <pcmanticore@gmail.com>
  4. # Copyright (c) 2015-2016 Cara Vinson <ceridwenv@gmail.com>
  5. # Copyright (c) 2015 Rene Zhang <rz99@cornell.edu>
  6. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  7. # For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER
  8. """
  9. This module contains the classes for "scoped" node, i.e. which are opening a
  10. new local scope in the language definition : Module, ClassDef, FunctionDef (and
  11. Lambda, GeneratorExp, DictComp and SetComp to some extent).
  12. """
  13. import sys
  14. import io
  15. import itertools
  16. import warnings
  17. import six
  18. from astroid import bases
  19. from astroid import context as contextmod
  20. from astroid import exceptions
  21. from astroid import decorators as decorators_mod
  22. from astroid.interpreter import objectmodel
  23. from astroid.interpreter import dunder_lookup
  24. from astroid import manager
  25. from astroid import mixins
  26. from astroid import node_classes
  27. from astroid import util
  28. BUILTINS = six.moves.builtins.__name__
  29. ITER_METHODS = ('__iter__', '__getitem__')
  30. def _c3_merge(sequences, cls, context):
  31. """Merges MROs in *sequences* to a single MRO using the C3 algorithm.
  32. Adapted from http://www.python.org/download/releases/2.3/mro/.
  33. """
  34. result = []
  35. while True:
  36. sequences = [s for s in sequences if s] # purge empty sequences
  37. if not sequences:
  38. return result
  39. for s1 in sequences: # find merge candidates among seq heads
  40. candidate = s1[0]
  41. for s2 in sequences:
  42. if candidate in s2[1:]:
  43. candidate = None
  44. break # reject the current head, it appears later
  45. else:
  46. break
  47. if not candidate:
  48. # Show all the remaining bases, which were considered as
  49. # candidates for the next mro sequence.
  50. raise exceptions.InconsistentMroError(
  51. message="Cannot create a consistent method resolution order "
  52. "for MROs {mros} of class {cls!r}.",
  53. mros=sequences, cls=cls, context=context)
  54. result.append(candidate)
  55. # remove the chosen candidate
  56. for seq in sequences:
  57. if seq[0] == candidate:
  58. del seq[0]
  59. return None
  60. def _verify_duplicates_mro(sequences, cls, context):
  61. for sequence in sequences:
  62. names = [(node.lineno, node.qname()) for node in sequence if node.name]
  63. if len(names) != len(set(names)):
  64. raise exceptions.DuplicateBasesError(
  65. message='Duplicates found in MROs {mros} for {cls!r}.',
  66. mros=sequences, cls=cls, context=context)
  67. def function_to_method(n, klass):
  68. if isinstance(n, FunctionDef):
  69. if n.type == 'classmethod':
  70. return bases.BoundMethod(n, klass)
  71. if n.type != 'staticmethod':
  72. return bases.UnboundMethod(n)
  73. return n
  74. MANAGER = manager.AstroidManager()
  75. def builtin_lookup(name):
  76. """lookup a name into the builtin module
  77. return the list of matching statements and the astroid for the builtin
  78. module
  79. """
  80. builtin_astroid = MANAGER.ast_from_module(six.moves.builtins)
  81. if name == '__dict__':
  82. return builtin_astroid, ()
  83. try:
  84. stmts = builtin_astroid.locals[name]
  85. except KeyError:
  86. stmts = ()
  87. return builtin_astroid, stmts
  88. # TODO move this Mixin to mixins.py; problem: 'FunctionDef' in _scope_lookup
  89. class LocalsDictNodeNG(node_classes.LookupMixIn,
  90. node_classes.NodeNG):
  91. """ this class provides locals handling common to Module, FunctionDef
  92. and ClassDef nodes, including a dict like interface for direct access
  93. to locals information
  94. """
  95. # attributes below are set by the builder module or by raw factories
  96. locals = {}
  97. """A map of the name of a local variable to the node defining the local.
  98. :type: dict(str, NodeNG)
  99. """
  100. def qname(self):
  101. """Get the 'qualified' name of the node.
  102. For example: module.name, module.class.name ...
  103. :returns: The qualified name.
  104. :rtype: str
  105. """
  106. # pylint: disable=no-member; github.com/pycqa/astroid/issues/278
  107. if self.parent is None:
  108. return self.name
  109. return '%s.%s' % (self.parent.frame().qname(), self.name)
  110. def frame(self):
  111. """The first parent frame node.
  112. A frame node is a :class:`Module`, :class:`FunctionDef`,
  113. or :class:`ClassDef`.
  114. :returns: The first parent frame node.
  115. :rtype: Module or FunctionDef or ClassDef
  116. """
  117. return self
  118. def scope(self):
  119. """The first parent node defining a new scope.
  120. :returns: The first parent scope node.
  121. :rtype: Module or FunctionDef or ClassDef or Lambda or GenExpr
  122. """
  123. return self
  124. def _scope_lookup(self, node, name, offset=0):
  125. """XXX method for interfacing the scope lookup"""
  126. try:
  127. stmts = node._filter_stmts(self.locals[name], self, offset)
  128. except KeyError:
  129. stmts = ()
  130. if stmts:
  131. return self, stmts
  132. if self.parent: # i.e. not Module
  133. # nested scope: if parent scope is a function, that's fine
  134. # else jump to the module
  135. pscope = self.parent.scope()
  136. if not pscope.is_function:
  137. pscope = pscope.root()
  138. return pscope.scope_lookup(node, name)
  139. return builtin_lookup(name) # Module
  140. def set_local(self, name, stmt):
  141. """Define that the given name is declared in the given statement node.
  142. .. seealso:: :meth:`scope`
  143. :param name: The name that is being defined.
  144. :type name: str
  145. :param stmt: The statement that defines the given name.
  146. :type stmt: NodeNG
  147. """
  148. #assert not stmt in self.locals.get(name, ()), (self, stmt)
  149. self.locals.setdefault(name, []).append(stmt)
  150. __setitem__ = set_local
  151. def _append_node(self, child):
  152. """append a child, linking it in the tree"""
  153. # pylint: disable=no-member; depending by the class
  154. # which uses the current class as a mixin or base class.
  155. # It's rewritten in 2.0, so it makes no sense for now
  156. # to spend development time on it.
  157. self.body.append(child)
  158. child.parent = self
  159. def add_local_node(self, child_node, name=None):
  160. """Append a child that should alter the locals of this scope node.
  161. :param child_node: The child node that will alter locals.
  162. :type child_node: NodeNG
  163. :param name: The name of the local that will be altered by
  164. the given child node.
  165. :type name: str or None
  166. """
  167. if name != '__class__':
  168. # add __class__ node as a child will cause infinite recursion later!
  169. self._append_node(child_node)
  170. self.set_local(name or child_node.name, child_node)
  171. def __getitem__(self, item):
  172. """The first node the defines the given local.
  173. :param item: The name of the locally defined object.
  174. :type item: str
  175. :raises KeyError: If the name is not defined.
  176. """
  177. return self.locals[item][0]
  178. def __iter__(self):
  179. """Iterate over the names of locals defined in this scoped node.
  180. :returns: The names of the defined locals.
  181. :rtype: iterable(str)
  182. """
  183. return iter(self.keys())
  184. def keys(self):
  185. """The names of locals defined in this scoped node.
  186. :returns: The names of the defined locals.
  187. :rtype: list(str)
  188. """
  189. return list(self.locals.keys())
  190. def values(self):
  191. """The nodes that define the locals in this scoped node.
  192. :returns: The nodes that define locals.
  193. :rtype: list(NodeNG)
  194. """
  195. return [self[key] for key in self.keys()]
  196. def items(self):
  197. """Get the names of the locals and the node that defines the local.
  198. :returns: The names of locals and their asociated node.
  199. :rtype: list(tuple(str, NodeNG))
  200. """
  201. return list(zip(self.keys(), self.values()))
  202. def __contains__(self, name):
  203. """Check if a local is defined in this scope.
  204. :param name: The name of the local to check for.
  205. :type name: str
  206. :returns: True if this node has a local of the given name,
  207. False otherwise.
  208. :rtype: bool
  209. """
  210. return name in self.locals
  211. class Module(LocalsDictNodeNG):
  212. """Class representing an :class:`ast.Module` node.
  213. >>> node = astroid.extract_node('import astroid')
  214. >>> node
  215. <Import l.1 at 0x7f23b2e4e5c0>
  216. >>> node.parent
  217. <Module l.0 at 0x7f23b2e4eda0>
  218. """
  219. _astroid_fields = ('body',)
  220. fromlineno = 0
  221. """The first line that this node appears on in the source code.
  222. :type: int or None
  223. """
  224. lineno = 0
  225. """The line that this node appears on in the source code.
  226. :type: int or None
  227. """
  228. # attributes below are set by the builder module or by raw factories
  229. file = None
  230. """The path to the file that this ast has been extracted from.
  231. This will be ``None`` when the representation has been built from a
  232. built-in module.
  233. :type: str or None
  234. """
  235. file_bytes = None
  236. """The string/bytes that this ast was built from.
  237. :type: str or bytes or None
  238. """
  239. file_encoding = None
  240. """The encoding of the source file.
  241. This is used to get unicode out of a source file.
  242. Python 2 only.
  243. :type: str or None
  244. """
  245. name = None
  246. """The name of the module.
  247. :type: str or None
  248. """
  249. pure_python = None
  250. """Whether the ast was built from source.
  251. :type: bool or None
  252. """
  253. package = None
  254. """Whether the node represents a package or a module.
  255. :type: bool or None
  256. """
  257. globals = None
  258. """A map of the name of a global variable to the node defining the global.
  259. :type: dict(str, NodeNG)
  260. """
  261. # Future imports
  262. future_imports = None
  263. """The imports from ``__future__``.
  264. :type: set(str) or None
  265. """
  266. special_attributes = objectmodel.ModuleModel()
  267. """The names of special attributes that this module has.
  268. :type: objectmodel.ModuleModel
  269. """
  270. # names of module attributes available through the global scope
  271. scope_attrs = {'__name__', '__doc__', '__file__', '__path__', '__package__'}
  272. """The names of module attributes available through the global scope.
  273. :type: str(str)
  274. """
  275. _other_fields = ('name', 'doc', 'file', 'path', 'package',
  276. 'pure_python', 'future_imports')
  277. _other_other_fields = ('locals', 'globals')
  278. def __init__(self, name, doc, file=None, path=None, package=None,
  279. parent=None, pure_python=True):
  280. """
  281. :param name: The name of the module.
  282. :type name: str
  283. :param doc: The module docstring.
  284. :type doc: str
  285. :param file: The path to the file that this ast has been extracted from.
  286. :type file: str or None
  287. :param path:
  288. :type path: str or None
  289. :param package: Whether the node represents a package or a module.
  290. :type package: bool or None
  291. :param parent: The parent node in the syntax tree.
  292. :type parent: NodeNG or None
  293. :param pure_python: Whether the ast was built from source.
  294. :type pure_python: bool or None
  295. """
  296. self.name = name
  297. self.doc = doc
  298. self.file = file
  299. self.path = path
  300. self.package = package
  301. self.parent = parent
  302. self.pure_python = pure_python
  303. self.locals = self.globals = {}
  304. """A map of the name of a local variable to the node defining the local.
  305. :type: dict(str, NodeNG)
  306. """
  307. self.body = []
  308. """The contents of the module.
  309. :type: list(NodeNG) or None
  310. """
  311. self.future_imports = set()
  312. # pylint: enable=redefined-builtin
  313. def postinit(self, body=None):
  314. """Do some setup after initialisation.
  315. :param body: The contents of the module.
  316. :type body: list(NodeNG) or None
  317. """
  318. self.body = body
  319. def _get_stream(self):
  320. if self.file_bytes is not None:
  321. return io.BytesIO(self.file_bytes)
  322. if self.file is not None:
  323. stream = open(self.file, 'rb')
  324. return stream
  325. return None
  326. def stream(self):
  327. """Get a stream to the underlying file or bytes.
  328. .. deprecated:: 1.5
  329. :type: file or io.BytesIO or None
  330. """
  331. return self._get_stream()
  332. def block_range(self, lineno):
  333. """Get a range from where this node starts to where this node ends.
  334. :param lineno: Unused.
  335. :type lineno: int
  336. :returns: The range of line numbers that this node belongs to.
  337. :rtype: tuple(int, int)
  338. """
  339. return self.fromlineno, self.tolineno
  340. def scope_lookup(self, node, name, offset=0):
  341. """Lookup where the given variable is assigned.
  342. :param node: The node to look for assignments up to.
  343. Any assignments after the given node are ignored.
  344. :type node: NodeNG
  345. :param name: The name of the variable to find assignments for.
  346. :type name: str
  347. :param offset: The line offset to filter statements up to.
  348. :type offset: int
  349. :returns: This scope node and the list of assignments associated to the
  350. given name according to the scope where it has been found (locals,
  351. globals or builtin).
  352. :rtype: tuple(str, list(NodeNG))
  353. """
  354. if name in self.scope_attrs and name not in self.locals:
  355. try:
  356. return self, self.getattr(name)
  357. except exceptions.AttributeInferenceError:
  358. return self, ()
  359. return self._scope_lookup(node, name, offset)
  360. def pytype(self):
  361. """Get the name of the type that this node represents.
  362. :returns: The name of the type.
  363. :rtype: str
  364. """
  365. return '%s.module' % BUILTINS
  366. def display_type(self):
  367. """A human readable type of this node.
  368. :returns: The type of this node.
  369. :rtype: str
  370. """
  371. return 'Module'
  372. def getattr(self, name, context=None, ignore_locals=False):
  373. result = []
  374. name_in_locals = name in self.locals
  375. if name in self.special_attributes and not ignore_locals and not name_in_locals:
  376. result = [self.special_attributes.lookup(name)]
  377. elif not ignore_locals and name_in_locals:
  378. result = self.locals[name]
  379. elif self.package:
  380. try:
  381. result = [self.import_module(name, relative_only=True)]
  382. except (exceptions.AstroidBuildingError, SyntaxError):
  383. util.reraise(exceptions.AttributeInferenceError(target=self,
  384. attribute=name,
  385. context=context))
  386. result = [n for n in result if not isinstance(n, node_classes.DelName)]
  387. if result:
  388. return result
  389. raise exceptions.AttributeInferenceError(target=self, attribute=name,
  390. context=context)
  391. def igetattr(self, name, context=None):
  392. """Infer the possible values of the given variable.
  393. :param name: The name of the variable to infer.
  394. :type name: str
  395. :returns: The inferred possible values.
  396. :rtype: iterable(NodeNG) or None
  397. """
  398. # set lookup name since this is necessary to infer on import nodes for
  399. # instance
  400. context = contextmod.copy_context(context)
  401. context.lookupname = name
  402. try:
  403. return bases._infer_stmts(self.getattr(name, context),
  404. context, frame=self)
  405. except exceptions.AttributeInferenceError as error:
  406. util.reraise(exceptions.InferenceError(
  407. error.message, target=self, attribute=name, context=context))
  408. def fully_defined(self):
  409. """Check if this module has been build from a .py file.
  410. If so, the module contains a complete representation,
  411. including the code.
  412. :returns: True if the module has been built from a .py file.
  413. :rtype: bool
  414. """
  415. return self.file is not None and self.file.endswith('.py')
  416. def statement(self):
  417. """The first parent node, including self, marked as statement node.
  418. :returns: The first parent statement.
  419. :rtype: NodeNG
  420. """
  421. return self
  422. def previous_sibling(self): #pylint: disable=useless-return
  423. """The previous sibling statement.
  424. :returns: The previous sibling statement node.
  425. :rtype: NodeNG or None
  426. """
  427. return
  428. def next_sibling(self): #pylint: disable=useless-return
  429. """The next sibling statement node.
  430. :returns: The next sibling statement node.
  431. :rtype: NodeNG or None
  432. """
  433. return
  434. if six.PY2:
  435. @decorators_mod.cachedproperty
  436. def _absolute_import_activated(self):
  437. for stmt in self.locals.get('absolute_import', ()):
  438. if isinstance(stmt, node_classes.ImportFrom) and stmt.modname == '__future__':
  439. return True
  440. return False
  441. else:
  442. _absolute_import_activated = True
  443. def absolute_import_activated(self):
  444. """Whether :pep:`328` absolute import behaviour has been enabled.
  445. :returns: True if :pep:`328` has been enabled, False otherwise.
  446. :rtype: bool
  447. """
  448. return self._absolute_import_activated
  449. def import_module(self, modname, relative_only=False, level=None):
  450. """Get the ast for a given module as if imported from this module.
  451. :param modname: The name of the module to "import".
  452. :type modname: str
  453. :param relative_only: Whether to only consider relative imports.
  454. :type relative_only: bool
  455. :param level: The level of relative import.
  456. :type level: int or None
  457. :returns: The imported module ast.
  458. :rtype: NodeNG
  459. """
  460. if relative_only and level is None:
  461. level = 0
  462. absmodname = self.relative_to_absolute_name(modname, level)
  463. try:
  464. return MANAGER.ast_from_module_name(absmodname)
  465. except exceptions.AstroidBuildingError:
  466. # we only want to import a sub module or package of this module,
  467. # skip here
  468. if relative_only:
  469. raise
  470. return MANAGER.ast_from_module_name(modname)
  471. def relative_to_absolute_name(self, modname, level):
  472. """Get the absolute module name for a relative import.
  473. The relative import can be implicit or explicit.
  474. :param modname: The module name to convert.
  475. :type modname: str
  476. :param level: The level of relative import.
  477. :type level: int
  478. :returns: The absolute module name.
  479. :rtype: str
  480. :raises TooManyLevelsError: When the relative import refers to a
  481. module too far above this one.
  482. """
  483. # XXX this returns non sens when called on an absolute import
  484. # like 'pylint.checkers.astroid.utils'
  485. # XXX doesn't return absolute name if self.name isn't absolute name
  486. if self.absolute_import_activated() and level is None:
  487. return modname
  488. if level:
  489. if self.package:
  490. level = level - 1
  491. if level and self.name.count('.') < level:
  492. raise exceptions.TooManyLevelsError(
  493. level=level, name=self.name)
  494. package_name = self.name.rsplit('.', level)[0]
  495. elif self.package:
  496. package_name = self.name
  497. else:
  498. package_name = self.name.rsplit('.', 1)[0]
  499. if package_name:
  500. if not modname:
  501. return package_name
  502. return '%s.%s' % (package_name, modname)
  503. return modname
  504. def wildcard_import_names(self):
  505. """The list of imported names when this module is 'wildcard imported'.
  506. It doesn't include the '__builtins__' name which is added by the
  507. current CPython implementation of wildcard imports.
  508. :returns: The list of imported names.
  509. :rtype: list(str)
  510. """
  511. # We separate the different steps of lookup in try/excepts
  512. # to avoid catching too many Exceptions
  513. default = [name for name in self.keys() if not name.startswith('_')]
  514. try:
  515. all_values = self['__all__']
  516. except KeyError:
  517. return default
  518. try:
  519. explicit = next(all_values.assigned_stmts())
  520. except exceptions.InferenceError:
  521. return default
  522. except AttributeError:
  523. # not an assignment node
  524. # XXX infer?
  525. return default
  526. # Try our best to detect the exported name.
  527. inferred = []
  528. try:
  529. explicit = next(explicit.infer())
  530. except exceptions.InferenceError:
  531. return default
  532. if not isinstance(explicit, (node_classes.Tuple, node_classes.List)):
  533. return default
  534. str_const = lambda node: (isinstance(node, node_classes.Const) and
  535. isinstance(node.value, six.string_types))
  536. for node in explicit.elts:
  537. if str_const(node):
  538. inferred.append(node.value)
  539. else:
  540. try:
  541. inferred_node = next(node.infer())
  542. except exceptions.InferenceError:
  543. continue
  544. if str_const(inferred_node):
  545. inferred.append(inferred_node.value)
  546. return inferred
  547. def public_names(self):
  548. """The list of the names that are publicly available in this module.
  549. :returns: The list of publc names.
  550. :rtype: list(str)
  551. """
  552. return [name for name in self.keys() if not name.startswith('_')]
  553. def bool_value(self):
  554. """Determine the boolean value of this node.
  555. :returns: The boolean value of this node.
  556. For a :class:`Module` this is always ``True``.
  557. :rtype: bool
  558. """
  559. return True
  560. class ComprehensionScope(LocalsDictNodeNG):
  561. """Scoping for different types of comprehensions."""
  562. def frame(self):
  563. """The first parent frame node.
  564. A frame node is a :class:`Module`, :class:`FunctionDef`,
  565. or :class:`ClassDef`.
  566. :returns: The first parent frame node.
  567. :rtype: Module or FunctionDef or ClassDef
  568. """
  569. return self.parent.frame()
  570. scope_lookup = LocalsDictNodeNG._scope_lookup
  571. class GeneratorExp(ComprehensionScope):
  572. """Class representing an :class:`ast.GeneratorExp` node.
  573. >>> node = astroid.extract_node('(thing for thing in things if thing)')
  574. >>> node
  575. <GeneratorExp l.1 at 0x7f23b2e4e400>
  576. """
  577. _astroid_fields = ('elt', 'generators')
  578. _other_other_fields = ('locals',)
  579. elt = None
  580. """The element that forms the output of the expression.
  581. :type: NodeNG or None
  582. """
  583. generators = None
  584. """The generators that are looped through.
  585. :type: list(Comprehension) or None
  586. """
  587. def __init__(self, lineno=None, col_offset=None, parent=None):
  588. """
  589. :param lineno: The line that this node appears on in the source code.
  590. :type lineno: int or None
  591. :param col_offset: The column that this node appears on in the
  592. source code.
  593. :type col_offset: int or None
  594. :param parent: The parent node in the syntax tree.
  595. :type parent: NodeNG or None
  596. """
  597. self.locals = {}
  598. """A map of the name of a local variable to the node defining the local.
  599. :type: dict(str, NodeNG)
  600. """
  601. super(GeneratorExp, self).__init__(lineno, col_offset, parent)
  602. def postinit(self, elt=None, generators=None):
  603. """Do some setup after initialisation.
  604. :param elt: The element that forms the output of the expression.
  605. :type elt: NodeNG or None
  606. :param generators: The generators that are looped through.
  607. :type generators: list(Comprehension) or None
  608. """
  609. self.elt = elt
  610. if generators is None:
  611. self.generators = []
  612. else:
  613. self.generators = generators
  614. def bool_value(self):
  615. """Determine the boolean value of this node.
  616. :returns: The boolean value of this node.
  617. For a :class:`GeneratorExp` this is always ``True``.
  618. :rtype: bool
  619. """
  620. return True
  621. class DictComp(ComprehensionScope):
  622. """Class representing an :class:`ast.DictComp` node.
  623. >>> node = astroid.extract_node('{k:v for k, v in things if k > v}')
  624. >>> node
  625. <DictComp l.1 at 0x7f23b2e41d68>
  626. """
  627. _astroid_fields = ('key', 'value', 'generators')
  628. _other_other_fields = ('locals',)
  629. key = None
  630. """What produces the keys.
  631. :type: NodeNG or None
  632. """
  633. value = None
  634. """What produces the values.
  635. :type: NodeNG or None
  636. """
  637. generators = None
  638. """The generators that are looped through.
  639. :type: list(Comprehension) or None
  640. """
  641. def __init__(self, lineno=None, col_offset=None, parent=None):
  642. """
  643. :param lineno: The line that this node appears on in the source code.
  644. :type lineno: int or None
  645. :param col_offset: The column that this node appears on in the
  646. source code.
  647. :type col_offset: int or None
  648. :param parent: The parent node in the syntax tree.
  649. :type parent: NodeNG or None
  650. """
  651. self.locals = {}
  652. """A map of the name of a local variable to the node defining the local.
  653. :type: dict(str, NodeNG)
  654. """
  655. super(DictComp, self).__init__(lineno, col_offset, parent)
  656. def postinit(self, key=None, value=None, generators=None):
  657. """Do some setup after initialisation.
  658. :param key: What produces the keys.
  659. :type key: NodeNG or None
  660. :param value: What produces the values.
  661. :type value: NodeNG or None
  662. :param generators: The generators that are looped through.
  663. :type generators: list(Comprehension) or None
  664. """
  665. self.key = key
  666. self.value = value
  667. if generators is None:
  668. self.generators = []
  669. else:
  670. self.generators = generators
  671. def bool_value(self):
  672. """Determine the boolean value of this node.
  673. :returns: The boolean value of this node.
  674. For a :class:`DictComp` this is always :class:`Uninferable`.
  675. :rtype: Uninferable
  676. """
  677. return util.Uninferable
  678. class SetComp(ComprehensionScope):
  679. """Class representing an :class:`ast.SetComp` node.
  680. >>> node = astroid.extract_node('{thing for thing in things if thing}')
  681. >>> node
  682. <SetComp l.1 at 0x7f23b2e41898>
  683. """
  684. _astroid_fields = ('elt', 'generators')
  685. _other_other_fields = ('locals',)
  686. elt = None
  687. """The element that forms the output of the expression.
  688. :type: NodeNG or None
  689. """
  690. generators = None
  691. """The generators that are looped through.
  692. :type: list(Comprehension) or None
  693. """
  694. def __init__(self, lineno=None, col_offset=None, parent=None):
  695. """
  696. :param lineno: The line that this node appears on in the source code.
  697. :type lineno: int or None
  698. :param col_offset: The column that this node appears on in the
  699. source code.
  700. :type col_offset: int or None
  701. :param parent: The parent node in the syntax tree.
  702. :type parent: NodeNG or None
  703. """
  704. self.locals = {}
  705. """A map of the name of a local variable to the node defining the local.
  706. :type: dict(str, NodeNG)
  707. """
  708. super(SetComp, self).__init__(lineno, col_offset, parent)
  709. def postinit(self, elt=None, generators=None):
  710. """Do some setup after initialisation.
  711. :param elt: The element that forms the output of the expression.
  712. :type elt: NodeNG or None
  713. :param generators: The generators that are looped through.
  714. :type generators: list(Comprehension) or None
  715. """
  716. self.elt = elt
  717. if generators is None:
  718. self.generators = []
  719. else:
  720. self.generators = generators
  721. def bool_value(self):
  722. """Determine the boolean value of this node.
  723. :returns: The boolean value of this node.
  724. For a :class:`SetComp` this is always :class:`Uninferable`.
  725. :rtype: Uninferable
  726. """
  727. return util.Uninferable
  728. class _ListComp(node_classes.NodeNG):
  729. """Class representing an :class:`ast.ListComp` node.
  730. >>> node = astroid.extract_node('[thing for thing in things if thing]')
  731. >>> node
  732. <ListComp l.1 at 0x7f23b2e418d0>
  733. """
  734. _astroid_fields = ('elt', 'generators')
  735. elt = None
  736. """The element that forms the output of the expression.
  737. :type: NodeNG or None
  738. """
  739. generators = None
  740. """The generators that are looped through.
  741. :type: list(Comprehension) or None
  742. """
  743. def postinit(self, elt=None, generators=None):
  744. """Do some setup after initialisation.
  745. :param elt: The element that forms the output of the expression.
  746. :type elt: NodeNG or None
  747. :param generators: The generators that are looped through.
  748. :type generators: list(Comprehension) or None
  749. """
  750. self.elt = elt
  751. self.generators = generators
  752. def bool_value(self):
  753. """Determine the boolean value of this node.
  754. :returns: The boolean value of this node.
  755. For a :class:`ListComp` this is always :class:`Uninferable`.
  756. :rtype: Uninferable
  757. """
  758. return util.Uninferable
  759. if six.PY3:
  760. class ListComp(_ListComp, ComprehensionScope):
  761. """Class representing an :class:`ast.ListComp` node.
  762. >>> node = astroid.extract_node('[thing for thing in things if thing]')
  763. >>> node
  764. <ListComp l.1 at 0x7f23b2e418d0>
  765. """
  766. _other_other_fields = ('locals',)
  767. def __init__(self, lineno=None, col_offset=None, parent=None):
  768. self.locals = {}
  769. """A map of the name of a local variable to the node defining it.
  770. :type: dict(str, NodeNG)
  771. """
  772. super(ListComp, self).__init__(lineno, col_offset, parent)
  773. else:
  774. class ListComp(_ListComp):
  775. """Class representing an :class:`ast.ListComp` node.
  776. >>> node = astroid.extract_node('[thing for thing in things if thing]')
  777. >>> node
  778. <ListComp l.1 at 0x7f23b2e418d0>
  779. """
  780. def _infer_decorator_callchain(node):
  781. """Detect decorator call chaining and see if the end result is a
  782. static or a classmethod.
  783. """
  784. if not isinstance(node, FunctionDef):
  785. return None
  786. if not node.parent:
  787. return None
  788. try:
  789. # TODO: We don't handle multiple inference results right now,
  790. # because there's no flow to reason when the return
  791. # is what we are looking for, a static or a class method.
  792. result = next(node.infer_call_result(node.parent))
  793. except (StopIteration, exceptions.InferenceError):
  794. return None
  795. if isinstance(result, bases.Instance):
  796. result = result._proxied
  797. if isinstance(result, ClassDef):
  798. if result.is_subtype_of('%s.classmethod' % BUILTINS):
  799. return 'classmethod'
  800. if result.is_subtype_of('%s.staticmethod' % BUILTINS):
  801. return 'staticmethod'
  802. return None
  803. class Lambda(mixins.FilterStmtsMixin, LocalsDictNodeNG):
  804. """Class representing an :class:`ast.Lambda` node.
  805. >>> node = astroid.extract_node('lambda arg: arg + 1')
  806. >>> node
  807. <Lambda.<lambda> l.1 at 0x7f23b2e41518>
  808. """
  809. _astroid_fields = ('args', 'body',)
  810. _other_other_fields = ('locals',)
  811. name = '<lambda>'
  812. # function's type, 'function' | 'method' | 'staticmethod' | 'classmethod'
  813. @property
  814. def type(self):
  815. """Whether this is a method or function.
  816. :returns: 'method' if this is a method, 'function' otherwise.
  817. :rtype: str
  818. """
  819. # pylint: disable=no-member
  820. if self.args.args and self.args.args[0].name == 'self':
  821. if isinstance(self.parent.scope(), ClassDef):
  822. return 'method'
  823. return 'function'
  824. def __init__(self, lineno=None, col_offset=None, parent=None):
  825. """
  826. :param lineno: The line that this node appears on in the source code.
  827. :type lineno: int or None
  828. :param col_offset: The column that this node appears on in the
  829. source code.
  830. :type col_offset: int or None
  831. :param parent: The parent node in the syntax tree.
  832. :type parent: NodeNG or None
  833. """
  834. self.locals = {}
  835. """A map of the name of a local variable to the node defining it.
  836. :type: dict(str, NodeNG)
  837. """
  838. self.args = []
  839. """The arguments that the function takes.
  840. :type: Arguments or list
  841. """
  842. self.body = []
  843. """The contents of the function body.
  844. :type: list(NodeNG)
  845. """
  846. super(Lambda, self).__init__(lineno, col_offset, parent)
  847. def postinit(self, args, body):
  848. """Do some setup after initialisation.
  849. :param args: The arguments that the function takes.
  850. :type args: Arguments
  851. :param body: The contents of the function body.
  852. :type body: list(NodeNG)
  853. """
  854. self.args = args
  855. self.body = body
  856. def pytype(self):
  857. """Get the name of the type that this node represents.
  858. :returns: The name of the type.
  859. :rtype: str
  860. """
  861. if 'method' in self.type:
  862. return '%s.instancemethod' % BUILTINS
  863. return '%s.function' % BUILTINS
  864. def display_type(self):
  865. """A human readable type of this node.
  866. :returns: The type of this node.
  867. :rtype: str
  868. """
  869. if 'method' in self.type:
  870. return 'Method'
  871. return 'Function'
  872. def callable(self):
  873. """Whether this node defines something that is callable.
  874. :returns: True if this defines something that is callable,
  875. False otherwise.
  876. For a :class:`Lambda` this is always ``True``.
  877. :rtype: bool
  878. """
  879. return True
  880. def argnames(self):
  881. """Get the names of each of the arguments.
  882. :returns: The names of the arguments.
  883. :rtype: list(str)
  884. """
  885. # pylint: disable=no-member; github.com/pycqa/astroid/issues/291
  886. # args is in fact redefined later on by postinit. Can't be changed
  887. # to None due to a strong interaction between Lambda and FunctionDef.
  888. if self.args.args: # maybe None with builtin functions
  889. names = _rec_get_names(self.args.args)
  890. else:
  891. names = []
  892. if self.args.vararg:
  893. names.append(self.args.vararg)
  894. if self.args.kwarg:
  895. names.append(self.args.kwarg)
  896. return names
  897. def infer_call_result(self, caller, context=None):
  898. """Infer what the function returns when called.
  899. :param caller: Unused
  900. :type caller: object
  901. """
  902. # pylint: disable=no-member; github.com/pycqa/astroid/issues/291
  903. # args is in fact redefined later on by postinit. Can't be changed
  904. # to None due to a strong interaction between Lambda and FunctionDef.
  905. return self.body.infer(context)
  906. def scope_lookup(self, node, name, offset=0):
  907. """Lookup where the given names is assigned.
  908. :param node: The node to look for assignments up to.
  909. Any assignments after the given node are ignored.
  910. :type node: NodeNG
  911. :param name: The name to find assignments for.
  912. :type name: str
  913. :param offset: The line offset to filter statements up to.
  914. :type offset: int
  915. :returns: This scope node and the list of assignments associated to the
  916. given name according to the scope where it has been found (locals,
  917. globals or builtin).
  918. :rtype: tuple(str, list(NodeNG))
  919. """
  920. # pylint: disable=no-member; github.com/pycqa/astroid/issues/291
  921. # args is in fact redefined later on by postinit. Can't be changed
  922. # to None due to a strong interaction between Lambda and FunctionDef.
  923. if node in self.args.defaults or node in self.args.kw_defaults:
  924. frame = self.parent.frame()
  925. # line offset to avoid that def func(f=func) resolve the default
  926. # value to the defined function
  927. offset = -1
  928. else:
  929. # check this is not used in function decorators
  930. frame = self
  931. return frame._scope_lookup(node, name, offset)
  932. def bool_value(self):
  933. """Determine the boolean value of this node.
  934. :returns: The boolean value of this node.
  935. For a :class:`Lambda` this is always ``True``.
  936. :rtype: bool
  937. """
  938. return True
  939. class FunctionDef(node_classes.Statement, Lambda):
  940. """Class representing an :class:`ast.FunctionDef`.
  941. >>> node = astroid.extract_node('''
  942. ... def my_func(arg):
  943. ... return arg + 1
  944. ... ''')
  945. >>> node
  946. <FunctionDef.my_func l.2 at 0x7f23b2e71e10>
  947. """
  948. if six.PY3:
  949. _astroid_fields = ('decorators', 'args', 'returns', 'body')
  950. returns = None
  951. else:
  952. _astroid_fields = ('decorators', 'args', 'body')
  953. decorators = None
  954. """The decorators that are applied to this method or function.
  955. :type: Decorators or None
  956. """
  957. special_attributes = objectmodel.FunctionModel()
  958. """The names of special attributes that this function has.
  959. :type: objectmodel.FunctionModel
  960. """
  961. is_function = True
  962. """Whether this node indicates a function.
  963. For a :class:`FunctionDef` this is always ``True``.
  964. :type: bool
  965. """
  966. # attributes below are set by the builder module or by raw factories
  967. _other_fields = ('name', 'doc')
  968. _other_other_fields = ('locals', '_type')
  969. _type = None
  970. def __init__(self, name=None, doc=None, lineno=None,
  971. col_offset=None, parent=None):
  972. """
  973. :param name: The name of the function.
  974. :type name: str or None
  975. :param doc: The function's docstring.
  976. :type doc: str or None
  977. :param lineno: The line that this node appears on in the source code.
  978. :type lineno: int or None
  979. :param col_offset: The column that this node appears on in the
  980. source code.
  981. :type col_offset: int or None
  982. :param parent: The parent node in the syntax tree.
  983. :type parent: NodeNG or None
  984. """
  985. self.name = name
  986. """The name of the function.
  987. :type name: str or None
  988. """
  989. self.doc = doc
  990. """The function's docstring.
  991. :type doc: str or None
  992. """
  993. self.instance_attrs = {}
  994. super(FunctionDef, self).__init__(lineno, col_offset, parent)
  995. if parent:
  996. frame = parent.frame()
  997. frame.set_local(name, self)
  998. # pylint: disable=arguments-differ; different than Lambdas
  999. def postinit(self, args, body, decorators=None, returns=None):
  1000. """Do some setup after initialisation.
  1001. :param args: The arguments that the function takes.
  1002. :type args: Arguments or list
  1003. :param body: The contents of the function body.
  1004. :type body: list(NodeNG)
  1005. :param decorators: The decorators that are applied to this
  1006. method or function.
  1007. :type decorators: Decorators or None
  1008. """
  1009. self.args = args
  1010. self.body = body
  1011. self.decorators = decorators
  1012. self.returns = returns
  1013. if six.PY3 and isinstance(self.parent.frame(), ClassDef):
  1014. self.set_local('__class__', self.parent.frame())
  1015. @decorators_mod.cachedproperty
  1016. def extra_decorators(self):
  1017. """The extra decorators that this function can have.
  1018. Additional decorators are considered when they are used as
  1019. assignments, as in ``method = staticmethod(method)``.
  1020. The property will return all the callables that are used for
  1021. decoration.
  1022. :type: list(NodeNG)
  1023. """
  1024. frame = self.parent.frame()
  1025. if not isinstance(frame, ClassDef):
  1026. return []
  1027. decorators = []
  1028. for assign in frame.nodes_of_class(node_classes.Assign):
  1029. if (isinstance(assign.value, node_classes.Call)
  1030. and isinstance(assign.value.func, node_classes.Name)):
  1031. for assign_node in assign.targets:
  1032. if not isinstance(assign_node, node_classes.AssignName):
  1033. # Support only `name = callable(name)`
  1034. continue
  1035. if assign_node.name != self.name:
  1036. # Interested only in the assignment nodes that
  1037. # decorates the current method.
  1038. continue
  1039. try:
  1040. meth = frame[self.name]
  1041. except KeyError:
  1042. continue
  1043. else:
  1044. # Must be a function and in the same frame as the
  1045. # original method.
  1046. if (isinstance(meth, FunctionDef)
  1047. and assign_node.frame() == frame):
  1048. decorators.append(assign.value)
  1049. return decorators
  1050. @decorators_mod.cachedproperty
  1051. def type(self):
  1052. """The function type for this node.
  1053. Possible values are: method, function, staticmethod, classmethod.
  1054. :type: str
  1055. """
  1056. builtin_descriptors = {'classmethod', 'staticmethod'}
  1057. for decorator in self.extra_decorators:
  1058. if decorator.func.name in builtin_descriptors:
  1059. return decorator.func.name
  1060. frame = self.parent.frame()
  1061. type_name = 'function'
  1062. if isinstance(frame, ClassDef):
  1063. if self.name == '__new__':
  1064. return 'classmethod'
  1065. elif sys.version_info >= (3, 6) and self.name == '__init_subclass__':
  1066. return 'classmethod'
  1067. else:
  1068. type_name = 'method'
  1069. if not self.decorators:
  1070. return type_name
  1071. for node in self.decorators.nodes:
  1072. if isinstance(node, node_classes.Name):
  1073. if node.name in builtin_descriptors:
  1074. return node.name
  1075. if isinstance(node, node_classes.Call):
  1076. # Handle the following case:
  1077. # @some_decorator(arg1, arg2)
  1078. # def func(...)
  1079. #
  1080. try:
  1081. current = next(node.func.infer())
  1082. except exceptions.InferenceError:
  1083. continue
  1084. _type = _infer_decorator_callchain(current)
  1085. if _type is not None:
  1086. return _type
  1087. try:
  1088. for inferred in node.infer():
  1089. # Check to see if this returns a static or a class method.
  1090. _type = _infer_decorator_callchain(inferred)
  1091. if _type is not None:
  1092. return _type
  1093. if not isinstance(inferred, ClassDef):
  1094. continue
  1095. for ancestor in inferred.ancestors():
  1096. if not isinstance(ancestor, ClassDef):
  1097. continue
  1098. if ancestor.is_subtype_of('%s.classmethod' % BUILTINS):
  1099. return 'classmethod'
  1100. elif ancestor.is_subtype_of('%s.staticmethod' % BUILTINS):
  1101. return 'staticmethod'
  1102. except exceptions.InferenceError:
  1103. pass
  1104. return type_name
  1105. @decorators_mod.cachedproperty
  1106. def fromlineno(self):
  1107. """The first line that this node appears on in the source code.
  1108. :type: int or None
  1109. """
  1110. # lineno is the line number of the first decorator, we want the def
  1111. # statement lineno
  1112. lineno = self.lineno
  1113. if self.decorators is not None:
  1114. lineno += sum(node.tolineno - node.lineno + 1
  1115. for node in self.decorators.nodes)
  1116. return lineno
  1117. @decorators_mod.cachedproperty
  1118. def blockstart_tolineno(self):
  1119. """The line on which the beginning of this block ends.
  1120. :type: int
  1121. """
  1122. return self.args.tolineno
  1123. def block_range(self, lineno):
  1124. """Get a range from the given line number to where this node ends.
  1125. :param lineno: Unused.
  1126. :type lineno: int
  1127. :returns: The range of line numbers that this node belongs to,
  1128. :rtype: tuple(int, int)
  1129. """
  1130. return self.fromlineno, self.tolineno
  1131. def getattr(self, name, context=None):
  1132. """this method doesn't look in the instance_attrs dictionary since it's
  1133. done by an Instance proxy at inference time.
  1134. """
  1135. if name in self.instance_attrs:
  1136. return self.instance_attrs[name]
  1137. if name in self.special_attributes:
  1138. return [self.special_attributes.lookup(name)]
  1139. raise exceptions.AttributeInferenceError(target=self, attribute=name)
  1140. def igetattr(self, name, context=None):
  1141. """Inferred getattr, which returns an iterator of inferred statements."""
  1142. try:
  1143. return bases._infer_stmts(self.getattr(name, context),
  1144. context, frame=self)
  1145. except exceptions.AttributeInferenceError as error:
  1146. util.reraise(exceptions.InferenceError(
  1147. error.message, target=self, attribute=name, context=context))
  1148. def is_method(self):
  1149. """Check if this function node represents a method.
  1150. :returns: True if this is a method, False otherwise.
  1151. :rtype: bool
  1152. """
  1153. # check we are defined in a ClassDef, because this is usually expected
  1154. # (e.g. pylint...) when is_method() return True
  1155. return self.type != 'function' and isinstance(self.parent.frame(), ClassDef)
  1156. @decorators_mod.cached
  1157. def decoratornames(self):
  1158. """Get the qualified names of each of the decorators on this function.
  1159. :returns: The names of the decorators.
  1160. :rtype: set(str)
  1161. """
  1162. result = set()
  1163. decoratornodes = []
  1164. if self.decorators is not None:
  1165. decoratornodes += self.decorators.nodes
  1166. decoratornodes += self.extra_decorators
  1167. for decnode in decoratornodes:
  1168. try:
  1169. for infnode in decnode.infer():
  1170. result.add(infnode.qname())
  1171. except exceptions.InferenceError:
  1172. continue
  1173. return result
  1174. def is_bound(self):
  1175. """Check if the function is bound to an instance or class.
  1176. :returns: True if the function is bound to an instance or class,
  1177. False otherwise.
  1178. :rtype: bool
  1179. """
  1180. return self.type == 'classmethod'
  1181. def is_abstract(self, pass_is_abstract=True):
  1182. """Check if the method is abstract.
  1183. A method is considered abstract if any of the following is true:
  1184. * The only statement is 'raise NotImplementedError'
  1185. * The only statement is 'pass' and pass_is_abstract is True
  1186. * The method is annotated with abc.astractproperty/abc.abstractmethod
  1187. :returns: True if the method is abstract, False otherwise.
  1188. :rtype: bool
  1189. """
  1190. if self.decorators:
  1191. for node in self.decorators.nodes:
  1192. try:
  1193. inferred = next(node.infer())
  1194. except exceptions.InferenceError:
  1195. continue
  1196. if inferred and inferred.qname() in ('abc.abstractproperty',
  1197. 'abc.abstractmethod'):
  1198. return True
  1199. for child_node in self.body:
  1200. if isinstance(child_node, node_classes.Raise):
  1201. if child_node.raises_not_implemented():
  1202. return True
  1203. return pass_is_abstract and isinstance(child_node, node_classes.Pass)
  1204. # empty function is the same as function with a single "pass" statement
  1205. if pass_is_abstract:
  1206. return True
  1207. def is_generator(self):
  1208. """Check if this is a generator function.
  1209. :returns: True is this is a generator function, False otherwise.
  1210. :rtype: bool
  1211. """
  1212. yield_nodes = (node_classes.Yield, node_classes.YieldFrom)
  1213. return next(self.nodes_of_class(yield_nodes,
  1214. skip_klass=(FunctionDef, Lambda)), False)
  1215. def infer_call_result(self, caller, context=None):
  1216. """Infer what the function returns when called.
  1217. :returns: What the function returns.
  1218. :rtype: iterable(NodeNG or Uninferable) or None
  1219. """
  1220. if self.is_generator():
  1221. result = bases.Generator(self)
  1222. yield result
  1223. return
  1224. # This is really a gigantic hack to work around metaclass generators
  1225. # that return transient class-generating functions. Pylint's AST structure
  1226. # cannot handle a base class object that is only used for calling __new__,
  1227. # but does not contribute to the inheritance structure itself. We inject
  1228. # a fake class into the hierarchy here for several well-known metaclass
  1229. # generators, and filter it out later.
  1230. if (self.name == 'with_metaclass' and
  1231. len(self.args.args) == 1 and
  1232. self.args.vararg is not None):
  1233. metaclass = next(caller.args[0].infer(context))
  1234. if isinstance(metaclass, ClassDef):
  1235. c = ClassDef('temporary_class', None)
  1236. c.hide = True
  1237. c.parent = self
  1238. class_bases = [next(b.infer(context)) for b in caller.args[1:]]
  1239. c.bases = [base for base in class_bases if base != util.Uninferable]
  1240. c._metaclass = metaclass
  1241. yield c
  1242. return
  1243. returns = self.nodes_of_class(node_classes.Return, skip_klass=FunctionDef)
  1244. for returnnode in returns:
  1245. if returnnode.value is None:
  1246. yield node_classes.Const(None)
  1247. else:
  1248. try:
  1249. for inferred in returnnode.value.infer(context):
  1250. yield inferred
  1251. except exceptions.InferenceError:
  1252. yield util.Uninferable
  1253. def bool_value(self):
  1254. """Determine the boolean value of this node.
  1255. :returns: The boolean value of this node.
  1256. For a :class:`FunctionDef` this is always ``True``.
  1257. :rtype: bool
  1258. """
  1259. return True
  1260. class AsyncFunctionDef(FunctionDef):
  1261. """Class representing an :class:`ast.FunctionDef` node.
  1262. A :class:`AsyncFunctionDef` is an asynchronous function
  1263. created with the `async` keyword.
  1264. >>> node = astroid.extract_node('''
  1265. async def func(things):
  1266. async for thing in things:
  1267. print(thing)
  1268. ''')
  1269. >>> node
  1270. <AsyncFunctionDef.func l.2 at 0x7f23b2e416d8>
  1271. >>> node.body[0]
  1272. <AsyncFor l.3 at 0x7f23b2e417b8>
  1273. """
  1274. def _rec_get_names(args, names=None):
  1275. """return a list of all argument names"""
  1276. if names is None:
  1277. names = []
  1278. for arg in args:
  1279. if isinstance(arg, node_classes.Tuple):
  1280. _rec_get_names(arg.elts, names)
  1281. else:
  1282. names.append(arg.name)
  1283. return names
  1284. def _is_metaclass(klass, seen=None):
  1285. """ Return if the given class can be
  1286. used as a metaclass.
  1287. """
  1288. if klass.name == 'type':
  1289. return True
  1290. if seen is None:
  1291. seen = set()
  1292. for base in klass.bases:
  1293. try:
  1294. for baseobj in base.infer():
  1295. baseobj_name = baseobj.qname()
  1296. if baseobj_name in seen:
  1297. continue
  1298. else:
  1299. seen.add(baseobj_name)
  1300. if isinstance(baseobj, bases.Instance):
  1301. # not abstract
  1302. return False
  1303. if baseobj is util.Uninferable:
  1304. continue
  1305. if baseobj is klass:
  1306. continue
  1307. if not isinstance(baseobj, ClassDef):
  1308. continue
  1309. if baseobj._type == 'metaclass':
  1310. return True
  1311. if _is_metaclass(baseobj, seen):
  1312. return True
  1313. except exceptions.InferenceError:
  1314. continue
  1315. return False
  1316. def _class_type(klass, ancestors=None):
  1317. """return a ClassDef node type to differ metaclass and exception
  1318. from 'regular' classes
  1319. """
  1320. # XXX we have to store ancestors in case we have a ancestor loop
  1321. if klass._type is not None:
  1322. return klass._type
  1323. if _is_metaclass(klass):
  1324. klass._type = 'metaclass'
  1325. elif klass.name.endswith('Exception'):
  1326. klass._type = 'exception'
  1327. else:
  1328. if ancestors is None:
  1329. ancestors = set()
  1330. klass_name = klass.qname()
  1331. if klass_name in ancestors:
  1332. # XXX we are in loop ancestors, and have found no type
  1333. klass._type = 'class'
  1334. return 'class'
  1335. ancestors.add(klass_name)
  1336. for base in klass.ancestors(recurs=False):
  1337. name = _class_type(base, ancestors)
  1338. if name != 'class':
  1339. if name == 'metaclass' and not _is_metaclass(klass):
  1340. # don't propagate it if the current class
  1341. # can't be a metaclass
  1342. continue
  1343. klass._type = base.type
  1344. break
  1345. if klass._type is None:
  1346. klass._type = 'class'
  1347. return klass._type
  1348. def get_wrapping_class(node):
  1349. """Get the class that wraps the given node.
  1350. We consider that a class wraps a node if the class
  1351. is a parent for the said node.
  1352. :returns: The class that wraps the given node
  1353. :rtype: ClassDef or None
  1354. """
  1355. klass = node.frame()
  1356. while klass is not None and not isinstance(klass, ClassDef):
  1357. if klass.parent is None:
  1358. klass = None
  1359. else:
  1360. klass = klass.parent.frame()
  1361. return klass
  1362. class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG,
  1363. node_classes.Statement):
  1364. """Class representing an :class:`ast.ClassDef` node.
  1365. >>> node = astroid.extract_node('''
  1366. class Thing:
  1367. def my_meth(self, arg):
  1368. return arg + self.offset
  1369. ''')
  1370. >>> node
  1371. <ClassDef.Thing l.2 at 0x7f23b2e9e748>
  1372. """
  1373. # some of the attributes below are set by the builder module or
  1374. # by a raw factories
  1375. # a dictionary of class instances attributes
  1376. _astroid_fields = ('decorators', 'bases', 'body') # name
  1377. decorators = None
  1378. """The decorators that are applied to this class.
  1379. :type: Decorators or None
  1380. """
  1381. special_attributes = objectmodel.ClassModel()
  1382. """The names of special attributes that this class has.
  1383. :type: objectmodel.ClassModel
  1384. """
  1385. _type = None
  1386. _metaclass_hack = False
  1387. hide = False
  1388. type = property(_class_type,
  1389. doc=("The class type for this node.\n\n"
  1390. "Possible values are: class, metaclass, exception.\n\n"
  1391. ":type: str"))
  1392. _other_fields = ('name', 'doc')
  1393. _other_other_fields = ('locals', '_newstyle')
  1394. _newstyle = None
  1395. def __init__(self, name=None, doc=None, lineno=None,
  1396. col_offset=None, parent=None):
  1397. """
  1398. :param name: The name of the class.
  1399. :type name: str or None
  1400. :param doc: The function's docstring.
  1401. :type doc: str or None
  1402. :param lineno: The line that this node appears on in the source code.
  1403. :type lineno: int or None
  1404. :param col_offset: The column that this node appears on in the
  1405. source code.
  1406. :type col_offset: int or None
  1407. :param parent: The parent node in the syntax tree.
  1408. :type parent: NodeNG or None
  1409. """
  1410. self.instance_attrs = {}
  1411. self.locals = {}
  1412. """A map of the name of a local variable to the node defining it.
  1413. :type: dict(str, NodeNG)
  1414. """
  1415. self.keywords = []
  1416. """The keywords given to the class definition.
  1417. This is usually for :pep:`3115` style metaclass declaration.
  1418. :type: list(Keyword) or None
  1419. """
  1420. self.bases = []
  1421. """What the class inherits from.
  1422. :type: list(NodeNG)
  1423. """
  1424. self.body = []
  1425. """The contents of the class body.
  1426. :type: list(NodeNG)
  1427. """
  1428. self.name = name
  1429. """The name of the class.
  1430. :type name: str or None
  1431. """
  1432. self.doc = doc
  1433. """The class' docstring.
  1434. :type doc: str or None
  1435. """
  1436. super(ClassDef, self).__init__(lineno, col_offset, parent)
  1437. if parent is not None:
  1438. parent.frame().set_local(name, self)
  1439. # pylint: disable=redefined-outer-name
  1440. def postinit(self, bases, body, decorators, newstyle=None, metaclass=None, keywords=None):
  1441. """Do some setup after initialisation.
  1442. :param bases: What the class inherits from.
  1443. :type bases: list(NodeNG)
  1444. :param body: The contents of the class body.
  1445. :type body: list(NodeNG)
  1446. :param decorators: The decorators that are applied to this class.
  1447. :type decorators: Decorators or None
  1448. :param newstyle: Whether this is a new style class or not.
  1449. :type newstyle: bool or None
  1450. :param metaclass: The metaclass of this class.
  1451. :type metaclass: NodeNG or None
  1452. :param keywords: The keywords given to the class definition.
  1453. :type keywords: list(Keyword) or None
  1454. """
  1455. self.keywords = keywords
  1456. self.bases = bases
  1457. self.body = body
  1458. self.decorators = decorators
  1459. if newstyle is not None:
  1460. self._newstyle = newstyle
  1461. if metaclass is not None:
  1462. self._metaclass = metaclass
  1463. def _newstyle_impl(self, context=None):
  1464. if context is None:
  1465. context = contextmod.InferenceContext()
  1466. if self._newstyle is not None:
  1467. return self._newstyle
  1468. for base in self.ancestors(recurs=False, context=context):
  1469. if base._newstyle_impl(context):
  1470. self._newstyle = True
  1471. break
  1472. klass = self.declared_metaclass()
  1473. # could be any callable, we'd need to infer the result of klass(name,
  1474. # bases, dict). punt if it's not a class node.
  1475. if klass is not None and isinstance(klass, ClassDef):
  1476. self._newstyle = klass._newstyle_impl(context)
  1477. if self._newstyle is None:
  1478. self._newstyle = False
  1479. return self._newstyle
  1480. _newstyle = None
  1481. newstyle = property(_newstyle_impl,
  1482. doc=("Whether this is a new style class or not\n\n"
  1483. ":type: bool or None"))
  1484. @decorators_mod.cachedproperty
  1485. def blockstart_tolineno(self):
  1486. """The line on which the beginning of this block ends.
  1487. :type: int
  1488. """
  1489. if self.bases:
  1490. return self.bases[-1].tolineno
  1491. return self.fromlineno
  1492. def block_range(self, lineno):
  1493. """Get a range from the given line number to where this node ends.
  1494. :param lineno: Unused.
  1495. :type lineno: int
  1496. :returns: The range of line numbers that this node belongs to,
  1497. :rtype: tuple(int, int)
  1498. """
  1499. return self.fromlineno, self.tolineno
  1500. def pytype(self):
  1501. """Get the name of the type that this node represents.
  1502. :returns: The name of the type.
  1503. :rtype: str
  1504. """
  1505. if self.newstyle:
  1506. return '%s.type' % BUILTINS
  1507. return '%s.classobj' % BUILTINS
  1508. def display_type(self):
  1509. """A human readable type of this node.
  1510. :returns: The type of this node.
  1511. :rtype: str
  1512. """
  1513. return 'Class'
  1514. def callable(self):
  1515. """Whether this node defines something that is callable.
  1516. :returns: True if this defines something that is callable,
  1517. False otherwise.
  1518. For a :class:`ClassDef` this is always ``True``.
  1519. :rtype: bool
  1520. """
  1521. return True
  1522. def is_subtype_of(self, type_name, context=None):
  1523. """Whether this class is a subtype of the given type.
  1524. :param type_name: The name of the type of check against.
  1525. :type type_name: str
  1526. :returns: True if this class is a subtype of the given type,
  1527. False otherwise.
  1528. :rtype: bool
  1529. """
  1530. if self.qname() == type_name:
  1531. return True
  1532. for anc in self.ancestors(context=context):
  1533. if anc.qname() == type_name:
  1534. return True
  1535. return False
  1536. def _infer_type_call(self, caller, context):
  1537. name_node = next(caller.args[0].infer(context))
  1538. if (isinstance(name_node, node_classes.Const) and
  1539. isinstance(name_node.value, six.string_types)):
  1540. name = name_node.value
  1541. else:
  1542. return util.Uninferable
  1543. result = ClassDef(name, None)
  1544. # Get the bases of the class.
  1545. class_bases = next(caller.args[1].infer(context))
  1546. if isinstance(class_bases, (node_classes.Tuple, node_classes.List)):
  1547. result.bases = class_bases.itered()
  1548. else:
  1549. # There is currently no AST node that can represent an 'unknown'
  1550. # node (Uninferable is not an AST node), therefore we simply return Uninferable here
  1551. # although we know at least the name of the class.
  1552. return util.Uninferable
  1553. # Get the members of the class
  1554. try:
  1555. members = next(caller.args[2].infer(context))
  1556. except exceptions.InferenceError:
  1557. members = None
  1558. if members and isinstance(members, node_classes.Dict):
  1559. for attr, value in members.items:
  1560. if (isinstance(attr, node_classes.Const) and
  1561. isinstance(attr.value, six.string_types)):
  1562. result.locals[attr.value] = [value]
  1563. result.parent = caller.parent
  1564. return result
  1565. def infer_call_result(self, caller, context=None):
  1566. """infer what a class is returning when called"""
  1567. if (self.is_subtype_of('%s.type' % (BUILTINS,), context)
  1568. and len(caller.args) == 3):
  1569. result = self._infer_type_call(caller, context)
  1570. yield result
  1571. else:
  1572. yield bases.Instance(self)
  1573. def scope_lookup(self, node, name, offset=0):
  1574. """Lookup where the given name is assigned.
  1575. :param node: The node to look for assignments up to.
  1576. Any assignments after the given node are ignored.
  1577. :type node: NodeNG
  1578. :param name: The name to find assignments for.
  1579. :type name: str
  1580. :param offset: The line offset to filter statements up to.
  1581. :type offset: int
  1582. :returns: This scope node and the list of assignments associated to the
  1583. given name according to the scope where it has been found (locals,
  1584. globals or builtin).
  1585. :rtype: tuple(str, list(NodeNG))
  1586. """
  1587. # If the name looks like a builtin name, just try to look
  1588. # into the upper scope of this class. We might have a
  1589. # decorator that it's poorly named after a builtin object
  1590. # inside this class.
  1591. lookup_upper_frame = (
  1592. isinstance(node.parent, node_classes.Decorators) and
  1593. name in MANAGER.astroid_cache[six.moves.builtins.__name__]
  1594. )
  1595. if any(node == base or base.parent_of(node)
  1596. for base in self.bases) or lookup_upper_frame:
  1597. # Handle the case where we have either a name
  1598. # in the bases of a class, which exists before
  1599. # the actual definition or the case where we have
  1600. # a Getattr node, with that name.
  1601. #
  1602. # name = ...
  1603. # class A(name):
  1604. # def name(self): ...
  1605. #
  1606. # import name
  1607. # class A(name.Name):
  1608. # def name(self): ...
  1609. frame = self.parent.frame()
  1610. # line offset to avoid that class A(A) resolve the ancestor to
  1611. # the defined class
  1612. offset = -1
  1613. else:
  1614. frame = self
  1615. return frame._scope_lookup(node, name, offset)
  1616. @property
  1617. def basenames(self):
  1618. """The names of the parent classes
  1619. Names are given in the order they appear in the class definition.
  1620. :type: list(str)
  1621. """
  1622. return [bnode.as_string() for bnode in self.bases]
  1623. def ancestors(self, recurs=True, context=None):
  1624. """Iterate over the base classes in prefixed depth first order.
  1625. :param recurs: Whether to recurse or return direct ancestors only.
  1626. :type recurs: bool
  1627. :returns: The base classes
  1628. :rtype: iterable(NodeNG)
  1629. """
  1630. # FIXME: should be possible to choose the resolution order
  1631. # FIXME: inference make infinite loops possible here
  1632. yielded = set([self])
  1633. if context is None:
  1634. context = contextmod.InferenceContext()
  1635. if six.PY3:
  1636. if not self.bases and self.qname() != 'builtins.object':
  1637. yield builtin_lookup("object")[1][0]
  1638. return
  1639. for stmt in self.bases:
  1640. with context.restore_path():
  1641. try:
  1642. for baseobj in stmt.infer(context):
  1643. if not isinstance(baseobj, ClassDef):
  1644. if isinstance(baseobj, bases.Instance):
  1645. baseobj = baseobj._proxied
  1646. else:
  1647. continue
  1648. if not baseobj.hide:
  1649. if baseobj in yielded:
  1650. continue
  1651. yielded.add(baseobj)
  1652. yield baseobj
  1653. if recurs:
  1654. for grandpa in baseobj.ancestors(recurs=True,
  1655. context=context):
  1656. if grandpa is self:
  1657. # This class is the ancestor of itself.
  1658. break
  1659. if grandpa in yielded:
  1660. continue
  1661. yielded.add(grandpa)
  1662. yield grandpa
  1663. except exceptions.InferenceError:
  1664. continue
  1665. def local_attr_ancestors(self, name, context=None):
  1666. """Iterate over the parents that define the given name.
  1667. :param name: The name to find definitions for.
  1668. :type name: str
  1669. :returns: The parents that define the given name.
  1670. :rtype: iterable(NodeNG)
  1671. """
  1672. if self.newstyle and all(n.newstyle for n in self.ancestors(context)):
  1673. # Look up in the mro if we can. This will result in the
  1674. # attribute being looked up just as Python does it.
  1675. try:
  1676. ancestors = self.mro(context)[1:]
  1677. except exceptions.MroError:
  1678. # Fallback to use ancestors, we can't determine
  1679. # a sane MRO.
  1680. ancestors = self.ancestors(context=context)
  1681. else:
  1682. ancestors = self.ancestors(context=context)
  1683. for astroid in ancestors:
  1684. if name in astroid:
  1685. yield astroid
  1686. def instance_attr_ancestors(self, name, context=None):
  1687. """Iterate over the parents that define the given name as an attribute.
  1688. :param name: The name to find definitions for.
  1689. :type name: str
  1690. :returns: The parents that define the given name as
  1691. an instance attribute.
  1692. :rtype: iterable(NodeNG)
  1693. """
  1694. for astroid in self.ancestors(context=context):
  1695. if name in astroid.instance_attrs:
  1696. yield astroid
  1697. def has_base(self, node):
  1698. """Whether this class directly inherits from the given node.
  1699. :param node: The node to check for.
  1700. :type node: NodeNG
  1701. :returns: True if this class directly inherits from the given node.
  1702. :rtype: bool
  1703. """
  1704. return node in self.bases
  1705. def local_attr(self, name, context=None):
  1706. """Get the list of assign nodes associated to the given name.
  1707. Assignments are looked for in both this class and in parents.
  1708. :returns: The list of assignments to the given name.
  1709. :rtype: list(NodeNG)
  1710. :raises AttributeInferenceError: If no attribute with this name
  1711. can be found in this class or parent classes.
  1712. """
  1713. result = []
  1714. if name in self.locals:
  1715. result = self.locals[name]
  1716. else:
  1717. class_node = next(self.local_attr_ancestors(name, context), ())
  1718. if class_node:
  1719. result = class_node.locals[name]
  1720. result = [n for n in result if not isinstance(n, node_classes.DelAttr)]
  1721. if result:
  1722. return result
  1723. raise exceptions.AttributeInferenceError(target=self, attribute=name,
  1724. context=context)
  1725. def instance_attr(self, name, context=None):
  1726. """Get the list of nodes associated to the given attribute name.
  1727. Assignments are looked for in both this class and in parents.
  1728. :returns: The list of assignments to the given name.
  1729. :rtype: list(NodeNG)
  1730. :raises AttributeInferenceError: If no attribute with this name
  1731. can be found in this class or parent classes.
  1732. """
  1733. # Return a copy, so we don't modify self.instance_attrs,
  1734. # which could lead to infinite loop.
  1735. values = list(self.instance_attrs.get(name, []))
  1736. # get all values from parents
  1737. for class_node in self.instance_attr_ancestors(name, context):
  1738. values += class_node.instance_attrs[name]
  1739. values = [n for n in values if not isinstance(n, node_classes.DelAttr)]
  1740. if values:
  1741. return values
  1742. raise exceptions.AttributeInferenceError(target=self, attribute=name,
  1743. context=context)
  1744. def instantiate_class(self):
  1745. """Get an :class:`Instance` of the :class:`ClassDef` node.
  1746. :returns: An :class:`Instance` of the :class:`ClassDef` node,
  1747. or self if this is not possible.
  1748. :rtype: Instance or ClassDef
  1749. """
  1750. return bases.Instance(self)
  1751. def instanciate_class(self):
  1752. """A deprecated alias for :meth:`instanciate_class`.
  1753. .. deprecated:: 1.5
  1754. :returns: An :class:`Instance` of the :class:`ClassDef` node,
  1755. or self if this is not possible.
  1756. :rtype: Instance or ClassDef
  1757. """
  1758. warnings.warn('%s.instanciate_class() is deprecated and slated for '
  1759. 'removal in astroid 2.0, use %s.instantiate_class() '
  1760. 'instead.' % (type(self).__name__, type(self).__name__),
  1761. PendingDeprecationWarning, stacklevel=2)
  1762. return self.instantiate_class()
  1763. def getattr(self, name, context=None, class_context=True):
  1764. """Get an attribute from this class, using Python's attribute semantic.
  1765. This method doesn't look in the :attr:`instance_attrs` dictionary
  1766. since it is done by an :class:`Instance` proxy at inference time.
  1767. It may return an :class:`Uninferable` object if
  1768. the attribute has not been
  1769. found, but a ``__getattr__`` or ``__getattribute__`` method is defined.
  1770. If ``class_context`` is given, then it is considered that the
  1771. attribute is accessed from a class context,
  1772. e.g. ClassDef.attribute, otherwise it might have been accessed
  1773. from an instance as well. If ``class_context`` is used in that
  1774. case, then a lookup in the implicit metaclass and the explicit
  1775. metaclass will be done.
  1776. :param name: The attribute to look for.
  1777. :type name: str
  1778. :param class_context: Whether the attribute can be accessed statically.
  1779. :type class_context: bool
  1780. :returns: The attribute.
  1781. :rtype: list(NodeNG)
  1782. :raises AttributeInferenceError: If the attribute cannot be inferred.
  1783. """
  1784. values = self.locals.get(name, [])
  1785. if name in self.special_attributes and class_context and not values:
  1786. result = [self.special_attributes.lookup(name)]
  1787. if name == '__bases__':
  1788. # Need special treatment, since they are mutable
  1789. # and we need to return all the values.
  1790. result += values
  1791. return result
  1792. # don't modify the list in self.locals!
  1793. values = list(values)
  1794. for classnode in self.ancestors(recurs=True, context=context):
  1795. values += classnode.locals.get(name, [])
  1796. if class_context:
  1797. values += self._metaclass_lookup_attribute(name, context)
  1798. if not values:
  1799. raise exceptions.AttributeInferenceError(target=self, attribute=name,
  1800. context=context)
  1801. return values
  1802. def _metaclass_lookup_attribute(self, name, context):
  1803. """Search the given name in the implicit and the explicit metaclass."""
  1804. attrs = set()
  1805. implicit_meta = self.implicit_metaclass()
  1806. metaclass = self.metaclass()
  1807. for cls in {implicit_meta, metaclass}:
  1808. if cls and cls != self and isinstance(cls, ClassDef):
  1809. cls_attributes = self._get_attribute_from_metaclass(
  1810. cls, name, context)
  1811. attrs.update(set(cls_attributes))
  1812. return attrs
  1813. def _get_attribute_from_metaclass(self, cls, name, context):
  1814. try:
  1815. attrs = cls.getattr(name, context=context,
  1816. class_context=True)
  1817. except exceptions.AttributeInferenceError:
  1818. return
  1819. for attr in bases._infer_stmts(attrs, context, frame=cls):
  1820. if not isinstance(attr, FunctionDef):
  1821. yield attr
  1822. continue
  1823. if bases._is_property(attr):
  1824. # TODO(cpopa): don't use a private API.
  1825. for inferred in attr.infer_call_result(self, context):
  1826. yield inferred
  1827. continue
  1828. if attr.type == 'classmethod':
  1829. # If the method is a classmethod, then it will
  1830. # be bound to the metaclass, not to the class
  1831. # from where the attribute is retrieved.
  1832. # get_wrapping_class could return None, so just
  1833. # default to the current class.
  1834. frame = get_wrapping_class(attr) or self
  1835. yield bases.BoundMethod(attr, frame)
  1836. elif attr.type == 'staticmethod':
  1837. yield attr
  1838. else:
  1839. yield bases.BoundMethod(attr, self)
  1840. def igetattr(self, name, context=None, class_context=True):
  1841. """Infer the possible values of the given variable.
  1842. :param name: The name of the variable to infer.
  1843. :type name: str
  1844. :returns: The inferred possible values.
  1845. :rtype: iterable(NodeNG or Uninferable)
  1846. """
  1847. # set lookup name since this is necessary to infer on import nodes for
  1848. # instance
  1849. context = contextmod.copy_context(context)
  1850. context.lookupname = name
  1851. try:
  1852. attrs = self.getattr(name, context, class_context=class_context)
  1853. for inferred in bases._infer_stmts(attrs, context, frame=self):
  1854. # yield Uninferable object instead of descriptors when necessary
  1855. if (not isinstance(inferred, node_classes.Const)
  1856. and isinstance(inferred, bases.Instance)):
  1857. try:
  1858. inferred._proxied.getattr('__get__', context)
  1859. except exceptions.AttributeInferenceError:
  1860. yield inferred
  1861. else:
  1862. yield util.Uninferable
  1863. else:
  1864. yield function_to_method(inferred, self)
  1865. except exceptions.AttributeInferenceError as error:
  1866. if not name.startswith('__') and self.has_dynamic_getattr(context):
  1867. # class handle some dynamic attributes, return a Uninferable object
  1868. yield util.Uninferable
  1869. else:
  1870. util.reraise(exceptions.InferenceError(
  1871. error.message, target=self, attribute=name, context=context))
  1872. def has_dynamic_getattr(self, context=None):
  1873. """Check if the class has a custom __getattr__ or __getattribute__.
  1874. If any such method is found and it is not from
  1875. builtins, nor from an extension module, then the function
  1876. will return True.
  1877. :returns: True if the class has a custom
  1878. __getattr__ or __getattribute__, False otherwise.
  1879. :rtype: bool
  1880. """
  1881. def _valid_getattr(node):
  1882. root = node.root()
  1883. return root.name != BUILTINS and getattr(root, 'pure_python', None)
  1884. try:
  1885. return _valid_getattr(self.getattr('__getattr__', context)[0])
  1886. except exceptions.AttributeInferenceError:
  1887. #if self.newstyle: XXX cause an infinite recursion error
  1888. try:
  1889. getattribute = self.getattr('__getattribute__', context)[0]
  1890. return _valid_getattr(getattribute)
  1891. except exceptions.AttributeInferenceError:
  1892. pass
  1893. return False
  1894. def getitem(self, index, context=None):
  1895. """Return the inference of a subscript.
  1896. This is basically looking up the method in the metaclass and calling it.
  1897. :returns: The inferred value of a subscript to this class.
  1898. :rtype: NodeNG
  1899. :raises AstroidTypeError: If this class does not define a
  1900. ``__getitem__`` method.
  1901. """
  1902. try:
  1903. methods = dunder_lookup.lookup(self, '__getitem__')
  1904. except exceptions.AttributeInferenceError as exc:
  1905. util.reraise(
  1906. exceptions.AstroidTypeError(
  1907. node=self, error=exc,
  1908. context=context
  1909. )
  1910. )
  1911. method = methods[0]
  1912. # Create a new callcontext for providing index as an argument.
  1913. if context:
  1914. new_context = context.clone()
  1915. else:
  1916. new_context = contextmod.InferenceContext()
  1917. new_context.callcontext = contextmod.CallContext(args=[index])
  1918. new_context.boundnode = self
  1919. return next(method.infer_call_result(self, new_context))
  1920. def methods(self):
  1921. """Iterate over all of the method defined in this class and its parents.
  1922. :returns: The methods defined on the class.
  1923. :rtype: iterable(FunctionDef)
  1924. """
  1925. done = {}
  1926. for astroid in itertools.chain(iter((self,)), self.ancestors()):
  1927. for meth in astroid.mymethods():
  1928. if meth.name in done:
  1929. continue
  1930. done[meth.name] = None
  1931. yield meth
  1932. def mymethods(self):
  1933. """Iterate over all of the method defined in this class only.
  1934. :returns: The methods defined on the class.
  1935. :rtype: iterable(FunctionDef)
  1936. """
  1937. for member in self.values():
  1938. if isinstance(member, FunctionDef):
  1939. yield member
  1940. def implicit_metaclass(self):
  1941. """Get the implicit metaclass of the current class.
  1942. For newstyle classes, this will return an instance of builtins.type.
  1943. For oldstyle classes, it will simply return None, since there's
  1944. no implicit metaclass there.
  1945. :returns: The metaclass.
  1946. :rtype: builtins.type or None
  1947. """
  1948. if self.newstyle:
  1949. return builtin_lookup('type')[1][0]
  1950. return None
  1951. _metaclass = None
  1952. def declared_metaclass(self):
  1953. """Return the explicit declared metaclass for the current class.
  1954. An explicit declared metaclass is defined
  1955. either by passing the ``metaclass`` keyword argument
  1956. in the class definition line (Python 3) or (Python 2) by
  1957. having a ``__metaclass__`` class attribute, or if there are
  1958. no explicit bases but there is a global ``__metaclass__`` variable.
  1959. :returns: The metaclass of this class,
  1960. or None if one could not be found.
  1961. :rtype: NodeNG or None
  1962. """
  1963. for base in self.bases:
  1964. try:
  1965. for baseobj in base.infer():
  1966. if isinstance(baseobj, ClassDef) and baseobj.hide:
  1967. self._metaclass = baseobj._metaclass
  1968. self._metaclass_hack = True
  1969. break
  1970. except exceptions.InferenceError:
  1971. pass
  1972. if self._metaclass:
  1973. # Expects this from Py3k TreeRebuilder
  1974. try:
  1975. return next(node for node in self._metaclass.infer()
  1976. if node is not util.Uninferable)
  1977. except (exceptions.InferenceError, StopIteration):
  1978. return None
  1979. if six.PY3:
  1980. return None
  1981. if '__metaclass__' in self.locals:
  1982. assignment = self.locals['__metaclass__'][-1]
  1983. elif self.bases:
  1984. return None
  1985. elif '__metaclass__' in self.root().locals:
  1986. assignments = [ass for ass in self.root().locals['__metaclass__']
  1987. if ass.lineno < self.lineno]
  1988. if not assignments:
  1989. return None
  1990. assignment = assignments[-1]
  1991. else:
  1992. return None
  1993. try:
  1994. inferred = next(assignment.infer())
  1995. except exceptions.InferenceError:
  1996. return None
  1997. if inferred is util.Uninferable: # don't expose this
  1998. return None
  1999. return inferred
  2000. def _find_metaclass(self, seen=None):
  2001. if seen is None:
  2002. seen = set()
  2003. seen.add(self)
  2004. klass = self.declared_metaclass()
  2005. if klass is None:
  2006. for parent in self.ancestors():
  2007. if parent not in seen:
  2008. klass = parent._find_metaclass(seen)
  2009. if klass is not None:
  2010. break
  2011. return klass
  2012. def metaclass(self):
  2013. """Get the metaclass of this class.
  2014. If this class does not define explicitly a metaclass,
  2015. then the first defined metaclass in ancestors will be used
  2016. instead.
  2017. :returns: The metaclass of this class.
  2018. :rtype: NodeNG or None
  2019. """
  2020. return self._find_metaclass()
  2021. def has_metaclass_hack(self):
  2022. return self._metaclass_hack
  2023. def _islots(self):
  2024. """ Return an iterator with the inferred slots. """
  2025. if '__slots__' not in self.locals:
  2026. return
  2027. for slots in self.igetattr('__slots__'):
  2028. # check if __slots__ is a valid type
  2029. for meth in ITER_METHODS:
  2030. try:
  2031. slots.getattr(meth)
  2032. break
  2033. except exceptions.AttributeInferenceError:
  2034. continue
  2035. else:
  2036. continue
  2037. if isinstance(slots, node_classes.Const):
  2038. # a string. Ignore the following checks,
  2039. # but yield the node, only if it has a value
  2040. if slots.value:
  2041. yield slots
  2042. continue
  2043. if not hasattr(slots, 'itered'):
  2044. # we can't obtain the values, maybe a .deque?
  2045. continue
  2046. if isinstance(slots, node_classes.Dict):
  2047. values = [item[0] for item in slots.items]
  2048. else:
  2049. values = slots.itered()
  2050. if values is util.Uninferable:
  2051. continue
  2052. if not values:
  2053. # Stop the iteration, because the class
  2054. # has an empty list of slots.
  2055. raise StopIteration(values)
  2056. for elt in values:
  2057. try:
  2058. for inferred in elt.infer():
  2059. if inferred is util.Uninferable:
  2060. continue
  2061. if (not isinstance(inferred, node_classes.Const) or
  2062. not isinstance(inferred.value,
  2063. six.string_types)):
  2064. continue
  2065. if not inferred.value:
  2066. continue
  2067. yield inferred
  2068. except exceptions.InferenceError:
  2069. continue
  2070. def _slots(self):
  2071. if not self.newstyle:
  2072. raise NotImplementedError(
  2073. "The concept of slots is undefined for old-style classes.")
  2074. slots = self._islots()
  2075. try:
  2076. first = next(slots)
  2077. except StopIteration as exc:
  2078. # The class doesn't have a __slots__ definition or empty slots.
  2079. if exc.args and exc.args[0] not in ('', None):
  2080. return exc.args[0]
  2081. return None
  2082. return [first] + list(slots)
  2083. # Cached, because inferring them all the time is expensive
  2084. @decorators_mod.cached
  2085. def slots(self):
  2086. """Get all the slots for this node.
  2087. :returns: The names of slots for this class.
  2088. If the class doesn't define any slot, through the ``__slots__``
  2089. variable, then this function will return a None.
  2090. Also, it will return None in the case the slots were not inferred.
  2091. :rtype: list(str) or None
  2092. """
  2093. def grouped_slots():
  2094. # Not interested in object, since it can't have slots.
  2095. for cls in self.mro()[:-1]:
  2096. try:
  2097. cls_slots = cls._slots()
  2098. except NotImplementedError:
  2099. continue
  2100. if cls_slots is not None:
  2101. for slot in cls_slots:
  2102. yield slot
  2103. else:
  2104. yield None
  2105. if not self.newstyle:
  2106. raise NotImplementedError(
  2107. "The concept of slots is undefined for old-style classes.")
  2108. slots = list(grouped_slots())
  2109. if not all(slot is not None for slot in slots):
  2110. return None
  2111. return sorted(slots, key=lambda item: item.value)
  2112. def _inferred_bases(self, context=None):
  2113. # TODO(cpopa): really similar with .ancestors,
  2114. # but the difference is when one base is inferred,
  2115. # only the first object is wanted. That's because
  2116. # we aren't interested in superclasses, as in the following
  2117. # example:
  2118. #
  2119. # class SomeSuperClass(object): pass
  2120. # class SomeClass(SomeSuperClass): pass
  2121. # class Test(SomeClass): pass
  2122. #
  2123. # Inferring SomeClass from the Test's bases will give
  2124. # us both SomeClass and SomeSuperClass, but we are interested
  2125. # only in SomeClass.
  2126. if context is None:
  2127. context = contextmod.InferenceContext()
  2128. if six.PY3:
  2129. if not self.bases and self.qname() != 'builtins.object':
  2130. yield builtin_lookup("object")[1][0]
  2131. return
  2132. for stmt in self.bases:
  2133. try:
  2134. baseobj = next(stmt.infer(context=context))
  2135. except exceptions.InferenceError:
  2136. continue
  2137. if isinstance(baseobj, bases.Instance):
  2138. baseobj = baseobj._proxied
  2139. if not isinstance(baseobj, ClassDef):
  2140. continue
  2141. if not baseobj.hide:
  2142. yield baseobj
  2143. else:
  2144. for base in baseobj.bases:
  2145. yield base
  2146. def _compute_mro(self, context=None):
  2147. inferred_bases = list(self._inferred_bases(context=context))
  2148. bases_mro = []
  2149. for base in inferred_bases:
  2150. if base is self:
  2151. continue
  2152. try:
  2153. mro = base._compute_mro(context=context)
  2154. bases_mro.append(mro)
  2155. except NotImplementedError:
  2156. # Some classes have in their ancestors both newstyle and
  2157. # old style classes. For these we can't retrieve the .mro,
  2158. # although in Python it's possible, since the class we are
  2159. # currently working is in fact new style.
  2160. # So, we fallback to ancestors here.
  2161. ancestors = list(base.ancestors(context=context))
  2162. bases_mro.append(ancestors)
  2163. unmerged_mro = ([[self]] + bases_mro + [inferred_bases])
  2164. _verify_duplicates_mro(unmerged_mro, self, context)
  2165. return _c3_merge(unmerged_mro, self, context)
  2166. def mro(self, context=None):
  2167. """Get the method resolution order, using C3 linearization.
  2168. :returns: The list of ancestors, sorted by the mro.
  2169. :rtype: list(NodeNG)
  2170. :raises NotImplementedError: If this is an old style class,
  2171. since they don't have the concept of an MRO.
  2172. """
  2173. if not self.newstyle:
  2174. raise NotImplementedError(
  2175. "Could not obtain mro for old-style classes.")
  2176. return self._compute_mro(context=context)
  2177. def bool_value(self):
  2178. """Determine the boolean value of this node.
  2179. :returns: The boolean value of this node.
  2180. For a :class:`ClassDef` this is always ``True``.
  2181. :rtype: bool
  2182. """
  2183. return True
  2184. # Backwards-compatibility aliases
  2185. Class = util.proxy_alias('Class', ClassDef)
  2186. Function = util.proxy_alias('Function', FunctionDef)
  2187. GenExpr = util.proxy_alias('GenExpr', GeneratorExp)