rebuilder.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475
  1. # Copyright (c) 2009-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2013-2020 Claudiu Popa <pcmanticore@gmail.com>
  3. # Copyright (c) 2013-2014 Google, Inc.
  4. # Copyright (c) 2014 Alexander Presnyakov <flagist0@gmail.com>
  5. # Copyright (c) 2014 Eevee (Alex Munroe) <amunroe@yelp.com>
  6. # Copyright (c) 2015-2016 Ceridwen <ceridwenv@gmail.com>
  7. # Copyright (c) 2016-2017 Derek Gustafson <degustaf@gmail.com>
  8. # Copyright (c) 2016 Jared Garst <jgarst@users.noreply.github.com>
  9. # Copyright (c) 2017 Hugo <hugovk@users.noreply.github.com>
  10. # Copyright (c) 2017 Łukasz Rogalski <rogalski.91@gmail.com>
  11. # Copyright (c) 2017 rr- <rr-@sakuya.pl>
  12. # Copyright (c) 2018-2019 Ville Skyttä <ville.skytta@iki.fi>
  13. # Copyright (c) 2018 Tomas Gavenciak <gavento@ucw.cz>
  14. # Copyright (c) 2018 Serhiy Storchaka <storchaka@gmail.com>
  15. # Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
  16. # Copyright (c) 2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  17. # Copyright (c) 2019-2021 Ashley Whetter <ashley@awhetter.co.uk>
  18. # Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
  19. # Copyright (c) 2019 Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
  20. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  21. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  22. # Copyright (c) 2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  23. # Copyright (c) 2021 Federico Bond <federicobond@gmail.com>
  24. # Copyright (c) 2021 hippo91 <guillaume.peillex@gmail.com>
  25. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  26. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  27. """this module contains utilities for rebuilding an _ast tree in
  28. order to get a single Astroid representation
  29. """
  30. import sys
  31. from typing import (
  32. TYPE_CHECKING,
  33. Callable,
  34. Dict,
  35. Generator,
  36. List,
  37. Optional,
  38. Tuple,
  39. Type,
  40. TypeVar,
  41. Union,
  42. cast,
  43. overload,
  44. )
  45. from astroid import nodes
  46. from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment
  47. from astroid.const import PY38, PY38_PLUS, Context
  48. from astroid.manager import AstroidManager
  49. from astroid.nodes import NodeNG
  50. if sys.version_info >= (3, 8):
  51. from typing import Final
  52. else:
  53. from typing_extensions import Final
  54. if TYPE_CHECKING:
  55. import ast
  56. REDIRECT: Final[Dict[str, str]] = {
  57. "arguments": "Arguments",
  58. "comprehension": "Comprehension",
  59. "ListCompFor": "Comprehension",
  60. "GenExprFor": "Comprehension",
  61. "excepthandler": "ExceptHandler",
  62. "keyword": "Keyword",
  63. "match_case": "MatchCase",
  64. }
  65. T_Doc = TypeVar(
  66. "T_Doc",
  67. "ast.Module",
  68. "ast.ClassDef",
  69. Union["ast.FunctionDef", "ast.AsyncFunctionDef"],
  70. )
  71. T_Function = TypeVar("T_Function", nodes.FunctionDef, nodes.AsyncFunctionDef)
  72. T_For = TypeVar("T_For", nodes.For, nodes.AsyncFor)
  73. T_With = TypeVar("T_With", nodes.With, nodes.AsyncWith)
  74. # noinspection PyMethodMayBeStatic
  75. class TreeRebuilder:
  76. """Rebuilds the _ast tree to become an Astroid tree"""
  77. def __init__(
  78. self, manager: AstroidManager, parser_module: Optional[ParserModule] = None
  79. ):
  80. self._manager = manager
  81. self._global_names: List[Dict[str, List[nodes.Global]]] = []
  82. self._import_from_nodes: List[nodes.ImportFrom] = []
  83. self._delayed_assattr: List[nodes.AssignAttr] = []
  84. self._visit_meths: Dict[
  85. Type["ast.AST"], Callable[["ast.AST", NodeNG], NodeNG]
  86. ] = {}
  87. if parser_module is None:
  88. self._parser_module = get_parser_module()
  89. else:
  90. self._parser_module = parser_module
  91. self._module = self._parser_module.module
  92. def _get_doc(self, node: T_Doc) -> Tuple[T_Doc, Optional[str]]:
  93. try:
  94. if node.body and isinstance(node.body[0], self._module.Expr):
  95. first_value = node.body[0].value
  96. if isinstance(first_value, self._module.Str) or (
  97. PY38_PLUS
  98. and isinstance(first_value, self._module.Constant)
  99. and isinstance(first_value.value, str)
  100. ):
  101. doc = first_value.value if PY38_PLUS else first_value.s
  102. node.body = node.body[1:]
  103. return node, doc
  104. except IndexError:
  105. pass # ast built from scratch
  106. return node, None
  107. def _get_context(
  108. self,
  109. node: Union[
  110. "ast.Attribute",
  111. "ast.List",
  112. "ast.Name",
  113. "ast.Subscript",
  114. "ast.Starred",
  115. "ast.Tuple",
  116. ],
  117. ) -> Context:
  118. return self._parser_module.context_classes.get(type(node.ctx), Context.Load)
  119. def visit_module(
  120. self, node: "ast.Module", modname: str, modpath: str, package: bool
  121. ) -> nodes.Module:
  122. """visit a Module node by returning a fresh instance of it
  123. Note: Method not called by 'visit'
  124. """
  125. node, doc = self._get_doc(node)
  126. newnode = nodes.Module(
  127. name=modname,
  128. doc=doc,
  129. file=modpath,
  130. path=[modpath],
  131. package=package,
  132. parent=None,
  133. )
  134. newnode.postinit([self.visit(child, newnode) for child in node.body])
  135. return newnode
  136. if sys.version_info >= (3, 10):
  137. @overload
  138. def visit(self, node: "ast.arg", parent: NodeNG) -> nodes.AssignName:
  139. ...
  140. @overload
  141. def visit(self, node: "ast.arguments", parent: NodeNG) -> nodes.Arguments:
  142. ...
  143. @overload
  144. def visit(self, node: "ast.Assert", parent: NodeNG) -> nodes.Assert:
  145. ...
  146. @overload
  147. def visit(
  148. self, node: "ast.AsyncFunctionDef", parent: NodeNG
  149. ) -> nodes.AsyncFunctionDef:
  150. ...
  151. @overload
  152. def visit(self, node: "ast.AsyncFor", parent: NodeNG) -> nodes.AsyncFor:
  153. ...
  154. @overload
  155. def visit(self, node: "ast.Await", parent: NodeNG) -> nodes.Await:
  156. ...
  157. @overload
  158. def visit(self, node: "ast.AsyncWith", parent: NodeNG) -> nodes.AsyncWith:
  159. ...
  160. @overload
  161. def visit(self, node: "ast.Assign", parent: NodeNG) -> nodes.Assign:
  162. ...
  163. @overload
  164. def visit(self, node: "ast.AnnAssign", parent: NodeNG) -> nodes.AnnAssign:
  165. ...
  166. @overload
  167. def visit(self, node: "ast.AugAssign", parent: NodeNG) -> nodes.AugAssign:
  168. ...
  169. @overload
  170. def visit(self, node: "ast.BinOp", parent: NodeNG) -> nodes.BinOp:
  171. ...
  172. @overload
  173. def visit(self, node: "ast.BoolOp", parent: NodeNG) -> nodes.BoolOp:
  174. ...
  175. @overload
  176. def visit(self, node: "ast.Break", parent: NodeNG) -> nodes.Break:
  177. ...
  178. @overload
  179. def visit(self, node: "ast.Call", parent: NodeNG) -> nodes.Call:
  180. ...
  181. @overload
  182. def visit(self, node: "ast.ClassDef", parent: NodeNG) -> nodes.ClassDef:
  183. ...
  184. @overload
  185. def visit(self, node: "ast.Continue", parent: NodeNG) -> nodes.Continue:
  186. ...
  187. @overload
  188. def visit(self, node: "ast.Compare", parent: NodeNG) -> nodes.Compare:
  189. ...
  190. @overload
  191. def visit(
  192. self, node: "ast.comprehension", parent: NodeNG
  193. ) -> nodes.Comprehension:
  194. ...
  195. @overload
  196. def visit(self, node: "ast.Delete", parent: NodeNG) -> nodes.Delete:
  197. ...
  198. @overload
  199. def visit(self, node: "ast.Dict", parent: NodeNG) -> nodes.Dict:
  200. ...
  201. @overload
  202. def visit(self, node: "ast.DictComp", parent: NodeNG) -> nodes.DictComp:
  203. ...
  204. @overload
  205. def visit(self, node: "ast.Expr", parent: NodeNG) -> nodes.Expr:
  206. ...
  207. # Not used in Python 3.8+
  208. @overload
  209. def visit(self, node: "ast.Ellipsis", parent: NodeNG) -> nodes.Const:
  210. ...
  211. @overload
  212. def visit(
  213. self, node: "ast.ExceptHandler", parent: NodeNG
  214. ) -> nodes.ExceptHandler:
  215. ...
  216. # Not used in Python 3.9+
  217. @overload
  218. def visit(self, node: "ast.ExtSlice", parent: nodes.Subscript) -> nodes.Tuple:
  219. ...
  220. @overload
  221. def visit(self, node: "ast.For", parent: NodeNG) -> nodes.For:
  222. ...
  223. @overload
  224. def visit(self, node: "ast.ImportFrom", parent: NodeNG) -> nodes.ImportFrom:
  225. ...
  226. @overload
  227. def visit(self, node: "ast.FunctionDef", parent: NodeNG) -> nodes.FunctionDef:
  228. ...
  229. @overload
  230. def visit(self, node: "ast.GeneratorExp", parent: NodeNG) -> nodes.GeneratorExp:
  231. ...
  232. @overload
  233. def visit(self, node: "ast.Attribute", parent: NodeNG) -> nodes.Attribute:
  234. ...
  235. @overload
  236. def visit(self, node: "ast.Global", parent: NodeNG) -> nodes.Global:
  237. ...
  238. @overload
  239. def visit(self, node: "ast.If", parent: NodeNG) -> nodes.If:
  240. ...
  241. @overload
  242. def visit(self, node: "ast.IfExp", parent: NodeNG) -> nodes.IfExp:
  243. ...
  244. @overload
  245. def visit(self, node: "ast.Import", parent: NodeNG) -> nodes.Import:
  246. ...
  247. @overload
  248. def visit(self, node: "ast.JoinedStr", parent: NodeNG) -> nodes.JoinedStr:
  249. ...
  250. @overload
  251. def visit(
  252. self, node: "ast.FormattedValue", parent: NodeNG
  253. ) -> nodes.FormattedValue:
  254. ...
  255. @overload
  256. def visit(self, node: "ast.NamedExpr", parent: NodeNG) -> nodes.NamedExpr:
  257. ...
  258. # Not used in Python 3.9+
  259. @overload
  260. def visit(self, node: "ast.Index", parent: nodes.Subscript) -> NodeNG:
  261. ...
  262. @overload
  263. def visit(self, node: "ast.keyword", parent: NodeNG) -> nodes.Keyword:
  264. ...
  265. @overload
  266. def visit(self, node: "ast.Lambda", parent: NodeNG) -> nodes.Lambda:
  267. ...
  268. @overload
  269. def visit(self, node: "ast.List", parent: NodeNG) -> nodes.List:
  270. ...
  271. @overload
  272. def visit(self, node: "ast.ListComp", parent: NodeNG) -> nodes.ListComp:
  273. ...
  274. @overload
  275. def visit(
  276. self, node: "ast.Name", parent: NodeNG
  277. ) -> Union[nodes.Name, nodes.Const, nodes.AssignName, nodes.DelName]:
  278. ...
  279. # Not used in Python 3.8+
  280. @overload
  281. def visit(self, node: "ast.NameConstant", parent: NodeNG) -> nodes.Const:
  282. ...
  283. @overload
  284. def visit(self, node: "ast.Nonlocal", parent: NodeNG) -> nodes.Nonlocal:
  285. ...
  286. # Not used in Python 3.8+
  287. @overload
  288. def visit(self, node: "ast.Str", parent: NodeNG) -> nodes.Const:
  289. ...
  290. # Not used in Python 3.8+
  291. @overload
  292. def visit(self, node: "ast.Bytes", parent: NodeNG) -> nodes.Const:
  293. ...
  294. # Not used in Python 3.8+
  295. @overload
  296. def visit(self, node: "ast.Num", parent: NodeNG) -> nodes.Const:
  297. ...
  298. @overload
  299. def visit(self, node: "ast.Constant", parent: NodeNG) -> nodes.Const:
  300. ...
  301. @overload
  302. def visit(self, node: "ast.Pass", parent: NodeNG) -> nodes.Pass:
  303. ...
  304. @overload
  305. def visit(self, node: "ast.Raise", parent: NodeNG) -> nodes.Raise:
  306. ...
  307. @overload
  308. def visit(self, node: "ast.Return", parent: NodeNG) -> nodes.Return:
  309. ...
  310. @overload
  311. def visit(self, node: "ast.Set", parent: NodeNG) -> nodes.Set:
  312. ...
  313. @overload
  314. def visit(self, node: "ast.SetComp", parent: NodeNG) -> nodes.SetComp:
  315. ...
  316. @overload
  317. def visit(self, node: "ast.Slice", parent: nodes.Subscript) -> nodes.Slice:
  318. ...
  319. @overload
  320. def visit(self, node: "ast.Subscript", parent: NodeNG) -> nodes.Subscript:
  321. ...
  322. @overload
  323. def visit(self, node: "ast.Starred", parent: NodeNG) -> nodes.Starred:
  324. ...
  325. @overload
  326. def visit(
  327. self, node: "ast.Try", parent: NodeNG
  328. ) -> Union[nodes.TryExcept, nodes.TryFinally]:
  329. ...
  330. @overload
  331. def visit(self, node: "ast.Tuple", parent: NodeNG) -> nodes.Tuple:
  332. ...
  333. @overload
  334. def visit(self, node: "ast.UnaryOp", parent: NodeNG) -> nodes.UnaryOp:
  335. ...
  336. @overload
  337. def visit(self, node: "ast.While", parent: NodeNG) -> nodes.While:
  338. ...
  339. @overload
  340. def visit(self, node: "ast.With", parent: NodeNG) -> nodes.With:
  341. ...
  342. @overload
  343. def visit(self, node: "ast.Yield", parent: NodeNG) -> nodes.Yield:
  344. ...
  345. @overload
  346. def visit(self, node: "ast.YieldFrom", parent: NodeNG) -> nodes.YieldFrom:
  347. ...
  348. @overload
  349. def visit(self, node: "ast.Match", parent: NodeNG) -> nodes.Match:
  350. ...
  351. @overload
  352. def visit(self, node: "ast.match_case", parent: NodeNG) -> nodes.MatchCase:
  353. ...
  354. @overload
  355. def visit(self, node: "ast.MatchValue", parent: NodeNG) -> nodes.MatchValue:
  356. ...
  357. @overload
  358. def visit(
  359. self, node: "ast.MatchSingleton", parent: NodeNG
  360. ) -> nodes.MatchSingleton:
  361. ...
  362. @overload
  363. def visit(
  364. self, node: "ast.MatchSequence", parent: NodeNG
  365. ) -> nodes.MatchSequence:
  366. ...
  367. @overload
  368. def visit(self, node: "ast.MatchMapping", parent: NodeNG) -> nodes.MatchMapping:
  369. ...
  370. @overload
  371. def visit(self, node: "ast.MatchClass", parent: NodeNG) -> nodes.MatchClass:
  372. ...
  373. @overload
  374. def visit(self, node: "ast.MatchStar", parent: NodeNG) -> nodes.MatchStar:
  375. ...
  376. @overload
  377. def visit(self, node: "ast.MatchAs", parent: NodeNG) -> nodes.MatchAs:
  378. ...
  379. @overload
  380. def visit(self, node: "ast.MatchOr", parent: NodeNG) -> nodes.MatchOr:
  381. ...
  382. @overload
  383. def visit(self, node: "ast.pattern", parent: NodeNG) -> nodes.Pattern:
  384. ...
  385. @overload
  386. def visit(self, node: "ast.AST", parent: NodeNG) -> NodeNG:
  387. ...
  388. @overload
  389. def visit(self, node: None, parent: NodeNG) -> None:
  390. ...
  391. def visit(self, node: Optional["ast.AST"], parent: NodeNG) -> Optional[NodeNG]:
  392. if node is None:
  393. return None
  394. cls = node.__class__
  395. if cls in self._visit_meths:
  396. visit_method = self._visit_meths[cls]
  397. else:
  398. cls_name = cls.__name__
  399. visit_name = "visit_" + REDIRECT.get(cls_name, cls_name).lower()
  400. visit_method = getattr(self, visit_name)
  401. self._visit_meths[cls] = visit_method
  402. return visit_method(node, parent)
  403. else:
  404. @overload
  405. def visit(self, node: "ast.arg", parent: NodeNG) -> nodes.AssignName:
  406. ...
  407. @overload
  408. def visit(self, node: "ast.arguments", parent: NodeNG) -> nodes.Arguments:
  409. ...
  410. @overload
  411. def visit(self, node: "ast.Assert", parent: NodeNG) -> nodes.Assert:
  412. ...
  413. @overload
  414. def visit(
  415. self, node: "ast.AsyncFunctionDef", parent: NodeNG
  416. ) -> nodes.AsyncFunctionDef:
  417. ...
  418. @overload
  419. def visit(self, node: "ast.AsyncFor", parent: NodeNG) -> nodes.AsyncFor:
  420. ...
  421. @overload
  422. def visit(self, node: "ast.Await", parent: NodeNG) -> nodes.Await:
  423. ...
  424. @overload
  425. def visit(self, node: "ast.AsyncWith", parent: NodeNG) -> nodes.AsyncWith:
  426. ...
  427. @overload
  428. def visit(self, node: "ast.Assign", parent: NodeNG) -> nodes.Assign:
  429. ...
  430. @overload
  431. def visit(self, node: "ast.AnnAssign", parent: NodeNG) -> nodes.AnnAssign:
  432. ...
  433. @overload
  434. def visit(self, node: "ast.AugAssign", parent: NodeNG) -> nodes.AugAssign:
  435. ...
  436. @overload
  437. def visit(self, node: "ast.BinOp", parent: NodeNG) -> nodes.BinOp:
  438. ...
  439. @overload
  440. def visit(self, node: "ast.BoolOp", parent: NodeNG) -> nodes.BoolOp:
  441. ...
  442. @overload
  443. def visit(self, node: "ast.Break", parent: NodeNG) -> nodes.Break:
  444. ...
  445. @overload
  446. def visit(self, node: "ast.Call", parent: NodeNG) -> nodes.Call:
  447. ...
  448. @overload
  449. def visit(self, node: "ast.ClassDef", parent: NodeNG) -> nodes.ClassDef:
  450. ...
  451. @overload
  452. def visit(self, node: "ast.Continue", parent: NodeNG) -> nodes.Continue:
  453. ...
  454. @overload
  455. def visit(self, node: "ast.Compare", parent: NodeNG) -> nodes.Compare:
  456. ...
  457. @overload
  458. def visit(
  459. self, node: "ast.comprehension", parent: NodeNG
  460. ) -> nodes.Comprehension:
  461. ...
  462. @overload
  463. def visit(self, node: "ast.Delete", parent: NodeNG) -> nodes.Delete:
  464. ...
  465. @overload
  466. def visit(self, node: "ast.Dict", parent: NodeNG) -> nodes.Dict:
  467. ...
  468. @overload
  469. def visit(self, node: "ast.DictComp", parent: NodeNG) -> nodes.DictComp:
  470. ...
  471. @overload
  472. def visit(self, node: "ast.Expr", parent: NodeNG) -> nodes.Expr:
  473. ...
  474. # Not used in Python 3.8+
  475. @overload
  476. def visit(self, node: "ast.Ellipsis", parent: NodeNG) -> nodes.Const:
  477. ...
  478. @overload
  479. def visit(
  480. self, node: "ast.ExceptHandler", parent: NodeNG
  481. ) -> nodes.ExceptHandler:
  482. ...
  483. # Not used in Python 3.9+
  484. @overload
  485. def visit(self, node: "ast.ExtSlice", parent: nodes.Subscript) -> nodes.Tuple:
  486. ...
  487. @overload
  488. def visit(self, node: "ast.For", parent: NodeNG) -> nodes.For:
  489. ...
  490. @overload
  491. def visit(self, node: "ast.ImportFrom", parent: NodeNG) -> nodes.ImportFrom:
  492. ...
  493. @overload
  494. def visit(self, node: "ast.FunctionDef", parent: NodeNG) -> nodes.FunctionDef:
  495. ...
  496. @overload
  497. def visit(self, node: "ast.GeneratorExp", parent: NodeNG) -> nodes.GeneratorExp:
  498. ...
  499. @overload
  500. def visit(self, node: "ast.Attribute", parent: NodeNG) -> nodes.Attribute:
  501. ...
  502. @overload
  503. def visit(self, node: "ast.Global", parent: NodeNG) -> nodes.Global:
  504. ...
  505. @overload
  506. def visit(self, node: "ast.If", parent: NodeNG) -> nodes.If:
  507. ...
  508. @overload
  509. def visit(self, node: "ast.IfExp", parent: NodeNG) -> nodes.IfExp:
  510. ...
  511. @overload
  512. def visit(self, node: "ast.Import", parent: NodeNG) -> nodes.Import:
  513. ...
  514. @overload
  515. def visit(self, node: "ast.JoinedStr", parent: NodeNG) -> nodes.JoinedStr:
  516. ...
  517. @overload
  518. def visit(
  519. self, node: "ast.FormattedValue", parent: NodeNG
  520. ) -> nodes.FormattedValue:
  521. ...
  522. @overload
  523. def visit(self, node: "ast.NamedExpr", parent: NodeNG) -> nodes.NamedExpr:
  524. ...
  525. # Not used in Python 3.9+
  526. @overload
  527. def visit(self, node: "ast.Index", parent: nodes.Subscript) -> NodeNG:
  528. ...
  529. @overload
  530. def visit(self, node: "ast.keyword", parent: NodeNG) -> nodes.Keyword:
  531. ...
  532. @overload
  533. def visit(self, node: "ast.Lambda", parent: NodeNG) -> nodes.Lambda:
  534. ...
  535. @overload
  536. def visit(self, node: "ast.List", parent: NodeNG) -> nodes.List:
  537. ...
  538. @overload
  539. def visit(self, node: "ast.ListComp", parent: NodeNG) -> nodes.ListComp:
  540. ...
  541. @overload
  542. def visit(
  543. self, node: "ast.Name", parent: NodeNG
  544. ) -> Union[nodes.Name, nodes.Const, nodes.AssignName, nodes.DelName]:
  545. ...
  546. # Not used in Python 3.8+
  547. @overload
  548. def visit(self, node: "ast.NameConstant", parent: NodeNG) -> nodes.Const:
  549. ...
  550. @overload
  551. def visit(self, node: "ast.Nonlocal", parent: NodeNG) -> nodes.Nonlocal:
  552. ...
  553. # Not used in Python 3.8+
  554. @overload
  555. def visit(self, node: "ast.Str", parent: NodeNG) -> nodes.Const:
  556. ...
  557. # Not used in Python 3.8+
  558. @overload
  559. def visit(self, node: "ast.Bytes", parent: NodeNG) -> nodes.Const:
  560. ...
  561. # Not used in Python 3.8+
  562. @overload
  563. def visit(self, node: "ast.Num", parent: NodeNG) -> nodes.Const:
  564. ...
  565. @overload
  566. def visit(self, node: "ast.Constant", parent: NodeNG) -> nodes.Const:
  567. ...
  568. @overload
  569. def visit(self, node: "ast.Pass", parent: NodeNG) -> nodes.Pass:
  570. ...
  571. @overload
  572. def visit(self, node: "ast.Raise", parent: NodeNG) -> nodes.Raise:
  573. ...
  574. @overload
  575. def visit(self, node: "ast.Return", parent: NodeNG) -> nodes.Return:
  576. ...
  577. @overload
  578. def visit(self, node: "ast.Set", parent: NodeNG) -> nodes.Set:
  579. ...
  580. @overload
  581. def visit(self, node: "ast.SetComp", parent: NodeNG) -> nodes.SetComp:
  582. ...
  583. @overload
  584. def visit(self, node: "ast.Slice", parent: nodes.Subscript) -> nodes.Slice:
  585. ...
  586. @overload
  587. def visit(self, node: "ast.Subscript", parent: NodeNG) -> nodes.Subscript:
  588. ...
  589. @overload
  590. def visit(self, node: "ast.Starred", parent: NodeNG) -> nodes.Starred:
  591. ...
  592. @overload
  593. def visit(
  594. self, node: "ast.Try", parent: NodeNG
  595. ) -> Union[nodes.TryExcept, nodes.TryFinally]:
  596. ...
  597. @overload
  598. def visit(self, node: "ast.Tuple", parent: NodeNG) -> nodes.Tuple:
  599. ...
  600. @overload
  601. def visit(self, node: "ast.UnaryOp", parent: NodeNG) -> nodes.UnaryOp:
  602. ...
  603. @overload
  604. def visit(self, node: "ast.While", parent: NodeNG) -> nodes.While:
  605. ...
  606. @overload
  607. def visit(self, node: "ast.With", parent: NodeNG) -> nodes.With:
  608. ...
  609. @overload
  610. def visit(self, node: "ast.Yield", parent: NodeNG) -> nodes.Yield:
  611. ...
  612. @overload
  613. def visit(self, node: "ast.YieldFrom", parent: NodeNG) -> nodes.YieldFrom:
  614. ...
  615. @overload
  616. def visit(self, node: "ast.AST", parent: NodeNG) -> NodeNG:
  617. ...
  618. @overload
  619. def visit(self, node: None, parent: NodeNG) -> None:
  620. ...
  621. def visit(self, node: Optional["ast.AST"], parent: NodeNG) -> Optional[NodeNG]:
  622. if node is None:
  623. return None
  624. cls = node.__class__
  625. if cls in self._visit_meths:
  626. visit_method = self._visit_meths[cls]
  627. else:
  628. cls_name = cls.__name__
  629. visit_name = "visit_" + REDIRECT.get(cls_name, cls_name).lower()
  630. visit_method = getattr(self, visit_name)
  631. self._visit_meths[cls] = visit_method
  632. return visit_method(node, parent)
  633. def _save_assignment(self, node: Union[nodes.AssignName, nodes.DelName]) -> None:
  634. """save assignment situation since node.parent is not available yet"""
  635. if self._global_names and node.name in self._global_names[-1]:
  636. node.root().set_local(node.name, node)
  637. else:
  638. assert node.parent
  639. node.parent.set_local(node.name, node)
  640. def visit_arg(self, node: "ast.arg", parent: NodeNG) -> nodes.AssignName:
  641. """visit an arg node by returning a fresh AssName instance"""
  642. return self.visit_assignname(node, parent, node.arg)
  643. def visit_arguments(self, node: "ast.arguments", parent: NodeNG) -> nodes.Arguments:
  644. """visit an Arguments node by returning a fresh instance of it"""
  645. vararg: Optional[str] = None
  646. kwarg: Optional[str] = None
  647. newnode = nodes.Arguments(
  648. node.vararg.arg if node.vararg else None,
  649. node.kwarg.arg if node.kwarg else None,
  650. parent,
  651. )
  652. args = [self.visit(child, newnode) for child in node.args]
  653. defaults = [self.visit(child, newnode) for child in node.defaults]
  654. varargannotation: Optional[NodeNG] = None
  655. kwargannotation: Optional[NodeNG] = None
  656. posonlyargs: List[nodes.AssignName] = []
  657. if node.vararg:
  658. vararg = node.vararg.arg
  659. varargannotation = self.visit(node.vararg.annotation, newnode)
  660. if node.kwarg:
  661. kwarg = node.kwarg.arg
  662. kwargannotation = self.visit(node.kwarg.annotation, newnode)
  663. if PY38:
  664. # In Python 3.8 'end_lineno' and 'end_col_offset'
  665. # for 'kwonlyargs' don't include the annotation.
  666. for arg in node.kwonlyargs:
  667. if arg.annotation is not None:
  668. arg.end_lineno = arg.annotation.end_lineno
  669. arg.end_col_offset = arg.annotation.end_col_offset
  670. kwonlyargs = [self.visit(child, newnode) for child in node.kwonlyargs]
  671. kw_defaults = [self.visit(child, newnode) for child in node.kw_defaults]
  672. annotations = [self.visit(arg.annotation, newnode) for arg in node.args]
  673. kwonlyargs_annotations = [
  674. self.visit(arg.annotation, newnode) for arg in node.kwonlyargs
  675. ]
  676. posonlyargs_annotations: List[Optional[NodeNG]] = []
  677. if PY38_PLUS:
  678. posonlyargs = [self.visit(child, newnode) for child in node.posonlyargs]
  679. posonlyargs_annotations = [
  680. self.visit(arg.annotation, newnode) for arg in node.posonlyargs
  681. ]
  682. type_comment_args = [
  683. self.check_type_comment(child, parent=newnode) for child in node.args
  684. ]
  685. type_comment_kwonlyargs = [
  686. self.check_type_comment(child, parent=newnode) for child in node.kwonlyargs
  687. ]
  688. type_comment_posonlyargs: List[Optional[NodeNG]] = []
  689. if PY38_PLUS:
  690. type_comment_posonlyargs = [
  691. self.check_type_comment(child, parent=newnode)
  692. for child in node.posonlyargs
  693. ]
  694. newnode.postinit(
  695. args=args,
  696. defaults=defaults,
  697. kwonlyargs=kwonlyargs,
  698. posonlyargs=posonlyargs,
  699. kw_defaults=kw_defaults,
  700. annotations=annotations,
  701. kwonlyargs_annotations=kwonlyargs_annotations,
  702. posonlyargs_annotations=posonlyargs_annotations,
  703. varargannotation=varargannotation,
  704. kwargannotation=kwargannotation,
  705. type_comment_args=type_comment_args,
  706. type_comment_kwonlyargs=type_comment_kwonlyargs,
  707. type_comment_posonlyargs=type_comment_posonlyargs,
  708. )
  709. # save argument names in locals:
  710. assert newnode.parent
  711. if vararg:
  712. newnode.parent.set_local(vararg, newnode)
  713. if kwarg:
  714. newnode.parent.set_local(kwarg, newnode)
  715. return newnode
  716. def visit_assert(self, node: "ast.Assert", parent: NodeNG) -> nodes.Assert:
  717. """visit a Assert node by returning a fresh instance of it"""
  718. if sys.version_info >= (3, 8):
  719. newnode = nodes.Assert(
  720. lineno=node.lineno,
  721. col_offset=node.col_offset,
  722. end_lineno=node.end_lineno,
  723. end_col_offset=node.end_col_offset,
  724. parent=parent,
  725. )
  726. else:
  727. newnode = nodes.Assert(node.lineno, node.col_offset, parent)
  728. msg: Optional[NodeNG] = None
  729. if node.msg:
  730. msg = self.visit(node.msg, newnode)
  731. newnode.postinit(self.visit(node.test, newnode), msg)
  732. return newnode
  733. def check_type_comment(
  734. self,
  735. node: Union[
  736. "ast.Assign",
  737. "ast.arg",
  738. "ast.For",
  739. "ast.AsyncFor",
  740. "ast.With",
  741. "ast.AsyncWith",
  742. ],
  743. parent: Union[
  744. nodes.Assign,
  745. nodes.Arguments,
  746. nodes.For,
  747. nodes.AsyncFor,
  748. nodes.With,
  749. nodes.AsyncWith,
  750. ],
  751. ) -> Optional[NodeNG]:
  752. type_comment = getattr(node, "type_comment", None) # Added in Python 3.8
  753. if not type_comment:
  754. return None
  755. try:
  756. type_comment_ast = self._parser_module.parse(type_comment)
  757. except SyntaxError:
  758. # Invalid type comment, just skip it.
  759. return None
  760. type_object = self.visit(type_comment_ast.body[0], parent=parent)
  761. if not isinstance(type_object, nodes.Expr):
  762. return None
  763. return type_object.value
  764. def check_function_type_comment(
  765. self, node: Union["ast.FunctionDef", "ast.AsyncFunctionDef"], parent: NodeNG
  766. ) -> Optional[Tuple[Optional[NodeNG], List[NodeNG]]]:
  767. type_comment = getattr(node, "type_comment", None) # Added in Python 3.8
  768. if not type_comment:
  769. return None
  770. try:
  771. type_comment_ast = parse_function_type_comment(type_comment)
  772. except SyntaxError:
  773. # Invalid type comment, just skip it.
  774. return None
  775. if not type_comment_ast:
  776. return None
  777. returns: Optional[NodeNG] = None
  778. argtypes: List[NodeNG] = [
  779. self.visit(elem, parent) for elem in (type_comment_ast.argtypes or [])
  780. ]
  781. if type_comment_ast.returns:
  782. returns = self.visit(type_comment_ast.returns, parent)
  783. return returns, argtypes
  784. def visit_asyncfunctiondef(
  785. self, node: "ast.AsyncFunctionDef", parent: NodeNG
  786. ) -> nodes.AsyncFunctionDef:
  787. return self._visit_functiondef(nodes.AsyncFunctionDef, node, parent)
  788. def visit_asyncfor(self, node: "ast.AsyncFor", parent: NodeNG) -> nodes.AsyncFor:
  789. return self._visit_for(nodes.AsyncFor, node, parent)
  790. def visit_await(self, node: "ast.Await", parent: NodeNG) -> nodes.Await:
  791. if sys.version_info >= (3, 8):
  792. newnode = nodes.Await(
  793. lineno=node.lineno,
  794. col_offset=node.col_offset,
  795. end_lineno=node.end_lineno,
  796. end_col_offset=node.end_col_offset,
  797. parent=parent,
  798. )
  799. else:
  800. newnode = nodes.Await(node.lineno, node.col_offset, parent)
  801. newnode.postinit(value=self.visit(node.value, newnode))
  802. return newnode
  803. def visit_asyncwith(self, node: "ast.AsyncWith", parent: NodeNG) -> nodes.AsyncWith:
  804. return self._visit_with(nodes.AsyncWith, node, parent)
  805. def visit_assign(self, node: "ast.Assign", parent: NodeNG) -> nodes.Assign:
  806. """visit a Assign node by returning a fresh instance of it"""
  807. if sys.version_info >= (3, 8):
  808. newnode = nodes.Assign(
  809. lineno=node.lineno,
  810. col_offset=node.col_offset,
  811. end_lineno=node.end_lineno,
  812. end_col_offset=node.end_col_offset,
  813. parent=parent,
  814. )
  815. else:
  816. newnode = nodes.Assign(node.lineno, node.col_offset, parent)
  817. type_annotation = self.check_type_comment(node, parent=newnode)
  818. newnode.postinit(
  819. targets=[self.visit(child, newnode) for child in node.targets],
  820. value=self.visit(node.value, newnode),
  821. type_annotation=type_annotation,
  822. )
  823. return newnode
  824. def visit_annassign(self, node: "ast.AnnAssign", parent: NodeNG) -> nodes.AnnAssign:
  825. """visit an AnnAssign node by returning a fresh instance of it"""
  826. if sys.version_info >= (3, 8):
  827. newnode = nodes.AnnAssign(
  828. lineno=node.lineno,
  829. col_offset=node.col_offset,
  830. end_lineno=node.end_lineno,
  831. end_col_offset=node.end_col_offset,
  832. parent=parent,
  833. )
  834. else:
  835. newnode = nodes.AnnAssign(node.lineno, node.col_offset, parent)
  836. newnode.postinit(
  837. target=self.visit(node.target, newnode),
  838. annotation=self.visit(node.annotation, newnode),
  839. simple=node.simple,
  840. value=self.visit(node.value, newnode),
  841. )
  842. return newnode
  843. @overload
  844. def visit_assignname(
  845. self, node: "ast.AST", parent: NodeNG, node_name: str
  846. ) -> nodes.AssignName:
  847. ...
  848. @overload
  849. def visit_assignname(
  850. self, node: "ast.AST", parent: NodeNG, node_name: None
  851. ) -> None:
  852. ...
  853. def visit_assignname(
  854. self, node: "ast.AST", parent: NodeNG, node_name: Optional[str]
  855. ) -> Optional[nodes.AssignName]:
  856. """visit a node and return a AssignName node
  857. Note: Method not called by 'visit'
  858. """
  859. if node_name is None:
  860. return None
  861. if sys.version_info >= (3, 8):
  862. newnode = nodes.AssignName(
  863. name=node_name,
  864. lineno=node.lineno,
  865. col_offset=node.col_offset,
  866. end_lineno=node.end_lineno,
  867. end_col_offset=node.end_col_offset,
  868. parent=parent,
  869. )
  870. else:
  871. newnode = nodes.AssignName(
  872. node_name,
  873. node.lineno,
  874. node.col_offset,
  875. parent,
  876. )
  877. self._save_assignment(newnode)
  878. return newnode
  879. def visit_augassign(self, node: "ast.AugAssign", parent: NodeNG) -> nodes.AugAssign:
  880. """visit a AugAssign node by returning a fresh instance of it"""
  881. if sys.version_info >= (3, 8):
  882. newnode = nodes.AugAssign(
  883. op=self._parser_module.bin_op_classes[type(node.op)] + "=",
  884. lineno=node.lineno,
  885. col_offset=node.col_offset,
  886. end_lineno=node.end_lineno,
  887. end_col_offset=node.end_col_offset,
  888. parent=parent,
  889. )
  890. else:
  891. newnode = nodes.AugAssign(
  892. self._parser_module.bin_op_classes[type(node.op)] + "=",
  893. node.lineno,
  894. node.col_offset,
  895. parent,
  896. )
  897. newnode.postinit(
  898. self.visit(node.target, newnode), self.visit(node.value, newnode)
  899. )
  900. return newnode
  901. def visit_binop(self, node: "ast.BinOp", parent: NodeNG) -> nodes.BinOp:
  902. """visit a BinOp node by returning a fresh instance of it"""
  903. if sys.version_info >= (3, 8):
  904. newnode = nodes.BinOp(
  905. op=self._parser_module.bin_op_classes[type(node.op)],
  906. lineno=node.lineno,
  907. col_offset=node.col_offset,
  908. end_lineno=node.end_lineno,
  909. end_col_offset=node.end_col_offset,
  910. parent=parent,
  911. )
  912. else:
  913. newnode = nodes.BinOp(
  914. self._parser_module.bin_op_classes[type(node.op)],
  915. node.lineno,
  916. node.col_offset,
  917. parent,
  918. )
  919. newnode.postinit(
  920. self.visit(node.left, newnode), self.visit(node.right, newnode)
  921. )
  922. return newnode
  923. def visit_boolop(self, node: "ast.BoolOp", parent: NodeNG) -> nodes.BoolOp:
  924. """visit a BoolOp node by returning a fresh instance of it"""
  925. if sys.version_info >= (3, 8):
  926. newnode = nodes.BoolOp(
  927. op=self._parser_module.bool_op_classes[type(node.op)],
  928. lineno=node.lineno,
  929. col_offset=node.col_offset,
  930. end_lineno=node.end_lineno,
  931. end_col_offset=node.end_col_offset,
  932. parent=parent,
  933. )
  934. else:
  935. newnode = nodes.BoolOp(
  936. self._parser_module.bool_op_classes[type(node.op)],
  937. node.lineno,
  938. node.col_offset,
  939. parent,
  940. )
  941. newnode.postinit([self.visit(child, newnode) for child in node.values])
  942. return newnode
  943. def visit_break(self, node: "ast.Break", parent: NodeNG) -> nodes.Break:
  944. """visit a Break node by returning a fresh instance of it"""
  945. if sys.version_info >= (3, 8):
  946. return nodes.Break(
  947. lineno=node.lineno,
  948. col_offset=node.col_offset,
  949. end_lineno=node.end_lineno,
  950. end_col_offset=node.end_col_offset,
  951. parent=parent,
  952. )
  953. return nodes.Break(node.lineno, node.col_offset, parent)
  954. def visit_call(self, node: "ast.Call", parent: NodeNG) -> nodes.Call:
  955. """visit a CallFunc node by returning a fresh instance of it"""
  956. if sys.version_info >= (3, 8):
  957. newnode = nodes.Call(
  958. lineno=node.lineno,
  959. col_offset=node.col_offset,
  960. end_lineno=node.end_lineno,
  961. end_col_offset=node.end_col_offset,
  962. parent=parent,
  963. )
  964. else:
  965. newnode = nodes.Call(node.lineno, node.col_offset, parent)
  966. newnode.postinit(
  967. func=self.visit(node.func, newnode),
  968. args=[self.visit(child, newnode) for child in node.args],
  969. keywords=[self.visit(child, newnode) for child in node.keywords],
  970. )
  971. return newnode
  972. def visit_classdef(
  973. self, node: "ast.ClassDef", parent: NodeNG, newstyle: bool = True
  974. ) -> nodes.ClassDef:
  975. """visit a ClassDef node to become astroid"""
  976. node, doc = self._get_doc(node)
  977. if sys.version_info >= (3, 8):
  978. newnode = nodes.ClassDef(
  979. name=node.name,
  980. doc=doc,
  981. lineno=node.lineno,
  982. col_offset=node.col_offset,
  983. end_lineno=node.end_lineno,
  984. end_col_offset=node.end_col_offset,
  985. parent=parent,
  986. )
  987. else:
  988. newnode = nodes.ClassDef(
  989. node.name, doc, node.lineno, node.col_offset, parent
  990. )
  991. metaclass = None
  992. for keyword in node.keywords:
  993. if keyword.arg == "metaclass":
  994. metaclass = self.visit(keyword, newnode).value
  995. break
  996. decorators = self.visit_decorators(node, newnode)
  997. newnode.postinit(
  998. [self.visit(child, newnode) for child in node.bases],
  999. [self.visit(child, newnode) for child in node.body],
  1000. decorators,
  1001. newstyle,
  1002. metaclass,
  1003. [
  1004. self.visit(kwd, newnode)
  1005. for kwd in node.keywords
  1006. if kwd.arg != "metaclass"
  1007. ],
  1008. )
  1009. return newnode
  1010. def visit_continue(self, node: "ast.Continue", parent: NodeNG) -> nodes.Continue:
  1011. """visit a Continue node by returning a fresh instance of it"""
  1012. if sys.version_info >= (3, 8):
  1013. return nodes.Continue(
  1014. lineno=node.lineno,
  1015. col_offset=node.col_offset,
  1016. end_lineno=node.end_lineno,
  1017. end_col_offset=node.end_col_offset,
  1018. parent=parent,
  1019. )
  1020. return nodes.Continue(node.lineno, node.col_offset, parent)
  1021. def visit_compare(self, node: "ast.Compare", parent: NodeNG) -> nodes.Compare:
  1022. """visit a Compare node by returning a fresh instance of it"""
  1023. if sys.version_info >= (3, 8):
  1024. newnode = nodes.Compare(
  1025. lineno=node.lineno,
  1026. col_offset=node.col_offset,
  1027. end_lineno=node.end_lineno,
  1028. end_col_offset=node.end_col_offset,
  1029. parent=parent,
  1030. )
  1031. else:
  1032. newnode = nodes.Compare(node.lineno, node.col_offset, parent)
  1033. newnode.postinit(
  1034. self.visit(node.left, newnode),
  1035. [
  1036. (
  1037. self._parser_module.cmp_op_classes[op.__class__],
  1038. self.visit(expr, newnode),
  1039. )
  1040. for (op, expr) in zip(node.ops, node.comparators)
  1041. ],
  1042. )
  1043. return newnode
  1044. def visit_comprehension(
  1045. self, node: "ast.comprehension", parent: NodeNG
  1046. ) -> nodes.Comprehension:
  1047. """visit a Comprehension node by returning a fresh instance of it"""
  1048. newnode = nodes.Comprehension(parent)
  1049. newnode.postinit(
  1050. self.visit(node.target, newnode),
  1051. self.visit(node.iter, newnode),
  1052. [self.visit(child, newnode) for child in node.ifs],
  1053. bool(node.is_async),
  1054. )
  1055. return newnode
  1056. def visit_decorators(
  1057. self,
  1058. node: Union["ast.ClassDef", "ast.FunctionDef", "ast.AsyncFunctionDef"],
  1059. parent: NodeNG,
  1060. ) -> Optional[nodes.Decorators]:
  1061. """visit a Decorators node by returning a fresh instance of it
  1062. Note: Method not called by 'visit'
  1063. """
  1064. if not node.decorator_list:
  1065. return None
  1066. # /!\ node is actually an _ast.FunctionDef node while
  1067. # parent is an astroid.nodes.FunctionDef node
  1068. if sys.version_info >= (3, 8):
  1069. # Set the line number of the first decorator for Python 3.8+.
  1070. lineno = node.decorator_list[0].lineno
  1071. end_lineno = node.decorator_list[-1].end_lineno
  1072. end_col_offset = node.decorator_list[-1].end_col_offset
  1073. else:
  1074. lineno = node.lineno
  1075. end_lineno = None
  1076. end_col_offset = None
  1077. newnode = nodes.Decorators(
  1078. lineno=lineno,
  1079. col_offset=node.col_offset,
  1080. end_lineno=end_lineno,
  1081. end_col_offset=end_col_offset,
  1082. parent=parent,
  1083. )
  1084. newnode.postinit([self.visit(child, newnode) for child in node.decorator_list])
  1085. return newnode
  1086. def visit_delete(self, node: "ast.Delete", parent: NodeNG) -> nodes.Delete:
  1087. """visit a Delete node by returning a fresh instance of it"""
  1088. if sys.version_info >= (3, 8):
  1089. newnode = nodes.Delete(
  1090. lineno=node.lineno,
  1091. col_offset=node.col_offset,
  1092. end_lineno=node.end_lineno,
  1093. end_col_offset=node.end_col_offset,
  1094. parent=parent,
  1095. )
  1096. else:
  1097. newnode = nodes.Delete(node.lineno, node.col_offset, parent)
  1098. newnode.postinit([self.visit(child, newnode) for child in node.targets])
  1099. return newnode
  1100. def _visit_dict_items(
  1101. self, node: "ast.Dict", parent: NodeNG, newnode: nodes.Dict
  1102. ) -> Generator[Tuple[NodeNG, NodeNG], None, None]:
  1103. for key, value in zip(node.keys, node.values):
  1104. rebuilt_key: NodeNG
  1105. rebuilt_value = self.visit(value, newnode)
  1106. if not key:
  1107. # Extended unpacking
  1108. if sys.version_info >= (3, 8):
  1109. rebuilt_key = nodes.DictUnpack(
  1110. lineno=rebuilt_value.lineno,
  1111. col_offset=rebuilt_value.col_offset,
  1112. end_lineno=rebuilt_value.end_lineno,
  1113. end_col_offset=rebuilt_value.end_col_offset,
  1114. parent=parent,
  1115. )
  1116. else:
  1117. rebuilt_key = nodes.DictUnpack(
  1118. rebuilt_value.lineno, rebuilt_value.col_offset, parent
  1119. )
  1120. else:
  1121. rebuilt_key = self.visit(key, newnode)
  1122. yield rebuilt_key, rebuilt_value
  1123. def visit_dict(self, node: "ast.Dict", parent: NodeNG) -> nodes.Dict:
  1124. """visit a Dict node by returning a fresh instance of it"""
  1125. if sys.version_info >= (3, 8):
  1126. newnode = nodes.Dict(
  1127. lineno=node.lineno,
  1128. col_offset=node.col_offset,
  1129. end_lineno=node.end_lineno,
  1130. end_col_offset=node.end_col_offset,
  1131. parent=parent,
  1132. )
  1133. else:
  1134. newnode = nodes.Dict(node.lineno, node.col_offset, parent)
  1135. items = list(self._visit_dict_items(node, parent, newnode))
  1136. newnode.postinit(items)
  1137. return newnode
  1138. def visit_dictcomp(self, node: "ast.DictComp", parent: NodeNG) -> nodes.DictComp:
  1139. """visit a DictComp node by returning a fresh instance of it"""
  1140. if sys.version_info >= (3, 8):
  1141. newnode = nodes.DictComp(
  1142. lineno=node.lineno,
  1143. col_offset=node.col_offset,
  1144. end_lineno=node.end_lineno,
  1145. end_col_offset=node.end_col_offset,
  1146. parent=parent,
  1147. )
  1148. else:
  1149. newnode = nodes.DictComp(node.lineno, node.col_offset, parent)
  1150. newnode.postinit(
  1151. self.visit(node.key, newnode),
  1152. self.visit(node.value, newnode),
  1153. [self.visit(child, newnode) for child in node.generators],
  1154. )
  1155. return newnode
  1156. def visit_expr(self, node: "ast.Expr", parent: NodeNG) -> nodes.Expr:
  1157. """visit a Expr node by returning a fresh instance of it"""
  1158. if sys.version_info >= (3, 8):
  1159. newnode = nodes.Expr(
  1160. lineno=node.lineno,
  1161. col_offset=node.col_offset,
  1162. end_lineno=node.end_lineno,
  1163. end_col_offset=node.end_col_offset,
  1164. parent=parent,
  1165. )
  1166. else:
  1167. newnode = nodes.Expr(node.lineno, node.col_offset, parent)
  1168. newnode.postinit(self.visit(node.value, newnode))
  1169. return newnode
  1170. # Not used in Python 3.8+.
  1171. def visit_ellipsis(self, node: "ast.Ellipsis", parent: NodeNG) -> nodes.Const:
  1172. """visit an Ellipsis node by returning a fresh instance of Const"""
  1173. return nodes.Const(
  1174. value=Ellipsis,
  1175. lineno=node.lineno,
  1176. col_offset=node.col_offset,
  1177. parent=parent,
  1178. )
  1179. def visit_excepthandler(
  1180. self, node: "ast.ExceptHandler", parent: NodeNG
  1181. ) -> nodes.ExceptHandler:
  1182. """visit an ExceptHandler node by returning a fresh instance of it"""
  1183. if sys.version_info >= (3, 8):
  1184. newnode = nodes.ExceptHandler(
  1185. lineno=node.lineno,
  1186. col_offset=node.col_offset,
  1187. end_lineno=node.end_lineno,
  1188. end_col_offset=node.end_col_offset,
  1189. parent=parent,
  1190. )
  1191. else:
  1192. newnode = nodes.ExceptHandler(node.lineno, node.col_offset, parent)
  1193. newnode.postinit(
  1194. self.visit(node.type, newnode),
  1195. self.visit_assignname(node, newnode, node.name),
  1196. [self.visit(child, newnode) for child in node.body],
  1197. )
  1198. return newnode
  1199. # Not used in Python 3.9+.
  1200. def visit_extslice(
  1201. self, node: "ast.ExtSlice", parent: nodes.Subscript
  1202. ) -> nodes.Tuple:
  1203. """visit an ExtSlice node by returning a fresh instance of Tuple"""
  1204. # ExtSlice doesn't have lineno or col_offset information
  1205. newnode = nodes.Tuple(ctx=Context.Load, parent=parent)
  1206. newnode.postinit([self.visit(dim, newnode) for dim in node.dims]) # type: ignore[attr-defined]
  1207. return newnode
  1208. @overload
  1209. def _visit_for(
  1210. self, cls: Type[nodes.For], node: "ast.For", parent: NodeNG
  1211. ) -> nodes.For:
  1212. ...
  1213. @overload
  1214. def _visit_for(
  1215. self, cls: Type[nodes.AsyncFor], node: "ast.AsyncFor", parent: NodeNG
  1216. ) -> nodes.AsyncFor:
  1217. ...
  1218. def _visit_for(
  1219. self, cls: Type[T_For], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG
  1220. ) -> T_For:
  1221. """visit a For node by returning a fresh instance of it"""
  1222. if sys.version_info >= (3, 8):
  1223. newnode = cls(
  1224. lineno=node.lineno,
  1225. col_offset=node.col_offset,
  1226. end_lineno=node.end_lineno,
  1227. end_col_offset=node.end_col_offset,
  1228. parent=parent,
  1229. )
  1230. else:
  1231. newnode = cls(node.lineno, node.col_offset, parent)
  1232. type_annotation = self.check_type_comment(node, parent=newnode)
  1233. newnode.postinit(
  1234. target=self.visit(node.target, newnode),
  1235. iter=self.visit(node.iter, newnode),
  1236. body=[self.visit(child, newnode) for child in node.body],
  1237. orelse=[self.visit(child, newnode) for child in node.orelse],
  1238. type_annotation=type_annotation,
  1239. )
  1240. return newnode
  1241. def visit_for(self, node: "ast.For", parent: NodeNG) -> nodes.For:
  1242. return self._visit_for(nodes.For, node, parent)
  1243. def visit_importfrom(
  1244. self, node: "ast.ImportFrom", parent: NodeNG
  1245. ) -> nodes.ImportFrom:
  1246. """visit an ImportFrom node by returning a fresh instance of it"""
  1247. names = [(alias.name, alias.asname) for alias in node.names]
  1248. if sys.version_info >= (3, 8):
  1249. newnode = nodes.ImportFrom(
  1250. fromname=node.module or "",
  1251. names=names,
  1252. level=node.level or None,
  1253. lineno=node.lineno,
  1254. col_offset=node.col_offset,
  1255. end_lineno=node.end_lineno,
  1256. end_col_offset=node.end_col_offset,
  1257. parent=parent,
  1258. )
  1259. else:
  1260. newnode = nodes.ImportFrom(
  1261. node.module or "",
  1262. names,
  1263. node.level or None,
  1264. node.lineno,
  1265. node.col_offset,
  1266. parent,
  1267. )
  1268. # store From names to add them to locals after building
  1269. self._import_from_nodes.append(newnode)
  1270. return newnode
  1271. @overload
  1272. def _visit_functiondef(
  1273. self, cls: Type[nodes.FunctionDef], node: "ast.FunctionDef", parent: NodeNG
  1274. ) -> nodes.FunctionDef:
  1275. ...
  1276. @overload
  1277. def _visit_functiondef(
  1278. self,
  1279. cls: Type[nodes.AsyncFunctionDef],
  1280. node: "ast.AsyncFunctionDef",
  1281. parent: NodeNG,
  1282. ) -> nodes.AsyncFunctionDef:
  1283. ...
  1284. def _visit_functiondef(
  1285. self,
  1286. cls: Type[T_Function],
  1287. node: Union["ast.FunctionDef", "ast.AsyncFunctionDef"],
  1288. parent: NodeNG,
  1289. ) -> T_Function:
  1290. """visit an FunctionDef node to become astroid"""
  1291. self._global_names.append({})
  1292. node, doc = self._get_doc(node)
  1293. lineno = node.lineno
  1294. if PY38_PLUS and node.decorator_list:
  1295. # Python 3.8 sets the line number of a decorated function
  1296. # to be the actual line number of the function, but the
  1297. # previous versions expected the decorator's line number instead.
  1298. # We reset the function's line number to that of the
  1299. # first decorator to maintain backward compatibility.
  1300. # It's not ideal but this discrepancy was baked into
  1301. # the framework for *years*.
  1302. lineno = node.decorator_list[0].lineno
  1303. if sys.version_info >= (3, 8):
  1304. newnode = cls(
  1305. name=node.name,
  1306. doc=doc,
  1307. lineno=lineno,
  1308. col_offset=node.col_offset,
  1309. end_lineno=node.end_lineno,
  1310. end_col_offset=node.end_col_offset,
  1311. parent=parent,
  1312. )
  1313. else:
  1314. newnode = cls(node.name, doc, lineno, node.col_offset, parent)
  1315. decorators = self.visit_decorators(node, newnode)
  1316. returns: Optional[NodeNG]
  1317. if node.returns:
  1318. returns = self.visit(node.returns, newnode)
  1319. else:
  1320. returns = None
  1321. type_comment_args = type_comment_returns = None
  1322. type_comment_annotation = self.check_function_type_comment(node, newnode)
  1323. if type_comment_annotation:
  1324. type_comment_returns, type_comment_args = type_comment_annotation
  1325. newnode.postinit(
  1326. args=self.visit(node.args, newnode),
  1327. body=[self.visit(child, newnode) for child in node.body],
  1328. decorators=decorators,
  1329. returns=returns,
  1330. type_comment_returns=type_comment_returns,
  1331. type_comment_args=type_comment_args,
  1332. )
  1333. self._global_names.pop()
  1334. return newnode
  1335. def visit_functiondef(
  1336. self, node: "ast.FunctionDef", parent: NodeNG
  1337. ) -> nodes.FunctionDef:
  1338. return self._visit_functiondef(nodes.FunctionDef, node, parent)
  1339. def visit_generatorexp(
  1340. self, node: "ast.GeneratorExp", parent: NodeNG
  1341. ) -> nodes.GeneratorExp:
  1342. """visit a GeneratorExp node by returning a fresh instance of it"""
  1343. if sys.version_info >= (3, 8):
  1344. newnode = nodes.GeneratorExp(
  1345. lineno=node.lineno,
  1346. col_offset=node.col_offset,
  1347. end_lineno=node.end_lineno,
  1348. end_col_offset=node.end_col_offset,
  1349. parent=parent,
  1350. )
  1351. else:
  1352. newnode = nodes.GeneratorExp(node.lineno, node.col_offset, parent)
  1353. newnode.postinit(
  1354. self.visit(node.elt, newnode),
  1355. [self.visit(child, newnode) for child in node.generators],
  1356. )
  1357. return newnode
  1358. def visit_attribute(
  1359. self, node: "ast.Attribute", parent: NodeNG
  1360. ) -> Union[nodes.Attribute, nodes.AssignAttr, nodes.DelAttr]:
  1361. """visit an Attribute node by returning a fresh instance of it"""
  1362. context = self._get_context(node)
  1363. newnode: Union[nodes.Attribute, nodes.AssignAttr, nodes.DelAttr]
  1364. if context == Context.Del:
  1365. # FIXME : maybe we should reintroduce and visit_delattr ?
  1366. # for instance, deactivating assign_ctx
  1367. if sys.version_info >= (3, 8):
  1368. newnode = nodes.DelAttr(
  1369. attrname=node.attr,
  1370. lineno=node.lineno,
  1371. col_offset=node.col_offset,
  1372. end_lineno=node.end_lineno,
  1373. end_col_offset=node.end_col_offset,
  1374. parent=parent,
  1375. )
  1376. else:
  1377. newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent)
  1378. elif context == Context.Store:
  1379. if sys.version_info >= (3, 8):
  1380. newnode = nodes.AssignAttr(
  1381. attrname=node.attr,
  1382. lineno=node.lineno,
  1383. col_offset=node.col_offset,
  1384. end_lineno=node.end_lineno,
  1385. end_col_offset=node.end_col_offset,
  1386. parent=parent,
  1387. )
  1388. else:
  1389. newnode = nodes.AssignAttr(
  1390. node.attr, node.lineno, node.col_offset, parent
  1391. )
  1392. # Prohibit a local save if we are in an ExceptHandler.
  1393. if not isinstance(parent, nodes.ExceptHandler):
  1394. # mypy doesn't recognize that newnode has to be AssignAttr because it doesn't support ParamSpec
  1395. # See https://github.com/python/mypy/issues/8645
  1396. self._delayed_assattr.append(newnode) # type: ignore[arg-type]
  1397. else:
  1398. # pylint: disable-next=else-if-used
  1399. # Preserve symmetry with other cases
  1400. if sys.version_info >= (3, 8):
  1401. newnode = nodes.Attribute(
  1402. attrname=node.attr,
  1403. lineno=node.lineno,
  1404. col_offset=node.col_offset,
  1405. end_lineno=node.end_lineno,
  1406. end_col_offset=node.end_col_offset,
  1407. parent=parent,
  1408. )
  1409. else:
  1410. newnode = nodes.Attribute(
  1411. node.attr, node.lineno, node.col_offset, parent
  1412. )
  1413. newnode.postinit(self.visit(node.value, newnode))
  1414. return newnode
  1415. def visit_global(self, node: "ast.Global", parent: NodeNG) -> nodes.Global:
  1416. """visit a Global node to become astroid"""
  1417. if sys.version_info >= (3, 8):
  1418. newnode = nodes.Global(
  1419. names=node.names,
  1420. lineno=node.lineno,
  1421. col_offset=node.col_offset,
  1422. end_lineno=node.end_lineno,
  1423. end_col_offset=node.end_col_offset,
  1424. parent=parent,
  1425. )
  1426. else:
  1427. newnode = nodes.Global(
  1428. node.names,
  1429. node.lineno,
  1430. node.col_offset,
  1431. parent,
  1432. )
  1433. if self._global_names: # global at the module level, no effect
  1434. for name in node.names:
  1435. self._global_names[-1].setdefault(name, []).append(newnode)
  1436. return newnode
  1437. def visit_if(self, node: "ast.If", parent: NodeNG) -> nodes.If:
  1438. """visit an If node by returning a fresh instance of it"""
  1439. if sys.version_info >= (3, 8):
  1440. newnode = nodes.If(
  1441. lineno=node.lineno,
  1442. col_offset=node.col_offset,
  1443. end_lineno=node.end_lineno,
  1444. end_col_offset=node.end_col_offset,
  1445. parent=parent,
  1446. )
  1447. else:
  1448. newnode = nodes.If(node.lineno, node.col_offset, parent)
  1449. newnode.postinit(
  1450. self.visit(node.test, newnode),
  1451. [self.visit(child, newnode) for child in node.body],
  1452. [self.visit(child, newnode) for child in node.orelse],
  1453. )
  1454. return newnode
  1455. def visit_ifexp(self, node: "ast.IfExp", parent: NodeNG) -> nodes.IfExp:
  1456. """visit a IfExp node by returning a fresh instance of it"""
  1457. if sys.version_info >= (3, 8):
  1458. newnode = nodes.IfExp(
  1459. lineno=node.lineno,
  1460. col_offset=node.col_offset,
  1461. end_lineno=node.end_lineno,
  1462. end_col_offset=node.end_col_offset,
  1463. parent=parent,
  1464. )
  1465. else:
  1466. newnode = nodes.IfExp(node.lineno, node.col_offset, parent)
  1467. newnode.postinit(
  1468. self.visit(node.test, newnode),
  1469. self.visit(node.body, newnode),
  1470. self.visit(node.orelse, newnode),
  1471. )
  1472. return newnode
  1473. def visit_import(self, node: "ast.Import", parent: NodeNG) -> nodes.Import:
  1474. """visit a Import node by returning a fresh instance of it"""
  1475. names = [(alias.name, alias.asname) for alias in node.names]
  1476. if sys.version_info >= (3, 8):
  1477. newnode = nodes.Import(
  1478. names=names,
  1479. lineno=node.lineno,
  1480. col_offset=node.col_offset,
  1481. end_lineno=node.end_lineno,
  1482. end_col_offset=node.end_col_offset,
  1483. parent=parent,
  1484. )
  1485. else:
  1486. newnode = nodes.Import(
  1487. names,
  1488. node.lineno,
  1489. node.col_offset,
  1490. parent,
  1491. )
  1492. # save import names in parent's locals:
  1493. for (name, asname) in newnode.names:
  1494. name = asname or name
  1495. parent.set_local(name.split(".")[0], newnode)
  1496. return newnode
  1497. def visit_joinedstr(self, node: "ast.JoinedStr", parent: NodeNG) -> nodes.JoinedStr:
  1498. if sys.version_info >= (3, 8):
  1499. newnode = nodes.JoinedStr(
  1500. lineno=node.lineno,
  1501. col_offset=node.col_offset,
  1502. end_lineno=node.end_lineno,
  1503. end_col_offset=node.end_col_offset,
  1504. parent=parent,
  1505. )
  1506. else:
  1507. newnode = nodes.JoinedStr(node.lineno, node.col_offset, parent)
  1508. newnode.postinit([self.visit(child, newnode) for child in node.values])
  1509. return newnode
  1510. def visit_formattedvalue(
  1511. self, node: "ast.FormattedValue", parent: NodeNG
  1512. ) -> nodes.FormattedValue:
  1513. if sys.version_info >= (3, 8):
  1514. newnode = nodes.FormattedValue(
  1515. lineno=node.lineno,
  1516. col_offset=node.col_offset,
  1517. end_lineno=node.end_lineno,
  1518. end_col_offset=node.end_col_offset,
  1519. parent=parent,
  1520. )
  1521. else:
  1522. newnode = nodes.FormattedValue(node.lineno, node.col_offset, parent)
  1523. newnode.postinit(
  1524. self.visit(node.value, newnode),
  1525. node.conversion,
  1526. self.visit(node.format_spec, newnode),
  1527. )
  1528. return newnode
  1529. def visit_namedexpr(self, node: "ast.NamedExpr", parent: NodeNG) -> nodes.NamedExpr:
  1530. if sys.version_info >= (3, 8):
  1531. newnode = nodes.NamedExpr(
  1532. lineno=node.lineno,
  1533. col_offset=node.col_offset,
  1534. end_lineno=node.end_lineno,
  1535. end_col_offset=node.end_col_offset,
  1536. parent=parent,
  1537. )
  1538. else:
  1539. newnode = nodes.NamedExpr(node.lineno, node.col_offset, parent)
  1540. newnode.postinit(
  1541. self.visit(node.target, newnode), self.visit(node.value, newnode)
  1542. )
  1543. return newnode
  1544. # Not used in Python 3.9+.
  1545. def visit_index(self, node: "ast.Index", parent: nodes.Subscript) -> NodeNG:
  1546. """visit a Index node by returning a fresh instance of NodeNG"""
  1547. return self.visit(node.value, parent) # type: ignore[attr-defined]
  1548. def visit_keyword(self, node: "ast.keyword", parent: NodeNG) -> nodes.Keyword:
  1549. """visit a Keyword node by returning a fresh instance of it"""
  1550. if sys.version_info >= (3, 9):
  1551. newnode = nodes.Keyword(
  1552. arg=node.arg,
  1553. lineno=node.lineno,
  1554. col_offset=node.col_offset,
  1555. end_lineno=node.end_lineno,
  1556. end_col_offset=node.end_col_offset,
  1557. parent=parent,
  1558. )
  1559. else:
  1560. newnode = nodes.Keyword(node.arg, parent=parent)
  1561. newnode.postinit(self.visit(node.value, newnode))
  1562. return newnode
  1563. def visit_lambda(self, node: "ast.Lambda", parent: NodeNG) -> nodes.Lambda:
  1564. """visit a Lambda node by returning a fresh instance of it"""
  1565. if sys.version_info >= (3, 8):
  1566. newnode = nodes.Lambda(
  1567. lineno=node.lineno,
  1568. col_offset=node.col_offset,
  1569. end_lineno=node.end_lineno,
  1570. end_col_offset=node.end_col_offset,
  1571. parent=parent,
  1572. )
  1573. else:
  1574. newnode = nodes.Lambda(node.lineno, node.col_offset, parent)
  1575. newnode.postinit(self.visit(node.args, newnode), self.visit(node.body, newnode))
  1576. return newnode
  1577. def visit_list(self, node: "ast.List", parent: NodeNG) -> nodes.List:
  1578. """visit a List node by returning a fresh instance of it"""
  1579. context = self._get_context(node)
  1580. if sys.version_info >= (3, 8):
  1581. newnode = nodes.List(
  1582. ctx=context,
  1583. lineno=node.lineno,
  1584. col_offset=node.col_offset,
  1585. end_lineno=node.end_lineno,
  1586. end_col_offset=node.end_col_offset,
  1587. parent=parent,
  1588. )
  1589. else:
  1590. newnode = nodes.List(
  1591. ctx=context,
  1592. lineno=node.lineno,
  1593. col_offset=node.col_offset,
  1594. parent=parent,
  1595. )
  1596. newnode.postinit([self.visit(child, newnode) for child in node.elts])
  1597. return newnode
  1598. def visit_listcomp(self, node: "ast.ListComp", parent: NodeNG) -> nodes.ListComp:
  1599. """visit a ListComp node by returning a fresh instance of it"""
  1600. if sys.version_info >= (3, 8):
  1601. newnode = nodes.ListComp(
  1602. lineno=node.lineno,
  1603. col_offset=node.col_offset,
  1604. end_lineno=node.end_lineno,
  1605. end_col_offset=node.end_col_offset,
  1606. parent=parent,
  1607. )
  1608. else:
  1609. newnode = nodes.ListComp(node.lineno, node.col_offset, parent)
  1610. newnode.postinit(
  1611. self.visit(node.elt, newnode),
  1612. [self.visit(child, newnode) for child in node.generators],
  1613. )
  1614. return newnode
  1615. def visit_name(
  1616. self, node: "ast.Name", parent: NodeNG
  1617. ) -> Union[nodes.Name, nodes.AssignName, nodes.DelName]:
  1618. """visit a Name node by returning a fresh instance of it"""
  1619. context = self._get_context(node)
  1620. newnode: Union[nodes.Name, nodes.AssignName, nodes.DelName]
  1621. if context == Context.Del:
  1622. if sys.version_info >= (3, 8):
  1623. newnode = nodes.DelName(
  1624. name=node.id,
  1625. lineno=node.lineno,
  1626. col_offset=node.col_offset,
  1627. end_lineno=node.end_lineno,
  1628. end_col_offset=node.end_col_offset,
  1629. parent=parent,
  1630. )
  1631. else:
  1632. newnode = nodes.DelName(node.id, node.lineno, node.col_offset, parent)
  1633. elif context == Context.Store:
  1634. if sys.version_info >= (3, 8):
  1635. newnode = nodes.AssignName(
  1636. name=node.id,
  1637. lineno=node.lineno,
  1638. col_offset=node.col_offset,
  1639. end_lineno=node.end_lineno,
  1640. end_col_offset=node.end_col_offset,
  1641. parent=parent,
  1642. )
  1643. else:
  1644. newnode = nodes.AssignName(
  1645. node.id, node.lineno, node.col_offset, parent
  1646. )
  1647. else:
  1648. # pylint: disable-next=else-if-used
  1649. # Preserve symmetry with other cases
  1650. if sys.version_info >= (3, 8):
  1651. newnode = nodes.Name(
  1652. name=node.id,
  1653. lineno=node.lineno,
  1654. col_offset=node.col_offset,
  1655. end_lineno=node.end_lineno,
  1656. end_col_offset=node.end_col_offset,
  1657. parent=parent,
  1658. )
  1659. else:
  1660. newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent)
  1661. # XXX REMOVE me :
  1662. if context in (Context.Del, Context.Store): # 'Aug' ??
  1663. newnode = cast(Union[nodes.AssignName, nodes.DelName], newnode)
  1664. self._save_assignment(newnode)
  1665. return newnode
  1666. # Not used in Python 3.8+.
  1667. def visit_nameconstant(
  1668. self, node: "ast.NameConstant", parent: NodeNG
  1669. ) -> nodes.Const:
  1670. # For singleton values True / False / None
  1671. return nodes.Const(
  1672. node.value,
  1673. node.lineno,
  1674. node.col_offset,
  1675. parent,
  1676. )
  1677. def visit_nonlocal(self, node: "ast.Nonlocal", parent: NodeNG) -> nodes.Nonlocal:
  1678. """visit a Nonlocal node and return a new instance of it"""
  1679. if sys.version_info >= (3, 8):
  1680. return nodes.Nonlocal(
  1681. names=node.names,
  1682. lineno=node.lineno,
  1683. col_offset=node.col_offset,
  1684. end_lineno=node.end_lineno,
  1685. end_col_offset=node.end_col_offset,
  1686. parent=parent,
  1687. )
  1688. return nodes.Nonlocal(
  1689. node.names,
  1690. node.lineno,
  1691. node.col_offset,
  1692. parent,
  1693. )
  1694. def visit_constant(self, node: "ast.Constant", parent: NodeNG) -> nodes.Const:
  1695. """visit a Constant node by returning a fresh instance of Const"""
  1696. if sys.version_info >= (3, 8):
  1697. return nodes.Const(
  1698. value=node.value,
  1699. kind=node.kind,
  1700. lineno=node.lineno,
  1701. col_offset=node.col_offset,
  1702. end_lineno=node.end_lineno,
  1703. end_col_offset=node.end_col_offset,
  1704. parent=parent,
  1705. )
  1706. return nodes.Const(
  1707. node.value,
  1708. node.lineno,
  1709. node.col_offset,
  1710. parent,
  1711. node.kind,
  1712. )
  1713. # Not used in Python 3.8+.
  1714. def visit_str(
  1715. self, node: Union["ast.Str", "ast.Bytes"], parent: NodeNG
  1716. ) -> nodes.Const:
  1717. """visit a String/Bytes node by returning a fresh instance of Const"""
  1718. return nodes.Const(
  1719. node.s,
  1720. node.lineno,
  1721. node.col_offset,
  1722. parent,
  1723. )
  1724. # Not used in Python 3.8+
  1725. visit_bytes = visit_str
  1726. # Not used in Python 3.8+.
  1727. def visit_num(self, node: "ast.Num", parent: NodeNG) -> nodes.Const:
  1728. """visit a Num node by returning a fresh instance of Const"""
  1729. return nodes.Const(
  1730. node.n,
  1731. node.lineno,
  1732. node.col_offset,
  1733. parent,
  1734. )
  1735. def visit_pass(self, node: "ast.Pass", parent: NodeNG) -> nodes.Pass:
  1736. """visit a Pass node by returning a fresh instance of it"""
  1737. if sys.version_info >= (3, 8):
  1738. return nodes.Pass(
  1739. lineno=node.lineno,
  1740. col_offset=node.col_offset,
  1741. end_lineno=node.end_lineno,
  1742. end_col_offset=node.end_col_offset,
  1743. parent=parent,
  1744. )
  1745. return nodes.Pass(node.lineno, node.col_offset, parent)
  1746. def visit_raise(self, node: "ast.Raise", parent: NodeNG) -> nodes.Raise:
  1747. """visit a Raise node by returning a fresh instance of it"""
  1748. if sys.version_info >= (3, 8):
  1749. newnode = nodes.Raise(
  1750. lineno=node.lineno,
  1751. col_offset=node.col_offset,
  1752. end_lineno=node.end_lineno,
  1753. end_col_offset=node.end_col_offset,
  1754. parent=parent,
  1755. )
  1756. else:
  1757. newnode = nodes.Raise(node.lineno, node.col_offset, parent)
  1758. # no traceback; anyway it is not used in Pylint
  1759. newnode.postinit(
  1760. exc=self.visit(node.exc, newnode),
  1761. cause=self.visit(node.cause, newnode),
  1762. )
  1763. return newnode
  1764. def visit_return(self, node: "ast.Return", parent: NodeNG) -> nodes.Return:
  1765. """visit a Return node by returning a fresh instance of it"""
  1766. if sys.version_info >= (3, 8):
  1767. newnode = nodes.Return(
  1768. lineno=node.lineno,
  1769. col_offset=node.col_offset,
  1770. end_lineno=node.end_lineno,
  1771. end_col_offset=node.end_col_offset,
  1772. parent=parent,
  1773. )
  1774. else:
  1775. newnode = nodes.Return(node.lineno, node.col_offset, parent)
  1776. if node.value is not None:
  1777. newnode.postinit(self.visit(node.value, newnode))
  1778. return newnode
  1779. def visit_set(self, node: "ast.Set", parent: NodeNG) -> nodes.Set:
  1780. """visit a Set node by returning a fresh instance of it"""
  1781. if sys.version_info >= (3, 8):
  1782. newnode = nodes.Set(
  1783. lineno=node.lineno,
  1784. col_offset=node.col_offset,
  1785. end_lineno=node.end_lineno,
  1786. end_col_offset=node.end_col_offset,
  1787. parent=parent,
  1788. )
  1789. else:
  1790. newnode = nodes.Set(node.lineno, node.col_offset, parent)
  1791. newnode.postinit([self.visit(child, newnode) for child in node.elts])
  1792. return newnode
  1793. def visit_setcomp(self, node: "ast.SetComp", parent: NodeNG) -> nodes.SetComp:
  1794. """visit a SetComp node by returning a fresh instance of it"""
  1795. if sys.version_info >= (3, 8):
  1796. newnode = nodes.SetComp(
  1797. lineno=node.lineno,
  1798. col_offset=node.col_offset,
  1799. end_lineno=node.end_lineno,
  1800. end_col_offset=node.end_col_offset,
  1801. parent=parent,
  1802. )
  1803. else:
  1804. newnode = nodes.SetComp(node.lineno, node.col_offset, parent)
  1805. newnode.postinit(
  1806. self.visit(node.elt, newnode),
  1807. [self.visit(child, newnode) for child in node.generators],
  1808. )
  1809. return newnode
  1810. def visit_slice(self, node: "ast.Slice", parent: nodes.Subscript) -> nodes.Slice:
  1811. """visit a Slice node by returning a fresh instance of it"""
  1812. if sys.version_info >= (3, 9):
  1813. newnode = nodes.Slice(
  1814. lineno=node.lineno,
  1815. col_offset=node.col_offset,
  1816. end_lineno=node.end_lineno,
  1817. end_col_offset=node.end_col_offset,
  1818. parent=parent,
  1819. )
  1820. else:
  1821. newnode = nodes.Slice(parent=parent)
  1822. newnode.postinit(
  1823. lower=self.visit(node.lower, newnode),
  1824. upper=self.visit(node.upper, newnode),
  1825. step=self.visit(node.step, newnode),
  1826. )
  1827. return newnode
  1828. def visit_subscript(self, node: "ast.Subscript", parent: NodeNG) -> nodes.Subscript:
  1829. """visit a Subscript node by returning a fresh instance of it"""
  1830. context = self._get_context(node)
  1831. if sys.version_info >= (3, 8):
  1832. newnode = nodes.Subscript(
  1833. ctx=context,
  1834. lineno=node.lineno,
  1835. col_offset=node.col_offset,
  1836. end_lineno=node.end_lineno,
  1837. end_col_offset=node.end_col_offset,
  1838. parent=parent,
  1839. )
  1840. else:
  1841. newnode = nodes.Subscript(
  1842. ctx=context,
  1843. lineno=node.lineno,
  1844. col_offset=node.col_offset,
  1845. parent=parent,
  1846. )
  1847. newnode.postinit(
  1848. self.visit(node.value, newnode), self.visit(node.slice, newnode)
  1849. )
  1850. return newnode
  1851. def visit_starred(self, node: "ast.Starred", parent: NodeNG) -> nodes.Starred:
  1852. """visit a Starred node and return a new instance of it"""
  1853. context = self._get_context(node)
  1854. if sys.version_info >= (3, 8):
  1855. newnode = nodes.Starred(
  1856. ctx=context,
  1857. lineno=node.lineno,
  1858. col_offset=node.col_offset,
  1859. end_lineno=node.end_lineno,
  1860. end_col_offset=node.end_col_offset,
  1861. parent=parent,
  1862. )
  1863. else:
  1864. newnode = nodes.Starred(
  1865. ctx=context,
  1866. lineno=node.lineno,
  1867. col_offset=node.col_offset,
  1868. parent=parent,
  1869. )
  1870. newnode.postinit(self.visit(node.value, newnode))
  1871. return newnode
  1872. def visit_tryexcept(self, node: "ast.Try", parent: NodeNG) -> nodes.TryExcept:
  1873. """visit a TryExcept node by returning a fresh instance of it"""
  1874. if sys.version_info >= (3, 8):
  1875. newnode = nodes.TryExcept(
  1876. lineno=node.lineno,
  1877. col_offset=node.col_offset,
  1878. end_lineno=node.end_lineno,
  1879. end_col_offset=node.end_col_offset,
  1880. parent=parent,
  1881. )
  1882. else:
  1883. newnode = nodes.TryExcept(node.lineno, node.col_offset, parent)
  1884. newnode.postinit(
  1885. [self.visit(child, newnode) for child in node.body],
  1886. [self.visit(child, newnode) for child in node.handlers],
  1887. [self.visit(child, newnode) for child in node.orelse],
  1888. )
  1889. return newnode
  1890. def visit_try(
  1891. self, node: "ast.Try", parent: NodeNG
  1892. ) -> Union[nodes.TryExcept, nodes.TryFinally, None]:
  1893. # python 3.3 introduce a new Try node replacing
  1894. # TryFinally/TryExcept nodes
  1895. if node.finalbody:
  1896. if sys.version_info >= (3, 8):
  1897. newnode = nodes.TryFinally(
  1898. lineno=node.lineno,
  1899. col_offset=node.col_offset,
  1900. end_lineno=node.end_lineno,
  1901. end_col_offset=node.end_col_offset,
  1902. parent=parent,
  1903. )
  1904. else:
  1905. newnode = nodes.TryFinally(node.lineno, node.col_offset, parent)
  1906. body: Union[List[nodes.TryExcept], List[NodeNG]]
  1907. if node.handlers:
  1908. body = [self.visit_tryexcept(node, newnode)]
  1909. else:
  1910. body = [self.visit(child, newnode) for child in node.body]
  1911. newnode.postinit(body, [self.visit(n, newnode) for n in node.finalbody])
  1912. return newnode
  1913. if node.handlers:
  1914. return self.visit_tryexcept(node, parent)
  1915. return None
  1916. def visit_tryfinally(self, node: "ast.Try", parent: NodeNG) -> nodes.TryFinally:
  1917. """visit a TryFinally node by returning a fresh instance of it"""
  1918. if sys.version_info >= (3, 8):
  1919. newnode = nodes.TryFinally(
  1920. lineno=node.lineno,
  1921. col_offset=node.col_offset,
  1922. end_lineno=node.end_lineno,
  1923. end_col_offset=node.end_col_offset,
  1924. parent=parent,
  1925. )
  1926. else:
  1927. newnode = nodes.TryFinally(node.lineno, node.col_offset, parent)
  1928. newnode.postinit(
  1929. [self.visit(child, newnode) for child in node.body],
  1930. [self.visit(n, newnode) for n in node.finalbody],
  1931. )
  1932. return newnode
  1933. def visit_tuple(self, node: "ast.Tuple", parent: NodeNG) -> nodes.Tuple:
  1934. """visit a Tuple node by returning a fresh instance of it"""
  1935. context = self._get_context(node)
  1936. if sys.version_info >= (3, 8):
  1937. newnode = nodes.Tuple(
  1938. ctx=context,
  1939. lineno=node.lineno,
  1940. col_offset=node.col_offset,
  1941. end_lineno=node.end_lineno,
  1942. end_col_offset=node.end_col_offset,
  1943. parent=parent,
  1944. )
  1945. else:
  1946. newnode = nodes.Tuple(
  1947. ctx=context,
  1948. lineno=node.lineno,
  1949. col_offset=node.col_offset,
  1950. parent=parent,
  1951. )
  1952. newnode.postinit([self.visit(child, newnode) for child in node.elts])
  1953. return newnode
  1954. def visit_unaryop(self, node: "ast.UnaryOp", parent: NodeNG) -> nodes.UnaryOp:
  1955. """visit a UnaryOp node by returning a fresh instance of it"""
  1956. if sys.version_info >= (3, 8):
  1957. newnode = nodes.UnaryOp(
  1958. op=self._parser_module.unary_op_classes[node.op.__class__],
  1959. lineno=node.lineno,
  1960. col_offset=node.col_offset,
  1961. end_lineno=node.end_lineno,
  1962. end_col_offset=node.end_col_offset,
  1963. parent=parent,
  1964. )
  1965. else:
  1966. newnode = nodes.UnaryOp(
  1967. self._parser_module.unary_op_classes[node.op.__class__],
  1968. node.lineno,
  1969. node.col_offset,
  1970. parent,
  1971. )
  1972. newnode.postinit(self.visit(node.operand, newnode))
  1973. return newnode
  1974. def visit_while(self, node: "ast.While", parent: NodeNG) -> nodes.While:
  1975. """visit a While node by returning a fresh instance of it"""
  1976. if sys.version_info >= (3, 8):
  1977. newnode = nodes.While(
  1978. lineno=node.lineno,
  1979. col_offset=node.col_offset,
  1980. end_lineno=node.end_lineno,
  1981. end_col_offset=node.end_col_offset,
  1982. parent=parent,
  1983. )
  1984. else:
  1985. newnode = nodes.While(node.lineno, node.col_offset, parent)
  1986. newnode.postinit(
  1987. self.visit(node.test, newnode),
  1988. [self.visit(child, newnode) for child in node.body],
  1989. [self.visit(child, newnode) for child in node.orelse],
  1990. )
  1991. return newnode
  1992. @overload
  1993. def _visit_with(
  1994. self, cls: Type[nodes.With], node: "ast.With", parent: NodeNG
  1995. ) -> nodes.With:
  1996. ...
  1997. @overload
  1998. def _visit_with(
  1999. self, cls: Type[nodes.AsyncWith], node: "ast.AsyncWith", parent: NodeNG
  2000. ) -> nodes.AsyncWith:
  2001. ...
  2002. def _visit_with(
  2003. self,
  2004. cls: Type[T_With],
  2005. node: Union["ast.With", "ast.AsyncWith"],
  2006. parent: NodeNG,
  2007. ) -> T_With:
  2008. if sys.version_info >= (3, 8):
  2009. newnode = cls(
  2010. lineno=node.lineno,
  2011. col_offset=node.col_offset,
  2012. end_lineno=node.end_lineno,
  2013. end_col_offset=node.end_col_offset,
  2014. parent=parent,
  2015. )
  2016. else:
  2017. newnode = cls(node.lineno, node.col_offset, parent)
  2018. def visit_child(child: "ast.withitem") -> Tuple[NodeNG, Optional[NodeNG]]:
  2019. expr = self.visit(child.context_expr, newnode)
  2020. var = self.visit(child.optional_vars, newnode)
  2021. return expr, var
  2022. type_annotation = self.check_type_comment(node, parent=newnode)
  2023. newnode.postinit(
  2024. items=[visit_child(child) for child in node.items],
  2025. body=[self.visit(child, newnode) for child in node.body],
  2026. type_annotation=type_annotation,
  2027. )
  2028. return newnode
  2029. def visit_with(self, node: "ast.With", parent: NodeNG) -> NodeNG:
  2030. return self._visit_with(nodes.With, node, parent)
  2031. def visit_yield(self, node: "ast.Yield", parent: NodeNG) -> NodeNG:
  2032. """visit a Yield node by returning a fresh instance of it"""
  2033. if sys.version_info >= (3, 8):
  2034. newnode = nodes.Yield(
  2035. lineno=node.lineno,
  2036. col_offset=node.col_offset,
  2037. end_lineno=node.end_lineno,
  2038. end_col_offset=node.end_col_offset,
  2039. parent=parent,
  2040. )
  2041. else:
  2042. newnode = nodes.Yield(node.lineno, node.col_offset, parent)
  2043. if node.value is not None:
  2044. newnode.postinit(self.visit(node.value, newnode))
  2045. return newnode
  2046. def visit_yieldfrom(self, node: "ast.YieldFrom", parent: NodeNG) -> NodeNG:
  2047. if sys.version_info >= (3, 8):
  2048. newnode = nodes.YieldFrom(
  2049. lineno=node.lineno,
  2050. col_offset=node.col_offset,
  2051. end_lineno=node.end_lineno,
  2052. end_col_offset=node.end_col_offset,
  2053. parent=parent,
  2054. )
  2055. else:
  2056. newnode = nodes.YieldFrom(node.lineno, node.col_offset, parent)
  2057. if node.value is not None:
  2058. newnode.postinit(self.visit(node.value, newnode))
  2059. return newnode
  2060. if sys.version_info >= (3, 10):
  2061. def visit_match(self, node: "ast.Match", parent: NodeNG) -> nodes.Match:
  2062. newnode = nodes.Match(
  2063. lineno=node.lineno,
  2064. col_offset=node.col_offset,
  2065. end_lineno=node.end_lineno,
  2066. end_col_offset=node.end_col_offset,
  2067. parent=parent,
  2068. )
  2069. newnode.postinit(
  2070. subject=self.visit(node.subject, newnode),
  2071. cases=[self.visit(case, newnode) for case in node.cases],
  2072. )
  2073. return newnode
  2074. def visit_matchcase(
  2075. self, node: "ast.match_case", parent: NodeNG
  2076. ) -> nodes.MatchCase:
  2077. newnode = nodes.MatchCase(parent=parent)
  2078. newnode.postinit(
  2079. pattern=self.visit(node.pattern, newnode),
  2080. guard=self.visit(node.guard, newnode),
  2081. body=[self.visit(child, newnode) for child in node.body],
  2082. )
  2083. return newnode
  2084. def visit_matchvalue(
  2085. self, node: "ast.MatchValue", parent: NodeNG
  2086. ) -> nodes.MatchValue:
  2087. newnode = nodes.MatchValue(
  2088. lineno=node.lineno,
  2089. col_offset=node.col_offset,
  2090. end_lineno=node.end_lineno,
  2091. end_col_offset=node.end_col_offset,
  2092. parent=parent,
  2093. )
  2094. newnode.postinit(value=self.visit(node.value, newnode))
  2095. return newnode
  2096. def visit_matchsingleton(
  2097. self, node: "ast.MatchSingleton", parent: NodeNG
  2098. ) -> nodes.MatchSingleton:
  2099. return nodes.MatchSingleton(
  2100. value=node.value, # type: ignore[arg-type] # See https://github.com/python/mypy/pull/10389
  2101. lineno=node.lineno,
  2102. col_offset=node.col_offset,
  2103. end_lineno=node.end_lineno,
  2104. end_col_offset=node.end_col_offset,
  2105. parent=parent,
  2106. )
  2107. def visit_matchsequence(
  2108. self, node: "ast.MatchSequence", parent: NodeNG
  2109. ) -> nodes.MatchSequence:
  2110. newnode = nodes.MatchSequence(
  2111. lineno=node.lineno,
  2112. col_offset=node.col_offset,
  2113. end_lineno=node.end_lineno,
  2114. end_col_offset=node.end_col_offset,
  2115. parent=parent,
  2116. )
  2117. newnode.postinit(
  2118. patterns=[self.visit(pattern, newnode) for pattern in node.patterns]
  2119. )
  2120. return newnode
  2121. def visit_matchmapping(
  2122. self, node: "ast.MatchMapping", parent: NodeNG
  2123. ) -> nodes.MatchMapping:
  2124. newnode = nodes.MatchMapping(
  2125. lineno=node.lineno,
  2126. col_offset=node.col_offset,
  2127. end_lineno=node.end_lineno,
  2128. end_col_offset=node.end_col_offset,
  2129. parent=parent,
  2130. )
  2131. # Add AssignName node for 'node.name'
  2132. # https://bugs.python.org/issue43994
  2133. newnode.postinit(
  2134. keys=[self.visit(child, newnode) for child in node.keys],
  2135. patterns=[self.visit(pattern, newnode) for pattern in node.patterns],
  2136. rest=self.visit_assignname(node, newnode, node.rest),
  2137. )
  2138. return newnode
  2139. def visit_matchclass(
  2140. self, node: "ast.MatchClass", parent: NodeNG
  2141. ) -> nodes.MatchClass:
  2142. newnode = nodes.MatchClass(
  2143. lineno=node.lineno,
  2144. col_offset=node.col_offset,
  2145. end_lineno=node.end_lineno,
  2146. end_col_offset=node.end_col_offset,
  2147. parent=parent,
  2148. )
  2149. newnode.postinit(
  2150. cls=self.visit(node.cls, newnode),
  2151. patterns=[self.visit(pattern, newnode) for pattern in node.patterns],
  2152. kwd_attrs=node.kwd_attrs,
  2153. kwd_patterns=[
  2154. self.visit(pattern, newnode) for pattern in node.kwd_patterns
  2155. ],
  2156. )
  2157. return newnode
  2158. def visit_matchstar(
  2159. self, node: "ast.MatchStar", parent: NodeNG
  2160. ) -> nodes.MatchStar:
  2161. newnode = nodes.MatchStar(
  2162. lineno=node.lineno,
  2163. col_offset=node.col_offset,
  2164. end_lineno=node.end_lineno,
  2165. end_col_offset=node.end_col_offset,
  2166. parent=parent,
  2167. )
  2168. # Add AssignName node for 'node.name'
  2169. # https://bugs.python.org/issue43994
  2170. newnode.postinit(name=self.visit_assignname(node, newnode, node.name))
  2171. return newnode
  2172. def visit_matchas(self, node: "ast.MatchAs", parent: NodeNG) -> nodes.MatchAs:
  2173. newnode = nodes.MatchAs(
  2174. lineno=node.lineno,
  2175. col_offset=node.col_offset,
  2176. end_lineno=node.end_lineno,
  2177. end_col_offset=node.end_col_offset,
  2178. parent=parent,
  2179. )
  2180. # Add AssignName node for 'node.name'
  2181. # https://bugs.python.org/issue43994
  2182. newnode.postinit(
  2183. pattern=self.visit(node.pattern, newnode),
  2184. name=self.visit_assignname(node, newnode, node.name),
  2185. )
  2186. return newnode
  2187. def visit_matchor(self, node: "ast.MatchOr", parent: NodeNG) -> nodes.MatchOr:
  2188. newnode = nodes.MatchOr(
  2189. lineno=node.lineno,
  2190. col_offset=node.col_offset,
  2191. end_lineno=node.end_lineno,
  2192. end_col_offset=node.end_col_offset,
  2193. parent=parent,
  2194. )
  2195. newnode.postinit(
  2196. patterns=[self.visit(pattern, newnode) for pattern in node.patterns]
  2197. )
  2198. return newnode