nodes.py 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104
  1. """Abstract syntax tree node classes (i.e. parse tree)."""
  2. from __future__ import annotations
  3. import os
  4. from abc import abstractmethod
  5. from collections import defaultdict
  6. from enum import Enum, unique
  7. from typing import (
  8. TYPE_CHECKING,
  9. Any,
  10. Callable,
  11. Dict,
  12. Final,
  13. Iterator,
  14. List,
  15. Optional,
  16. Sequence,
  17. Tuple,
  18. TypeVar,
  19. Union,
  20. cast,
  21. )
  22. from typing_extensions import TypeAlias as _TypeAlias, TypeGuard
  23. from mypy_extensions import trait
  24. import mypy.strconv
  25. from mypy.options import Options
  26. from mypy.util import short_type
  27. from mypy.visitor import ExpressionVisitor, NodeVisitor, StatementVisitor
  28. if TYPE_CHECKING:
  29. from mypy.patterns import Pattern
  30. class Context:
  31. """Base type for objects that are valid as error message locations."""
  32. __slots__ = ("line", "column", "end_line", "end_column")
  33. def __init__(self, line: int = -1, column: int = -1) -> None:
  34. self.line = line
  35. self.column = column
  36. self.end_line: int | None = None
  37. self.end_column: int | None = None
  38. def set_line(
  39. self,
  40. target: Context | int,
  41. column: int | None = None,
  42. end_line: int | None = None,
  43. end_column: int | None = None,
  44. ) -> None:
  45. """If target is a node, pull line (and column) information
  46. into this node. If column is specified, this will override any column
  47. information coming from a node.
  48. """
  49. if isinstance(target, int):
  50. self.line = target
  51. else:
  52. self.line = target.line
  53. self.column = target.column
  54. self.end_line = target.end_line
  55. self.end_column = target.end_column
  56. if column is not None:
  57. self.column = column
  58. if end_line is not None:
  59. self.end_line = end_line
  60. if end_column is not None:
  61. self.end_column = end_column
  62. if TYPE_CHECKING:
  63. # break import cycle only needed for mypy
  64. import mypy.types
  65. T = TypeVar("T")
  66. JsonDict: _TypeAlias = Dict[str, Any]
  67. # Symbol table node kinds
  68. #
  69. # TODO rename to use more descriptive names
  70. LDEF: Final = 0
  71. GDEF: Final = 1
  72. MDEF: Final = 2
  73. # Placeholder for a name imported via 'from ... import'. Second phase of
  74. # semantic will replace this the actual imported reference. This is
  75. # needed so that we can detect whether a name has been imported during
  76. # XXX what?
  77. UNBOUND_IMPORTED: Final = 3
  78. # RevealExpr node kinds
  79. REVEAL_TYPE: Final = 0
  80. REVEAL_LOCALS: Final = 1
  81. LITERAL_YES: Final = 2
  82. LITERAL_TYPE: Final = 1
  83. LITERAL_NO: Final = 0
  84. node_kinds: Final = {LDEF: "Ldef", GDEF: "Gdef", MDEF: "Mdef", UNBOUND_IMPORTED: "UnboundImported"}
  85. inverse_node_kinds: Final = {_kind: _name for _name, _kind in node_kinds.items()}
  86. implicit_module_attrs: Final = {
  87. "__name__": "__builtins__.str",
  88. "__doc__": None, # depends on Python version, see semanal.py
  89. "__path__": None, # depends on if the module is a package
  90. "__file__": "__builtins__.str",
  91. "__package__": "__builtins__.str",
  92. "__annotations__": None, # dict[str, Any] bounded in add_implicit_module_attrs()
  93. }
  94. # These aliases exist because built-in class objects are not subscriptable.
  95. # For example `list[int]` fails at runtime. Instead List[int] should be used.
  96. type_aliases: Final = {
  97. "typing.List": "builtins.list",
  98. "typing.Dict": "builtins.dict",
  99. "typing.Set": "builtins.set",
  100. "typing.FrozenSet": "builtins.frozenset",
  101. "typing.ChainMap": "collections.ChainMap",
  102. "typing.Counter": "collections.Counter",
  103. "typing.DefaultDict": "collections.defaultdict",
  104. "typing.Deque": "collections.deque",
  105. "typing.OrderedDict": "collections.OrderedDict",
  106. # HACK: a lie in lieu of actual support for PEP 675
  107. "typing.LiteralString": "builtins.str",
  108. }
  109. # This keeps track of the oldest supported Python version where the corresponding
  110. # alias source is available.
  111. type_aliases_source_versions: Final = {
  112. "typing.List": (2, 7),
  113. "typing.Dict": (2, 7),
  114. "typing.Set": (2, 7),
  115. "typing.FrozenSet": (2, 7),
  116. "typing.ChainMap": (3, 3),
  117. "typing.Counter": (2, 7),
  118. "typing.DefaultDict": (2, 7),
  119. "typing.Deque": (2, 7),
  120. "typing.OrderedDict": (3, 7),
  121. "typing.LiteralString": (3, 11),
  122. }
  123. # This keeps track of aliases in `typing_extensions`, which we treat specially.
  124. typing_extensions_aliases: Final = {
  125. # See: https://github.com/python/mypy/issues/11528
  126. "typing_extensions.OrderedDict": "collections.OrderedDict",
  127. # HACK: a lie in lieu of actual support for PEP 675
  128. "typing_extensions.LiteralString": "builtins.str",
  129. }
  130. reverse_builtin_aliases: Final = {
  131. "builtins.list": "typing.List",
  132. "builtins.dict": "typing.Dict",
  133. "builtins.set": "typing.Set",
  134. "builtins.frozenset": "typing.FrozenSet",
  135. }
  136. _nongen_builtins: Final = {"builtins.tuple": "typing.Tuple", "builtins.enumerate": ""}
  137. _nongen_builtins.update((name, alias) for alias, name in type_aliases.items())
  138. # Drop OrderedDict from this for backward compatibility
  139. del _nongen_builtins["collections.OrderedDict"]
  140. # HACK: consequence of hackily treating LiteralString as an alias for str
  141. del _nongen_builtins["builtins.str"]
  142. def get_nongen_builtins(python_version: tuple[int, int]) -> dict[str, str]:
  143. # After 3.9 with pep585 generic builtins are allowed
  144. return _nongen_builtins if python_version < (3, 9) else {}
  145. RUNTIME_PROTOCOL_DECOS: Final = (
  146. "typing.runtime_checkable",
  147. "typing_extensions.runtime",
  148. "typing_extensions.runtime_checkable",
  149. )
  150. class Node(Context):
  151. """Common base class for all non-type parse tree nodes."""
  152. __slots__ = ()
  153. def __str__(self) -> str:
  154. ans = self.accept(mypy.strconv.StrConv(options=Options()))
  155. if ans is None:
  156. return repr(self)
  157. return ans
  158. def str_with_options(self, options: Options) -> str:
  159. ans = self.accept(mypy.strconv.StrConv(options=options))
  160. assert ans
  161. return ans
  162. def accept(self, visitor: NodeVisitor[T]) -> T:
  163. raise RuntimeError("Not implemented")
  164. @trait
  165. class Statement(Node):
  166. """A statement node."""
  167. __slots__ = ()
  168. def accept(self, visitor: StatementVisitor[T]) -> T:
  169. raise RuntimeError("Not implemented")
  170. @trait
  171. class Expression(Node):
  172. """An expression node."""
  173. __slots__ = ()
  174. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  175. raise RuntimeError("Not implemented")
  176. class FakeExpression(Expression):
  177. """A dummy expression.
  178. We need a dummy expression in one place, and can't instantiate Expression
  179. because it is a trait and mypyc barfs.
  180. """
  181. __slots__ = ()
  182. # TODO:
  183. # Lvalue = Union['NameExpr', 'MemberExpr', 'IndexExpr', 'SuperExpr', 'StarExpr'
  184. # 'TupleExpr']; see #1783.
  185. Lvalue: _TypeAlias = Expression
  186. @trait
  187. class SymbolNode(Node):
  188. """Nodes that can be stored in a symbol table."""
  189. __slots__ = ()
  190. @property
  191. @abstractmethod
  192. def name(self) -> str:
  193. pass
  194. # Fully qualified name
  195. @property
  196. @abstractmethod
  197. def fullname(self) -> str:
  198. pass
  199. @abstractmethod
  200. def serialize(self) -> JsonDict:
  201. pass
  202. @classmethod
  203. def deserialize(cls, data: JsonDict) -> SymbolNode:
  204. classname = data[".class"]
  205. method = deserialize_map.get(classname)
  206. if method is not None:
  207. return method(data)
  208. raise NotImplementedError(f"unexpected .class {classname}")
  209. # Items: fullname, related symbol table node, surrounding type (if any)
  210. Definition: _TypeAlias = Tuple[str, "SymbolTableNode", Optional["TypeInfo"]]
  211. class MypyFile(SymbolNode):
  212. """The abstract syntax tree of a single source file."""
  213. __slots__ = (
  214. "_fullname",
  215. "path",
  216. "defs",
  217. "alias_deps",
  218. "is_bom",
  219. "names",
  220. "imports",
  221. "ignored_lines",
  222. "skipped_lines",
  223. "is_stub",
  224. "is_cache_skeleton",
  225. "is_partial_stub_package",
  226. "plugin_deps",
  227. "future_import_flags",
  228. )
  229. __match_args__ = ("name", "path", "defs")
  230. # Fully qualified module name
  231. _fullname: str
  232. # Path to the file (empty string if not known)
  233. path: str
  234. # Top-level definitions and statements
  235. defs: list[Statement]
  236. # Type alias dependencies as mapping from target to set of alias full names
  237. alias_deps: defaultdict[str, set[str]]
  238. # Is there a UTF-8 BOM at the start?
  239. is_bom: bool
  240. names: SymbolTable
  241. # All import nodes within the file (also ones within functions etc.)
  242. imports: list[ImportBase]
  243. # Lines on which to ignore certain errors when checking.
  244. # If the value is empty, ignore all errors; otherwise, the list contains all
  245. # error codes to ignore.
  246. ignored_lines: dict[int, list[str]]
  247. # Lines that were skipped during semantic analysis e.g. due to ALWAYS_FALSE, MYPY_FALSE,
  248. # or platform/version checks. Those lines would not be type-checked.
  249. skipped_lines: set[int]
  250. # Is this file represented by a stub file (.pyi)?
  251. is_stub: bool
  252. # Is this loaded from the cache and thus missing the actual body of the file?
  253. is_cache_skeleton: bool
  254. # Does this represent an __init__.pyi stub with a module __getattr__
  255. # (i.e. a partial stub package), for such packages we suppress any missing
  256. # module errors in addition to missing attribute errors.
  257. is_partial_stub_package: bool
  258. # Plugin-created dependencies
  259. plugin_deps: dict[str, set[str]]
  260. # Future imports defined in this file. Populated during semantic analysis.
  261. future_import_flags: set[str]
  262. def __init__(
  263. self,
  264. defs: list[Statement],
  265. imports: list[ImportBase],
  266. is_bom: bool = False,
  267. ignored_lines: dict[int, list[str]] | None = None,
  268. ) -> None:
  269. super().__init__()
  270. self.defs = defs
  271. self.line = 1 # Dummy line number
  272. self.column = 0 # Dummy column
  273. self.imports = imports
  274. self.is_bom = is_bom
  275. self.alias_deps = defaultdict(set)
  276. self.plugin_deps = {}
  277. if ignored_lines:
  278. self.ignored_lines = ignored_lines
  279. else:
  280. self.ignored_lines = {}
  281. self.skipped_lines = set()
  282. self.path = ""
  283. self.is_stub = False
  284. self.is_cache_skeleton = False
  285. self.is_partial_stub_package = False
  286. self.future_import_flags = set()
  287. def local_definitions(self) -> Iterator[Definition]:
  288. """Return all definitions within the module (including nested).
  289. This doesn't include imported definitions.
  290. """
  291. return local_definitions(self.names, self.fullname)
  292. @property
  293. def name(self) -> str:
  294. return "" if not self._fullname else self._fullname.split(".")[-1]
  295. @property
  296. def fullname(self) -> str:
  297. return self._fullname
  298. def accept(self, visitor: NodeVisitor[T]) -> T:
  299. return visitor.visit_mypy_file(self)
  300. def is_package_init_file(self) -> bool:
  301. return len(self.path) != 0 and os.path.basename(self.path).startswith("__init__.")
  302. def is_future_flag_set(self, flag: str) -> bool:
  303. return flag in self.future_import_flags
  304. def serialize(self) -> JsonDict:
  305. return {
  306. ".class": "MypyFile",
  307. "_fullname": self._fullname,
  308. "names": self.names.serialize(self._fullname),
  309. "is_stub": self.is_stub,
  310. "path": self.path,
  311. "is_partial_stub_package": self.is_partial_stub_package,
  312. "future_import_flags": list(self.future_import_flags),
  313. }
  314. @classmethod
  315. def deserialize(cls, data: JsonDict) -> MypyFile:
  316. assert data[".class"] == "MypyFile", data
  317. tree = MypyFile([], [])
  318. tree._fullname = data["_fullname"]
  319. tree.names = SymbolTable.deserialize(data["names"])
  320. tree.is_stub = data["is_stub"]
  321. tree.path = data["path"]
  322. tree.is_partial_stub_package = data["is_partial_stub_package"]
  323. tree.is_cache_skeleton = True
  324. tree.future_import_flags = set(data["future_import_flags"])
  325. return tree
  326. class ImportBase(Statement):
  327. """Base class for all import statements."""
  328. __slots__ = ("is_unreachable", "is_top_level", "is_mypy_only", "assignments")
  329. is_unreachable: bool # Set by semanal.SemanticAnalyzerPass1 if inside `if False` etc.
  330. is_top_level: bool # Ditto if outside any class or def
  331. is_mypy_only: bool # Ditto if inside `if TYPE_CHECKING` or `if MYPY`
  332. # If an import replaces existing definitions, we construct dummy assignment
  333. # statements that assign the imported names to the names in the current scope,
  334. # for type checking purposes. Example:
  335. #
  336. # x = 1
  337. # from m import x <-- add assignment representing "x = m.x"
  338. assignments: list[AssignmentStmt]
  339. def __init__(self) -> None:
  340. super().__init__()
  341. self.assignments = []
  342. self.is_unreachable = False
  343. self.is_top_level = False
  344. self.is_mypy_only = False
  345. class Import(ImportBase):
  346. """import m [as n]"""
  347. __slots__ = ("ids",)
  348. __match_args__ = ("ids",)
  349. ids: list[tuple[str, str | None]] # (module id, as id)
  350. def __init__(self, ids: list[tuple[str, str | None]]) -> None:
  351. super().__init__()
  352. self.ids = ids
  353. def accept(self, visitor: StatementVisitor[T]) -> T:
  354. return visitor.visit_import(self)
  355. class ImportFrom(ImportBase):
  356. """from m import x [as y], ..."""
  357. __slots__ = ("id", "names", "relative")
  358. __match_args__ = ("id", "names", "relative")
  359. id: str
  360. relative: int
  361. names: list[tuple[str, str | None]] # Tuples (name, as name)
  362. def __init__(self, id: str, relative: int, names: list[tuple[str, str | None]]) -> None:
  363. super().__init__()
  364. self.id = id
  365. self.names = names
  366. self.relative = relative
  367. def accept(self, visitor: StatementVisitor[T]) -> T:
  368. return visitor.visit_import_from(self)
  369. class ImportAll(ImportBase):
  370. """from m import *"""
  371. __slots__ = ("id", "relative")
  372. __match_args__ = ("id", "relative")
  373. id: str
  374. relative: int
  375. def __init__(self, id: str, relative: int) -> None:
  376. super().__init__()
  377. self.id = id
  378. self.relative = relative
  379. def accept(self, visitor: StatementVisitor[T]) -> T:
  380. return visitor.visit_import_all(self)
  381. FUNCBASE_FLAGS: Final = ["is_property", "is_class", "is_static", "is_final"]
  382. class FuncBase(Node):
  383. """Abstract base class for function-like nodes.
  384. N.B: Although this has SymbolNode subclasses (FuncDef,
  385. OverloadedFuncDef), avoid calling isinstance(..., FuncBase) on
  386. something that is typed as SymbolNode. This is to work around
  387. mypy bug #3603, in which mypy doesn't understand multiple
  388. inheritance very well, and will assume that a SymbolNode
  389. cannot be a FuncBase.
  390. Instead, test against SYMBOL_FUNCBASE_TYPES, which enumerates
  391. SymbolNode subclasses that are also FuncBase subclasses.
  392. """
  393. __slots__ = (
  394. "type",
  395. "unanalyzed_type",
  396. "info",
  397. "is_property",
  398. "is_class", # Uses "@classmethod" (explicit or implicit)
  399. "is_static", # Uses "@staticmethod" (explicit or implicit)
  400. "is_final", # Uses "@final"
  401. "is_explicit_override", # Uses "@override"
  402. "_fullname",
  403. )
  404. def __init__(self) -> None:
  405. super().__init__()
  406. # Type signature. This is usually CallableType or Overloaded, but it can be
  407. # something else for decorated functions.
  408. self.type: mypy.types.ProperType | None = None
  409. # Original, not semantically analyzed type (used for reprocessing)
  410. self.unanalyzed_type: mypy.types.ProperType | None = None
  411. # If method, reference to TypeInfo
  412. # TODO: Type should be Optional[TypeInfo]
  413. self.info = FUNC_NO_INFO
  414. self.is_property = False
  415. self.is_class = False
  416. self.is_static = False
  417. self.is_final = False
  418. self.is_explicit_override = False
  419. # Name with module prefix
  420. self._fullname = ""
  421. @property
  422. @abstractmethod
  423. def name(self) -> str:
  424. pass
  425. @property
  426. def fullname(self) -> str:
  427. return self._fullname
  428. OverloadPart: _TypeAlias = Union["FuncDef", "Decorator"]
  429. class OverloadedFuncDef(FuncBase, SymbolNode, Statement):
  430. """A logical node representing all the variants of a multi-declaration function.
  431. A multi-declaration function is often an @overload, but can also be a
  432. @property with a setter and a/or a deleter.
  433. This node has no explicit representation in the source program.
  434. Overloaded variants must be consecutive in the source file.
  435. """
  436. __slots__ = ("items", "unanalyzed_items", "impl")
  437. items: list[OverloadPart]
  438. unanalyzed_items: list[OverloadPart]
  439. impl: OverloadPart | None
  440. def __init__(self, items: list[OverloadPart]) -> None:
  441. super().__init__()
  442. self.items = items
  443. self.unanalyzed_items = items.copy()
  444. self.impl = None
  445. if items:
  446. # TODO: figure out how to reliably set end position (we don't know the impl here).
  447. self.set_line(items[0].line, items[0].column)
  448. self.is_final = False
  449. @property
  450. def name(self) -> str:
  451. if self.items:
  452. return self.items[0].name
  453. else:
  454. # This may happen for malformed overload
  455. assert self.impl is not None
  456. return self.impl.name
  457. def accept(self, visitor: StatementVisitor[T]) -> T:
  458. return visitor.visit_overloaded_func_def(self)
  459. def serialize(self) -> JsonDict:
  460. return {
  461. ".class": "OverloadedFuncDef",
  462. "items": [i.serialize() for i in self.items],
  463. "type": None if self.type is None else self.type.serialize(),
  464. "fullname": self._fullname,
  465. "impl": None if self.impl is None else self.impl.serialize(),
  466. "flags": get_flags(self, FUNCBASE_FLAGS),
  467. }
  468. @classmethod
  469. def deserialize(cls, data: JsonDict) -> OverloadedFuncDef:
  470. assert data[".class"] == "OverloadedFuncDef"
  471. res = OverloadedFuncDef(
  472. [cast(OverloadPart, SymbolNode.deserialize(d)) for d in data["items"]]
  473. )
  474. if data.get("impl") is not None:
  475. res.impl = cast(OverloadPart, SymbolNode.deserialize(data["impl"]))
  476. # set line for empty overload items, as not set in __init__
  477. if len(res.items) > 0:
  478. res.set_line(res.impl.line)
  479. if data.get("type") is not None:
  480. typ = mypy.types.deserialize_type(data["type"])
  481. assert isinstance(typ, mypy.types.ProperType)
  482. res.type = typ
  483. res._fullname = data["fullname"]
  484. set_flags(res, data["flags"])
  485. # NOTE: res.info will be set in the fixup phase.
  486. return res
  487. class Argument(Node):
  488. """A single argument in a FuncItem."""
  489. __slots__ = ("variable", "type_annotation", "initializer", "kind", "pos_only")
  490. __match_args__ = ("variable", "type_annotation", "initializer", "kind", "pos_only")
  491. def __init__(
  492. self,
  493. variable: Var,
  494. type_annotation: mypy.types.Type | None,
  495. initializer: Expression | None,
  496. kind: ArgKind,
  497. pos_only: bool = False,
  498. ) -> None:
  499. super().__init__()
  500. self.variable = variable
  501. self.type_annotation = type_annotation
  502. self.initializer = initializer
  503. self.kind = kind # must be an ARG_* constant
  504. self.pos_only = pos_only
  505. def set_line(
  506. self,
  507. target: Context | int,
  508. column: int | None = None,
  509. end_line: int | None = None,
  510. end_column: int | None = None,
  511. ) -> None:
  512. super().set_line(target, column, end_line, end_column)
  513. if self.initializer and self.initializer.line < 0:
  514. self.initializer.set_line(self.line, self.column, self.end_line, self.end_column)
  515. self.variable.set_line(self.line, self.column, self.end_line, self.end_column)
  516. FUNCITEM_FLAGS: Final = FUNCBASE_FLAGS + [
  517. "is_overload",
  518. "is_generator",
  519. "is_coroutine",
  520. "is_async_generator",
  521. "is_awaitable_coroutine",
  522. ]
  523. class FuncItem(FuncBase):
  524. """Base class for nodes usable as overloaded function items."""
  525. __slots__ = (
  526. "arguments", # Note that can be unset if deserialized (type is a lie!)
  527. "arg_names", # Names of arguments
  528. "arg_kinds", # Kinds of arguments
  529. "min_args", # Minimum number of arguments
  530. "max_pos", # Maximum number of positional arguments, -1 if no explicit
  531. # limit (*args not included)
  532. "body", # Body of the function
  533. "is_overload", # Is this an overload variant of function with more than
  534. # one overload variant?
  535. "is_generator", # Contains a yield statement?
  536. "is_coroutine", # Defined using 'async def' syntax?
  537. "is_async_generator", # Is an async def generator?
  538. "is_awaitable_coroutine", # Decorated with '@{typing,asyncio}.coroutine'?
  539. "expanded", # Variants of function with type variables with values expanded
  540. )
  541. __deletable__ = ("arguments", "max_pos", "min_args")
  542. def __init__(
  543. self,
  544. arguments: list[Argument] | None = None,
  545. body: Block | None = None,
  546. typ: mypy.types.FunctionLike | None = None,
  547. ) -> None:
  548. super().__init__()
  549. self.arguments = arguments or []
  550. self.arg_names = [None if arg.pos_only else arg.variable.name for arg in self.arguments]
  551. self.arg_kinds: list[ArgKind] = [arg.kind for arg in self.arguments]
  552. self.max_pos: int = self.arg_kinds.count(ARG_POS) + self.arg_kinds.count(ARG_OPT)
  553. self.body: Block = body or Block([])
  554. self.type = typ
  555. self.unanalyzed_type = typ
  556. self.is_overload: bool = False
  557. self.is_generator: bool = False
  558. self.is_coroutine: bool = False
  559. self.is_async_generator: bool = False
  560. self.is_awaitable_coroutine: bool = False
  561. self.expanded: list[FuncItem] = []
  562. self.min_args = 0
  563. for i in range(len(self.arguments)):
  564. if self.arguments[i] is None and i < self.max_fixed_argc():
  565. self.min_args = i + 1
  566. def max_fixed_argc(self) -> int:
  567. return self.max_pos
  568. def is_dynamic(self) -> bool:
  569. return self.type is None
  570. FUNCDEF_FLAGS: Final = FUNCITEM_FLAGS + [
  571. "is_decorated",
  572. "is_conditional",
  573. "is_trivial_body",
  574. "is_mypy_only",
  575. ]
  576. # Abstract status of a function
  577. NOT_ABSTRACT: Final = 0
  578. # Explicitly abstract (with @abstractmethod or overload without implementation)
  579. IS_ABSTRACT: Final = 1
  580. # Implicitly abstract: used for functions with trivial bodies defined in Protocols
  581. IMPLICITLY_ABSTRACT: Final = 2
  582. class FuncDef(FuncItem, SymbolNode, Statement):
  583. """Function definition.
  584. This is a non-lambda function defined using 'def'.
  585. """
  586. __slots__ = (
  587. "_name",
  588. "is_decorated",
  589. "is_conditional",
  590. "abstract_status",
  591. "original_def",
  592. "deco_line",
  593. "is_trivial_body",
  594. "is_mypy_only",
  595. # Present only when a function is decorated with @typing.datasclass_transform or similar
  596. "dataclass_transform_spec",
  597. )
  598. __match_args__ = ("name", "arguments", "type", "body")
  599. # Note that all __init__ args must have default values
  600. def __init__(
  601. self,
  602. name: str = "", # Function name
  603. arguments: list[Argument] | None = None,
  604. body: Block | None = None,
  605. typ: mypy.types.FunctionLike | None = None,
  606. ) -> None:
  607. super().__init__(arguments, body, typ)
  608. self._name = name
  609. self.is_decorated = False
  610. self.is_conditional = False # Defined conditionally (within block)?
  611. self.abstract_status = NOT_ABSTRACT
  612. # Is this an abstract method with trivial body?
  613. # Such methods can't be called via super().
  614. self.is_trivial_body = False
  615. self.is_final = False
  616. # Original conditional definition
  617. self.original_def: None | FuncDef | Var | Decorator = None
  618. # Used for error reporting (to keep backward compatibility with pre-3.8)
  619. self.deco_line: int | None = None
  620. # Definitions that appear in if TYPE_CHECKING are marked with this flag.
  621. self.is_mypy_only = False
  622. self.dataclass_transform_spec: DataclassTransformSpec | None = None
  623. @property
  624. def name(self) -> str:
  625. return self._name
  626. def accept(self, visitor: StatementVisitor[T]) -> T:
  627. return visitor.visit_func_def(self)
  628. def serialize(self) -> JsonDict:
  629. # We're deliberating omitting arguments and storing only arg_names and
  630. # arg_kinds for space-saving reasons (arguments is not used in later
  631. # stages of mypy).
  632. # TODO: After a FuncDef is deserialized, the only time we use `arg_names`
  633. # and `arg_kinds` is when `type` is None and we need to infer a type. Can
  634. # we store the inferred type ahead of time?
  635. return {
  636. ".class": "FuncDef",
  637. "name": self._name,
  638. "fullname": self._fullname,
  639. "arg_names": self.arg_names,
  640. "arg_kinds": [int(x.value) for x in self.arg_kinds],
  641. "type": None if self.type is None else self.type.serialize(),
  642. "flags": get_flags(self, FUNCDEF_FLAGS),
  643. "abstract_status": self.abstract_status,
  644. # TODO: Do we need expanded, original_def?
  645. "dataclass_transform_spec": (
  646. None
  647. if self.dataclass_transform_spec is None
  648. else self.dataclass_transform_spec.serialize()
  649. ),
  650. }
  651. @classmethod
  652. def deserialize(cls, data: JsonDict) -> FuncDef:
  653. assert data[".class"] == "FuncDef"
  654. body = Block([])
  655. ret = FuncDef(
  656. data["name"],
  657. [],
  658. body,
  659. (
  660. None
  661. if data["type"] is None
  662. else cast(mypy.types.FunctionLike, mypy.types.deserialize_type(data["type"]))
  663. ),
  664. )
  665. ret._fullname = data["fullname"]
  666. set_flags(ret, data["flags"])
  667. # NOTE: ret.info is set in the fixup phase.
  668. ret.arg_names = data["arg_names"]
  669. ret.arg_kinds = [ArgKind(x) for x in data["arg_kinds"]]
  670. ret.abstract_status = data["abstract_status"]
  671. ret.dataclass_transform_spec = (
  672. DataclassTransformSpec.deserialize(data["dataclass_transform_spec"])
  673. if data["dataclass_transform_spec"] is not None
  674. else None
  675. )
  676. # Leave these uninitialized so that future uses will trigger an error
  677. del ret.arguments
  678. del ret.max_pos
  679. del ret.min_args
  680. return ret
  681. # All types that are both SymbolNodes and FuncBases. See the FuncBase
  682. # docstring for the rationale.
  683. SYMBOL_FUNCBASE_TYPES = (OverloadedFuncDef, FuncDef)
  684. class Decorator(SymbolNode, Statement):
  685. """A decorated function.
  686. A single Decorator object can include any number of function decorators.
  687. """
  688. __slots__ = ("func", "decorators", "original_decorators", "var", "is_overload")
  689. __match_args__ = ("decorators", "var", "func")
  690. func: FuncDef # Decorated function
  691. decorators: list[Expression] # Decorators (may be empty)
  692. # Some decorators are removed by semanal, keep the original here.
  693. original_decorators: list[Expression]
  694. # TODO: This is mostly used for the type; consider replacing with a 'type' attribute
  695. var: Var # Represents the decorated function obj
  696. is_overload: bool
  697. def __init__(self, func: FuncDef, decorators: list[Expression], var: Var) -> None:
  698. super().__init__()
  699. self.func = func
  700. self.decorators = decorators
  701. self.original_decorators = decorators.copy()
  702. self.var = var
  703. self.is_overload = False
  704. @property
  705. def name(self) -> str:
  706. return self.func.name
  707. @property
  708. def fullname(self) -> str:
  709. return self.func.fullname
  710. @property
  711. def is_final(self) -> bool:
  712. return self.func.is_final
  713. @property
  714. def info(self) -> TypeInfo:
  715. return self.func.info
  716. @property
  717. def type(self) -> mypy.types.Type | None:
  718. return self.var.type
  719. def accept(self, visitor: StatementVisitor[T]) -> T:
  720. return visitor.visit_decorator(self)
  721. def serialize(self) -> JsonDict:
  722. return {
  723. ".class": "Decorator",
  724. "func": self.func.serialize(),
  725. "var": self.var.serialize(),
  726. "is_overload": self.is_overload,
  727. }
  728. @classmethod
  729. def deserialize(cls, data: JsonDict) -> Decorator:
  730. assert data[".class"] == "Decorator"
  731. dec = Decorator(FuncDef.deserialize(data["func"]), [], Var.deserialize(data["var"]))
  732. dec.is_overload = data["is_overload"]
  733. return dec
  734. VAR_FLAGS: Final = [
  735. "is_self",
  736. "is_cls",
  737. "is_initialized_in_class",
  738. "is_staticmethod",
  739. "is_classmethod",
  740. "is_property",
  741. "is_settable_property",
  742. "is_suppressed_import",
  743. "is_classvar",
  744. "is_abstract_var",
  745. "is_final",
  746. "final_unset_in_class",
  747. "final_set_in_init",
  748. "explicit_self_type",
  749. "is_ready",
  750. "is_inferred",
  751. "invalid_partial_type",
  752. "from_module_getattr",
  753. "has_explicit_value",
  754. "allow_incompatible_override",
  755. ]
  756. class Var(SymbolNode):
  757. """A variable.
  758. It can refer to global/local variable or a data attribute.
  759. """
  760. __slots__ = (
  761. "_name",
  762. "_fullname",
  763. "info",
  764. "type",
  765. "final_value",
  766. "is_self",
  767. "is_cls",
  768. "is_ready",
  769. "is_inferred",
  770. "is_initialized_in_class",
  771. "is_staticmethod",
  772. "is_classmethod",
  773. "is_property",
  774. "is_settable_property",
  775. "is_classvar",
  776. "is_abstract_var",
  777. "is_final",
  778. "final_unset_in_class",
  779. "final_set_in_init",
  780. "is_suppressed_import",
  781. "explicit_self_type",
  782. "from_module_getattr",
  783. "has_explicit_value",
  784. "allow_incompatible_override",
  785. "invalid_partial_type",
  786. )
  787. __match_args__ = ("name", "type", "final_value")
  788. def __init__(self, name: str, type: mypy.types.Type | None = None) -> None:
  789. super().__init__()
  790. self._name = name # Name without module prefix
  791. # TODO: Should be Optional[str]
  792. self._fullname = "" # Name with module prefix
  793. # TODO: Should be Optional[TypeInfo]
  794. self.info = VAR_NO_INFO
  795. self.type: mypy.types.Type | None = type # Declared or inferred type, or None
  796. # Is this the first argument to an ordinary method (usually "self")?
  797. self.is_self = False
  798. # Is this the first argument to a classmethod (typically "cls")?
  799. self.is_cls = False
  800. self.is_ready = True # If inferred, is the inferred type available?
  801. self.is_inferred = self.type is None
  802. # Is this initialized explicitly to a non-None value in class body?
  803. self.is_initialized_in_class = False
  804. self.is_staticmethod = False
  805. self.is_classmethod = False
  806. self.is_property = False
  807. self.is_settable_property = False
  808. self.is_classvar = False
  809. self.is_abstract_var = False
  810. # Set to true when this variable refers to a module we were unable to
  811. # parse for some reason (eg a silenced module)
  812. self.is_suppressed_import = False
  813. # Was this "variable" (rather a constant) defined as Final[...]?
  814. self.is_final = False
  815. # If constant value is a simple literal,
  816. # store the literal value (unboxed) for the benefit of
  817. # tools like mypyc.
  818. self.final_value: int | float | complex | bool | str | None = None
  819. # Where the value was set (only for class attributes)
  820. self.final_unset_in_class = False
  821. self.final_set_in_init = False
  822. # This is True for a variable that was declared on self with an explicit type:
  823. # class C:
  824. # def __init__(self) -> None:
  825. # self.x: int
  826. # This case is important because this defines a new Var, even if there is one
  827. # present in a superclass (without explicit type this doesn't create a new Var).
  828. # See SemanticAnalyzer.analyze_member_lvalue() for details.
  829. self.explicit_self_type = False
  830. # If True, this is an implicit Var created due to module-level __getattr__.
  831. self.from_module_getattr = False
  832. # Var can be created with an explicit value `a = 1` or without one `a: int`,
  833. # we need a way to tell which one is which.
  834. self.has_explicit_value = False
  835. # If True, subclasses can override this with an incompatible type.
  836. self.allow_incompatible_override = False
  837. # If True, this means we didn't manage to infer full type and fall back to
  838. # something like list[Any]. We may decide to not use such types as context.
  839. self.invalid_partial_type = False
  840. @property
  841. def name(self) -> str:
  842. return self._name
  843. @property
  844. def fullname(self) -> str:
  845. return self._fullname
  846. def accept(self, visitor: NodeVisitor[T]) -> T:
  847. return visitor.visit_var(self)
  848. def serialize(self) -> JsonDict:
  849. # TODO: Leave default values out?
  850. # NOTE: Sometimes self.is_ready is False here, but we don't care.
  851. data: JsonDict = {
  852. ".class": "Var",
  853. "name": self._name,
  854. "fullname": self._fullname,
  855. "type": None if self.type is None else self.type.serialize(),
  856. "flags": get_flags(self, VAR_FLAGS),
  857. }
  858. if self.final_value is not None:
  859. data["final_value"] = self.final_value
  860. return data
  861. @classmethod
  862. def deserialize(cls, data: JsonDict) -> Var:
  863. assert data[".class"] == "Var"
  864. name = data["name"]
  865. type = None if data["type"] is None else mypy.types.deserialize_type(data["type"])
  866. v = Var(name, type)
  867. v.is_ready = False # Override True default set in __init__
  868. v._fullname = data["fullname"]
  869. set_flags(v, data["flags"])
  870. v.final_value = data.get("final_value")
  871. return v
  872. class ClassDef(Statement):
  873. """Class definition"""
  874. __slots__ = (
  875. "name",
  876. "_fullname",
  877. "defs",
  878. "type_vars",
  879. "base_type_exprs",
  880. "removed_base_type_exprs",
  881. "info",
  882. "metaclass",
  883. "decorators",
  884. "keywords",
  885. "analyzed",
  886. "has_incompatible_baseclass",
  887. "deco_line",
  888. "removed_statements",
  889. )
  890. __match_args__ = ("name", "defs")
  891. name: str # Name of the class without module prefix
  892. _fullname: str # Fully qualified name of the class
  893. defs: Block
  894. type_vars: list[mypy.types.TypeVarLikeType]
  895. # Base class expressions (not semantically analyzed -- can be arbitrary expressions)
  896. base_type_exprs: list[Expression]
  897. # Special base classes like Generic[...] get moved here during semantic analysis
  898. removed_base_type_exprs: list[Expression]
  899. info: TypeInfo # Related TypeInfo
  900. metaclass: Expression | None
  901. decorators: list[Expression]
  902. keywords: dict[str, Expression]
  903. analyzed: Expression | None
  904. has_incompatible_baseclass: bool
  905. # Used by special forms like NamedTuple and TypedDict to store invalid statements
  906. removed_statements: list[Statement]
  907. def __init__(
  908. self,
  909. name: str,
  910. defs: Block,
  911. type_vars: list[mypy.types.TypeVarLikeType] | None = None,
  912. base_type_exprs: list[Expression] | None = None,
  913. metaclass: Expression | None = None,
  914. keywords: list[tuple[str, Expression]] | None = None,
  915. ) -> None:
  916. super().__init__()
  917. self.name = name
  918. self._fullname = ""
  919. self.defs = defs
  920. self.type_vars = type_vars or []
  921. self.base_type_exprs = base_type_exprs or []
  922. self.removed_base_type_exprs = []
  923. self.info = CLASSDEF_NO_INFO
  924. self.metaclass = metaclass
  925. self.decorators = []
  926. self.keywords = dict(keywords) if keywords else {}
  927. self.analyzed = None
  928. self.has_incompatible_baseclass = False
  929. # Used for error reporting (to keep backwad compatibility with pre-3.8)
  930. self.deco_line: int | None = None
  931. self.removed_statements = []
  932. @property
  933. def fullname(self) -> str:
  934. return self._fullname
  935. @fullname.setter
  936. def fullname(self, v: str) -> None:
  937. self._fullname = v
  938. def accept(self, visitor: StatementVisitor[T]) -> T:
  939. return visitor.visit_class_def(self)
  940. def is_generic(self) -> bool:
  941. return self.info.is_generic()
  942. def serialize(self) -> JsonDict:
  943. # Not serialized: defs, base_type_exprs, metaclass, decorators,
  944. # analyzed (for named tuples etc.)
  945. return {
  946. ".class": "ClassDef",
  947. "name": self.name,
  948. "fullname": self.fullname,
  949. "type_vars": [v.serialize() for v in self.type_vars],
  950. }
  951. @classmethod
  952. def deserialize(self, data: JsonDict) -> ClassDef:
  953. assert data[".class"] == "ClassDef"
  954. res = ClassDef(
  955. data["name"],
  956. Block([]),
  957. # https://github.com/python/mypy/issues/12257
  958. [
  959. cast(mypy.types.TypeVarLikeType, mypy.types.deserialize_type(v))
  960. for v in data["type_vars"]
  961. ],
  962. )
  963. res.fullname = data["fullname"]
  964. return res
  965. class GlobalDecl(Statement):
  966. """Declaration global x, y, ..."""
  967. __slots__ = ("names",)
  968. __match_args__ = ("names",)
  969. names: list[str]
  970. def __init__(self, names: list[str]) -> None:
  971. super().__init__()
  972. self.names = names
  973. def accept(self, visitor: StatementVisitor[T]) -> T:
  974. return visitor.visit_global_decl(self)
  975. class NonlocalDecl(Statement):
  976. """Declaration nonlocal x, y, ..."""
  977. __slots__ = ("names",)
  978. __match_args__ = ("names",)
  979. names: list[str]
  980. def __init__(self, names: list[str]) -> None:
  981. super().__init__()
  982. self.names = names
  983. def accept(self, visitor: StatementVisitor[T]) -> T:
  984. return visitor.visit_nonlocal_decl(self)
  985. class Block(Statement):
  986. __slots__ = ("body", "is_unreachable")
  987. __match_args__ = ("body", "is_unreachable")
  988. def __init__(self, body: list[Statement]) -> None:
  989. super().__init__()
  990. self.body = body
  991. # True if we can determine that this block is not executed during semantic
  992. # analysis. For example, this applies to blocks that are protected by
  993. # something like "if PY3:" when using Python 2. However, some code is
  994. # only considered unreachable during type checking and this is not true
  995. # in those cases.
  996. self.is_unreachable = False
  997. def accept(self, visitor: StatementVisitor[T]) -> T:
  998. return visitor.visit_block(self)
  999. # Statements
  1000. class ExpressionStmt(Statement):
  1001. """An expression as a statement, such as print(s)."""
  1002. __slots__ = ("expr",)
  1003. __match_args__ = ("expr",)
  1004. expr: Expression
  1005. def __init__(self, expr: Expression) -> None:
  1006. super().__init__()
  1007. self.expr = expr
  1008. def accept(self, visitor: StatementVisitor[T]) -> T:
  1009. return visitor.visit_expression_stmt(self)
  1010. class AssignmentStmt(Statement):
  1011. """Assignment statement.
  1012. The same node class is used for single assignment, multiple assignment
  1013. (e.g. x, y = z) and chained assignment (e.g. x = y = z), assignments
  1014. that define new names, and assignments with explicit types ("# type: t"
  1015. or "x: t [= ...]").
  1016. An lvalue can be NameExpr, TupleExpr, ListExpr, MemberExpr, or IndexExpr.
  1017. """
  1018. __slots__ = (
  1019. "lvalues",
  1020. "rvalue",
  1021. "type",
  1022. "unanalyzed_type",
  1023. "new_syntax",
  1024. "is_alias_def",
  1025. "is_final_def",
  1026. "invalid_recursive_alias",
  1027. )
  1028. __match_args__ = ("lvalues", "rvalues", "type")
  1029. lvalues: list[Lvalue]
  1030. # This is a TempNode if and only if no rvalue (x: t).
  1031. rvalue: Expression
  1032. # Declared type in a comment, may be None.
  1033. type: mypy.types.Type | None
  1034. # Original, not semantically analyzed type in annotation (used for reprocessing)
  1035. unanalyzed_type: mypy.types.Type | None
  1036. # This indicates usage of PEP 526 type annotation syntax in assignment.
  1037. new_syntax: bool
  1038. # Does this assignment define a type alias?
  1039. is_alias_def: bool
  1040. # Is this a final definition?
  1041. # Final attributes can't be re-assigned once set, and can't be overridden
  1042. # in a subclass. This flag is not set if an attempted declaration was found to
  1043. # be invalid during semantic analysis. It is still set to `True` if
  1044. # a final declaration overrides another final declaration (this is checked
  1045. # during type checking when MROs are known).
  1046. is_final_def: bool
  1047. # Stop further processing of this assignment, to prevent flipping back and forth
  1048. # during semantic analysis passes.
  1049. invalid_recursive_alias: bool
  1050. def __init__(
  1051. self,
  1052. lvalues: list[Lvalue],
  1053. rvalue: Expression,
  1054. type: mypy.types.Type | None = None,
  1055. new_syntax: bool = False,
  1056. ) -> None:
  1057. super().__init__()
  1058. self.lvalues = lvalues
  1059. self.rvalue = rvalue
  1060. self.type = type
  1061. self.unanalyzed_type = type
  1062. self.new_syntax = new_syntax
  1063. self.is_alias_def = False
  1064. self.is_final_def = False
  1065. self.invalid_recursive_alias = False
  1066. def accept(self, visitor: StatementVisitor[T]) -> T:
  1067. return visitor.visit_assignment_stmt(self)
  1068. class OperatorAssignmentStmt(Statement):
  1069. """Operator assignment statement such as x += 1"""
  1070. __slots__ = ("op", "lvalue", "rvalue")
  1071. __match_args__ = ("lvalue", "op", "rvalue")
  1072. op: str # TODO: Enum?
  1073. lvalue: Lvalue
  1074. rvalue: Expression
  1075. def __init__(self, op: str, lvalue: Lvalue, rvalue: Expression) -> None:
  1076. super().__init__()
  1077. self.op = op
  1078. self.lvalue = lvalue
  1079. self.rvalue = rvalue
  1080. def accept(self, visitor: StatementVisitor[T]) -> T:
  1081. return visitor.visit_operator_assignment_stmt(self)
  1082. class WhileStmt(Statement):
  1083. __slots__ = ("expr", "body", "else_body")
  1084. __match_args__ = ("expr", "body", "else_body")
  1085. expr: Expression
  1086. body: Block
  1087. else_body: Block | None
  1088. def __init__(self, expr: Expression, body: Block, else_body: Block | None) -> None:
  1089. super().__init__()
  1090. self.expr = expr
  1091. self.body = body
  1092. self.else_body = else_body
  1093. def accept(self, visitor: StatementVisitor[T]) -> T:
  1094. return visitor.visit_while_stmt(self)
  1095. class ForStmt(Statement):
  1096. __slots__ = (
  1097. "index",
  1098. "index_type",
  1099. "unanalyzed_index_type",
  1100. "inferred_item_type",
  1101. "inferred_iterator_type",
  1102. "expr",
  1103. "body",
  1104. "else_body",
  1105. "is_async",
  1106. )
  1107. __match_args__ = ("index", "index_type", "expr", "body", "else_body")
  1108. # Index variables
  1109. index: Lvalue
  1110. # Type given by type comments for index, can be None
  1111. index_type: mypy.types.Type | None
  1112. # Original, not semantically analyzed type in annotation (used for reprocessing)
  1113. unanalyzed_index_type: mypy.types.Type | None
  1114. # Inferred iterable item type
  1115. inferred_item_type: mypy.types.Type | None
  1116. # Inferred iterator type
  1117. inferred_iterator_type: mypy.types.Type | None
  1118. # Expression to iterate
  1119. expr: Expression
  1120. body: Block
  1121. else_body: Block | None
  1122. is_async: bool # True if `async for ...` (PEP 492, Python 3.5)
  1123. def __init__(
  1124. self,
  1125. index: Lvalue,
  1126. expr: Expression,
  1127. body: Block,
  1128. else_body: Block | None,
  1129. index_type: mypy.types.Type | None = None,
  1130. ) -> None:
  1131. super().__init__()
  1132. self.index = index
  1133. self.index_type = index_type
  1134. self.unanalyzed_index_type = index_type
  1135. self.inferred_item_type = None
  1136. self.inferred_iterator_type = None
  1137. self.expr = expr
  1138. self.body = body
  1139. self.else_body = else_body
  1140. self.is_async = False
  1141. def accept(self, visitor: StatementVisitor[T]) -> T:
  1142. return visitor.visit_for_stmt(self)
  1143. class ReturnStmt(Statement):
  1144. __slots__ = ("expr",)
  1145. __match_args__ = ("expr",)
  1146. expr: Expression | None
  1147. def __init__(self, expr: Expression | None) -> None:
  1148. super().__init__()
  1149. self.expr = expr
  1150. def accept(self, visitor: StatementVisitor[T]) -> T:
  1151. return visitor.visit_return_stmt(self)
  1152. class AssertStmt(Statement):
  1153. __slots__ = ("expr", "msg")
  1154. __match_args__ = ("expr", "msg")
  1155. expr: Expression
  1156. msg: Expression | None
  1157. def __init__(self, expr: Expression, msg: Expression | None = None) -> None:
  1158. super().__init__()
  1159. self.expr = expr
  1160. self.msg = msg
  1161. def accept(self, visitor: StatementVisitor[T]) -> T:
  1162. return visitor.visit_assert_stmt(self)
  1163. class DelStmt(Statement):
  1164. __slots__ = ("expr",)
  1165. __match_args__ = ("expr",)
  1166. expr: Lvalue
  1167. def __init__(self, expr: Lvalue) -> None:
  1168. super().__init__()
  1169. self.expr = expr
  1170. def accept(self, visitor: StatementVisitor[T]) -> T:
  1171. return visitor.visit_del_stmt(self)
  1172. class BreakStmt(Statement):
  1173. __slots__ = ()
  1174. def accept(self, visitor: StatementVisitor[T]) -> T:
  1175. return visitor.visit_break_stmt(self)
  1176. class ContinueStmt(Statement):
  1177. __slots__ = ()
  1178. def accept(self, visitor: StatementVisitor[T]) -> T:
  1179. return visitor.visit_continue_stmt(self)
  1180. class PassStmt(Statement):
  1181. __slots__ = ()
  1182. def accept(self, visitor: StatementVisitor[T]) -> T:
  1183. return visitor.visit_pass_stmt(self)
  1184. class IfStmt(Statement):
  1185. __slots__ = ("expr", "body", "else_body")
  1186. __match_args__ = ("expr", "body", "else_body")
  1187. expr: list[Expression]
  1188. body: list[Block]
  1189. else_body: Block | None
  1190. def __init__(self, expr: list[Expression], body: list[Block], else_body: Block | None) -> None:
  1191. super().__init__()
  1192. self.expr = expr
  1193. self.body = body
  1194. self.else_body = else_body
  1195. def accept(self, visitor: StatementVisitor[T]) -> T:
  1196. return visitor.visit_if_stmt(self)
  1197. class RaiseStmt(Statement):
  1198. __slots__ = ("expr", "from_expr")
  1199. __match_args__ = ("expr", "from_expr")
  1200. # Plain 'raise' is a valid statement.
  1201. expr: Expression | None
  1202. from_expr: Expression | None
  1203. def __init__(self, expr: Expression | None, from_expr: Expression | None) -> None:
  1204. super().__init__()
  1205. self.expr = expr
  1206. self.from_expr = from_expr
  1207. def accept(self, visitor: StatementVisitor[T]) -> T:
  1208. return visitor.visit_raise_stmt(self)
  1209. class TryStmt(Statement):
  1210. __slots__ = ("body", "types", "vars", "handlers", "else_body", "finally_body", "is_star")
  1211. __match_args__ = ("body", "types", "vars", "handlers", "else_body", "finally_body", "is_star")
  1212. body: Block # Try body
  1213. # Plain 'except:' also possible
  1214. types: list[Expression | None] # Except type expressions
  1215. vars: list[NameExpr | None] # Except variable names
  1216. handlers: list[Block] # Except bodies
  1217. else_body: Block | None
  1218. finally_body: Block | None
  1219. # Whether this is try ... except* (added in Python 3.11)
  1220. is_star: bool
  1221. def __init__(
  1222. self,
  1223. body: Block,
  1224. vars: list[NameExpr | None],
  1225. types: list[Expression | None],
  1226. handlers: list[Block],
  1227. else_body: Block | None,
  1228. finally_body: Block | None,
  1229. ) -> None:
  1230. super().__init__()
  1231. self.body = body
  1232. self.vars = vars
  1233. self.types = types
  1234. self.handlers = handlers
  1235. self.else_body = else_body
  1236. self.finally_body = finally_body
  1237. self.is_star = False
  1238. def accept(self, visitor: StatementVisitor[T]) -> T:
  1239. return visitor.visit_try_stmt(self)
  1240. class WithStmt(Statement):
  1241. __slots__ = ("expr", "target", "unanalyzed_type", "analyzed_types", "body", "is_async")
  1242. __match_args__ = ("expr", "target", "body")
  1243. expr: list[Expression]
  1244. target: list[Lvalue | None]
  1245. # Type given by type comments for target, can be None
  1246. unanalyzed_type: mypy.types.Type | None
  1247. # Semantically analyzed types from type comment (TypeList type expanded)
  1248. analyzed_types: list[mypy.types.Type]
  1249. body: Block
  1250. is_async: bool # True if `async with ...` (PEP 492, Python 3.5)
  1251. def __init__(
  1252. self,
  1253. expr: list[Expression],
  1254. target: list[Lvalue | None],
  1255. body: Block,
  1256. target_type: mypy.types.Type | None = None,
  1257. ) -> None:
  1258. super().__init__()
  1259. self.expr = expr
  1260. self.target = target
  1261. self.unanalyzed_type = target_type
  1262. self.analyzed_types = []
  1263. self.body = body
  1264. self.is_async = False
  1265. def accept(self, visitor: StatementVisitor[T]) -> T:
  1266. return visitor.visit_with_stmt(self)
  1267. class MatchStmt(Statement):
  1268. __slots__ = ("subject", "patterns", "guards", "bodies")
  1269. __match_args__ = ("subject", "patterns", "guards", "bodies")
  1270. subject: Expression
  1271. patterns: list[Pattern]
  1272. guards: list[Expression | None]
  1273. bodies: list[Block]
  1274. def __init__(
  1275. self,
  1276. subject: Expression,
  1277. patterns: list[Pattern],
  1278. guards: list[Expression | None],
  1279. bodies: list[Block],
  1280. ) -> None:
  1281. super().__init__()
  1282. assert len(patterns) == len(guards) == len(bodies)
  1283. self.subject = subject
  1284. self.patterns = patterns
  1285. self.guards = guards
  1286. self.bodies = bodies
  1287. def accept(self, visitor: StatementVisitor[T]) -> T:
  1288. return visitor.visit_match_stmt(self)
  1289. # Expressions
  1290. class IntExpr(Expression):
  1291. """Integer literal"""
  1292. __slots__ = ("value",)
  1293. __match_args__ = ("value",)
  1294. value: int # 0 by default
  1295. def __init__(self, value: int) -> None:
  1296. super().__init__()
  1297. self.value = value
  1298. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1299. return visitor.visit_int_expr(self)
  1300. # How mypy uses StrExpr and BytesExpr:
  1301. #
  1302. # b'x' -> BytesExpr
  1303. # 'x', u'x' -> StrExpr
  1304. class StrExpr(Expression):
  1305. """String literal"""
  1306. __slots__ = ("value",)
  1307. __match_args__ = ("value",)
  1308. value: str # '' by default
  1309. def __init__(self, value: str) -> None:
  1310. super().__init__()
  1311. self.value = value
  1312. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1313. return visitor.visit_str_expr(self)
  1314. def is_StrExpr_list(seq: list[Expression]) -> TypeGuard[list[StrExpr]]:
  1315. return all(isinstance(item, StrExpr) for item in seq)
  1316. class BytesExpr(Expression):
  1317. """Bytes literal"""
  1318. __slots__ = ("value",)
  1319. __match_args__ = ("value",)
  1320. # Note: we deliberately do NOT use bytes here because it ends up
  1321. # unnecessarily complicating a lot of the result logic. For example,
  1322. # we'd have to worry about converting the bytes into a format we can
  1323. # easily serialize/deserialize to and from JSON, would have to worry
  1324. # about turning the bytes into a human-readable representation in
  1325. # error messages...
  1326. #
  1327. # It's more convenient to just store the human-readable representation
  1328. # from the very start.
  1329. value: str
  1330. def __init__(self, value: str) -> None:
  1331. super().__init__()
  1332. self.value = value
  1333. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1334. return visitor.visit_bytes_expr(self)
  1335. class FloatExpr(Expression):
  1336. """Float literal"""
  1337. __slots__ = ("value",)
  1338. __match_args__ = ("value",)
  1339. value: float # 0.0 by default
  1340. def __init__(self, value: float) -> None:
  1341. super().__init__()
  1342. self.value = value
  1343. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1344. return visitor.visit_float_expr(self)
  1345. class ComplexExpr(Expression):
  1346. """Complex literal"""
  1347. __slots__ = ("value",)
  1348. __match_args__ = ("value",)
  1349. value: complex
  1350. def __init__(self, value: complex) -> None:
  1351. super().__init__()
  1352. self.value = value
  1353. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1354. return visitor.visit_complex_expr(self)
  1355. class EllipsisExpr(Expression):
  1356. """Ellipsis (...)"""
  1357. __slots__ = ()
  1358. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1359. return visitor.visit_ellipsis(self)
  1360. class StarExpr(Expression):
  1361. """Star expression"""
  1362. __slots__ = ("expr", "valid")
  1363. __match_args__ = ("expr", "valid")
  1364. expr: Expression
  1365. valid: bool
  1366. def __init__(self, expr: Expression) -> None:
  1367. super().__init__()
  1368. self.expr = expr
  1369. # Whether this starred expression is used in a tuple/list and as lvalue
  1370. self.valid = False
  1371. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1372. return visitor.visit_star_expr(self)
  1373. class RefExpr(Expression):
  1374. """Abstract base class for name-like constructs"""
  1375. __slots__ = (
  1376. "kind",
  1377. "node",
  1378. "_fullname",
  1379. "is_new_def",
  1380. "is_inferred_def",
  1381. "is_alias_rvalue",
  1382. "type_guard",
  1383. )
  1384. def __init__(self) -> None:
  1385. super().__init__()
  1386. # LDEF/GDEF/MDEF/... (None if not available)
  1387. self.kind: int | None = None
  1388. # Var, FuncDef or TypeInfo that describes this
  1389. self.node: SymbolNode | None = None
  1390. # Fully qualified name (or name if not global)
  1391. self._fullname = ""
  1392. # Does this define a new name?
  1393. self.is_new_def = False
  1394. # Does this define a new name with inferred type?
  1395. #
  1396. # For members, after semantic analysis, this does not take base
  1397. # classes into consideration at all; the type checker deals with these.
  1398. self.is_inferred_def = False
  1399. # Is this expression appears as an rvalue of a valid type alias definition?
  1400. self.is_alias_rvalue = False
  1401. # Cache type guard from callable_type.type_guard
  1402. self.type_guard: mypy.types.Type | None = None
  1403. @property
  1404. def fullname(self) -> str:
  1405. return self._fullname
  1406. @fullname.setter
  1407. def fullname(self, v: str) -> None:
  1408. self._fullname = v
  1409. class NameExpr(RefExpr):
  1410. """Name expression
  1411. This refers to a local name, global name or a module.
  1412. """
  1413. __slots__ = ("name", "is_special_form")
  1414. __match_args__ = ("name", "node")
  1415. def __init__(self, name: str) -> None:
  1416. super().__init__()
  1417. self.name = name # Name referred to
  1418. # Is this a l.h.s. of a special form assignment like typed dict or type variable?
  1419. self.is_special_form = False
  1420. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1421. return visitor.visit_name_expr(self)
  1422. def serialize(self) -> JsonDict:
  1423. assert False, f"Serializing NameExpr: {self}"
  1424. class MemberExpr(RefExpr):
  1425. """Member access expression x.y"""
  1426. __slots__ = ("expr", "name", "def_var")
  1427. __match_args__ = ("expr", "name", "node")
  1428. def __init__(self, expr: Expression, name: str) -> None:
  1429. super().__init__()
  1430. self.expr = expr
  1431. self.name = name
  1432. # The variable node related to a definition through 'self.x = <initializer>'.
  1433. # The nodes of other kinds of member expressions are resolved during type checking.
  1434. self.def_var: Var | None = None
  1435. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1436. return visitor.visit_member_expr(self)
  1437. # Kinds of arguments
  1438. @unique
  1439. class ArgKind(Enum):
  1440. # Positional argument
  1441. ARG_POS = 0
  1442. # Positional, optional argument (functions only, not calls)
  1443. ARG_OPT = 1
  1444. # *arg argument
  1445. ARG_STAR = 2
  1446. # Keyword argument x=y in call, or keyword-only function arg
  1447. ARG_NAMED = 3
  1448. # **arg argument
  1449. ARG_STAR2 = 4
  1450. # In an argument list, keyword-only and also optional
  1451. ARG_NAMED_OPT = 5
  1452. def is_positional(self, star: bool = False) -> bool:
  1453. return self == ARG_POS or self == ARG_OPT or (star and self == ARG_STAR)
  1454. def is_named(self, star: bool = False) -> bool:
  1455. return self == ARG_NAMED or self == ARG_NAMED_OPT or (star and self == ARG_STAR2)
  1456. def is_required(self) -> bool:
  1457. return self == ARG_POS or self == ARG_NAMED
  1458. def is_optional(self) -> bool:
  1459. return self == ARG_OPT or self == ARG_NAMED_OPT
  1460. def is_star(self) -> bool:
  1461. return self == ARG_STAR or self == ARG_STAR2
  1462. ARG_POS: Final = ArgKind.ARG_POS
  1463. ARG_OPT: Final = ArgKind.ARG_OPT
  1464. ARG_STAR: Final = ArgKind.ARG_STAR
  1465. ARG_NAMED: Final = ArgKind.ARG_NAMED
  1466. ARG_STAR2: Final = ArgKind.ARG_STAR2
  1467. ARG_NAMED_OPT: Final = ArgKind.ARG_NAMED_OPT
  1468. class CallExpr(Expression):
  1469. """Call expression.
  1470. This can also represent several special forms that are syntactically calls
  1471. such as cast(...) and None # type: ....
  1472. """
  1473. __slots__ = ("callee", "args", "arg_kinds", "arg_names", "analyzed")
  1474. __match_args__ = ("callee", "args", "arg_kinds", "arg_names")
  1475. def __init__(
  1476. self,
  1477. callee: Expression,
  1478. args: list[Expression],
  1479. arg_kinds: list[ArgKind],
  1480. arg_names: list[str | None],
  1481. analyzed: Expression | None = None,
  1482. ) -> None:
  1483. super().__init__()
  1484. if not arg_names:
  1485. arg_names = [None] * len(args)
  1486. self.callee = callee
  1487. self.args = args
  1488. self.arg_kinds = arg_kinds # ARG_ constants
  1489. # Each name can be None if not a keyword argument.
  1490. self.arg_names: list[str | None] = arg_names
  1491. # If not None, the node that represents the meaning of the CallExpr. For
  1492. # cast(...) this is a CastExpr.
  1493. self.analyzed = analyzed
  1494. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1495. return visitor.visit_call_expr(self)
  1496. class YieldFromExpr(Expression):
  1497. __slots__ = ("expr",)
  1498. __match_args__ = ("expr",)
  1499. expr: Expression
  1500. def __init__(self, expr: Expression) -> None:
  1501. super().__init__()
  1502. self.expr = expr
  1503. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1504. return visitor.visit_yield_from_expr(self)
  1505. class YieldExpr(Expression):
  1506. __slots__ = ("expr",)
  1507. __match_args__ = ("expr",)
  1508. expr: Expression | None
  1509. def __init__(self, expr: Expression | None) -> None:
  1510. super().__init__()
  1511. self.expr = expr
  1512. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1513. return visitor.visit_yield_expr(self)
  1514. class IndexExpr(Expression):
  1515. """Index expression x[y].
  1516. Also wraps type application such as List[int] as a special form.
  1517. """
  1518. __slots__ = ("base", "index", "method_type", "analyzed")
  1519. __match_args__ = ("base", "index")
  1520. base: Expression
  1521. index: Expression
  1522. # Inferred __getitem__ method type
  1523. method_type: mypy.types.Type | None
  1524. # If not None, this is actually semantically a type application
  1525. # Class[type, ...] or a type alias initializer.
  1526. analyzed: TypeApplication | TypeAliasExpr | None
  1527. def __init__(self, base: Expression, index: Expression) -> None:
  1528. super().__init__()
  1529. self.base = base
  1530. self.index = index
  1531. self.method_type = None
  1532. self.analyzed = None
  1533. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1534. return visitor.visit_index_expr(self)
  1535. class UnaryExpr(Expression):
  1536. """Unary operation"""
  1537. __slots__ = ("op", "expr", "method_type")
  1538. __match_args__ = ("op", "expr")
  1539. op: str # TODO: Enum?
  1540. expr: Expression
  1541. # Inferred operator method type
  1542. method_type: mypy.types.Type | None
  1543. def __init__(self, op: str, expr: Expression) -> None:
  1544. super().__init__()
  1545. self.op = op
  1546. self.expr = expr
  1547. self.method_type = None
  1548. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1549. return visitor.visit_unary_expr(self)
  1550. class AssignmentExpr(Expression):
  1551. """Assignment expressions in Python 3.8+, like "a := 2"."""
  1552. __slots__ = ("target", "value")
  1553. __match_args__ = ("target", "value")
  1554. def __init__(self, target: Expression, value: Expression) -> None:
  1555. super().__init__()
  1556. self.target = target
  1557. self.value = value
  1558. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1559. return visitor.visit_assignment_expr(self)
  1560. class OpExpr(Expression):
  1561. """Binary operation.
  1562. The dot (.), [] and comparison operators have more specific nodes.
  1563. """
  1564. __slots__ = (
  1565. "op",
  1566. "left",
  1567. "right",
  1568. "method_type",
  1569. "right_always",
  1570. "right_unreachable",
  1571. "analyzed",
  1572. )
  1573. __match_args__ = ("left", "op", "right")
  1574. op: str # TODO: Enum?
  1575. left: Expression
  1576. right: Expression
  1577. # Inferred type for the operator method type (when relevant).
  1578. method_type: mypy.types.Type | None
  1579. # Per static analysis only: Is the right side going to be evaluated every time?
  1580. right_always: bool
  1581. # Per static analysis only: Is the right side unreachable?
  1582. right_unreachable: bool
  1583. # Used for expressions that represent a type "X | Y" in some contexts
  1584. analyzed: TypeAliasExpr | None
  1585. def __init__(
  1586. self, op: str, left: Expression, right: Expression, analyzed: TypeAliasExpr | None = None
  1587. ) -> None:
  1588. super().__init__()
  1589. self.op = op
  1590. self.left = left
  1591. self.right = right
  1592. self.method_type = None
  1593. self.right_always = False
  1594. self.right_unreachable = False
  1595. self.analyzed = analyzed
  1596. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1597. return visitor.visit_op_expr(self)
  1598. class ComparisonExpr(Expression):
  1599. """Comparison expression (e.g. a < b > c < d)."""
  1600. __slots__ = ("operators", "operands", "method_types")
  1601. __match_args__ = ("operands", "operators")
  1602. operators: list[str]
  1603. operands: list[Expression]
  1604. # Inferred type for the operator methods (when relevant; None for 'is').
  1605. method_types: list[mypy.types.Type | None]
  1606. def __init__(self, operators: list[str], operands: list[Expression]) -> None:
  1607. super().__init__()
  1608. self.operators = operators
  1609. self.operands = operands
  1610. self.method_types = []
  1611. def pairwise(self) -> Iterator[tuple[str, Expression, Expression]]:
  1612. """If this comparison expr is "a < b is c == d", yields the sequence
  1613. ("<", a, b), ("is", b, c), ("==", c, d)
  1614. """
  1615. for i, operator in enumerate(self.operators):
  1616. yield operator, self.operands[i], self.operands[i + 1]
  1617. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1618. return visitor.visit_comparison_expr(self)
  1619. class SliceExpr(Expression):
  1620. """Slice expression (e.g. 'x:y', 'x:', '::2' or ':').
  1621. This is only valid as index in index expressions.
  1622. """
  1623. __slots__ = ("begin_index", "end_index", "stride")
  1624. __match_args__ = ("begin_index", "end_index", "stride")
  1625. begin_index: Expression | None
  1626. end_index: Expression | None
  1627. stride: Expression | None
  1628. def __init__(
  1629. self,
  1630. begin_index: Expression | None,
  1631. end_index: Expression | None,
  1632. stride: Expression | None,
  1633. ) -> None:
  1634. super().__init__()
  1635. self.begin_index = begin_index
  1636. self.end_index = end_index
  1637. self.stride = stride
  1638. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1639. return visitor.visit_slice_expr(self)
  1640. class CastExpr(Expression):
  1641. """Cast expression cast(type, expr)."""
  1642. __slots__ = ("expr", "type")
  1643. __match_args__ = ("expr", "type")
  1644. expr: Expression
  1645. type: mypy.types.Type
  1646. def __init__(self, expr: Expression, typ: mypy.types.Type) -> None:
  1647. super().__init__()
  1648. self.expr = expr
  1649. self.type = typ
  1650. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1651. return visitor.visit_cast_expr(self)
  1652. class AssertTypeExpr(Expression):
  1653. """Represents a typing.assert_type(expr, type) call."""
  1654. __slots__ = ("expr", "type")
  1655. __match_args__ = ("expr", "type")
  1656. expr: Expression
  1657. type: mypy.types.Type
  1658. def __init__(self, expr: Expression, typ: mypy.types.Type) -> None:
  1659. super().__init__()
  1660. self.expr = expr
  1661. self.type = typ
  1662. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1663. return visitor.visit_assert_type_expr(self)
  1664. class RevealExpr(Expression):
  1665. """Reveal type expression reveal_type(expr) or reveal_locals() expression."""
  1666. __slots__ = ("expr", "kind", "local_nodes")
  1667. __match_args__ = ("expr", "kind", "local_nodes")
  1668. expr: Expression | None
  1669. kind: int
  1670. local_nodes: list[Var] | None
  1671. def __init__(
  1672. self, kind: int, expr: Expression | None = None, local_nodes: list[Var] | None = None
  1673. ) -> None:
  1674. super().__init__()
  1675. self.expr = expr
  1676. self.kind = kind
  1677. self.local_nodes = local_nodes
  1678. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1679. return visitor.visit_reveal_expr(self)
  1680. class SuperExpr(Expression):
  1681. """Expression super().name"""
  1682. __slots__ = ("name", "info", "call")
  1683. __match_args__ = ("name", "call", "info")
  1684. name: str
  1685. info: TypeInfo | None # Type that contains this super expression
  1686. call: CallExpr # The expression super(...)
  1687. def __init__(self, name: str, call: CallExpr) -> None:
  1688. super().__init__()
  1689. self.name = name
  1690. self.call = call
  1691. self.info = None
  1692. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1693. return visitor.visit_super_expr(self)
  1694. class LambdaExpr(FuncItem, Expression):
  1695. """Lambda expression"""
  1696. __match_args__ = ("arguments", "arg_names", "arg_kinds", "body")
  1697. @property
  1698. def name(self) -> str:
  1699. return "<lambda>"
  1700. def expr(self) -> Expression:
  1701. """Return the expression (the body) of the lambda."""
  1702. ret = self.body.body[-1]
  1703. assert isinstance(ret, ReturnStmt)
  1704. expr = ret.expr
  1705. assert expr is not None # lambda can't have empty body
  1706. return expr
  1707. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1708. return visitor.visit_lambda_expr(self)
  1709. def is_dynamic(self) -> bool:
  1710. return False
  1711. class ListExpr(Expression):
  1712. """List literal expression [...]."""
  1713. __slots__ = ("items",)
  1714. __match_args__ = ("items",)
  1715. items: list[Expression]
  1716. def __init__(self, items: list[Expression]) -> None:
  1717. super().__init__()
  1718. self.items = items
  1719. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1720. return visitor.visit_list_expr(self)
  1721. class DictExpr(Expression):
  1722. """Dictionary literal expression {key: value, ...}."""
  1723. __slots__ = ("items",)
  1724. __match_args__ = ("items",)
  1725. items: list[tuple[Expression | None, Expression]]
  1726. def __init__(self, items: list[tuple[Expression | None, Expression]]) -> None:
  1727. super().__init__()
  1728. self.items = items
  1729. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1730. return visitor.visit_dict_expr(self)
  1731. class TupleExpr(Expression):
  1732. """Tuple literal expression (..., ...)
  1733. Also lvalue sequences (..., ...) and [..., ...]"""
  1734. __slots__ = ("items",)
  1735. __match_args__ = ("items",)
  1736. items: list[Expression]
  1737. def __init__(self, items: list[Expression]) -> None:
  1738. super().__init__()
  1739. self.items = items
  1740. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1741. return visitor.visit_tuple_expr(self)
  1742. class SetExpr(Expression):
  1743. """Set literal expression {value, ...}."""
  1744. __slots__ = ("items",)
  1745. __match_args__ = ("items",)
  1746. items: list[Expression]
  1747. def __init__(self, items: list[Expression]) -> None:
  1748. super().__init__()
  1749. self.items = items
  1750. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1751. return visitor.visit_set_expr(self)
  1752. class GeneratorExpr(Expression):
  1753. """Generator expression ... for ... in ... [ for ... in ... ] [ if ... ]."""
  1754. __slots__ = ("left_expr", "sequences", "condlists", "is_async", "indices")
  1755. __match_args__ = ("left_expr", "indices", "sequences", "condlists")
  1756. left_expr: Expression
  1757. sequences: list[Expression]
  1758. condlists: list[list[Expression]]
  1759. is_async: list[bool]
  1760. indices: list[Lvalue]
  1761. def __init__(
  1762. self,
  1763. left_expr: Expression,
  1764. indices: list[Lvalue],
  1765. sequences: list[Expression],
  1766. condlists: list[list[Expression]],
  1767. is_async: list[bool],
  1768. ) -> None:
  1769. super().__init__()
  1770. self.left_expr = left_expr
  1771. self.sequences = sequences
  1772. self.condlists = condlists
  1773. self.indices = indices
  1774. self.is_async = is_async
  1775. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1776. return visitor.visit_generator_expr(self)
  1777. class ListComprehension(Expression):
  1778. """List comprehension (e.g. [x + 1 for x in a])"""
  1779. __slots__ = ("generator",)
  1780. __match_args__ = ("generator",)
  1781. generator: GeneratorExpr
  1782. def __init__(self, generator: GeneratorExpr) -> None:
  1783. super().__init__()
  1784. self.generator = generator
  1785. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1786. return visitor.visit_list_comprehension(self)
  1787. class SetComprehension(Expression):
  1788. """Set comprehension (e.g. {x + 1 for x in a})"""
  1789. __slots__ = ("generator",)
  1790. __match_args__ = ("generator",)
  1791. generator: GeneratorExpr
  1792. def __init__(self, generator: GeneratorExpr) -> None:
  1793. super().__init__()
  1794. self.generator = generator
  1795. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1796. return visitor.visit_set_comprehension(self)
  1797. class DictionaryComprehension(Expression):
  1798. """Dictionary comprehension (e.g. {k: v for k, v in a}"""
  1799. __slots__ = ("key", "value", "sequences", "condlists", "is_async", "indices")
  1800. __match_args__ = ("key", "value", "indices", "sequences", "condlists")
  1801. key: Expression
  1802. value: Expression
  1803. sequences: list[Expression]
  1804. condlists: list[list[Expression]]
  1805. is_async: list[bool]
  1806. indices: list[Lvalue]
  1807. def __init__(
  1808. self,
  1809. key: Expression,
  1810. value: Expression,
  1811. indices: list[Lvalue],
  1812. sequences: list[Expression],
  1813. condlists: list[list[Expression]],
  1814. is_async: list[bool],
  1815. ) -> None:
  1816. super().__init__()
  1817. self.key = key
  1818. self.value = value
  1819. self.sequences = sequences
  1820. self.condlists = condlists
  1821. self.indices = indices
  1822. self.is_async = is_async
  1823. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1824. return visitor.visit_dictionary_comprehension(self)
  1825. class ConditionalExpr(Expression):
  1826. """Conditional expression (e.g. x if y else z)"""
  1827. __slots__ = ("cond", "if_expr", "else_expr")
  1828. __match_args__ = ("if_expr", "cond", "else_expr")
  1829. cond: Expression
  1830. if_expr: Expression
  1831. else_expr: Expression
  1832. def __init__(self, cond: Expression, if_expr: Expression, else_expr: Expression) -> None:
  1833. super().__init__()
  1834. self.cond = cond
  1835. self.if_expr = if_expr
  1836. self.else_expr = else_expr
  1837. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1838. return visitor.visit_conditional_expr(self)
  1839. class TypeApplication(Expression):
  1840. """Type application expr[type, ...]"""
  1841. __slots__ = ("expr", "types")
  1842. __match_args__ = ("expr", "types")
  1843. expr: Expression
  1844. types: list[mypy.types.Type]
  1845. def __init__(self, expr: Expression, types: list[mypy.types.Type]) -> None:
  1846. super().__init__()
  1847. self.expr = expr
  1848. self.types = types
  1849. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1850. return visitor.visit_type_application(self)
  1851. # Variance of a type variable. For example, T in the definition of
  1852. # List[T] is invariant, so List[int] is not a subtype of List[object],
  1853. # and also List[object] is not a subtype of List[int].
  1854. #
  1855. # The T in Iterable[T] is covariant, so Iterable[int] is a subtype of
  1856. # Iterable[object], but not vice versa.
  1857. #
  1858. # If T is contravariant in Foo[T], Foo[object] is a subtype of
  1859. # Foo[int], but not vice versa.
  1860. INVARIANT: Final = 0
  1861. COVARIANT: Final = 1
  1862. CONTRAVARIANT: Final = 2
  1863. class TypeVarLikeExpr(SymbolNode, Expression):
  1864. """Base class for TypeVarExpr, ParamSpecExpr and TypeVarTupleExpr.
  1865. Note that they are constructed by the semantic analyzer.
  1866. """
  1867. __slots__ = ("_name", "_fullname", "upper_bound", "default", "variance")
  1868. _name: str
  1869. _fullname: str
  1870. # Upper bound: only subtypes of upper_bound are valid as values. By default
  1871. # this is 'object', meaning no restriction.
  1872. upper_bound: mypy.types.Type
  1873. # Default: used to resolve the TypeVar if the default is not explicitly given.
  1874. # By default this is 'AnyType(TypeOfAny.from_omitted_generics)'. See PEP 696.
  1875. default: mypy.types.Type
  1876. # Variance of the type variable. Invariant is the default.
  1877. # TypeVar(..., covariant=True) defines a covariant type variable.
  1878. # TypeVar(..., contravariant=True) defines a contravariant type
  1879. # variable.
  1880. variance: int
  1881. def __init__(
  1882. self,
  1883. name: str,
  1884. fullname: str,
  1885. upper_bound: mypy.types.Type,
  1886. default: mypy.types.Type,
  1887. variance: int = INVARIANT,
  1888. ) -> None:
  1889. super().__init__()
  1890. self._name = name
  1891. self._fullname = fullname
  1892. self.upper_bound = upper_bound
  1893. self.default = default
  1894. self.variance = variance
  1895. @property
  1896. def name(self) -> str:
  1897. return self._name
  1898. @property
  1899. def fullname(self) -> str:
  1900. return self._fullname
  1901. class TypeVarExpr(TypeVarLikeExpr):
  1902. """Type variable expression TypeVar(...).
  1903. This is also used to represent type variables in symbol tables.
  1904. A type variable is not valid as a type unless bound in a TypeVarLikeScope.
  1905. That happens within:
  1906. 1. a generic class that uses the type variable as a type argument or
  1907. 2. a generic function that refers to the type variable in its signature.
  1908. """
  1909. __slots__ = ("values",)
  1910. __match_args__ = ("name", "values", "upper_bound", "default")
  1911. # Value restriction: only types in the list are valid as values. If the
  1912. # list is empty, there is no restriction.
  1913. values: list[mypy.types.Type]
  1914. def __init__(
  1915. self,
  1916. name: str,
  1917. fullname: str,
  1918. values: list[mypy.types.Type],
  1919. upper_bound: mypy.types.Type,
  1920. default: mypy.types.Type,
  1921. variance: int = INVARIANT,
  1922. ) -> None:
  1923. super().__init__(name, fullname, upper_bound, default, variance)
  1924. self.values = values
  1925. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1926. return visitor.visit_type_var_expr(self)
  1927. def serialize(self) -> JsonDict:
  1928. return {
  1929. ".class": "TypeVarExpr",
  1930. "name": self._name,
  1931. "fullname": self._fullname,
  1932. "values": [t.serialize() for t in self.values],
  1933. "upper_bound": self.upper_bound.serialize(),
  1934. "default": self.default.serialize(),
  1935. "variance": self.variance,
  1936. }
  1937. @classmethod
  1938. def deserialize(cls, data: JsonDict) -> TypeVarExpr:
  1939. assert data[".class"] == "TypeVarExpr"
  1940. return TypeVarExpr(
  1941. data["name"],
  1942. data["fullname"],
  1943. [mypy.types.deserialize_type(v) for v in data["values"]],
  1944. mypy.types.deserialize_type(data["upper_bound"]),
  1945. mypy.types.deserialize_type(data["default"]),
  1946. data["variance"],
  1947. )
  1948. class ParamSpecExpr(TypeVarLikeExpr):
  1949. __slots__ = ()
  1950. __match_args__ = ("name", "upper_bound", "default")
  1951. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1952. return visitor.visit_paramspec_expr(self)
  1953. def serialize(self) -> JsonDict:
  1954. return {
  1955. ".class": "ParamSpecExpr",
  1956. "name": self._name,
  1957. "fullname": self._fullname,
  1958. "upper_bound": self.upper_bound.serialize(),
  1959. "default": self.default.serialize(),
  1960. "variance": self.variance,
  1961. }
  1962. @classmethod
  1963. def deserialize(cls, data: JsonDict) -> ParamSpecExpr:
  1964. assert data[".class"] == "ParamSpecExpr"
  1965. return ParamSpecExpr(
  1966. data["name"],
  1967. data["fullname"],
  1968. mypy.types.deserialize_type(data["upper_bound"]),
  1969. mypy.types.deserialize_type(data["default"]),
  1970. data["variance"],
  1971. )
  1972. class TypeVarTupleExpr(TypeVarLikeExpr):
  1973. """Type variable tuple expression TypeVarTuple(...)."""
  1974. __slots__ = "tuple_fallback"
  1975. tuple_fallback: mypy.types.Instance
  1976. __match_args__ = ("name", "upper_bound", "default")
  1977. def __init__(
  1978. self,
  1979. name: str,
  1980. fullname: str,
  1981. upper_bound: mypy.types.Type,
  1982. tuple_fallback: mypy.types.Instance,
  1983. default: mypy.types.Type,
  1984. variance: int = INVARIANT,
  1985. ) -> None:
  1986. super().__init__(name, fullname, upper_bound, default, variance)
  1987. self.tuple_fallback = tuple_fallback
  1988. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  1989. return visitor.visit_type_var_tuple_expr(self)
  1990. def serialize(self) -> JsonDict:
  1991. return {
  1992. ".class": "TypeVarTupleExpr",
  1993. "name": self._name,
  1994. "fullname": self._fullname,
  1995. "upper_bound": self.upper_bound.serialize(),
  1996. "tuple_fallback": self.tuple_fallback.serialize(),
  1997. "default": self.default.serialize(),
  1998. "variance": self.variance,
  1999. }
  2000. @classmethod
  2001. def deserialize(cls, data: JsonDict) -> TypeVarTupleExpr:
  2002. assert data[".class"] == "TypeVarTupleExpr"
  2003. return TypeVarTupleExpr(
  2004. data["name"],
  2005. data["fullname"],
  2006. mypy.types.deserialize_type(data["upper_bound"]),
  2007. mypy.types.Instance.deserialize(data["tuple_fallback"]),
  2008. mypy.types.deserialize_type(data["default"]),
  2009. data["variance"],
  2010. )
  2011. class TypeAliasExpr(Expression):
  2012. """Type alias expression (rvalue)."""
  2013. __slots__ = ("type", "tvars", "no_args", "node")
  2014. __match_args__ = ("type", "tvars", "no_args", "node")
  2015. # The target type.
  2016. type: mypy.types.Type
  2017. # Names of type variables used to define the alias
  2018. tvars: list[str]
  2019. # Whether this alias was defined in bare form. Used to distinguish
  2020. # between
  2021. # A = List
  2022. # and
  2023. # A = List[Any]
  2024. no_args: bool
  2025. node: TypeAlias
  2026. def __init__(self, node: TypeAlias) -> None:
  2027. super().__init__()
  2028. self.type = node.target
  2029. self.tvars = [v.name for v in node.alias_tvars]
  2030. self.no_args = node.no_args
  2031. self.node = node
  2032. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2033. return visitor.visit_type_alias_expr(self)
  2034. class NamedTupleExpr(Expression):
  2035. """Named tuple expression namedtuple(...) or NamedTuple(...)."""
  2036. __slots__ = ("info", "is_typed")
  2037. __match_args__ = ("info",)
  2038. # The class representation of this named tuple (its tuple_type attribute contains
  2039. # the tuple item types)
  2040. info: TypeInfo
  2041. is_typed: bool # whether this class was created with typing(_extensions).NamedTuple
  2042. def __init__(self, info: TypeInfo, is_typed: bool = False) -> None:
  2043. super().__init__()
  2044. self.info = info
  2045. self.is_typed = is_typed
  2046. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2047. return visitor.visit_namedtuple_expr(self)
  2048. class TypedDictExpr(Expression):
  2049. """Typed dict expression TypedDict(...)."""
  2050. __slots__ = ("info",)
  2051. __match_args__ = ("info",)
  2052. # The class representation of this typed dict
  2053. info: TypeInfo
  2054. def __init__(self, info: TypeInfo) -> None:
  2055. super().__init__()
  2056. self.info = info
  2057. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2058. return visitor.visit_typeddict_expr(self)
  2059. class EnumCallExpr(Expression):
  2060. """Named tuple expression Enum('name', 'val1 val2 ...')."""
  2061. __slots__ = ("info", "items", "values")
  2062. __match_args__ = ("info", "items", "values")
  2063. # The class representation of this enumerated type
  2064. info: TypeInfo
  2065. # The item names (for debugging)
  2066. items: list[str]
  2067. values: list[Expression | None]
  2068. def __init__(self, info: TypeInfo, items: list[str], values: list[Expression | None]) -> None:
  2069. super().__init__()
  2070. self.info = info
  2071. self.items = items
  2072. self.values = values
  2073. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2074. return visitor.visit_enum_call_expr(self)
  2075. class PromoteExpr(Expression):
  2076. """Ducktype class decorator expression _promote(...)."""
  2077. __slots__ = ("type",)
  2078. type: mypy.types.ProperType
  2079. def __init__(self, type: mypy.types.ProperType) -> None:
  2080. super().__init__()
  2081. self.type = type
  2082. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2083. return visitor.visit__promote_expr(self)
  2084. class NewTypeExpr(Expression):
  2085. """NewType expression NewType(...)."""
  2086. __slots__ = ("name", "old_type", "info")
  2087. __match_args__ = ("name", "old_type", "info")
  2088. name: str
  2089. # The base type (the second argument to NewType)
  2090. old_type: mypy.types.Type | None
  2091. # The synthesized class representing the new type (inherits old_type)
  2092. info: TypeInfo | None
  2093. def __init__(
  2094. self, name: str, old_type: mypy.types.Type | None, line: int, column: int
  2095. ) -> None:
  2096. super().__init__(line=line, column=column)
  2097. self.name = name
  2098. self.old_type = old_type
  2099. self.info = None
  2100. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2101. return visitor.visit_newtype_expr(self)
  2102. class AwaitExpr(Expression):
  2103. """Await expression (await ...)."""
  2104. __slots__ = ("expr",)
  2105. __match_args__ = ("expr",)
  2106. expr: Expression
  2107. def __init__(self, expr: Expression) -> None:
  2108. super().__init__()
  2109. self.expr = expr
  2110. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2111. return visitor.visit_await_expr(self)
  2112. # Constants
  2113. class TempNode(Expression):
  2114. """Temporary dummy node used during type checking.
  2115. This node is not present in the original program; it is just an artifact
  2116. of the type checker implementation. It only represents an opaque node with
  2117. some fixed type.
  2118. """
  2119. __slots__ = ("type", "no_rhs")
  2120. type: mypy.types.Type
  2121. # Is this TempNode used to indicate absence of a right hand side in an annotated assignment?
  2122. # (e.g. for 'x: int' the rvalue is TempNode(AnyType(TypeOfAny.special_form), no_rhs=True))
  2123. no_rhs: bool
  2124. def __init__(
  2125. self, typ: mypy.types.Type, no_rhs: bool = False, *, context: Context | None = None
  2126. ) -> None:
  2127. """Construct a dummy node; optionally borrow line/column from context object."""
  2128. super().__init__()
  2129. self.type = typ
  2130. self.no_rhs = no_rhs
  2131. if context is not None:
  2132. self.line = context.line
  2133. self.column = context.column
  2134. def __repr__(self) -> str:
  2135. return "TempNode:%d(%s)" % (self.line, str(self.type))
  2136. def accept(self, visitor: ExpressionVisitor[T]) -> T:
  2137. return visitor.visit_temp_node(self)
  2138. # Special attributes not collected as protocol members by Python 3.12
  2139. # See typing._SPECIAL_NAMES
  2140. EXCLUDED_PROTOCOL_ATTRIBUTES: Final = frozenset(
  2141. {
  2142. "__abstractmethods__",
  2143. "__annotations__",
  2144. "__dict__",
  2145. "__doc__",
  2146. "__init__",
  2147. "__module__",
  2148. "__new__",
  2149. "__slots__",
  2150. "__subclasshook__",
  2151. "__weakref__",
  2152. "__class_getitem__", # Since Python 3.9
  2153. }
  2154. )
  2155. class TypeInfo(SymbolNode):
  2156. """The type structure of a single class.
  2157. Each TypeInfo corresponds one-to-one to a ClassDef, which
  2158. represents the AST of the class.
  2159. In type-theory terms, this is a "type constructor", and if the
  2160. class is generic then it will be a type constructor of higher kind.
  2161. Where the class is used in an actual type, it's in the form of an
  2162. Instance, which amounts to a type application of the tycon to
  2163. the appropriate number of arguments.
  2164. """
  2165. __slots__ = (
  2166. "_fullname",
  2167. "module_name",
  2168. "defn",
  2169. "mro",
  2170. "_mro_refs",
  2171. "bad_mro",
  2172. "is_final",
  2173. "declared_metaclass",
  2174. "metaclass_type",
  2175. "names",
  2176. "is_abstract",
  2177. "is_protocol",
  2178. "runtime_protocol",
  2179. "abstract_attributes",
  2180. "deletable_attributes",
  2181. "slots",
  2182. "assuming",
  2183. "assuming_proper",
  2184. "inferring",
  2185. "is_enum",
  2186. "fallback_to_any",
  2187. "meta_fallback_to_any",
  2188. "type_vars",
  2189. "has_param_spec_type",
  2190. "bases",
  2191. "_promote",
  2192. "tuple_type",
  2193. "special_alias",
  2194. "is_named_tuple",
  2195. "typeddict_type",
  2196. "is_newtype",
  2197. "is_intersection",
  2198. "metadata",
  2199. "alt_promote",
  2200. "has_type_var_tuple_type",
  2201. "type_var_tuple_prefix",
  2202. "type_var_tuple_suffix",
  2203. "self_type",
  2204. "dataclass_transform_spec",
  2205. )
  2206. _fullname: str # Fully qualified name
  2207. # Fully qualified name for the module this type was defined in. This
  2208. # information is also in the fullname, but is harder to extract in the
  2209. # case of nested class definitions.
  2210. module_name: str
  2211. defn: ClassDef # Corresponding ClassDef
  2212. # Method Resolution Order: the order of looking up attributes. The first
  2213. # value always to refers to this class.
  2214. mro: list[TypeInfo]
  2215. # Used to stash the names of the mro classes temporarily between
  2216. # deserialization and fixup. See deserialize() for why.
  2217. _mro_refs: list[str] | None
  2218. bad_mro: bool # Could not construct full MRO
  2219. is_final: bool
  2220. declared_metaclass: mypy.types.Instance | None
  2221. metaclass_type: mypy.types.Instance | None
  2222. names: SymbolTable # Names defined directly in this type
  2223. is_abstract: bool # Does the class have any abstract attributes?
  2224. is_protocol: bool # Is this a protocol class?
  2225. runtime_protocol: bool # Does this protocol support isinstance checks?
  2226. # List of names of abstract attributes together with their abstract status.
  2227. # The abstract status must be one of `NOT_ABSTRACT`, `IS_ABSTRACT`, `IMPLICITLY_ABSTRACT`.
  2228. abstract_attributes: list[tuple[str, int]]
  2229. deletable_attributes: list[str] # Used by mypyc only
  2230. # Does this type have concrete `__slots__` defined?
  2231. # If class does not have `__slots__` defined then it is `None`,
  2232. # if it has empty `__slots__` then it is an empty set.
  2233. slots: set[str] | None
  2234. # The attributes 'assuming' and 'assuming_proper' represent structural subtype matrices.
  2235. #
  2236. # In languages with structural subtyping, one can keep a global subtype matrix like this:
  2237. # . A B C .
  2238. # A 1 0 0
  2239. # B 1 1 1
  2240. # C 1 0 1
  2241. # .
  2242. # where 1 indicates that the type in corresponding row is a subtype of the type
  2243. # in corresponding column. This matrix typically starts filled with all 1's and
  2244. # a typechecker tries to "disprove" every subtyping relation using atomic (or nominal) types.
  2245. # However, we don't want to keep this huge global state. Instead, we keep the subtype
  2246. # information in the form of list of pairs (subtype, supertype) shared by all Instances
  2247. # with given supertype's TypeInfo. When we enter a subtype check we push a pair in this list
  2248. # thus assuming that we started with 1 in corresponding matrix element. Such algorithm allows
  2249. # to treat recursive and mutually recursive protocols and other kinds of complex situations.
  2250. #
  2251. # If concurrent/parallel type checking will be added in future,
  2252. # then there should be one matrix per thread/process to avoid false negatives
  2253. # during the type checking phase.
  2254. assuming: list[tuple[mypy.types.Instance, mypy.types.Instance]]
  2255. assuming_proper: list[tuple[mypy.types.Instance, mypy.types.Instance]]
  2256. # Ditto for temporary 'inferring' stack of recursive constraint inference.
  2257. # It contains Instances of protocol types that appeared as an argument to
  2258. # constraints.infer_constraints(). We need 'inferring' to avoid infinite recursion for
  2259. # recursive and mutually recursive protocols.
  2260. #
  2261. # We make 'assuming' and 'inferring' attributes here instead of passing they as kwargs,
  2262. # since this would require to pass them in many dozens of calls. In particular,
  2263. # there is a dependency infer_constraint -> is_subtype -> is_callable_subtype ->
  2264. # -> infer_constraints.
  2265. inferring: list[mypy.types.Instance]
  2266. # 'inferring' and 'assuming' can't be made sets, since we need to use
  2267. # is_same_type to correctly treat unions.
  2268. # Classes inheriting from Enum shadow their true members with a __getattr__, so we
  2269. # have to treat them as a special case.
  2270. is_enum: bool
  2271. # If true, any unknown attributes should have type 'Any' instead
  2272. # of generating a type error. This would be true if there is a
  2273. # base class with type 'Any', but other use cases may be
  2274. # possible. This is similar to having __getattr__ that returns Any
  2275. # (and __setattr__), but without the __getattr__ method.
  2276. fallback_to_any: bool
  2277. # Same as above but for cases where metaclass has type Any. This will suppress
  2278. # all attribute errors only for *class object* access.
  2279. meta_fallback_to_any: bool
  2280. # Information related to type annotations.
  2281. # Generic type variable names (full names)
  2282. type_vars: list[str]
  2283. # Whether this class has a ParamSpec type variable
  2284. has_param_spec_type: bool
  2285. # Direct base classes.
  2286. bases: list[mypy.types.Instance]
  2287. # Another type which this type will be treated as a subtype of,
  2288. # even though it's not a subclass in Python. The non-standard
  2289. # `@_promote` decorator introduces this, and there are also
  2290. # several builtin examples, in particular `int` -> `float`.
  2291. _promote: list[mypy.types.ProperType]
  2292. # This is used for promoting native integer types such as 'i64' to
  2293. # 'int'. (_promote is used for the other direction.) This only
  2294. # supports one-step promotions (e.g., i64 -> int, not
  2295. # i64 -> int -> float, and this isn't used to promote in joins.
  2296. #
  2297. # This results in some unintuitive results, such as that even
  2298. # though i64 is compatible with int and int is compatible with
  2299. # float, i64 is *not* compatible with float.
  2300. alt_promote: mypy.types.Instance | None
  2301. # Representation of a Tuple[...] base class, if the class has any
  2302. # (e.g., for named tuples). If this is not None, the actual Type
  2303. # object used for this class is not an Instance but a TupleType;
  2304. # the corresponding Instance is set as the fallback type of the
  2305. # tuple type.
  2306. tuple_type: mypy.types.TupleType | None
  2307. # Is this a named tuple type?
  2308. is_named_tuple: bool
  2309. # If this class is defined by the TypedDict type constructor,
  2310. # then this is not None.
  2311. typeddict_type: mypy.types.TypedDictType | None
  2312. # Is this a newtype type?
  2313. is_newtype: bool
  2314. # Is this a synthesized intersection type?
  2315. is_intersection: bool
  2316. # This is a dictionary that will be serialized and un-serialized as is.
  2317. # It is useful for plugins to add their data to save in the cache.
  2318. metadata: dict[str, JsonDict]
  2319. # Store type alias representing this type (for named tuples and TypedDicts).
  2320. # Although definitions of these types are stored in symbol tables as TypeInfo,
  2321. # when a type analyzer will find them, it should construct a TupleType, or
  2322. # a TypedDict type. However, we can't use the plain types, since if the definition
  2323. # is recursive, this will create an actual recursive structure of types (i.e. as
  2324. # internal Python objects) causing infinite recursions everywhere during type checking.
  2325. # To overcome this, we create a TypeAlias node, that will point to these types.
  2326. # We store this node in the `special_alias` attribute, because it must be the same node
  2327. # in case we are doing multiple semantic analysis passes.
  2328. special_alias: TypeAlias | None
  2329. # Shared type variable for typing.Self in this class (if used, otherwise None).
  2330. self_type: mypy.types.TypeVarType | None
  2331. # Added if the corresponding class is directly decorated with `typing.dataclass_transform`
  2332. dataclass_transform_spec: DataclassTransformSpec | None
  2333. FLAGS: Final = [
  2334. "is_abstract",
  2335. "is_enum",
  2336. "fallback_to_any",
  2337. "meta_fallback_to_any",
  2338. "is_named_tuple",
  2339. "is_newtype",
  2340. "is_protocol",
  2341. "runtime_protocol",
  2342. "is_final",
  2343. "is_intersection",
  2344. ]
  2345. def __init__(self, names: SymbolTable, defn: ClassDef, module_name: str) -> None:
  2346. """Initialize a TypeInfo."""
  2347. super().__init__()
  2348. self._fullname = defn.fullname
  2349. self.names = names
  2350. self.defn = defn
  2351. self.module_name = module_name
  2352. self.type_vars = []
  2353. self.has_param_spec_type = False
  2354. self.has_type_var_tuple_type = False
  2355. self.bases = []
  2356. self.mro = []
  2357. self._mro_refs = None
  2358. self.bad_mro = False
  2359. self.declared_metaclass = None
  2360. self.metaclass_type = None
  2361. self.is_abstract = False
  2362. self.abstract_attributes = []
  2363. self.deletable_attributes = []
  2364. self.slots = None
  2365. self.assuming = []
  2366. self.assuming_proper = []
  2367. self.inferring = []
  2368. self.is_protocol = False
  2369. self.runtime_protocol = False
  2370. self.type_var_tuple_prefix: int | None = None
  2371. self.type_var_tuple_suffix: int | None = None
  2372. self.add_type_vars()
  2373. self.is_final = False
  2374. self.is_enum = False
  2375. self.fallback_to_any = False
  2376. self.meta_fallback_to_any = False
  2377. self._promote = []
  2378. self.alt_promote = None
  2379. self.tuple_type = None
  2380. self.special_alias = None
  2381. self.is_named_tuple = False
  2382. self.typeddict_type = None
  2383. self.is_newtype = False
  2384. self.is_intersection = False
  2385. self.metadata = {}
  2386. self.self_type = None
  2387. self.dataclass_transform_spec = None
  2388. def add_type_vars(self) -> None:
  2389. self.has_type_var_tuple_type = False
  2390. if self.defn.type_vars:
  2391. for i, vd in enumerate(self.defn.type_vars):
  2392. if isinstance(vd, mypy.types.ParamSpecType):
  2393. self.has_param_spec_type = True
  2394. if isinstance(vd, mypy.types.TypeVarTupleType):
  2395. assert not self.has_type_var_tuple_type
  2396. self.has_type_var_tuple_type = True
  2397. self.type_var_tuple_prefix = i
  2398. self.type_var_tuple_suffix = len(self.defn.type_vars) - i - 1
  2399. self.type_vars.append(vd.name)
  2400. assert not (
  2401. self.has_param_spec_type and self.has_type_var_tuple_type
  2402. ), "Mixing type var tuples and param specs not supported yet"
  2403. @property
  2404. def name(self) -> str:
  2405. """Short name."""
  2406. return self.defn.name
  2407. @property
  2408. def fullname(self) -> str:
  2409. return self._fullname
  2410. def is_generic(self) -> bool:
  2411. """Is the type generic (i.e. does it have type variables)?"""
  2412. return len(self.type_vars) > 0
  2413. def get(self, name: str) -> SymbolTableNode | None:
  2414. for cls in self.mro:
  2415. n = cls.names.get(name)
  2416. if n:
  2417. return n
  2418. return None
  2419. def get_containing_type_info(self, name: str) -> TypeInfo | None:
  2420. for cls in self.mro:
  2421. if name in cls.names:
  2422. return cls
  2423. return None
  2424. @property
  2425. def protocol_members(self) -> list[str]:
  2426. # Protocol members are names of all attributes/methods defined in a protocol
  2427. # and in all its supertypes (except for 'object').
  2428. members: set[str] = set()
  2429. assert self.mro, "This property can be only accessed after MRO is (re-)calculated"
  2430. for base in self.mro[:-1]: # we skip "object" since everyone implements it
  2431. if base.is_protocol:
  2432. for name, node in base.names.items():
  2433. if isinstance(node.node, (TypeAlias, TypeVarExpr, MypyFile)):
  2434. # These are auxiliary definitions (and type aliases are prohibited).
  2435. continue
  2436. if name in EXCLUDED_PROTOCOL_ATTRIBUTES:
  2437. continue
  2438. members.add(name)
  2439. return sorted(list(members))
  2440. def __getitem__(self, name: str) -> SymbolTableNode:
  2441. n = self.get(name)
  2442. if n:
  2443. return n
  2444. else:
  2445. raise KeyError(name)
  2446. def __repr__(self) -> str:
  2447. return f"<TypeInfo {self.fullname}>"
  2448. def __bool__(self) -> bool:
  2449. # We defined this here instead of just overriding it in
  2450. # FakeInfo so that mypyc can generate a direct call instead of
  2451. # using the generic bool handling.
  2452. return not isinstance(self, FakeInfo)
  2453. def has_readable_member(self, name: str) -> bool:
  2454. return self.get(name) is not None
  2455. def get_method(self, name: str) -> FuncBase | Decorator | None:
  2456. for cls in self.mro:
  2457. if name in cls.names:
  2458. node = cls.names[name].node
  2459. if isinstance(node, FuncBase):
  2460. return node
  2461. elif isinstance(node, Decorator): # Two `if`s make `mypyc` happy
  2462. return node
  2463. else:
  2464. return None
  2465. return None
  2466. def calculate_metaclass_type(self) -> mypy.types.Instance | None:
  2467. declared = self.declared_metaclass
  2468. if declared is not None and not declared.type.has_base("builtins.type"):
  2469. return declared
  2470. if self._fullname == "builtins.type":
  2471. return mypy.types.Instance(self, [])
  2472. candidates = [
  2473. s.declared_metaclass
  2474. for s in self.mro
  2475. if s.declared_metaclass is not None and s.declared_metaclass.type is not None
  2476. ]
  2477. for c in candidates:
  2478. if all(other.type in c.type.mro for other in candidates):
  2479. return c
  2480. return None
  2481. def is_metaclass(self) -> bool:
  2482. return (
  2483. self.has_base("builtins.type")
  2484. or self.fullname == "abc.ABCMeta"
  2485. or self.fallback_to_any
  2486. )
  2487. def has_base(self, fullname: str) -> bool:
  2488. """Return True if type has a base type with the specified name.
  2489. This can be either via extension or via implementation.
  2490. """
  2491. for cls in self.mro:
  2492. if cls.fullname == fullname:
  2493. return True
  2494. return False
  2495. def direct_base_classes(self) -> list[TypeInfo]:
  2496. """Return a direct base classes.
  2497. Omit base classes of other base classes.
  2498. """
  2499. return [base.type for base in self.bases]
  2500. def update_tuple_type(self, typ: mypy.types.TupleType) -> None:
  2501. """Update tuple_type and special_alias as needed."""
  2502. self.tuple_type = typ
  2503. alias = TypeAlias.from_tuple_type(self)
  2504. if not self.special_alias:
  2505. self.special_alias = alias
  2506. else:
  2507. self.special_alias.target = alias.target
  2508. def update_typeddict_type(self, typ: mypy.types.TypedDictType) -> None:
  2509. """Update typeddict_type and special_alias as needed."""
  2510. self.typeddict_type = typ
  2511. alias = TypeAlias.from_typeddict_type(self)
  2512. if not self.special_alias:
  2513. self.special_alias = alias
  2514. else:
  2515. self.special_alias.target = alias.target
  2516. def __str__(self) -> str:
  2517. """Return a string representation of the type.
  2518. This includes the most important information about the type.
  2519. """
  2520. options = Options()
  2521. return self.dump(
  2522. str_conv=mypy.strconv.StrConv(options=options),
  2523. type_str_conv=mypy.types.TypeStrVisitor(options=options),
  2524. )
  2525. def dump(
  2526. self, str_conv: mypy.strconv.StrConv, type_str_conv: mypy.types.TypeStrVisitor
  2527. ) -> str:
  2528. """Return a string dump of the contents of the TypeInfo."""
  2529. base: str = ""
  2530. def type_str(typ: mypy.types.Type) -> str:
  2531. return typ.accept(type_str_conv)
  2532. head = "TypeInfo" + str_conv.format_id(self)
  2533. if self.bases:
  2534. base = f"Bases({', '.join(type_str(base) for base in self.bases)})"
  2535. mro = "Mro({})".format(
  2536. ", ".join(item.fullname + str_conv.format_id(item) for item in self.mro)
  2537. )
  2538. names = []
  2539. for name in sorted(self.names):
  2540. description = name + str_conv.format_id(self.names[name].node)
  2541. node = self.names[name].node
  2542. if isinstance(node, Var) and node.type:
  2543. description += f" ({type_str(node.type)})"
  2544. names.append(description)
  2545. items = [f"Name({self.fullname})", base, mro, ("Names", names)]
  2546. if self.declared_metaclass:
  2547. items.append(f"DeclaredMetaclass({type_str(self.declared_metaclass)})")
  2548. if self.metaclass_type:
  2549. items.append(f"MetaclassType({type_str(self.metaclass_type)})")
  2550. return mypy.strconv.dump_tagged(items, head, str_conv=str_conv)
  2551. def serialize(self) -> JsonDict:
  2552. # NOTE: This is where all ClassDefs originate, so there shouldn't be duplicates.
  2553. data = {
  2554. ".class": "TypeInfo",
  2555. "module_name": self.module_name,
  2556. "fullname": self.fullname,
  2557. "names": self.names.serialize(self.fullname),
  2558. "defn": self.defn.serialize(),
  2559. "abstract_attributes": self.abstract_attributes,
  2560. "type_vars": self.type_vars,
  2561. "has_param_spec_type": self.has_param_spec_type,
  2562. "bases": [b.serialize() for b in self.bases],
  2563. "mro": [c.fullname for c in self.mro],
  2564. "_promote": [p.serialize() for p in self._promote],
  2565. "alt_promote": None if self.alt_promote is None else self.alt_promote.serialize(),
  2566. "declared_metaclass": (
  2567. None if self.declared_metaclass is None else self.declared_metaclass.serialize()
  2568. ),
  2569. "metaclass_type": None
  2570. if self.metaclass_type is None
  2571. else self.metaclass_type.serialize(),
  2572. "tuple_type": None if self.tuple_type is None else self.tuple_type.serialize(),
  2573. "typeddict_type": None
  2574. if self.typeddict_type is None
  2575. else self.typeddict_type.serialize(),
  2576. "flags": get_flags(self, TypeInfo.FLAGS),
  2577. "metadata": self.metadata,
  2578. "slots": list(sorted(self.slots)) if self.slots is not None else None,
  2579. "deletable_attributes": self.deletable_attributes,
  2580. "self_type": self.self_type.serialize() if self.self_type is not None else None,
  2581. "dataclass_transform_spec": (
  2582. self.dataclass_transform_spec.serialize()
  2583. if self.dataclass_transform_spec is not None
  2584. else None
  2585. ),
  2586. }
  2587. return data
  2588. @classmethod
  2589. def deserialize(cls, data: JsonDict) -> TypeInfo:
  2590. names = SymbolTable.deserialize(data["names"])
  2591. defn = ClassDef.deserialize(data["defn"])
  2592. module_name = data["module_name"]
  2593. ti = TypeInfo(names, defn, module_name)
  2594. ti._fullname = data["fullname"]
  2595. # TODO: Is there a reason to reconstruct ti.subtypes?
  2596. ti.abstract_attributes = [(attr[0], attr[1]) for attr in data["abstract_attributes"]]
  2597. ti.type_vars = data["type_vars"]
  2598. ti.has_param_spec_type = data["has_param_spec_type"]
  2599. ti.bases = [mypy.types.Instance.deserialize(b) for b in data["bases"]]
  2600. _promote = []
  2601. for p in data["_promote"]:
  2602. t = mypy.types.deserialize_type(p)
  2603. assert isinstance(t, mypy.types.ProperType)
  2604. _promote.append(t)
  2605. ti._promote = _promote
  2606. ti.alt_promote = (
  2607. None
  2608. if data["alt_promote"] is None
  2609. else mypy.types.Instance.deserialize(data["alt_promote"])
  2610. )
  2611. ti.declared_metaclass = (
  2612. None
  2613. if data["declared_metaclass"] is None
  2614. else mypy.types.Instance.deserialize(data["declared_metaclass"])
  2615. )
  2616. ti.metaclass_type = (
  2617. None
  2618. if data["metaclass_type"] is None
  2619. else mypy.types.Instance.deserialize(data["metaclass_type"])
  2620. )
  2621. # NOTE: ti.mro will be set in the fixup phase based on these
  2622. # names. The reason we need to store the mro instead of just
  2623. # recomputing it from base classes has to do with a subtle
  2624. # point about fine-grained incremental: the cache files might
  2625. # not be loaded until after a class in the mro has changed its
  2626. # bases, which causes the mro to change. If we recomputed our
  2627. # mro, we would compute the *new* mro, which leaves us with no
  2628. # way to detect that the mro has changed! Thus we need to make
  2629. # sure to load the original mro so that once the class is
  2630. # rechecked, it can tell that the mro has changed.
  2631. ti._mro_refs = data["mro"]
  2632. ti.tuple_type = (
  2633. None
  2634. if data["tuple_type"] is None
  2635. else mypy.types.TupleType.deserialize(data["tuple_type"])
  2636. )
  2637. ti.typeddict_type = (
  2638. None
  2639. if data["typeddict_type"] is None
  2640. else mypy.types.TypedDictType.deserialize(data["typeddict_type"])
  2641. )
  2642. ti.metadata = data["metadata"]
  2643. ti.slots = set(data["slots"]) if data["slots"] is not None else None
  2644. ti.deletable_attributes = data["deletable_attributes"]
  2645. set_flags(ti, data["flags"])
  2646. st = data["self_type"]
  2647. ti.self_type = mypy.types.TypeVarType.deserialize(st) if st is not None else None
  2648. if data.get("dataclass_transform_spec") is not None:
  2649. ti.dataclass_transform_spec = DataclassTransformSpec.deserialize(
  2650. data["dataclass_transform_spec"]
  2651. )
  2652. return ti
  2653. class FakeInfo(TypeInfo):
  2654. __slots__ = ("msg",)
  2655. # types.py defines a single instance of this class, called types.NOT_READY.
  2656. # This instance is used as a temporary placeholder in the process of de-serialization
  2657. # of 'Instance' types. The de-serialization happens in two steps: In the first step,
  2658. # Instance.type is set to NOT_READY. In the second step (in fixup.py) it is replaced by
  2659. # an actual TypeInfo. If you see the assertion error below, then most probably something
  2660. # went wrong during the second step and an 'Instance' that raised this error was not fixed.
  2661. # Note:
  2662. # 'None' is not used as a dummy value for two reasons:
  2663. # 1. This will require around 80-100 asserts to make 'mypy --strict-optional mypy'
  2664. # pass cleanly.
  2665. # 2. If NOT_READY value is accidentally used somewhere, it will be obvious where the value
  2666. # is from, whereas a 'None' value could come from anywhere.
  2667. #
  2668. # Additionally, this serves as a more general-purpose placeholder
  2669. # for missing TypeInfos in a number of places where the excuses
  2670. # for not being Optional are a little weaker.
  2671. #
  2672. # TypeInfo defines a __bool__ method that returns False for FakeInfo
  2673. # so that it can be conveniently tested against in the same way that it
  2674. # would be if things were properly optional.
  2675. def __init__(self, msg: str) -> None:
  2676. self.msg = msg
  2677. def __getattribute__(self, attr: str) -> type:
  2678. # Handle __class__ so that isinstance still works...
  2679. if attr == "__class__":
  2680. return object.__getattribute__(self, attr) # type: ignore[no-any-return]
  2681. raise AssertionError(object.__getattribute__(self, "msg"))
  2682. VAR_NO_INFO: Final[TypeInfo] = FakeInfo("Var is lacking info")
  2683. CLASSDEF_NO_INFO: Final[TypeInfo] = FakeInfo("ClassDef is lacking info")
  2684. FUNC_NO_INFO: Final[TypeInfo] = FakeInfo("FuncBase for non-methods lack info")
  2685. class TypeAlias(SymbolNode):
  2686. """
  2687. A symbol node representing a type alias.
  2688. Type alias is a static concept, in contrast to variables with types
  2689. like Type[...]. Namely:
  2690. * type aliases
  2691. - can be used in type context (annotations)
  2692. - cannot be re-assigned
  2693. * variables with type Type[...]
  2694. - cannot be used in type context
  2695. - but can be re-assigned
  2696. An alias can be defined only by an assignment to a name (not any other lvalues).
  2697. Such assignment defines an alias by default. To define a variable,
  2698. an explicit Type[...] annotation is required. As an exception,
  2699. at non-global scope non-subscripted rvalue creates a variable even without
  2700. an annotation. This exception exists to accommodate the common use case of
  2701. class-valued attributes. See SemanticAnalyzerPass2.check_and_set_up_type_alias
  2702. for details.
  2703. Aliases can be generic. We use bound type variables for generic aliases, similar
  2704. to classes. Essentially, type aliases work as macros that expand textually.
  2705. The definition and expansion rules are following:
  2706. 1. An alias targeting a generic class without explicit variables act as
  2707. the given class (this doesn't apply to TypedDict, Tuple and Callable, which
  2708. are not proper classes but special type constructors):
  2709. A = List
  2710. AA = List[Any]
  2711. x: A # same as List[Any]
  2712. x: A[int] # same as List[int]
  2713. x: AA # same as List[Any]
  2714. x: AA[int] # Error!
  2715. C = Callable # Same as Callable[..., Any]
  2716. T = Tuple # Same as Tuple[Any, ...]
  2717. 2. An alias using explicit type variables in its rvalue expects
  2718. replacements (type arguments) for these variables. If missing, they
  2719. are treated as Any, like for other generics:
  2720. B = List[Tuple[T, T]]
  2721. x: B # same as List[Tuple[Any, Any]]
  2722. x: B[int] # same as List[Tuple[int, int]]
  2723. def f(x: B[T]) -> T: ... # without T, Any would be used here
  2724. 3. An alias can be defined using another aliases. In the definition
  2725. rvalue the Any substitution doesn't happen for top level unsubscripted
  2726. generic classes:
  2727. A = List
  2728. B = A # here A is expanded to List, _not_ List[Any],
  2729. # to match the Python runtime behaviour
  2730. x: B[int] # same as List[int]
  2731. C = List[A] # this expands to List[List[Any]]
  2732. AA = List[T]
  2733. D = AA # here AA expands to List[Any]
  2734. x: D[int] # Error!
  2735. Note: the fact that we support aliases like `A = List` means that the target
  2736. type will be initially an instance type with wrong number of type arguments.
  2737. Such instances are all fixed either during or after main semantic analysis passes.
  2738. We therefore store the difference between `List` and `List[Any]` rvalues (targets)
  2739. using the `no_args` flag. See also TypeAliasExpr.no_args.
  2740. Meaning of other fields:
  2741. target: The target type. For generic aliases contains bound type variables
  2742. as nested types (currently TypeVar and ParamSpec are supported).
  2743. _fullname: Qualified name of this type alias. This is used in particular
  2744. to track fine grained dependencies from aliases.
  2745. alias_tvars: Type variables used to define this alias.
  2746. normalized: Used to distinguish between `A = List`, and `A = list`. Both
  2747. are internally stored using `builtins.list` (because `typing.List` is
  2748. itself an alias), while the second cannot be subscripted because of
  2749. Python runtime limitation.
  2750. line and column: Line and column on the original alias definition.
  2751. eager: If True, immediately expand alias when referred to (useful for aliases
  2752. within functions that can't be looked up from the symbol table)
  2753. """
  2754. __slots__ = (
  2755. "target",
  2756. "_fullname",
  2757. "alias_tvars",
  2758. "no_args",
  2759. "normalized",
  2760. "_is_recursive",
  2761. "eager",
  2762. "tvar_tuple_index",
  2763. )
  2764. __match_args__ = ("name", "target", "alias_tvars", "no_args")
  2765. def __init__(
  2766. self,
  2767. target: mypy.types.Type,
  2768. fullname: str,
  2769. line: int,
  2770. column: int,
  2771. *,
  2772. alias_tvars: list[mypy.types.TypeVarLikeType] | None = None,
  2773. no_args: bool = False,
  2774. normalized: bool = False,
  2775. eager: bool = False,
  2776. ) -> None:
  2777. self._fullname = fullname
  2778. self.target = target
  2779. if alias_tvars is None:
  2780. alias_tvars = []
  2781. self.alias_tvars = alias_tvars
  2782. self.no_args = no_args
  2783. self.normalized = normalized
  2784. # This attribute is manipulated by TypeAliasType. If non-None,
  2785. # it is the cached value.
  2786. self._is_recursive: bool | None = None
  2787. self.eager = eager
  2788. self.tvar_tuple_index = None
  2789. for i, t in enumerate(alias_tvars):
  2790. if isinstance(t, mypy.types.TypeVarTupleType):
  2791. self.tvar_tuple_index = i
  2792. super().__init__(line, column)
  2793. @classmethod
  2794. def from_tuple_type(cls, info: TypeInfo) -> TypeAlias:
  2795. """Generate an alias to the tuple type described by a given TypeInfo.
  2796. NOTE: this doesn't set type alias type variables (for generic tuple types),
  2797. they must be set by the caller (when fully analyzed).
  2798. """
  2799. assert info.tuple_type
  2800. # TODO: is it possible to refactor this to set the correct type vars here?
  2801. return TypeAlias(
  2802. info.tuple_type.copy_modified(fallback=mypy.types.Instance(info, info.defn.type_vars)),
  2803. info.fullname,
  2804. info.line,
  2805. info.column,
  2806. )
  2807. @classmethod
  2808. def from_typeddict_type(cls, info: TypeInfo) -> TypeAlias:
  2809. """Generate an alias to the TypedDict type described by a given TypeInfo.
  2810. NOTE: this doesn't set type alias type variables (for generic TypedDicts),
  2811. they must be set by the caller (when fully analyzed).
  2812. """
  2813. assert info.typeddict_type
  2814. # TODO: is it possible to refactor this to set the correct type vars here?
  2815. return TypeAlias(
  2816. info.typeddict_type.copy_modified(
  2817. fallback=mypy.types.Instance(info, info.defn.type_vars)
  2818. ),
  2819. info.fullname,
  2820. info.line,
  2821. info.column,
  2822. )
  2823. @property
  2824. def name(self) -> str:
  2825. return self._fullname.split(".")[-1]
  2826. @property
  2827. def fullname(self) -> str:
  2828. return self._fullname
  2829. @property
  2830. def has_param_spec_type(self) -> bool:
  2831. return any(isinstance(v, mypy.types.ParamSpecType) for v in self.alias_tvars)
  2832. def serialize(self) -> JsonDict:
  2833. data: JsonDict = {
  2834. ".class": "TypeAlias",
  2835. "fullname": self._fullname,
  2836. "target": self.target.serialize(),
  2837. "alias_tvars": [v.serialize() for v in self.alias_tvars],
  2838. "no_args": self.no_args,
  2839. "normalized": self.normalized,
  2840. "line": self.line,
  2841. "column": self.column,
  2842. }
  2843. return data
  2844. def accept(self, visitor: NodeVisitor[T]) -> T:
  2845. return visitor.visit_type_alias(self)
  2846. @classmethod
  2847. def deserialize(cls, data: JsonDict) -> TypeAlias:
  2848. assert data[".class"] == "TypeAlias"
  2849. fullname = data["fullname"]
  2850. alias_tvars = [mypy.types.deserialize_type(v) for v in data["alias_tvars"]]
  2851. assert all(isinstance(t, mypy.types.TypeVarLikeType) for t in alias_tvars)
  2852. target = mypy.types.deserialize_type(data["target"])
  2853. no_args = data["no_args"]
  2854. normalized = data["normalized"]
  2855. line = data["line"]
  2856. column = data["column"]
  2857. return cls(
  2858. target,
  2859. fullname,
  2860. line,
  2861. column,
  2862. alias_tvars=cast(List[mypy.types.TypeVarLikeType], alias_tvars),
  2863. no_args=no_args,
  2864. normalized=normalized,
  2865. )
  2866. class PlaceholderNode(SymbolNode):
  2867. """Temporary symbol node that will later become a real SymbolNode.
  2868. These are only present during semantic analysis when using the new
  2869. semantic analyzer. These are created if some essential dependencies
  2870. of a definition are not yet complete.
  2871. A typical use is for names imported from a module which is still
  2872. incomplete (within an import cycle):
  2873. from m import f # Initially may create PlaceholderNode
  2874. This is particularly important if the imported shadows a name from
  2875. an enclosing scope or builtins:
  2876. from m import int # Placeholder avoids mixups with builtins.int
  2877. Another case where this is useful is when there is another definition
  2878. or assignment:
  2879. from m import f
  2880. def f() -> None: ...
  2881. In the above example, the presence of PlaceholderNode allows us to
  2882. handle the second definition as a redefinition.
  2883. They are also used to create PlaceholderType instances for types
  2884. that refer to incomplete types. Example:
  2885. class C(Sequence[C]): ...
  2886. We create a PlaceholderNode (with becomes_typeinfo=True) for C so
  2887. that the type C in Sequence[C] can be bound.
  2888. Attributes:
  2889. fullname: Full name of the PlaceholderNode.
  2890. node: AST node that contains the definition that caused this to
  2891. be created. This is useful for tracking order of incomplete definitions
  2892. and for debugging.
  2893. becomes_typeinfo: If True, this refers something that could later
  2894. become a TypeInfo. It can't be used with type variables, in
  2895. particular, as this would cause issues with class type variable
  2896. detection.
  2897. The long-term purpose of placeholder nodes/types is to evolve into
  2898. something that can support general recursive types.
  2899. """
  2900. __slots__ = ("_fullname", "node", "becomes_typeinfo")
  2901. def __init__(
  2902. self, fullname: str, node: Node, line: int, *, becomes_typeinfo: bool = False
  2903. ) -> None:
  2904. self._fullname = fullname
  2905. self.node = node
  2906. self.becomes_typeinfo = becomes_typeinfo
  2907. self.line = line
  2908. @property
  2909. def name(self) -> str:
  2910. return self._fullname.split(".")[-1]
  2911. @property
  2912. def fullname(self) -> str:
  2913. return self._fullname
  2914. def serialize(self) -> JsonDict:
  2915. assert False, "PlaceholderNode can't be serialized"
  2916. def accept(self, visitor: NodeVisitor[T]) -> T:
  2917. return visitor.visit_placeholder_node(self)
  2918. class SymbolTableNode:
  2919. """Description of a name binding in a symbol table.
  2920. These are only used as values in module (global), function (local)
  2921. and class symbol tables (see SymbolTable). The name that is bound is
  2922. the key in SymbolTable.
  2923. Symbol tables don't contain direct references to AST nodes primarily
  2924. because there can be multiple symbol table references to a single
  2925. AST node (due to imports and aliases), and different references can
  2926. behave differently. This class describes the unique properties of
  2927. each reference.
  2928. The most fundamental attribute is 'node', which is the AST node that
  2929. the name refers to.
  2930. The kind is usually one of LDEF, GDEF or MDEF, depending on the scope
  2931. of the definition. These three kinds can usually be used
  2932. interchangeably and the difference between local, global and class
  2933. scopes is mostly descriptive, with no semantic significance.
  2934. However, some tools that consume mypy ASTs may care about these so
  2935. they should be correct.
  2936. Attributes:
  2937. node: AST node of definition. Among others, this can be one of
  2938. FuncDef, Var, TypeInfo, TypeVarExpr or MypyFile -- or None
  2939. for cross_ref that hasn't been fixed up yet.
  2940. kind: Kind of node. Possible values:
  2941. - LDEF: local definition
  2942. - GDEF: global (module-level) definition
  2943. - MDEF: class member definition
  2944. - UNBOUND_IMPORTED: temporary kind for imported names (we
  2945. don't know the final kind yet)
  2946. module_public: If False, this name won't be imported via
  2947. 'from <module> import *'. This has no effect on names within
  2948. classes.
  2949. module_hidden: If True, the name will be never exported (needed for
  2950. stub files)
  2951. cross_ref: For deserialized MypyFile nodes, the referenced module
  2952. name; for other nodes, optionally the name of the referenced object.
  2953. implicit: Was this defined by assignment to self attribute?
  2954. plugin_generated: Was this symbol generated by a plugin?
  2955. (And therefore needs to be removed in aststrip.)
  2956. no_serialize: Do not serialize this node if True. This is used to prevent
  2957. keys in the cache that refer to modules on which this file does not
  2958. depend. Currently this can happen if there is a module not in build
  2959. used e.g. like this:
  2960. import a.b.c # type: ignore
  2961. This will add a submodule symbol to parent module `a` symbol table,
  2962. but `a.b` is _not_ added as its dependency. Therefore, we should
  2963. not serialize these symbols as they may not be found during fixup
  2964. phase, instead they will be re-added during subsequent patch parents
  2965. phase.
  2966. TODO: Refactor build.py to make dependency tracking more transparent
  2967. and/or refactor look-up functions to not require parent patching.
  2968. NOTE: No other attributes should be added to this class unless they
  2969. are shared by all node kinds.
  2970. """
  2971. __slots__ = (
  2972. "kind",
  2973. "node",
  2974. "module_public",
  2975. "module_hidden",
  2976. "cross_ref",
  2977. "implicit",
  2978. "plugin_generated",
  2979. "no_serialize",
  2980. )
  2981. def __init__(
  2982. self,
  2983. kind: int,
  2984. node: SymbolNode | None,
  2985. module_public: bool = True,
  2986. implicit: bool = False,
  2987. module_hidden: bool = False,
  2988. *,
  2989. plugin_generated: bool = False,
  2990. no_serialize: bool = False,
  2991. ) -> None:
  2992. self.kind = kind
  2993. self.node = node
  2994. self.module_public = module_public
  2995. self.implicit = implicit
  2996. self.module_hidden = module_hidden
  2997. self.cross_ref: str | None = None
  2998. self.plugin_generated = plugin_generated
  2999. self.no_serialize = no_serialize
  3000. @property
  3001. def fullname(self) -> str | None:
  3002. if self.node is not None:
  3003. return self.node.fullname
  3004. else:
  3005. return None
  3006. @property
  3007. def type(self) -> mypy.types.Type | None:
  3008. node = self.node
  3009. if isinstance(node, (Var, SYMBOL_FUNCBASE_TYPES)) and node.type is not None:
  3010. return node.type
  3011. elif isinstance(node, Decorator):
  3012. return node.var.type
  3013. else:
  3014. return None
  3015. def copy(self) -> SymbolTableNode:
  3016. new = SymbolTableNode(
  3017. self.kind, self.node, self.module_public, self.implicit, self.module_hidden
  3018. )
  3019. new.cross_ref = self.cross_ref
  3020. return new
  3021. def __str__(self) -> str:
  3022. s = f"{node_kinds[self.kind]}/{short_type(self.node)}"
  3023. if isinstance(self.node, SymbolNode):
  3024. s += f" ({self.node.fullname})"
  3025. # Include declared type of variables and functions.
  3026. if self.type is not None:
  3027. s += f" : {self.type}"
  3028. return s
  3029. def serialize(self, prefix: str, name: str) -> JsonDict:
  3030. """Serialize a SymbolTableNode.
  3031. Args:
  3032. prefix: full name of the containing module or class; or None
  3033. name: name of this object relative to the containing object
  3034. """
  3035. data: JsonDict = {".class": "SymbolTableNode", "kind": node_kinds[self.kind]}
  3036. if self.module_hidden:
  3037. data["module_hidden"] = True
  3038. if not self.module_public:
  3039. data["module_public"] = False
  3040. if self.implicit:
  3041. data["implicit"] = True
  3042. if self.plugin_generated:
  3043. data["plugin_generated"] = True
  3044. if isinstance(self.node, MypyFile):
  3045. data["cross_ref"] = self.node.fullname
  3046. else:
  3047. assert self.node is not None, f"{prefix}:{name}"
  3048. if prefix is not None:
  3049. fullname = self.node.fullname
  3050. if (
  3051. "." in fullname
  3052. and fullname != prefix + "." + name
  3053. and not (isinstance(self.node, Var) and self.node.from_module_getattr)
  3054. ):
  3055. assert not isinstance(
  3056. self.node, PlaceholderNode
  3057. ), f"Definition of {fullname} is unexpectedly incomplete"
  3058. data["cross_ref"] = fullname
  3059. return data
  3060. data["node"] = self.node.serialize()
  3061. return data
  3062. @classmethod
  3063. def deserialize(cls, data: JsonDict) -> SymbolTableNode:
  3064. assert data[".class"] == "SymbolTableNode"
  3065. kind = inverse_node_kinds[data["kind"]]
  3066. if "cross_ref" in data:
  3067. # This will be fixed up later.
  3068. stnode = SymbolTableNode(kind, None)
  3069. stnode.cross_ref = data["cross_ref"]
  3070. else:
  3071. assert "node" in data, data
  3072. node = SymbolNode.deserialize(data["node"])
  3073. stnode = SymbolTableNode(kind, node)
  3074. if "module_hidden" in data:
  3075. stnode.module_hidden = data["module_hidden"]
  3076. if "module_public" in data:
  3077. stnode.module_public = data["module_public"]
  3078. if "implicit" in data:
  3079. stnode.implicit = data["implicit"]
  3080. if "plugin_generated" in data:
  3081. stnode.plugin_generated = data["plugin_generated"]
  3082. return stnode
  3083. class SymbolTable(Dict[str, SymbolTableNode]):
  3084. """Static representation of a namespace dictionary.
  3085. This is used for module, class and function namespaces.
  3086. """
  3087. __slots__ = ()
  3088. def __str__(self) -> str:
  3089. a: list[str] = []
  3090. for key, value in self.items():
  3091. # Filter out the implicit import of builtins.
  3092. if isinstance(value, SymbolTableNode):
  3093. if (
  3094. value.fullname != "builtins"
  3095. and (value.fullname or "").split(".")[-1] not in implicit_module_attrs
  3096. ):
  3097. a.append(" " + str(key) + " : " + str(value))
  3098. else:
  3099. a.append(" <invalid item>")
  3100. a = sorted(a)
  3101. a.insert(0, "SymbolTable(")
  3102. a[-1] += ")"
  3103. return "\n".join(a)
  3104. def copy(self) -> SymbolTable:
  3105. return SymbolTable([(key, node.copy()) for key, node in self.items()])
  3106. def serialize(self, fullname: str) -> JsonDict:
  3107. data: JsonDict = {".class": "SymbolTable"}
  3108. for key, value in self.items():
  3109. # Skip __builtins__: it's a reference to the builtins
  3110. # module that gets added to every module by
  3111. # SemanticAnalyzerPass2.visit_file(), but it shouldn't be
  3112. # accessed by users of the module.
  3113. if key == "__builtins__" or value.no_serialize:
  3114. continue
  3115. data[key] = value.serialize(fullname, key)
  3116. return data
  3117. @classmethod
  3118. def deserialize(cls, data: JsonDict) -> SymbolTable:
  3119. assert data[".class"] == "SymbolTable"
  3120. st = SymbolTable()
  3121. for key, value in data.items():
  3122. if key != ".class":
  3123. st[key] = SymbolTableNode.deserialize(value)
  3124. return st
  3125. class DataclassTransformSpec:
  3126. """Specifies how a dataclass-like transform should be applied. The fields here are based on the
  3127. parameters accepted by `typing.dataclass_transform`."""
  3128. __slots__ = (
  3129. "eq_default",
  3130. "order_default",
  3131. "kw_only_default",
  3132. "frozen_default",
  3133. "field_specifiers",
  3134. )
  3135. def __init__(
  3136. self,
  3137. *,
  3138. eq_default: bool | None = None,
  3139. order_default: bool | None = None,
  3140. kw_only_default: bool | None = None,
  3141. field_specifiers: tuple[str, ...] | None = None,
  3142. # Specified outside of PEP 681:
  3143. # frozen_default was added to CPythonin https://github.com/python/cpython/pull/99958 citing
  3144. # positive discussion in typing-sig
  3145. frozen_default: bool | None = None,
  3146. ):
  3147. self.eq_default = eq_default if eq_default is not None else True
  3148. self.order_default = order_default if order_default is not None else False
  3149. self.kw_only_default = kw_only_default if kw_only_default is not None else False
  3150. self.frozen_default = frozen_default if frozen_default is not None else False
  3151. self.field_specifiers = field_specifiers if field_specifiers is not None else ()
  3152. def serialize(self) -> JsonDict:
  3153. return {
  3154. "eq_default": self.eq_default,
  3155. "order_default": self.order_default,
  3156. "kw_only_default": self.kw_only_default,
  3157. "frozen_default": self.frozen_default,
  3158. "field_specifiers": list(self.field_specifiers),
  3159. }
  3160. @classmethod
  3161. def deserialize(cls, data: JsonDict) -> DataclassTransformSpec:
  3162. return DataclassTransformSpec(
  3163. eq_default=data.get("eq_default"),
  3164. order_default=data.get("order_default"),
  3165. kw_only_default=data.get("kw_only_default"),
  3166. frozen_default=data.get("frozen_default"),
  3167. field_specifiers=tuple(data.get("field_specifiers", [])),
  3168. )
  3169. def get_flags(node: Node, names: list[str]) -> list[str]:
  3170. return [name for name in names if getattr(node, name)]
  3171. def set_flags(node: Node, flags: list[str]) -> None:
  3172. for name in flags:
  3173. setattr(node, name, True)
  3174. def get_member_expr_fullname(expr: MemberExpr) -> str | None:
  3175. """Return the qualified name representation of a member expression.
  3176. Return a string of form foo.bar, foo.bar.baz, or similar, or None if the
  3177. argument cannot be represented in this form.
  3178. """
  3179. initial: str | None = None
  3180. if isinstance(expr.expr, NameExpr):
  3181. initial = expr.expr.name
  3182. elif isinstance(expr.expr, MemberExpr):
  3183. initial = get_member_expr_fullname(expr.expr)
  3184. else:
  3185. return None
  3186. return f"{initial}.{expr.name}"
  3187. deserialize_map: Final = {
  3188. key: obj.deserialize
  3189. for key, obj in globals().items()
  3190. if type(obj) is not FakeInfo
  3191. and isinstance(obj, type)
  3192. and issubclass(obj, SymbolNode)
  3193. and obj is not SymbolNode
  3194. }
  3195. def check_arg_kinds(
  3196. arg_kinds: list[ArgKind], nodes: list[T], fail: Callable[[str, T], None]
  3197. ) -> None:
  3198. is_var_arg = False
  3199. is_kw_arg = False
  3200. seen_named = False
  3201. seen_opt = False
  3202. for kind, node in zip(arg_kinds, nodes):
  3203. if kind == ARG_POS:
  3204. if is_var_arg or is_kw_arg or seen_named or seen_opt:
  3205. fail(
  3206. "Required positional args may not appear after default, named or var args",
  3207. node,
  3208. )
  3209. break
  3210. elif kind == ARG_OPT:
  3211. if is_var_arg or is_kw_arg or seen_named:
  3212. fail("Positional default args may not appear after named or var args", node)
  3213. break
  3214. seen_opt = True
  3215. elif kind == ARG_STAR:
  3216. if is_var_arg or is_kw_arg or seen_named:
  3217. fail("Var args may not appear after named or var args", node)
  3218. break
  3219. is_var_arg = True
  3220. elif kind == ARG_NAMED or kind == ARG_NAMED_OPT:
  3221. seen_named = True
  3222. if is_kw_arg:
  3223. fail("A **kwargs argument must be the last argument", node)
  3224. break
  3225. elif kind == ARG_STAR2:
  3226. if is_kw_arg:
  3227. fail("You may only have one **kwargs argument", node)
  3228. break
  3229. is_kw_arg = True
  3230. def check_arg_names(
  3231. names: Sequence[str | None],
  3232. nodes: list[T],
  3233. fail: Callable[[str, T], None],
  3234. description: str = "function definition",
  3235. ) -> None:
  3236. seen_names: set[str | None] = set()
  3237. for name, node in zip(names, nodes):
  3238. if name is not None and name in seen_names:
  3239. fail(f'Duplicate argument "{name}" in {description}', node)
  3240. break
  3241. seen_names.add(name)
  3242. def is_class_var(expr: NameExpr) -> bool:
  3243. """Return whether the expression is ClassVar[...]"""
  3244. if isinstance(expr.node, Var):
  3245. return expr.node.is_classvar
  3246. return False
  3247. def is_final_node(node: SymbolNode | None) -> bool:
  3248. """Check whether `node` corresponds to a final attribute."""
  3249. return isinstance(node, (Var, FuncDef, OverloadedFuncDef, Decorator)) and node.is_final
  3250. def local_definitions(
  3251. names: SymbolTable, name_prefix: str, info: TypeInfo | None = None
  3252. ) -> Iterator[Definition]:
  3253. """Iterate over local definitions (not imported) in a symbol table.
  3254. Recursively iterate over class members and nested classes.
  3255. """
  3256. # TODO: What should the name be? Or maybe remove it?
  3257. for name, symnode in names.items():
  3258. shortname = name
  3259. if "-redef" in name:
  3260. # Restore original name from mangled name of multiply defined function
  3261. shortname = name.split("-redef")[0]
  3262. fullname = name_prefix + "." + shortname
  3263. node = symnode.node
  3264. if node and node.fullname == fullname:
  3265. yield fullname, symnode, info
  3266. if isinstance(node, TypeInfo):
  3267. yield from local_definitions(node.names, fullname, node)