semanal.py 290 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940
  1. """The semantic analyzer.
  2. Bind names to definitions and do various other simple consistency
  3. checks. Populate symbol tables. The semantic analyzer also detects
  4. special forms which reuse generic syntax such as NamedTuple and
  5. cast(). Multiple analysis iterations may be needed to analyze forward
  6. references and import cycles. Each iteration "fills in" additional
  7. bindings and references until everything has been bound.
  8. For example, consider this program:
  9. x = 1
  10. y = x
  11. Here semantic analysis would detect that the assignment 'x = 1'
  12. defines a new variable, the type of which is to be inferred (in a
  13. later pass; type inference or type checking is not part of semantic
  14. analysis). Also, it would bind both references to 'x' to the same
  15. module-level variable (Var) node. The second assignment would also
  16. be analyzed, and the type of 'y' marked as being inferred.
  17. Semantic analysis of types is implemented in typeanal.py.
  18. See semanal_main.py for the top-level logic.
  19. Some important properties:
  20. * After semantic analysis is complete, no PlaceholderNode and
  21. PlaceholderType instances should remain. During semantic analysis,
  22. if we encounter one of these, the current target should be deferred.
  23. * A TypeInfo is only created once we know certain basic information about
  24. a type, such as the MRO, existence of a Tuple base class (e.g., for named
  25. tuples), and whether we have a TypedDict. We use a temporary
  26. PlaceholderNode node in the symbol table if some such information is
  27. missing.
  28. * For assignments, we only add a non-placeholder symbol table entry once
  29. we know the sort of thing being defined (variable, NamedTuple, type alias,
  30. etc.).
  31. * Every part of the analysis step must support multiple iterations over
  32. the same AST nodes, and each iteration must be able to fill in arbitrary
  33. things that were missing or incomplete in previous iterations.
  34. * Changes performed by the analysis need to be reversible, since mypy
  35. daemon strips and reuses existing ASTs (to improve performance and/or
  36. reduce memory use).
  37. """
  38. from __future__ import annotations
  39. from contextlib import contextmanager
  40. from typing import Any, Callable, Collection, Final, Iterable, Iterator, List, TypeVar, cast
  41. from typing_extensions import TypeAlias as _TypeAlias
  42. from mypy import errorcodes as codes, message_registry
  43. from mypy.constant_fold import constant_fold_expr
  44. from mypy.errorcodes import ErrorCode
  45. from mypy.errors import Errors, report_internal_error
  46. from mypy.exprtotype import TypeTranslationError, expr_to_unanalyzed_type
  47. from mypy.messages import (
  48. SUGGESTED_TEST_FIXTURES,
  49. TYPES_FOR_UNIMPORTED_HINTS,
  50. MessageBuilder,
  51. best_matches,
  52. pretty_seq,
  53. )
  54. from mypy.mro import MroError, calculate_mro
  55. from mypy.nodes import (
  56. ARG_NAMED,
  57. ARG_POS,
  58. ARG_STAR,
  59. ARG_STAR2,
  60. CONTRAVARIANT,
  61. COVARIANT,
  62. GDEF,
  63. IMPLICITLY_ABSTRACT,
  64. INVARIANT,
  65. IS_ABSTRACT,
  66. LDEF,
  67. MDEF,
  68. NOT_ABSTRACT,
  69. REVEAL_LOCALS,
  70. REVEAL_TYPE,
  71. RUNTIME_PROTOCOL_DECOS,
  72. ArgKind,
  73. AssertStmt,
  74. AssertTypeExpr,
  75. AssignmentExpr,
  76. AssignmentStmt,
  77. AwaitExpr,
  78. Block,
  79. BreakStmt,
  80. CallExpr,
  81. CastExpr,
  82. ClassDef,
  83. ComparisonExpr,
  84. ConditionalExpr,
  85. Context,
  86. ContinueStmt,
  87. DataclassTransformSpec,
  88. Decorator,
  89. DelStmt,
  90. DictExpr,
  91. DictionaryComprehension,
  92. EllipsisExpr,
  93. EnumCallExpr,
  94. Expression,
  95. ExpressionStmt,
  96. FakeExpression,
  97. ForStmt,
  98. FuncBase,
  99. FuncDef,
  100. FuncItem,
  101. GeneratorExpr,
  102. GlobalDecl,
  103. IfStmt,
  104. Import,
  105. ImportAll,
  106. ImportBase,
  107. ImportFrom,
  108. IndexExpr,
  109. LambdaExpr,
  110. ListComprehension,
  111. ListExpr,
  112. Lvalue,
  113. MatchStmt,
  114. MemberExpr,
  115. MypyFile,
  116. NamedTupleExpr,
  117. NameExpr,
  118. Node,
  119. NonlocalDecl,
  120. OperatorAssignmentStmt,
  121. OpExpr,
  122. OverloadedFuncDef,
  123. OverloadPart,
  124. ParamSpecExpr,
  125. PassStmt,
  126. PlaceholderNode,
  127. PromoteExpr,
  128. RaiseStmt,
  129. RefExpr,
  130. ReturnStmt,
  131. RevealExpr,
  132. SetComprehension,
  133. SetExpr,
  134. SliceExpr,
  135. StarExpr,
  136. Statement,
  137. StrExpr,
  138. SuperExpr,
  139. SymbolNode,
  140. SymbolTable,
  141. SymbolTableNode,
  142. TempNode,
  143. TryStmt,
  144. TupleExpr,
  145. TypeAlias,
  146. TypeAliasExpr,
  147. TypeApplication,
  148. TypedDictExpr,
  149. TypeInfo,
  150. TypeVarExpr,
  151. TypeVarLikeExpr,
  152. TypeVarTupleExpr,
  153. UnaryExpr,
  154. Var,
  155. WhileStmt,
  156. WithStmt,
  157. YieldExpr,
  158. YieldFromExpr,
  159. get_member_expr_fullname,
  160. get_nongen_builtins,
  161. implicit_module_attrs,
  162. is_final_node,
  163. type_aliases,
  164. type_aliases_source_versions,
  165. typing_extensions_aliases,
  166. )
  167. from mypy.options import TYPE_VAR_TUPLE, Options
  168. from mypy.patterns import (
  169. AsPattern,
  170. ClassPattern,
  171. MappingPattern,
  172. OrPattern,
  173. SequencePattern,
  174. StarredPattern,
  175. ValuePattern,
  176. )
  177. from mypy.plugin import (
  178. ClassDefContext,
  179. DynamicClassDefContext,
  180. Plugin,
  181. SemanticAnalyzerPluginInterface,
  182. )
  183. from mypy.plugins import dataclasses as dataclasses_plugin
  184. from mypy.reachability import (
  185. ALWAYS_FALSE,
  186. ALWAYS_TRUE,
  187. MYPY_FALSE,
  188. MYPY_TRUE,
  189. infer_condition_value,
  190. infer_reachability_of_if_statement,
  191. infer_reachability_of_match_statement,
  192. )
  193. from mypy.scope import Scope
  194. from mypy.semanal_enum import EnumCallAnalyzer
  195. from mypy.semanal_namedtuple import NamedTupleAnalyzer
  196. from mypy.semanal_newtype import NewTypeAnalyzer
  197. from mypy.semanal_shared import (
  198. ALLOW_INCOMPATIBLE_OVERRIDE,
  199. PRIORITY_FALLBACKS,
  200. SemanticAnalyzerInterface,
  201. calculate_tuple_fallback,
  202. find_dataclass_transform_spec,
  203. has_placeholder,
  204. parse_bool,
  205. require_bool_literal_argument,
  206. set_callable_name as set_callable_name,
  207. )
  208. from mypy.semanal_typeddict import TypedDictAnalyzer
  209. from mypy.tvar_scope import TypeVarLikeScope
  210. from mypy.typeanal import (
  211. SELF_TYPE_NAMES,
  212. TypeAnalyser,
  213. TypeVarLikeList,
  214. TypeVarLikeQuery,
  215. analyze_type_alias,
  216. check_for_explicit_any,
  217. detect_diverging_alias,
  218. find_self_type,
  219. fix_instance_types,
  220. has_any_from_unimported_type,
  221. no_subscript_builtin_alias,
  222. type_constructors,
  223. )
  224. from mypy.typeops import function_type, get_type_vars, try_getting_str_literals_from_type
  225. from mypy.types import (
  226. ASSERT_TYPE_NAMES,
  227. DATACLASS_TRANSFORM_NAMES,
  228. FINAL_DECORATOR_NAMES,
  229. FINAL_TYPE_NAMES,
  230. NEVER_NAMES,
  231. OVERLOAD_NAMES,
  232. OVERRIDE_DECORATOR_NAMES,
  233. PROTOCOL_NAMES,
  234. REVEAL_TYPE_NAMES,
  235. TPDICT_NAMES,
  236. TYPE_ALIAS_NAMES,
  237. TYPED_NAMEDTUPLE_NAMES,
  238. AnyType,
  239. CallableType,
  240. FunctionLike,
  241. Instance,
  242. LiteralType,
  243. NoneType,
  244. Overloaded,
  245. Parameters,
  246. ParamSpecType,
  247. PlaceholderType,
  248. ProperType,
  249. TrivialSyntheticTypeTranslator,
  250. TupleType,
  251. Type,
  252. TypeAliasType,
  253. TypedDictType,
  254. TypeOfAny,
  255. TypeType,
  256. TypeVarLikeType,
  257. TypeVarTupleType,
  258. TypeVarType,
  259. UnboundType,
  260. UnpackType,
  261. get_proper_type,
  262. get_proper_types,
  263. is_named_instance,
  264. remove_dups,
  265. )
  266. from mypy.types_utils import is_invalid_recursive_alias, store_argument_type
  267. from mypy.typevars import fill_typevars
  268. from mypy.util import (
  269. correct_relative_import,
  270. is_dunder,
  271. is_typeshed_file,
  272. module_prefix,
  273. unmangle,
  274. unnamed_function,
  275. )
  276. from mypy.visitor import NodeVisitor
  277. T = TypeVar("T")
  278. FUTURE_IMPORTS: Final = {
  279. "__future__.nested_scopes": "nested_scopes",
  280. "__future__.generators": "generators",
  281. "__future__.division": "division",
  282. "__future__.absolute_import": "absolute_import",
  283. "__future__.with_statement": "with_statement",
  284. "__future__.print_function": "print_function",
  285. "__future__.unicode_literals": "unicode_literals",
  286. "__future__.barry_as_FLUFL": "barry_as_FLUFL",
  287. "__future__.generator_stop": "generator_stop",
  288. "__future__.annotations": "annotations",
  289. }
  290. # Special cased built-in classes that are needed for basic functionality and need to be
  291. # available very early on.
  292. CORE_BUILTIN_CLASSES: Final = ["object", "bool", "function"]
  293. # Used for tracking incomplete references
  294. Tag: _TypeAlias = int
  295. class SemanticAnalyzer(
  296. NodeVisitor[None], SemanticAnalyzerInterface, SemanticAnalyzerPluginInterface
  297. ):
  298. """Semantically analyze parsed mypy files.
  299. The analyzer binds names and does various consistency checks for an
  300. AST. Note that type checking is performed as a separate pass.
  301. """
  302. __deletable__ = ["patches", "options", "cur_mod_node"]
  303. # Module name space
  304. modules: dict[str, MypyFile]
  305. # Global name space for current module
  306. globals: SymbolTable
  307. # Names declared using "global" (separate set for each scope)
  308. global_decls: list[set[str]]
  309. # Names declared using "nonlocal" (separate set for each scope)
  310. nonlocal_decls: list[set[str]]
  311. # Local names of function scopes; None for non-function scopes.
  312. locals: list[SymbolTable | None]
  313. # Whether each scope is a comprehension scope.
  314. is_comprehension_stack: list[bool]
  315. # Nested block depths of scopes
  316. block_depth: list[int]
  317. # TypeInfo of directly enclosing class (or None)
  318. _type: TypeInfo | None = None
  319. # Stack of outer classes (the second tuple item contains tvars).
  320. type_stack: list[TypeInfo | None]
  321. # Type variables bound by the current scope, be it class or function
  322. tvar_scope: TypeVarLikeScope
  323. # Per-module options
  324. options: Options
  325. # Stack of functions being analyzed
  326. function_stack: list[FuncItem]
  327. # Set to True if semantic analysis defines a name, or replaces a
  328. # placeholder definition. If some iteration makes no progress,
  329. # there can be at most one additional final iteration (see below).
  330. progress = False
  331. deferred = False # Set to true if another analysis pass is needed
  332. incomplete = False # Set to true if current module namespace is missing things
  333. # Is this the final iteration of semantic analysis (where we report
  334. # unbound names due to cyclic definitions and should not defer)?
  335. _final_iteration = False
  336. # These names couldn't be added to the symbol table due to incomplete deps.
  337. # Note that missing names are per module, _not_ per namespace. This means that e.g.
  338. # a missing name at global scope will block adding same name at a class scope.
  339. # This should not affect correctness and is purely a performance issue,
  340. # since it can cause unnecessary deferrals. These are represented as
  341. # PlaceholderNodes in the symbol table. We use this to ensure that the first
  342. # definition takes precedence even if it's incomplete.
  343. #
  344. # Note that a star import adds a special name '*' to the set, this blocks
  345. # adding _any_ names in the current file.
  346. missing_names: list[set[str]]
  347. # Callbacks that will be called after semantic analysis to tweak things.
  348. patches: list[tuple[int, Callable[[], None]]]
  349. loop_depth: list[int] # Depth of breakable loops
  350. cur_mod_id = "" # Current module id (or None) (phase 2)
  351. _is_stub_file = False # Are we analyzing a stub file?
  352. _is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
  353. imports: set[str] # Imported modules (during phase 2 analysis)
  354. # Note: some imports (and therefore dependencies) might
  355. # not be found in phase 1, for example due to * imports.
  356. errors: Errors # Keeps track of generated errors
  357. plugin: Plugin # Mypy plugin for special casing of library features
  358. statement: Statement | None = None # Statement/definition being analyzed
  359. # Mapping from 'async def' function definitions to their return type wrapped as a
  360. # 'Coroutine[Any, Any, T]'. Used to keep track of whether a function definition's
  361. # return type has already been wrapped, by checking if the function definition's
  362. # type is stored in this mapping and that it still matches.
  363. wrapped_coro_return_types: dict[FuncDef, Type] = {}
  364. def __init__(
  365. self,
  366. modules: dict[str, MypyFile],
  367. missing_modules: set[str],
  368. incomplete_namespaces: set[str],
  369. errors: Errors,
  370. plugin: Plugin,
  371. ) -> None:
  372. """Construct semantic analyzer.
  373. We reuse the same semantic analyzer instance across multiple modules.
  374. Args:
  375. modules: Global modules dictionary
  376. missing_modules: Modules that could not be imported encountered so far
  377. incomplete_namespaces: Namespaces that are being populated during semantic analysis
  378. (can contain modules and classes within the current SCC; mutated by the caller)
  379. errors: Report analysis errors using this instance
  380. """
  381. self.locals = [None]
  382. self.is_comprehension_stack = [False]
  383. # Saved namespaces from previous iteration. Every top-level function/method body is
  384. # analyzed in several iterations until all names are resolved. We need to save
  385. # the local namespaces for the top level function and all nested functions between
  386. # these iterations. See also semanal_main.process_top_level_function().
  387. self.saved_locals: dict[
  388. FuncItem | GeneratorExpr | DictionaryComprehension, SymbolTable
  389. ] = {}
  390. self.imports = set()
  391. self._type = None
  392. self.type_stack = []
  393. # Are the namespaces of classes being processed complete?
  394. self.incomplete_type_stack: list[bool] = []
  395. self.tvar_scope = TypeVarLikeScope()
  396. self.function_stack = []
  397. self.block_depth = [0]
  398. self.loop_depth = [0]
  399. self.errors = errors
  400. self.modules = modules
  401. self.msg = MessageBuilder(errors, modules)
  402. self.missing_modules = missing_modules
  403. self.missing_names = [set()]
  404. # These namespaces are still in process of being populated. If we encounter a
  405. # missing name in these namespaces, we need to defer the current analysis target,
  406. # since it's possible that the name will be there once the namespace is complete.
  407. self.incomplete_namespaces = incomplete_namespaces
  408. self.all_exports: list[str] = []
  409. # Map from module id to list of explicitly exported names (i.e. names in __all__).
  410. self.export_map: dict[str, list[str]] = {}
  411. self.plugin = plugin
  412. # If True, process function definitions. If False, don't. This is used
  413. # for processing module top levels in fine-grained incremental mode.
  414. self.recurse_into_functions = True
  415. self.scope = Scope()
  416. # Trace line numbers for every file where deferral happened during analysis of
  417. # current SCC or top-level function.
  418. self.deferral_debug_context: list[tuple[str, int]] = []
  419. # This is needed to properly support recursive type aliases. The problem is that
  420. # Foo[Bar] could mean three things depending on context: a target for type alias,
  421. # a normal index expression (including enum index), or a type application.
  422. # The latter is particularly problematic as it can falsely create incomplete
  423. # refs while analysing rvalues of type aliases. To avoid this we first analyse
  424. # rvalues while temporarily setting this to True.
  425. self.basic_type_applications = False
  426. # Used to temporarily enable unbound type variables in some contexts. Namely,
  427. # in base class expressions, and in right hand sides of type aliases. Do not add
  428. # new uses of this, as this may cause leaking `UnboundType`s to type checking.
  429. self.allow_unbound_tvars = False
  430. # mypyc doesn't properly handle implementing an abstractproperty
  431. # with a regular attribute so we make them properties
  432. @property
  433. def type(self) -> TypeInfo | None:
  434. return self._type
  435. @property
  436. def is_stub_file(self) -> bool:
  437. return self._is_stub_file
  438. @property
  439. def is_typeshed_stub_file(self) -> bool:
  440. return self._is_typeshed_stub_file
  441. @property
  442. def final_iteration(self) -> bool:
  443. return self._final_iteration
  444. @contextmanager
  445. def allow_unbound_tvars_set(self) -> Iterator[None]:
  446. old = self.allow_unbound_tvars
  447. self.allow_unbound_tvars = True
  448. try:
  449. yield
  450. finally:
  451. self.allow_unbound_tvars = old
  452. #
  453. # Preparing module (performed before semantic analysis)
  454. #
  455. def prepare_file(self, file_node: MypyFile) -> None:
  456. """Prepare a freshly parsed file for semantic analysis."""
  457. if "builtins" in self.modules:
  458. file_node.names["__builtins__"] = SymbolTableNode(GDEF, self.modules["builtins"])
  459. if file_node.fullname == "builtins":
  460. self.prepare_builtins_namespace(file_node)
  461. if file_node.fullname == "typing":
  462. self.prepare_typing_namespace(file_node, type_aliases)
  463. if file_node.fullname == "typing_extensions":
  464. self.prepare_typing_namespace(file_node, typing_extensions_aliases)
  465. def prepare_typing_namespace(self, file_node: MypyFile, aliases: dict[str, str]) -> None:
  466. """Remove dummy alias definitions such as List = TypeAlias(object) from typing.
  467. They will be replaced with real aliases when corresponding targets are ready.
  468. """
  469. # This is all pretty unfortunate. typeshed now has a
  470. # sys.version_info check for OrderedDict, and we shouldn't
  471. # take it out, because it is correct and a typechecker should
  472. # use that as a source of truth. But instead we rummage
  473. # through IfStmts to remove the info first. (I tried to
  474. # remove this whole machinery and ran into issues with the
  475. # builtins/typing import cycle.)
  476. def helper(defs: list[Statement]) -> None:
  477. for stmt in defs.copy():
  478. if isinstance(stmt, IfStmt):
  479. for body in stmt.body:
  480. helper(body.body)
  481. if stmt.else_body:
  482. helper(stmt.else_body.body)
  483. if (
  484. isinstance(stmt, AssignmentStmt)
  485. and len(stmt.lvalues) == 1
  486. and isinstance(stmt.lvalues[0], NameExpr)
  487. ):
  488. # Assignment to a simple name, remove it if it is a dummy alias.
  489. if f"{file_node.fullname}.{stmt.lvalues[0].name}" in aliases:
  490. defs.remove(stmt)
  491. helper(file_node.defs)
  492. def prepare_builtins_namespace(self, file_node: MypyFile) -> None:
  493. """Add certain special-cased definitions to the builtins module.
  494. Some definitions are too special or fundamental to be processed
  495. normally from the AST.
  496. """
  497. names = file_node.names
  498. # Add empty definition for core built-in classes, since they are required for basic
  499. # operation. These will be completed later on.
  500. for name in CORE_BUILTIN_CLASSES:
  501. cdef = ClassDef(name, Block([])) # Dummy ClassDef, will be replaced later
  502. info = TypeInfo(SymbolTable(), cdef, "builtins")
  503. info._fullname = f"builtins.{name}"
  504. names[name] = SymbolTableNode(GDEF, info)
  505. bool_info = names["bool"].node
  506. assert isinstance(bool_info, TypeInfo)
  507. bool_type = Instance(bool_info, [])
  508. special_var_types: list[tuple[str, Type]] = [
  509. ("None", NoneType()),
  510. # reveal_type is a mypy-only function that gives an error with
  511. # the type of its arg.
  512. ("reveal_type", AnyType(TypeOfAny.special_form)),
  513. # reveal_locals is a mypy-only function that gives an error with the types of
  514. # locals
  515. ("reveal_locals", AnyType(TypeOfAny.special_form)),
  516. ("True", bool_type),
  517. ("False", bool_type),
  518. ("__debug__", bool_type),
  519. ]
  520. for name, typ in special_var_types:
  521. v = Var(name, typ)
  522. v._fullname = f"builtins.{name}"
  523. file_node.names[name] = SymbolTableNode(GDEF, v)
  524. #
  525. # Analyzing a target
  526. #
  527. def refresh_partial(
  528. self,
  529. node: MypyFile | FuncDef | OverloadedFuncDef,
  530. patches: list[tuple[int, Callable[[], None]]],
  531. final_iteration: bool,
  532. file_node: MypyFile,
  533. options: Options,
  534. active_type: TypeInfo | None = None,
  535. ) -> None:
  536. """Refresh a stale target in fine-grained incremental mode."""
  537. self.patches = patches
  538. self.deferred = False
  539. self.incomplete = False
  540. self._final_iteration = final_iteration
  541. self.missing_names[-1] = set()
  542. with self.file_context(file_node, options, active_type):
  543. if isinstance(node, MypyFile):
  544. self.refresh_top_level(node)
  545. else:
  546. self.recurse_into_functions = True
  547. self.accept(node)
  548. del self.patches
  549. def refresh_top_level(self, file_node: MypyFile) -> None:
  550. """Reanalyze a stale module top-level in fine-grained incremental mode."""
  551. self.recurse_into_functions = False
  552. self.add_implicit_module_attrs(file_node)
  553. for d in file_node.defs:
  554. self.accept(d)
  555. if file_node.fullname == "typing":
  556. self.add_builtin_aliases(file_node)
  557. if file_node.fullname == "typing_extensions":
  558. self.add_typing_extension_aliases(file_node)
  559. self.adjust_public_exports()
  560. self.export_map[self.cur_mod_id] = self.all_exports
  561. self.all_exports = []
  562. def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
  563. """Manually add implicit definitions of module '__name__' etc."""
  564. str_type: Type | None = self.named_type_or_none("builtins.str")
  565. if str_type is None:
  566. str_type = UnboundType("builtins.str")
  567. for name, t in implicit_module_attrs.items():
  568. if name == "__doc__":
  569. typ: Type = str_type
  570. elif name == "__path__":
  571. if not file_node.is_package_init_file():
  572. continue
  573. # Need to construct the type ourselves, to avoid issues with __builtins__.list
  574. # not being subscriptable or typing.List not getting bound
  575. inst = self.named_type_or_none("builtins.list", [str_type])
  576. if inst is None:
  577. assert not self.final_iteration, "Cannot find builtins.list to add __path__"
  578. self.defer()
  579. return
  580. typ = inst
  581. elif name == "__annotations__":
  582. inst = self.named_type_or_none(
  583. "builtins.dict", [str_type, AnyType(TypeOfAny.special_form)]
  584. )
  585. if inst is None:
  586. assert (
  587. not self.final_iteration
  588. ), "Cannot find builtins.dict to add __annotations__"
  589. self.defer()
  590. return
  591. typ = inst
  592. else:
  593. assert t is not None, f"type should be specified for {name}"
  594. typ = UnboundType(t)
  595. existing = file_node.names.get(name)
  596. if existing is not None and not isinstance(existing.node, PlaceholderNode):
  597. # Already exists.
  598. continue
  599. an_type = self.anal_type(typ)
  600. if an_type:
  601. var = Var(name, an_type)
  602. var._fullname = self.qualified_name(name)
  603. var.is_ready = True
  604. self.add_symbol(name, var, dummy_context())
  605. else:
  606. self.add_symbol(
  607. name,
  608. PlaceholderNode(self.qualified_name(name), file_node, -1),
  609. dummy_context(),
  610. )
  611. def add_builtin_aliases(self, tree: MypyFile) -> None:
  612. """Add builtin type aliases to typing module.
  613. For historical reasons, the aliases like `List = list` are not defined
  614. in typeshed stubs for typing module. Instead we need to manually add the
  615. corresponding nodes on the fly. We explicitly mark these aliases as normalized,
  616. so that a user can write `typing.List[int]`.
  617. """
  618. assert tree.fullname == "typing"
  619. for alias, target_name in type_aliases.items():
  620. if type_aliases_source_versions[alias] > self.options.python_version:
  621. # This alias is not available on this Python version.
  622. continue
  623. name = alias.split(".")[-1]
  624. if name in tree.names and not isinstance(tree.names[name].node, PlaceholderNode):
  625. continue
  626. self.create_alias(tree, target_name, alias, name)
  627. def add_typing_extension_aliases(self, tree: MypyFile) -> None:
  628. """Typing extensions module does contain some type aliases.
  629. We need to analyze them as such, because in typeshed
  630. they are just defined as `_Alias()` call.
  631. Which is not supported natively.
  632. """
  633. assert tree.fullname == "typing_extensions"
  634. for alias, target_name in typing_extensions_aliases.items():
  635. name = alias.split(".")[-1]
  636. if name in tree.names and isinstance(tree.names[name].node, TypeAlias):
  637. continue # Do not reset TypeAliases on the second pass.
  638. # We need to remove any node that is there at the moment. It is invalid.
  639. tree.names.pop(name, None)
  640. # Now, create a new alias.
  641. self.create_alias(tree, target_name, alias, name)
  642. def create_alias(self, tree: MypyFile, target_name: str, alias: str, name: str) -> None:
  643. tag = self.track_incomplete_refs()
  644. n = self.lookup_fully_qualified_or_none(target_name)
  645. if n:
  646. if isinstance(n.node, PlaceholderNode):
  647. self.mark_incomplete(name, tree)
  648. else:
  649. # Found built-in class target. Create alias.
  650. target = self.named_type_or_none(target_name, [])
  651. assert target is not None
  652. # Transform List to List[Any], etc.
  653. fix_instance_types(target, self.fail, self.note, self.options)
  654. alias_node = TypeAlias(
  655. target,
  656. alias,
  657. line=-1,
  658. column=-1, # there is no context
  659. no_args=True,
  660. normalized=True,
  661. )
  662. self.add_symbol(name, alias_node, tree)
  663. elif self.found_incomplete_ref(tag):
  664. # Built-in class target may not ready yet -- defer.
  665. self.mark_incomplete(name, tree)
  666. else:
  667. # Test fixtures may be missing some builtin classes, which is okay.
  668. # Kill the placeholder if there is one.
  669. if name in tree.names:
  670. assert isinstance(tree.names[name].node, PlaceholderNode)
  671. del tree.names[name]
  672. def adjust_public_exports(self) -> None:
  673. """Adjust the module visibility of globals due to __all__."""
  674. if "__all__" in self.globals:
  675. for name, g in self.globals.items():
  676. # Being included in __all__ explicitly exports and makes public.
  677. if name in self.all_exports:
  678. g.module_public = True
  679. g.module_hidden = False
  680. # But when __all__ is defined, and a symbol is not included in it,
  681. # it cannot be public.
  682. else:
  683. g.module_public = False
  684. @contextmanager
  685. def file_context(
  686. self, file_node: MypyFile, options: Options, active_type: TypeInfo | None = None
  687. ) -> Iterator[None]:
  688. """Configure analyzer for analyzing targets within a file/class.
  689. Args:
  690. file_node: target file
  691. options: options specific to the file
  692. active_type: must be the surrounding class to analyze method targets
  693. """
  694. scope = self.scope
  695. self.options = options
  696. self.errors.set_file(file_node.path, file_node.fullname, scope=scope, options=options)
  697. self.cur_mod_node = file_node
  698. self.cur_mod_id = file_node.fullname
  699. with scope.module_scope(self.cur_mod_id):
  700. self._is_stub_file = file_node.path.lower().endswith(".pyi")
  701. self._is_typeshed_stub_file = is_typeshed_file(
  702. options.abs_custom_typeshed_dir, file_node.path
  703. )
  704. self.globals = file_node.names
  705. self.tvar_scope = TypeVarLikeScope()
  706. self.named_tuple_analyzer = NamedTupleAnalyzer(options, self)
  707. self.typed_dict_analyzer = TypedDictAnalyzer(options, self, self.msg)
  708. self.enum_call_analyzer = EnumCallAnalyzer(options, self)
  709. self.newtype_analyzer = NewTypeAnalyzer(options, self, self.msg)
  710. # Counter that keeps track of references to undefined things potentially caused by
  711. # incomplete namespaces.
  712. self.num_incomplete_refs = 0
  713. if active_type:
  714. self.incomplete_type_stack.append(False)
  715. scope.enter_class(active_type)
  716. self.enter_class(active_type.defn.info)
  717. for tvar in active_type.defn.type_vars:
  718. self.tvar_scope.bind_existing(tvar)
  719. yield
  720. if active_type:
  721. scope.leave_class()
  722. self.leave_class()
  723. self._type = None
  724. self.incomplete_type_stack.pop()
  725. del self.options
  726. #
  727. # Functions
  728. #
  729. def visit_func_def(self, defn: FuncDef) -> None:
  730. self.statement = defn
  731. # Visit default values because they may contain assignment expressions.
  732. for arg in defn.arguments:
  733. if arg.initializer:
  734. arg.initializer.accept(self)
  735. defn.is_conditional = self.block_depth[-1] > 0
  736. # Set full names even for those definitions that aren't added
  737. # to a symbol table. For example, for overload items.
  738. defn._fullname = self.qualified_name(defn.name)
  739. # We don't add module top-level functions to symbol tables
  740. # when we analyze their bodies in the second phase on analysis,
  741. # since they were added in the first phase. Nested functions
  742. # get always added, since they aren't separate targets.
  743. if not self.recurse_into_functions or len(self.function_stack) > 0:
  744. if not defn.is_decorated and not defn.is_overload:
  745. self.add_function_to_symbol_table(defn)
  746. if not self.recurse_into_functions:
  747. return
  748. with self.scope.function_scope(defn):
  749. self.analyze_func_def(defn)
  750. def analyze_func_def(self, defn: FuncDef) -> None:
  751. self.function_stack.append(defn)
  752. if defn.type:
  753. assert isinstance(defn.type, CallableType)
  754. has_self_type = self.update_function_type_variables(defn.type, defn)
  755. else:
  756. has_self_type = False
  757. self.function_stack.pop()
  758. if self.is_class_scope():
  759. # Method definition
  760. assert self.type is not None
  761. defn.info = self.type
  762. if defn.type is not None and defn.name in ("__init__", "__init_subclass__"):
  763. assert isinstance(defn.type, CallableType)
  764. if isinstance(get_proper_type(defn.type.ret_type), AnyType):
  765. defn.type = defn.type.copy_modified(ret_type=NoneType())
  766. self.prepare_method_signature(defn, self.type, has_self_type)
  767. # Analyze function signature
  768. with self.tvar_scope_frame(self.tvar_scope.method_frame()):
  769. if defn.type:
  770. self.check_classvar_in_signature(defn.type)
  771. assert isinstance(defn.type, CallableType)
  772. # Signature must be analyzed in the surrounding scope so that
  773. # class-level imported names and type variables are in scope.
  774. analyzer = self.type_analyzer()
  775. tag = self.track_incomplete_refs()
  776. result = analyzer.visit_callable_type(defn.type, nested=False)
  777. # Don't store not ready types (including placeholders).
  778. if self.found_incomplete_ref(tag) or has_placeholder(result):
  779. self.defer(defn)
  780. return
  781. assert isinstance(result, ProperType)
  782. if isinstance(result, CallableType):
  783. # type guards need to have a positional argument, to spec
  784. skip_self = self.is_class_scope() and not defn.is_static
  785. if result.type_guard and ARG_POS not in result.arg_kinds[skip_self:]:
  786. self.fail(
  787. "TypeGuard functions must have a positional argument",
  788. result,
  789. code=codes.VALID_TYPE,
  790. )
  791. # in this case, we just kind of just ... remove the type guard.
  792. result = result.copy_modified(type_guard=None)
  793. result = self.remove_unpack_kwargs(defn, result)
  794. if has_self_type and self.type is not None:
  795. info = self.type
  796. if info.self_type is not None:
  797. result.variables = [info.self_type] + list(result.variables)
  798. defn.type = result
  799. self.add_type_alias_deps(analyzer.aliases_used)
  800. self.check_function_signature(defn)
  801. self.check_paramspec_definition(defn)
  802. if isinstance(defn, FuncDef):
  803. assert isinstance(defn.type, CallableType)
  804. defn.type = set_callable_name(defn.type, defn)
  805. self.analyze_arg_initializers(defn)
  806. self.analyze_function_body(defn)
  807. if self.is_class_scope():
  808. assert self.type is not None
  809. # Mark protocol methods with empty bodies as implicitly abstract.
  810. # This makes explicit protocol subclassing type-safe.
  811. if (
  812. self.type.is_protocol
  813. and not self.is_stub_file # Bodies in stub files are always empty.
  814. and (not isinstance(self.scope.function, OverloadedFuncDef) or defn.is_property)
  815. and defn.abstract_status != IS_ABSTRACT
  816. and is_trivial_body(defn.body)
  817. ):
  818. defn.abstract_status = IMPLICITLY_ABSTRACT
  819. if (
  820. is_trivial_body(defn.body)
  821. and not self.is_stub_file
  822. and defn.abstract_status != NOT_ABSTRACT
  823. ):
  824. defn.is_trivial_body = True
  825. if (
  826. defn.is_coroutine
  827. and isinstance(defn.type, CallableType)
  828. and self.wrapped_coro_return_types.get(defn) != defn.type
  829. ):
  830. if defn.is_async_generator:
  831. # Async generator types are handled elsewhere
  832. pass
  833. else:
  834. # A coroutine defined as `async def foo(...) -> T: ...`
  835. # has external return type `Coroutine[Any, Any, T]`.
  836. any_type = AnyType(TypeOfAny.special_form)
  837. ret_type = self.named_type_or_none(
  838. "typing.Coroutine", [any_type, any_type, defn.type.ret_type]
  839. )
  840. assert ret_type is not None, "Internal error: typing.Coroutine not found"
  841. defn.type = defn.type.copy_modified(ret_type=ret_type)
  842. self.wrapped_coro_return_types[defn] = defn.type
  843. def remove_unpack_kwargs(self, defn: FuncDef, typ: CallableType) -> CallableType:
  844. if not typ.arg_kinds or typ.arg_kinds[-1] is not ArgKind.ARG_STAR2:
  845. return typ
  846. last_type = typ.arg_types[-1]
  847. if not isinstance(last_type, UnpackType):
  848. return typ
  849. last_type = get_proper_type(last_type.type)
  850. if not isinstance(last_type, TypedDictType):
  851. self.fail("Unpack item in ** argument must be a TypedDict", defn)
  852. new_arg_types = typ.arg_types[:-1] + [AnyType(TypeOfAny.from_error)]
  853. return typ.copy_modified(arg_types=new_arg_types)
  854. overlap = set(typ.arg_names) & set(last_type.items)
  855. # It is OK for TypedDict to have a key named 'kwargs'.
  856. overlap.discard(typ.arg_names[-1])
  857. if overlap:
  858. overlapped = ", ".join([f'"{name}"' for name in overlap])
  859. self.fail(f"Overlap between argument names and ** TypedDict items: {overlapped}", defn)
  860. new_arg_types = typ.arg_types[:-1] + [AnyType(TypeOfAny.from_error)]
  861. return typ.copy_modified(arg_types=new_arg_types)
  862. # OK, everything looks right now, mark the callable type as using unpack.
  863. new_arg_types = typ.arg_types[:-1] + [last_type]
  864. return typ.copy_modified(arg_types=new_arg_types, unpack_kwargs=True)
  865. def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: bool) -> None:
  866. """Check basic signature validity and tweak annotation of self/cls argument."""
  867. # Only non-static methods are special, as well as __new__.
  868. functype = func.type
  869. if func.name == "__new__":
  870. func.is_static = True
  871. if not func.is_static or func.name == "__new__":
  872. if func.name in ["__init_subclass__", "__class_getitem__"]:
  873. func.is_class = True
  874. if not func.arguments:
  875. self.fail(
  876. 'Method must have at least one argument. Did you forget the "self" argument?',
  877. func,
  878. )
  879. elif isinstance(functype, CallableType):
  880. self_type = get_proper_type(functype.arg_types[0])
  881. if isinstance(self_type, AnyType):
  882. if has_self_type:
  883. assert self.type is not None and self.type.self_type is not None
  884. leading_type: Type = self.type.self_type
  885. else:
  886. leading_type = fill_typevars(info)
  887. if func.is_class or func.name == "__new__":
  888. leading_type = self.class_type(leading_type)
  889. func.type = replace_implicit_first_type(functype, leading_type)
  890. elif has_self_type and isinstance(func.unanalyzed_type, CallableType):
  891. if not isinstance(get_proper_type(func.unanalyzed_type.arg_types[0]), AnyType):
  892. if self.is_expected_self_type(
  893. self_type, func.is_class or func.name == "__new__"
  894. ):
  895. # This error is off by default, since it is explicitly allowed
  896. # by the PEP 673.
  897. self.fail(
  898. 'Redundant "Self" annotation for the first method argument',
  899. func,
  900. code=codes.REDUNDANT_SELF_TYPE,
  901. )
  902. else:
  903. self.fail(
  904. "Method cannot have explicit self annotation and Self type", func
  905. )
  906. elif has_self_type:
  907. self.fail("Static methods cannot use Self type", func)
  908. def is_expected_self_type(self, typ: Type, is_classmethod: bool) -> bool:
  909. """Does this (analyzed or not) type represent the expected Self type for a method?"""
  910. assert self.type is not None
  911. typ = get_proper_type(typ)
  912. if is_classmethod:
  913. if isinstance(typ, TypeType):
  914. return self.is_expected_self_type(typ.item, is_classmethod=False)
  915. if isinstance(typ, UnboundType):
  916. sym = self.lookup_qualified(typ.name, typ, suppress_errors=True)
  917. if (
  918. sym is not None
  919. and (
  920. sym.fullname == "typing.Type"
  921. or (
  922. sym.fullname == "builtins.type"
  923. and (
  924. self.is_stub_file
  925. or self.is_future_flag_set("annotations")
  926. or self.options.python_version >= (3, 9)
  927. )
  928. )
  929. )
  930. and typ.args
  931. ):
  932. return self.is_expected_self_type(typ.args[0], is_classmethod=False)
  933. return False
  934. if isinstance(typ, TypeVarType):
  935. return typ == self.type.self_type
  936. if isinstance(typ, UnboundType):
  937. sym = self.lookup_qualified(typ.name, typ, suppress_errors=True)
  938. return sym is not None and sym.fullname in SELF_TYPE_NAMES
  939. return False
  940. def set_original_def(self, previous: Node | None, new: FuncDef | Decorator) -> bool:
  941. """If 'new' conditionally redefine 'previous', set 'previous' as original
  942. We reject straight redefinitions of functions, as they are usually
  943. a programming error. For example:
  944. def f(): ...
  945. def f(): ... # Error: 'f' redefined
  946. """
  947. if isinstance(new, Decorator):
  948. new = new.func
  949. if (
  950. isinstance(previous, (FuncDef, Decorator))
  951. and unnamed_function(new.name)
  952. and unnamed_function(previous.name)
  953. ):
  954. return True
  955. if isinstance(previous, (FuncDef, Var, Decorator)) and new.is_conditional:
  956. new.original_def = previous
  957. return True
  958. else:
  959. return False
  960. def update_function_type_variables(self, fun_type: CallableType, defn: FuncItem) -> bool:
  961. """Make any type variables in the signature of defn explicit.
  962. Update the signature of defn to contain type variable definitions
  963. if defn is generic. Return True, if the signature contains typing.Self
  964. type, or False otherwise.
  965. """
  966. with self.tvar_scope_frame(self.tvar_scope.method_frame()):
  967. a = self.type_analyzer()
  968. fun_type.variables, has_self_type = a.bind_function_type_variables(fun_type, defn)
  969. if has_self_type and self.type is not None:
  970. self.setup_self_type()
  971. return has_self_type
  972. def setup_self_type(self) -> None:
  973. """Setup a (shared) Self type variable for current class.
  974. We intentionally don't add it to the class symbol table,
  975. so it can be accessed only by mypy and will not cause
  976. clashes with user defined names.
  977. """
  978. assert self.type is not None
  979. info = self.type
  980. if info.self_type is not None:
  981. if has_placeholder(info.self_type.upper_bound):
  982. # Similar to regular (user defined) type variables.
  983. self.process_placeholder(
  984. None,
  985. "Self upper bound",
  986. info,
  987. force_progress=info.self_type.upper_bound != fill_typevars(info),
  988. )
  989. else:
  990. return
  991. info.self_type = TypeVarType(
  992. "Self",
  993. f"{info.fullname}.Self",
  994. id=0,
  995. values=[],
  996. upper_bound=fill_typevars(info),
  997. default=AnyType(TypeOfAny.from_omitted_generics),
  998. )
  999. def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
  1000. self.statement = defn
  1001. self.add_function_to_symbol_table(defn)
  1002. if not self.recurse_into_functions:
  1003. return
  1004. # NB: Since _visit_overloaded_func_def will call accept on the
  1005. # underlying FuncDefs, the function might get entered twice.
  1006. # This is fine, though, because only the outermost function is
  1007. # used to compute targets.
  1008. with self.scope.function_scope(defn):
  1009. self.analyze_overloaded_func_def(defn)
  1010. def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
  1011. # OverloadedFuncDef refers to any legitimate situation where you have
  1012. # more than one declaration for the same function in a row. This occurs
  1013. # with a @property with a setter or a deleter, and for a classic
  1014. # @overload.
  1015. defn._fullname = self.qualified_name(defn.name)
  1016. # TODO: avoid modifying items.
  1017. defn.items = defn.unanalyzed_items.copy()
  1018. first_item = defn.items[0]
  1019. first_item.is_overload = True
  1020. first_item.accept(self)
  1021. if isinstance(first_item, Decorator) and first_item.func.is_property:
  1022. # This is a property.
  1023. first_item.func.is_overload = True
  1024. self.analyze_property_with_multi_part_definition(defn)
  1025. typ = function_type(first_item.func, self.named_type("builtins.function"))
  1026. assert isinstance(typ, CallableType)
  1027. types = [typ]
  1028. else:
  1029. # This is an a normal overload. Find the item signatures, the
  1030. # implementation (if outside a stub), and any missing @overload
  1031. # decorators.
  1032. types, impl, non_overload_indexes = self.analyze_overload_sigs_and_impl(defn)
  1033. defn.impl = impl
  1034. if non_overload_indexes:
  1035. self.handle_missing_overload_decorators(
  1036. defn, non_overload_indexes, some_overload_decorators=len(types) > 0
  1037. )
  1038. # If we found an implementation, remove it from the overload item list,
  1039. # as it's special.
  1040. if impl is not None:
  1041. assert impl is defn.items[-1]
  1042. defn.items = defn.items[:-1]
  1043. elif not non_overload_indexes:
  1044. self.handle_missing_overload_implementation(defn)
  1045. if types:
  1046. defn.type = Overloaded(types)
  1047. defn.type.line = defn.line
  1048. if not defn.items:
  1049. # It was not a real overload after all, but function redefinition. We've
  1050. # visited the redefinition(s) already.
  1051. if not defn.impl:
  1052. # For really broken overloads with no items and no implementation we need to keep
  1053. # at least one item to hold basic information like function name.
  1054. defn.impl = defn.unanalyzed_items[-1]
  1055. return
  1056. # We know this is an overload def. Infer properties and perform some checks.
  1057. self.process_final_in_overload(defn)
  1058. self.process_static_or_class_method_in_overload(defn)
  1059. self.process_overload_impl(defn)
  1060. def process_overload_impl(self, defn: OverloadedFuncDef) -> None:
  1061. """Set flags for an overload implementation.
  1062. Currently, this checks for a trivial body in protocols classes,
  1063. where it makes the method implicitly abstract.
  1064. """
  1065. if defn.impl is None:
  1066. return
  1067. impl = defn.impl if isinstance(defn.impl, FuncDef) else defn.impl.func
  1068. if is_trivial_body(impl.body) and self.is_class_scope() and not self.is_stub_file:
  1069. assert self.type is not None
  1070. if self.type.is_protocol:
  1071. impl.abstract_status = IMPLICITLY_ABSTRACT
  1072. if impl.abstract_status != NOT_ABSTRACT:
  1073. impl.is_trivial_body = True
  1074. def analyze_overload_sigs_and_impl(
  1075. self, defn: OverloadedFuncDef
  1076. ) -> tuple[list[CallableType], OverloadPart | None, list[int]]:
  1077. """Find overload signatures, the implementation, and items with missing @overload.
  1078. Assume that the first was already analyzed. As a side effect:
  1079. analyzes remaining items and updates 'is_overload' flags.
  1080. """
  1081. types = []
  1082. non_overload_indexes = []
  1083. impl: OverloadPart | None = None
  1084. for i, item in enumerate(defn.items):
  1085. if i != 0:
  1086. # Assume that the first item was already visited
  1087. item.is_overload = True
  1088. item.accept(self)
  1089. # TODO: support decorated overloaded functions properly
  1090. if isinstance(item, Decorator):
  1091. callable = function_type(item.func, self.named_type("builtins.function"))
  1092. assert isinstance(callable, CallableType)
  1093. if not any(refers_to_fullname(dec, OVERLOAD_NAMES) for dec in item.decorators):
  1094. if i == len(defn.items) - 1 and not self.is_stub_file:
  1095. # Last item outside a stub is impl
  1096. impl = item
  1097. else:
  1098. # Oops it wasn't an overload after all. A clear error
  1099. # will vary based on where in the list it is, record
  1100. # that.
  1101. non_overload_indexes.append(i)
  1102. else:
  1103. item.func.is_overload = True
  1104. types.append(callable)
  1105. if item.var.is_property:
  1106. self.fail("An overload can not be a property", item)
  1107. # If any item was decorated with `@override`, the whole overload
  1108. # becomes an explicit override.
  1109. defn.is_explicit_override |= item.func.is_explicit_override
  1110. elif isinstance(item, FuncDef):
  1111. if i == len(defn.items) - 1 and not self.is_stub_file:
  1112. impl = item
  1113. else:
  1114. non_overload_indexes.append(i)
  1115. return types, impl, non_overload_indexes
  1116. def handle_missing_overload_decorators(
  1117. self,
  1118. defn: OverloadedFuncDef,
  1119. non_overload_indexes: list[int],
  1120. some_overload_decorators: bool,
  1121. ) -> None:
  1122. """Generate errors for overload items without @overload.
  1123. Side effect: remote non-overload items.
  1124. """
  1125. if some_overload_decorators:
  1126. # Some of them were overloads, but not all.
  1127. for idx in non_overload_indexes:
  1128. if self.is_stub_file:
  1129. self.fail(
  1130. "An implementation for an overloaded function "
  1131. "is not allowed in a stub file",
  1132. defn.items[idx],
  1133. )
  1134. else:
  1135. self.fail(
  1136. "The implementation for an overloaded function must come last",
  1137. defn.items[idx],
  1138. )
  1139. else:
  1140. for idx in non_overload_indexes[1:]:
  1141. self.name_already_defined(defn.name, defn.items[idx], defn.items[0])
  1142. if defn.impl:
  1143. self.name_already_defined(defn.name, defn.impl, defn.items[0])
  1144. # Remove the non-overloads
  1145. for idx in reversed(non_overload_indexes):
  1146. del defn.items[idx]
  1147. def handle_missing_overload_implementation(self, defn: OverloadedFuncDef) -> None:
  1148. """Generate error about missing overload implementation (only if needed)."""
  1149. if not self.is_stub_file:
  1150. if self.type and self.type.is_protocol and not self.is_func_scope():
  1151. # An overloaded protocol method doesn't need an implementation,
  1152. # but if it doesn't have one, then it is considered abstract.
  1153. for item in defn.items:
  1154. if isinstance(item, Decorator):
  1155. item.func.abstract_status = IS_ABSTRACT
  1156. else:
  1157. item.abstract_status = IS_ABSTRACT
  1158. else:
  1159. # TODO: also allow omitting an implementation for abstract methods in ABCs?
  1160. self.fail(
  1161. "An overloaded function outside a stub file must have an implementation",
  1162. defn,
  1163. code=codes.NO_OVERLOAD_IMPL,
  1164. )
  1165. def process_final_in_overload(self, defn: OverloadedFuncDef) -> None:
  1166. """Detect the @final status of an overloaded function (and perform checks)."""
  1167. # If the implementation is marked as @final (or the first overload in
  1168. # stubs), then the whole overloaded definition if @final.
  1169. if any(item.is_final for item in defn.items):
  1170. # We anyway mark it as final because it was probably the intention.
  1171. defn.is_final = True
  1172. # Only show the error once per overload
  1173. bad_final = next(ov for ov in defn.items if ov.is_final)
  1174. if not self.is_stub_file:
  1175. self.fail("@final should be applied only to overload implementation", bad_final)
  1176. elif any(item.is_final for item in defn.items[1:]):
  1177. bad_final = next(ov for ov in defn.items[1:] if ov.is_final)
  1178. self.fail(
  1179. "In a stub file @final must be applied only to the first overload", bad_final
  1180. )
  1181. if defn.impl is not None and defn.impl.is_final:
  1182. defn.is_final = True
  1183. def process_static_or_class_method_in_overload(self, defn: OverloadedFuncDef) -> None:
  1184. class_status = []
  1185. static_status = []
  1186. for item in defn.items:
  1187. if isinstance(item, Decorator):
  1188. inner = item.func
  1189. elif isinstance(item, FuncDef):
  1190. inner = item
  1191. else:
  1192. assert False, f"The 'item' variable is an unexpected type: {type(item)}"
  1193. class_status.append(inner.is_class)
  1194. static_status.append(inner.is_static)
  1195. if defn.impl is not None:
  1196. if isinstance(defn.impl, Decorator):
  1197. inner = defn.impl.func
  1198. elif isinstance(defn.impl, FuncDef):
  1199. inner = defn.impl
  1200. else:
  1201. assert False, f"Unexpected impl type: {type(defn.impl)}"
  1202. class_status.append(inner.is_class)
  1203. static_status.append(inner.is_static)
  1204. if len(set(class_status)) != 1:
  1205. self.msg.overload_inconsistently_applies_decorator("classmethod", defn)
  1206. elif len(set(static_status)) != 1:
  1207. self.msg.overload_inconsistently_applies_decorator("staticmethod", defn)
  1208. else:
  1209. defn.is_class = class_status[0]
  1210. defn.is_static = static_status[0]
  1211. def analyze_property_with_multi_part_definition(self, defn: OverloadedFuncDef) -> None:
  1212. """Analyze a property defined using multiple methods (e.g., using @x.setter).
  1213. Assume that the first method (@property) has already been analyzed.
  1214. """
  1215. defn.is_property = True
  1216. items = defn.items
  1217. first_item = defn.items[0]
  1218. assert isinstance(first_item, Decorator)
  1219. deleted_items = []
  1220. for i, item in enumerate(items[1:]):
  1221. if isinstance(item, Decorator):
  1222. if len(item.decorators) >= 1:
  1223. node = item.decorators[0]
  1224. if isinstance(node, MemberExpr):
  1225. if node.name == "setter":
  1226. # The first item represents the entire property.
  1227. first_item.var.is_settable_property = True
  1228. # Get abstractness from the original definition.
  1229. item.func.abstract_status = first_item.func.abstract_status
  1230. if node.name == "deleter":
  1231. item.func.abstract_status = first_item.func.abstract_status
  1232. else:
  1233. self.fail(
  1234. f"Only supported top decorator is @{first_item.func.name}.setter", item
  1235. )
  1236. item.func.accept(self)
  1237. else:
  1238. self.fail(f'Unexpected definition for property "{first_item.func.name}"', item)
  1239. deleted_items.append(i + 1)
  1240. for i in reversed(deleted_items):
  1241. del items[i]
  1242. def add_function_to_symbol_table(self, func: FuncDef | OverloadedFuncDef) -> None:
  1243. if self.is_class_scope():
  1244. assert self.type is not None
  1245. func.info = self.type
  1246. func._fullname = self.qualified_name(func.name)
  1247. self.add_symbol(func.name, func, func)
  1248. def analyze_arg_initializers(self, defn: FuncItem) -> None:
  1249. with self.tvar_scope_frame(self.tvar_scope.method_frame()):
  1250. # Analyze default arguments
  1251. for arg in defn.arguments:
  1252. if arg.initializer:
  1253. arg.initializer.accept(self)
  1254. def analyze_function_body(self, defn: FuncItem) -> None:
  1255. is_method = self.is_class_scope()
  1256. with self.tvar_scope_frame(self.tvar_scope.method_frame()):
  1257. # Bind the type variables again to visit the body.
  1258. if defn.type:
  1259. a = self.type_analyzer()
  1260. typ = defn.type
  1261. assert isinstance(typ, CallableType)
  1262. a.bind_function_type_variables(typ, defn)
  1263. for i in range(len(typ.arg_types)):
  1264. store_argument_type(defn, i, typ, self.named_type)
  1265. self.function_stack.append(defn)
  1266. with self.enter(defn):
  1267. for arg in defn.arguments:
  1268. self.add_local(arg.variable, defn)
  1269. # The first argument of a non-static, non-class method is like 'self'
  1270. # (though the name could be different), having the enclosing class's
  1271. # instance type.
  1272. if is_method and (not defn.is_static or defn.name == "__new__") and defn.arguments:
  1273. if not defn.is_class:
  1274. defn.arguments[0].variable.is_self = True
  1275. else:
  1276. defn.arguments[0].variable.is_cls = True
  1277. defn.body.accept(self)
  1278. self.function_stack.pop()
  1279. def check_classvar_in_signature(self, typ: ProperType) -> None:
  1280. t: ProperType
  1281. if isinstance(typ, Overloaded):
  1282. for t in typ.items:
  1283. self.check_classvar_in_signature(t)
  1284. return
  1285. if not isinstance(typ, CallableType):
  1286. return
  1287. for t in get_proper_types(typ.arg_types) + [get_proper_type(typ.ret_type)]:
  1288. if self.is_classvar(t):
  1289. self.fail_invalid_classvar(t)
  1290. # Show only one error per signature
  1291. break
  1292. def check_function_signature(self, fdef: FuncItem) -> None:
  1293. sig = fdef.type
  1294. assert isinstance(sig, CallableType)
  1295. if len(sig.arg_types) < len(fdef.arguments):
  1296. self.fail("Type signature has too few arguments", fdef)
  1297. # Add dummy Any arguments to prevent crashes later.
  1298. num_extra_anys = len(fdef.arguments) - len(sig.arg_types)
  1299. extra_anys = [AnyType(TypeOfAny.from_error)] * num_extra_anys
  1300. sig.arg_types.extend(extra_anys)
  1301. elif len(sig.arg_types) > len(fdef.arguments):
  1302. self.fail("Type signature has too many arguments", fdef, blocker=True)
  1303. def check_paramspec_definition(self, defn: FuncDef) -> None:
  1304. func = defn.type
  1305. assert isinstance(func, CallableType)
  1306. if not any(isinstance(var, ParamSpecType) for var in func.variables):
  1307. return # Function does not have param spec variables
  1308. args = func.var_arg()
  1309. kwargs = func.kw_arg()
  1310. if args is None and kwargs is None:
  1311. return # Looks like this function does not have starred args
  1312. args_defn_type = None
  1313. kwargs_defn_type = None
  1314. for arg_def, arg_kind in zip(defn.arguments, defn.arg_kinds):
  1315. if arg_kind == ARG_STAR:
  1316. args_defn_type = arg_def.type_annotation
  1317. elif arg_kind == ARG_STAR2:
  1318. kwargs_defn_type = arg_def.type_annotation
  1319. # This may happen on invalid `ParamSpec` args / kwargs definition,
  1320. # type analyzer sets types of arguments to `Any`, but keeps
  1321. # definition types as `UnboundType` for now.
  1322. if not (
  1323. (isinstance(args_defn_type, UnboundType) and args_defn_type.name.endswith(".args"))
  1324. or (
  1325. isinstance(kwargs_defn_type, UnboundType)
  1326. and kwargs_defn_type.name.endswith(".kwargs")
  1327. )
  1328. ):
  1329. # Looks like both `*args` and `**kwargs` are not `ParamSpec`
  1330. # It might be something else, skipping.
  1331. return
  1332. args_type = args.typ if args is not None else None
  1333. kwargs_type = kwargs.typ if kwargs is not None else None
  1334. if (
  1335. not isinstance(args_type, ParamSpecType)
  1336. or not isinstance(kwargs_type, ParamSpecType)
  1337. or args_type.name != kwargs_type.name
  1338. ):
  1339. if isinstance(args_defn_type, UnboundType) and args_defn_type.name.endswith(".args"):
  1340. param_name = args_defn_type.name.split(".")[0]
  1341. elif isinstance(kwargs_defn_type, UnboundType) and kwargs_defn_type.name.endswith(
  1342. ".kwargs"
  1343. ):
  1344. param_name = kwargs_defn_type.name.split(".")[0]
  1345. else:
  1346. # Fallback for cases that probably should not ever happen:
  1347. param_name = "P"
  1348. self.fail(
  1349. f'ParamSpec must have "*args" typed as "{param_name}.args" and "**kwargs" typed as "{param_name}.kwargs"',
  1350. func,
  1351. code=codes.VALID_TYPE,
  1352. )
  1353. def visit_decorator(self, dec: Decorator) -> None:
  1354. self.statement = dec
  1355. # TODO: better don't modify them at all.
  1356. dec.decorators = dec.original_decorators.copy()
  1357. dec.func.is_conditional = self.block_depth[-1] > 0
  1358. if not dec.is_overload:
  1359. self.add_symbol(dec.name, dec, dec)
  1360. dec.func._fullname = self.qualified_name(dec.name)
  1361. dec.var._fullname = self.qualified_name(dec.name)
  1362. for d in dec.decorators:
  1363. d.accept(self)
  1364. removed: list[int] = []
  1365. no_type_check = False
  1366. could_be_decorated_property = False
  1367. for i, d in enumerate(dec.decorators):
  1368. # A bunch of decorators are special cased here.
  1369. if refers_to_fullname(d, "abc.abstractmethod"):
  1370. removed.append(i)
  1371. dec.func.abstract_status = IS_ABSTRACT
  1372. self.check_decorated_function_is_method("abstractmethod", dec)
  1373. elif refers_to_fullname(d, ("asyncio.coroutines.coroutine", "types.coroutine")):
  1374. removed.append(i)
  1375. dec.func.is_awaitable_coroutine = True
  1376. elif refers_to_fullname(d, "builtins.staticmethod"):
  1377. removed.append(i)
  1378. dec.func.is_static = True
  1379. dec.var.is_staticmethod = True
  1380. self.check_decorated_function_is_method("staticmethod", dec)
  1381. elif refers_to_fullname(d, "builtins.classmethod"):
  1382. removed.append(i)
  1383. dec.func.is_class = True
  1384. dec.var.is_classmethod = True
  1385. self.check_decorated_function_is_method("classmethod", dec)
  1386. elif refers_to_fullname(d, OVERRIDE_DECORATOR_NAMES):
  1387. removed.append(i)
  1388. dec.func.is_explicit_override = True
  1389. self.check_decorated_function_is_method("override", dec)
  1390. elif refers_to_fullname(
  1391. d,
  1392. (
  1393. "builtins.property",
  1394. "abc.abstractproperty",
  1395. "functools.cached_property",
  1396. "enum.property",
  1397. ),
  1398. ):
  1399. removed.append(i)
  1400. dec.func.is_property = True
  1401. dec.var.is_property = True
  1402. if refers_to_fullname(d, "abc.abstractproperty"):
  1403. dec.func.abstract_status = IS_ABSTRACT
  1404. elif refers_to_fullname(d, "functools.cached_property"):
  1405. dec.var.is_settable_property = True
  1406. self.check_decorated_function_is_method("property", dec)
  1407. elif refers_to_fullname(d, "typing.no_type_check"):
  1408. dec.var.type = AnyType(TypeOfAny.special_form)
  1409. no_type_check = True
  1410. elif refers_to_fullname(d, FINAL_DECORATOR_NAMES):
  1411. if self.is_class_scope():
  1412. assert self.type is not None, "No type set at class scope"
  1413. if self.type.is_protocol:
  1414. self.msg.protocol_members_cant_be_final(d)
  1415. else:
  1416. dec.func.is_final = True
  1417. dec.var.is_final = True
  1418. removed.append(i)
  1419. else:
  1420. self.fail("@final cannot be used with non-method functions", d)
  1421. elif isinstance(d, CallExpr) and refers_to_fullname(
  1422. d.callee, DATACLASS_TRANSFORM_NAMES
  1423. ):
  1424. dec.func.dataclass_transform_spec = self.parse_dataclass_transform_spec(d)
  1425. elif not dec.var.is_property:
  1426. # We have seen a "non-trivial" decorator before seeing @property, if
  1427. # we will see a @property later, give an error, as we don't support this.
  1428. could_be_decorated_property = True
  1429. for i in reversed(removed):
  1430. del dec.decorators[i]
  1431. if (not dec.is_overload or dec.var.is_property) and self.type:
  1432. dec.var.info = self.type
  1433. dec.var.is_initialized_in_class = True
  1434. if not no_type_check and self.recurse_into_functions:
  1435. dec.func.accept(self)
  1436. if could_be_decorated_property and dec.decorators and dec.var.is_property:
  1437. self.fail("Decorators on top of @property are not supported", dec)
  1438. if (dec.func.is_static or dec.func.is_class) and dec.var.is_property:
  1439. self.fail("Only instance methods can be decorated with @property", dec)
  1440. if dec.func.abstract_status == IS_ABSTRACT and dec.func.is_final:
  1441. self.fail(f"Method {dec.func.name} is both abstract and final", dec)
  1442. if dec.func.is_static and dec.func.is_class:
  1443. self.fail(message_registry.CLASS_PATTERN_CLASS_OR_STATIC_METHOD, dec)
  1444. def check_decorated_function_is_method(self, decorator: str, context: Context) -> None:
  1445. if not self.type or self.is_func_scope():
  1446. self.fail(f'"{decorator}" used with a non-method', context)
  1447. #
  1448. # Classes
  1449. #
  1450. def visit_class_def(self, defn: ClassDef) -> None:
  1451. self.statement = defn
  1452. self.incomplete_type_stack.append(not defn.info)
  1453. namespace = self.qualified_name(defn.name)
  1454. with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)):
  1455. self.analyze_class(defn)
  1456. self.incomplete_type_stack.pop()
  1457. def analyze_class(self, defn: ClassDef) -> None:
  1458. fullname = self.qualified_name(defn.name)
  1459. if not defn.info and not self.is_core_builtin_class(defn):
  1460. # Add placeholder so that self-references in base classes can be
  1461. # resolved. We don't want this to cause a deferral, since if there
  1462. # are no incomplete references, we'll replace this with a TypeInfo
  1463. # before returning.
  1464. placeholder = PlaceholderNode(fullname, defn, defn.line, becomes_typeinfo=True)
  1465. self.add_symbol(defn.name, placeholder, defn, can_defer=False)
  1466. tag = self.track_incomplete_refs()
  1467. # Restore base classes after previous iteration (things like Generic[T] might be removed).
  1468. defn.base_type_exprs.extend(defn.removed_base_type_exprs)
  1469. defn.removed_base_type_exprs.clear()
  1470. self.infer_metaclass_and_bases_from_compat_helpers(defn)
  1471. bases = defn.base_type_exprs
  1472. bases, tvar_defs, is_protocol = self.clean_up_bases_and_infer_type_variables(
  1473. defn, bases, context=defn
  1474. )
  1475. for tvd in tvar_defs:
  1476. if isinstance(tvd, TypeVarType) and any(
  1477. has_placeholder(t) for t in [tvd.upper_bound] + tvd.values
  1478. ):
  1479. # Some type variable bounds or values are not ready, we need
  1480. # to re-analyze this class.
  1481. self.defer()
  1482. if has_placeholder(tvd.default):
  1483. # Placeholder values in TypeVarLikeTypes may get substituted in.
  1484. # Defer current target until they are ready.
  1485. self.mark_incomplete(defn.name, defn)
  1486. return
  1487. self.analyze_class_keywords(defn)
  1488. bases_result = self.analyze_base_classes(bases)
  1489. if bases_result is None or self.found_incomplete_ref(tag):
  1490. # Something was incomplete. Defer current target.
  1491. self.mark_incomplete(defn.name, defn)
  1492. return
  1493. base_types, base_error = bases_result
  1494. if any(isinstance(base, PlaceholderType) for base, _ in base_types):
  1495. # We need to know the TypeInfo of each base to construct the MRO. Placeholder types
  1496. # are okay in nested positions, since they can't affect the MRO.
  1497. self.mark_incomplete(defn.name, defn)
  1498. return
  1499. declared_metaclass, should_defer, any_meta = self.get_declared_metaclass(
  1500. defn.name, defn.metaclass
  1501. )
  1502. if should_defer or self.found_incomplete_ref(tag):
  1503. # Metaclass was not ready. Defer current target.
  1504. self.mark_incomplete(defn.name, defn)
  1505. return
  1506. if self.analyze_typeddict_classdef(defn):
  1507. if defn.info:
  1508. self.setup_type_vars(defn, tvar_defs)
  1509. self.setup_alias_type_vars(defn)
  1510. return
  1511. if self.analyze_namedtuple_classdef(defn, tvar_defs):
  1512. return
  1513. # Create TypeInfo for class now that base classes and the MRO can be calculated.
  1514. self.prepare_class_def(defn)
  1515. self.setup_type_vars(defn, tvar_defs)
  1516. if base_error:
  1517. defn.info.fallback_to_any = True
  1518. if any_meta:
  1519. defn.info.meta_fallback_to_any = True
  1520. with self.scope.class_scope(defn.info):
  1521. self.configure_base_classes(defn, base_types)
  1522. defn.info.is_protocol = is_protocol
  1523. self.recalculate_metaclass(defn, declared_metaclass)
  1524. defn.info.runtime_protocol = False
  1525. for decorator in defn.decorators:
  1526. self.analyze_class_decorator(defn, decorator)
  1527. self.analyze_class_body_common(defn)
  1528. def setup_type_vars(self, defn: ClassDef, tvar_defs: list[TypeVarLikeType]) -> None:
  1529. defn.type_vars = tvar_defs
  1530. defn.info.type_vars = []
  1531. # we want to make sure any additional logic in add_type_vars gets run
  1532. defn.info.add_type_vars()
  1533. def setup_alias_type_vars(self, defn: ClassDef) -> None:
  1534. assert defn.info.special_alias is not None
  1535. defn.info.special_alias.alias_tvars = list(defn.type_vars)
  1536. target = defn.info.special_alias.target
  1537. assert isinstance(target, ProperType)
  1538. if isinstance(target, TypedDictType):
  1539. target.fallback.args = tuple(defn.type_vars)
  1540. elif isinstance(target, TupleType):
  1541. target.partial_fallback.args = tuple(defn.type_vars)
  1542. else:
  1543. assert False, f"Unexpected special alias type: {type(target)}"
  1544. def is_core_builtin_class(self, defn: ClassDef) -> bool:
  1545. return self.cur_mod_id == "builtins" and defn.name in CORE_BUILTIN_CLASSES
  1546. def analyze_class_body_common(self, defn: ClassDef) -> None:
  1547. """Parts of class body analysis that are common to all kinds of class defs."""
  1548. self.enter_class(defn.info)
  1549. if any(b.self_type is not None for b in defn.info.mro):
  1550. self.setup_self_type()
  1551. defn.defs.accept(self)
  1552. self.apply_class_plugin_hooks(defn)
  1553. self.leave_class()
  1554. def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
  1555. if (
  1556. defn.info
  1557. and defn.info.typeddict_type
  1558. and not has_placeholder(defn.info.typeddict_type)
  1559. ):
  1560. # This is a valid TypedDict, and it is fully analyzed.
  1561. return True
  1562. is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
  1563. if is_typeddict:
  1564. for decorator in defn.decorators:
  1565. decorator.accept(self)
  1566. if isinstance(decorator, RefExpr):
  1567. if decorator.fullname in FINAL_DECORATOR_NAMES and info is not None:
  1568. info.is_final = True
  1569. if info is None:
  1570. self.mark_incomplete(defn.name, defn)
  1571. else:
  1572. self.prepare_class_def(defn, info)
  1573. return True
  1574. return False
  1575. def analyze_namedtuple_classdef(
  1576. self, defn: ClassDef, tvar_defs: list[TypeVarLikeType]
  1577. ) -> bool:
  1578. """Check if this class can define a named tuple."""
  1579. if (
  1580. defn.info
  1581. and defn.info.is_named_tuple
  1582. and defn.info.tuple_type
  1583. and not has_placeholder(defn.info.tuple_type)
  1584. ):
  1585. # Don't reprocess everything. We just need to process methods defined
  1586. # in the named tuple class body.
  1587. is_named_tuple = True
  1588. info: TypeInfo | None = defn.info
  1589. else:
  1590. is_named_tuple, info = self.named_tuple_analyzer.analyze_namedtuple_classdef(
  1591. defn, self.is_stub_file, self.is_func_scope()
  1592. )
  1593. if is_named_tuple:
  1594. if info is None:
  1595. self.mark_incomplete(defn.name, defn)
  1596. else:
  1597. self.prepare_class_def(defn, info, custom_names=True)
  1598. self.setup_type_vars(defn, tvar_defs)
  1599. self.setup_alias_type_vars(defn)
  1600. with self.scope.class_scope(defn.info):
  1601. for deco in defn.decorators:
  1602. deco.accept(self)
  1603. if isinstance(deco, RefExpr) and deco.fullname in FINAL_DECORATOR_NAMES:
  1604. info.is_final = True
  1605. with self.named_tuple_analyzer.save_namedtuple_body(info):
  1606. self.analyze_class_body_common(defn)
  1607. return True
  1608. return False
  1609. def apply_class_plugin_hooks(self, defn: ClassDef) -> None:
  1610. """Apply a plugin hook that may infer a more precise definition for a class."""
  1611. for decorator in defn.decorators:
  1612. decorator_name = self.get_fullname_for_hook(decorator)
  1613. if decorator_name:
  1614. hook = self.plugin.get_class_decorator_hook(decorator_name)
  1615. # Special case: if the decorator is itself decorated with
  1616. # typing.dataclass_transform, apply the hook for the dataclasses plugin
  1617. # TODO: remove special casing here
  1618. if hook is None and find_dataclass_transform_spec(decorator):
  1619. hook = dataclasses_plugin.dataclass_tag_callback
  1620. if hook:
  1621. hook(ClassDefContext(defn, decorator, self))
  1622. if defn.metaclass:
  1623. metaclass_name = self.get_fullname_for_hook(defn.metaclass)
  1624. if metaclass_name:
  1625. hook = self.plugin.get_metaclass_hook(metaclass_name)
  1626. if hook:
  1627. hook(ClassDefContext(defn, defn.metaclass, self))
  1628. for base_expr in defn.base_type_exprs:
  1629. base_name = self.get_fullname_for_hook(base_expr)
  1630. if base_name:
  1631. hook = self.plugin.get_base_class_hook(base_name)
  1632. if hook:
  1633. hook(ClassDefContext(defn, base_expr, self))
  1634. # Check if the class definition itself triggers a dataclass transform (via a parent class/
  1635. # metaclass)
  1636. spec = find_dataclass_transform_spec(defn)
  1637. if spec is not None:
  1638. dataclasses_plugin.add_dataclass_tag(defn.info)
  1639. def get_fullname_for_hook(self, expr: Expression) -> str | None:
  1640. if isinstance(expr, CallExpr):
  1641. return self.get_fullname_for_hook(expr.callee)
  1642. elif isinstance(expr, IndexExpr):
  1643. return self.get_fullname_for_hook(expr.base)
  1644. elif isinstance(expr, RefExpr):
  1645. if expr.fullname:
  1646. return expr.fullname
  1647. # If we don't have a fullname look it up. This happens because base classes are
  1648. # analyzed in a different manner (see exprtotype.py) and therefore those AST
  1649. # nodes will not have full names.
  1650. sym = self.lookup_type_node(expr)
  1651. if sym:
  1652. return sym.fullname
  1653. return None
  1654. def analyze_class_keywords(self, defn: ClassDef) -> None:
  1655. for value in defn.keywords.values():
  1656. value.accept(self)
  1657. def enter_class(self, info: TypeInfo) -> None:
  1658. # Remember previous active class
  1659. self.type_stack.append(self.type)
  1660. self.locals.append(None) # Add class scope
  1661. self.is_comprehension_stack.append(False)
  1662. self.block_depth.append(-1) # The class body increments this to 0
  1663. self.loop_depth.append(0)
  1664. self._type = info
  1665. self.missing_names.append(set())
  1666. def leave_class(self) -> None:
  1667. """Restore analyzer state."""
  1668. self.block_depth.pop()
  1669. self.loop_depth.pop()
  1670. self.locals.pop()
  1671. self.is_comprehension_stack.pop()
  1672. self._type = self.type_stack.pop()
  1673. self.missing_names.pop()
  1674. def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
  1675. decorator.accept(self)
  1676. if isinstance(decorator, RefExpr):
  1677. if decorator.fullname in RUNTIME_PROTOCOL_DECOS:
  1678. if defn.info.is_protocol:
  1679. defn.info.runtime_protocol = True
  1680. else:
  1681. self.fail("@runtime_checkable can only be used with protocol classes", defn)
  1682. elif decorator.fullname in FINAL_DECORATOR_NAMES:
  1683. defn.info.is_final = True
  1684. elif isinstance(decorator, CallExpr) and refers_to_fullname(
  1685. decorator.callee, DATACLASS_TRANSFORM_NAMES
  1686. ):
  1687. defn.info.dataclass_transform_spec = self.parse_dataclass_transform_spec(decorator)
  1688. def clean_up_bases_and_infer_type_variables(
  1689. self, defn: ClassDef, base_type_exprs: list[Expression], context: Context
  1690. ) -> tuple[list[Expression], list[TypeVarLikeType], bool]:
  1691. """Remove extra base classes such as Generic and infer type vars.
  1692. For example, consider this class:
  1693. class Foo(Bar, Generic[T]): ...
  1694. Now we will remove Generic[T] from bases of Foo and infer that the
  1695. type variable 'T' is a type argument of Foo.
  1696. Note that this is performed *before* semantic analysis.
  1697. Returns (remaining base expressions, inferred type variables, is protocol).
  1698. """
  1699. removed: list[int] = []
  1700. declared_tvars: TypeVarLikeList = []
  1701. is_protocol = False
  1702. for i, base_expr in enumerate(base_type_exprs):
  1703. if isinstance(base_expr, StarExpr):
  1704. base_expr.valid = True
  1705. self.analyze_type_expr(base_expr)
  1706. try:
  1707. base = self.expr_to_unanalyzed_type(base_expr)
  1708. except TypeTranslationError:
  1709. # This error will be caught later.
  1710. continue
  1711. result = self.analyze_class_typevar_declaration(base)
  1712. if result is not None:
  1713. if declared_tvars:
  1714. self.fail("Only single Generic[...] or Protocol[...] can be in bases", context)
  1715. removed.append(i)
  1716. tvars = result[0]
  1717. is_protocol |= result[1]
  1718. declared_tvars.extend(tvars)
  1719. if isinstance(base, UnboundType):
  1720. sym = self.lookup_qualified(base.name, base)
  1721. if sym is not None and sym.node is not None:
  1722. if sym.node.fullname in PROTOCOL_NAMES and i not in removed:
  1723. # also remove bare 'Protocol' bases
  1724. removed.append(i)
  1725. is_protocol = True
  1726. all_tvars = self.get_all_bases_tvars(base_type_exprs, removed)
  1727. if declared_tvars:
  1728. if len(remove_dups(declared_tvars)) < len(declared_tvars):
  1729. self.fail("Duplicate type variables in Generic[...] or Protocol[...]", context)
  1730. declared_tvars = remove_dups(declared_tvars)
  1731. if not set(all_tvars).issubset(set(declared_tvars)):
  1732. self.fail(
  1733. "If Generic[...] or Protocol[...] is present"
  1734. " it should list all type variables",
  1735. context,
  1736. )
  1737. # In case of error, Generic tvars will go first
  1738. declared_tvars = remove_dups(declared_tvars + all_tvars)
  1739. else:
  1740. declared_tvars = all_tvars
  1741. for i in reversed(removed):
  1742. # We need to actually remove the base class expressions like Generic[T],
  1743. # mostly because otherwise they will create spurious dependencies in fine
  1744. # grained incremental mode.
  1745. defn.removed_base_type_exprs.append(defn.base_type_exprs[i])
  1746. del base_type_exprs[i]
  1747. tvar_defs: list[TypeVarLikeType] = []
  1748. for name, tvar_expr in declared_tvars:
  1749. tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
  1750. tvar_defs.append(tvar_def)
  1751. return base_type_exprs, tvar_defs, is_protocol
  1752. def analyze_class_typevar_declaration(self, base: Type) -> tuple[TypeVarLikeList, bool] | None:
  1753. """Analyze type variables declared using Generic[...] or Protocol[...].
  1754. Args:
  1755. base: Non-analyzed base class
  1756. Return None if the base class does not declare type variables. Otherwise,
  1757. return the type variables.
  1758. """
  1759. if not isinstance(base, UnboundType):
  1760. return None
  1761. unbound = base
  1762. sym = self.lookup_qualified(unbound.name, unbound)
  1763. if sym is None or sym.node is None:
  1764. return None
  1765. if (
  1766. sym.node.fullname == "typing.Generic"
  1767. or sym.node.fullname in PROTOCOL_NAMES
  1768. and base.args
  1769. ):
  1770. is_proto = sym.node.fullname != "typing.Generic"
  1771. tvars: TypeVarLikeList = []
  1772. have_type_var_tuple = False
  1773. for arg in unbound.args:
  1774. tag = self.track_incomplete_refs()
  1775. tvar = self.analyze_unbound_tvar(arg)
  1776. if tvar:
  1777. if isinstance(tvar[1], TypeVarTupleExpr):
  1778. if have_type_var_tuple:
  1779. self.fail("Can only use one type var tuple in a class def", base)
  1780. continue
  1781. have_type_var_tuple = True
  1782. tvars.append(tvar)
  1783. elif not self.found_incomplete_ref(tag):
  1784. self.fail("Free type variable expected in %s[...]" % sym.node.name, base)
  1785. return tvars, is_proto
  1786. return None
  1787. def analyze_unbound_tvar(self, t: Type) -> tuple[str, TypeVarLikeExpr] | None:
  1788. if not isinstance(t, UnboundType):
  1789. return None
  1790. unbound = t
  1791. sym = self.lookup_qualified(unbound.name, unbound)
  1792. if sym and isinstance(sym.node, PlaceholderNode):
  1793. self.record_incomplete_ref()
  1794. if sym and isinstance(sym.node, ParamSpecExpr):
  1795. if sym.fullname and not self.tvar_scope.allow_binding(sym.fullname):
  1796. # It's bound by our type variable scope
  1797. return None
  1798. return unbound.name, sym.node
  1799. if sym and sym.fullname in ("typing.Unpack", "typing_extensions.Unpack"):
  1800. inner_t = unbound.args[0]
  1801. if not isinstance(inner_t, UnboundType):
  1802. return None
  1803. inner_unbound = inner_t
  1804. inner_sym = self.lookup_qualified(inner_unbound.name, inner_unbound)
  1805. if inner_sym and isinstance(inner_sym.node, PlaceholderNode):
  1806. self.record_incomplete_ref()
  1807. if inner_sym and isinstance(inner_sym.node, TypeVarTupleExpr):
  1808. if inner_sym.fullname and not self.tvar_scope.allow_binding(inner_sym.fullname):
  1809. # It's bound by our type variable scope
  1810. return None
  1811. return inner_unbound.name, inner_sym.node
  1812. if sym is None or not isinstance(sym.node, TypeVarExpr):
  1813. return None
  1814. elif sym.fullname and not self.tvar_scope.allow_binding(sym.fullname):
  1815. # It's bound by our type variable scope
  1816. return None
  1817. else:
  1818. assert isinstance(sym.node, TypeVarExpr)
  1819. return unbound.name, sym.node
  1820. def get_all_bases_tvars(
  1821. self, base_type_exprs: list[Expression], removed: list[int]
  1822. ) -> TypeVarLikeList:
  1823. """Return all type variable references in bases."""
  1824. tvars: TypeVarLikeList = []
  1825. for i, base_expr in enumerate(base_type_exprs):
  1826. if i not in removed:
  1827. try:
  1828. base = self.expr_to_unanalyzed_type(base_expr)
  1829. except TypeTranslationError:
  1830. # This error will be caught later.
  1831. continue
  1832. base_tvars = base.accept(TypeVarLikeQuery(self, self.tvar_scope))
  1833. tvars.extend(base_tvars)
  1834. return remove_dups(tvars)
  1835. def get_and_bind_all_tvars(self, type_exprs: list[Expression]) -> list[TypeVarLikeType]:
  1836. """Return all type variable references in item type expressions.
  1837. This is a helper for generic TypedDicts and NamedTuples. Essentially it is
  1838. a simplified version of the logic we use for ClassDef bases. We duplicate
  1839. some amount of code, because it is hard to refactor common pieces.
  1840. """
  1841. tvars = []
  1842. for base_expr in type_exprs:
  1843. try:
  1844. base = self.expr_to_unanalyzed_type(base_expr)
  1845. except TypeTranslationError:
  1846. # This error will be caught later.
  1847. continue
  1848. base_tvars = base.accept(TypeVarLikeQuery(self, self.tvar_scope))
  1849. tvars.extend(base_tvars)
  1850. tvars = remove_dups(tvars) # Variables are defined in order of textual appearance.
  1851. tvar_defs = []
  1852. for name, tvar_expr in tvars:
  1853. tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
  1854. tvar_defs.append(tvar_def)
  1855. return tvar_defs
  1856. def prepare_class_def(
  1857. self, defn: ClassDef, info: TypeInfo | None = None, custom_names: bool = False
  1858. ) -> None:
  1859. """Prepare for the analysis of a class definition.
  1860. Create an empty TypeInfo and store it in a symbol table, or if the 'info'
  1861. argument is provided, store it instead (used for magic type definitions).
  1862. """
  1863. if not defn.info:
  1864. defn.fullname = self.qualified_name(defn.name)
  1865. # TODO: Nested classes
  1866. info = info or self.make_empty_type_info(defn)
  1867. defn.info = info
  1868. info.defn = defn
  1869. if not custom_names:
  1870. # Some special classes (in particular NamedTuples) use custom fullname logic.
  1871. # Don't override it here (also see comment below, this needs cleanup).
  1872. if not self.is_func_scope():
  1873. info._fullname = self.qualified_name(defn.name)
  1874. else:
  1875. info._fullname = info.name
  1876. local_name = defn.name
  1877. if "@" in local_name:
  1878. local_name = local_name.split("@")[0]
  1879. self.add_symbol(local_name, defn.info, defn)
  1880. if self.is_nested_within_func_scope():
  1881. # We need to preserve local classes, let's store them
  1882. # in globals under mangled unique names
  1883. #
  1884. # TODO: Putting local classes into globals breaks assumptions in fine-grained
  1885. # incremental mode and we should avoid it. In general, this logic is too
  1886. # ad-hoc and needs to be removed/refactored.
  1887. if "@" not in defn.info._fullname:
  1888. global_name = defn.info.name + "@" + str(defn.line)
  1889. defn.info._fullname = self.cur_mod_id + "." + global_name
  1890. else:
  1891. # Preserve name from previous fine-grained incremental run.
  1892. global_name = defn.info.name
  1893. defn.fullname = defn.info._fullname
  1894. if defn.info.is_named_tuple:
  1895. # Named tuple nested within a class is stored in the class symbol table.
  1896. self.add_symbol_skip_local(global_name, defn.info)
  1897. else:
  1898. self.globals[global_name] = SymbolTableNode(GDEF, defn.info)
  1899. def make_empty_type_info(self, defn: ClassDef) -> TypeInfo:
  1900. if (
  1901. self.is_module_scope()
  1902. and self.cur_mod_id == "builtins"
  1903. and defn.name in CORE_BUILTIN_CLASSES
  1904. ):
  1905. # Special case core built-in classes. A TypeInfo was already
  1906. # created for it before semantic analysis, but with a dummy
  1907. # ClassDef. Patch the real ClassDef object.
  1908. info = self.globals[defn.name].node
  1909. assert isinstance(info, TypeInfo)
  1910. else:
  1911. info = TypeInfo(SymbolTable(), defn, self.cur_mod_id)
  1912. info.set_line(defn)
  1913. return info
  1914. def get_name_repr_of_expr(self, expr: Expression) -> str | None:
  1915. """Try finding a short simplified textual representation of a base class expression."""
  1916. if isinstance(expr, NameExpr):
  1917. return expr.name
  1918. if isinstance(expr, MemberExpr):
  1919. return get_member_expr_fullname(expr)
  1920. if isinstance(expr, IndexExpr):
  1921. return self.get_name_repr_of_expr(expr.base)
  1922. if isinstance(expr, CallExpr):
  1923. return self.get_name_repr_of_expr(expr.callee)
  1924. return None
  1925. def analyze_base_classes(
  1926. self, base_type_exprs: list[Expression]
  1927. ) -> tuple[list[tuple[ProperType, Expression]], bool] | None:
  1928. """Analyze base class types.
  1929. Return None if some definition was incomplete. Otherwise, return a tuple
  1930. with these items:
  1931. * List of (analyzed type, original expression) tuples
  1932. * Boolean indicating whether one of the bases had a semantic analysis error
  1933. """
  1934. is_error = False
  1935. bases = []
  1936. for base_expr in base_type_exprs:
  1937. if (
  1938. isinstance(base_expr, RefExpr)
  1939. and base_expr.fullname in TYPED_NAMEDTUPLE_NAMES + TPDICT_NAMES
  1940. ):
  1941. # Ignore magic bases for now.
  1942. continue
  1943. try:
  1944. base = self.expr_to_analyzed_type(
  1945. base_expr, allow_placeholder=True, allow_type_any=True
  1946. )
  1947. except TypeTranslationError:
  1948. name = self.get_name_repr_of_expr(base_expr)
  1949. if isinstance(base_expr, CallExpr):
  1950. msg = "Unsupported dynamic base class"
  1951. else:
  1952. msg = "Invalid base class"
  1953. if name:
  1954. msg += f' "{name}"'
  1955. self.fail(msg, base_expr)
  1956. is_error = True
  1957. continue
  1958. if base is None:
  1959. return None
  1960. base = get_proper_type(base)
  1961. bases.append((base, base_expr))
  1962. return bases, is_error
  1963. def configure_base_classes(
  1964. self, defn: ClassDef, bases: list[tuple[ProperType, Expression]]
  1965. ) -> None:
  1966. """Set up base classes.
  1967. This computes several attributes on the corresponding TypeInfo defn.info
  1968. related to the base classes: defn.info.bases, defn.info.mro, and
  1969. miscellaneous others (at least tuple_type, fallback_to_any, and is_enum.)
  1970. """
  1971. base_types: list[Instance] = []
  1972. info = defn.info
  1973. for base, base_expr in bases:
  1974. if isinstance(base, TupleType):
  1975. actual_base = self.configure_tuple_base_class(defn, base)
  1976. base_types.append(actual_base)
  1977. elif isinstance(base, Instance):
  1978. if base.type.is_newtype:
  1979. self.fail('Cannot subclass "NewType"', defn)
  1980. base_types.append(base)
  1981. elif isinstance(base, AnyType):
  1982. if self.options.disallow_subclassing_any:
  1983. if isinstance(base_expr, (NameExpr, MemberExpr)):
  1984. msg = f'Class cannot subclass "{base_expr.name}" (has type "Any")'
  1985. else:
  1986. msg = 'Class cannot subclass value of type "Any"'
  1987. self.fail(msg, base_expr)
  1988. info.fallback_to_any = True
  1989. elif isinstance(base, TypedDictType):
  1990. base_types.append(base.fallback)
  1991. else:
  1992. msg = "Invalid base class"
  1993. name = self.get_name_repr_of_expr(base_expr)
  1994. if name:
  1995. msg += f' "{name}"'
  1996. self.fail(msg, base_expr)
  1997. info.fallback_to_any = True
  1998. if self.options.disallow_any_unimported and has_any_from_unimported_type(base):
  1999. if isinstance(base_expr, (NameExpr, MemberExpr)):
  2000. prefix = f"Base type {base_expr.name}"
  2001. else:
  2002. prefix = "Base type"
  2003. self.msg.unimported_type_becomes_any(prefix, base, base_expr)
  2004. check_for_explicit_any(
  2005. base, self.options, self.is_typeshed_stub_file, self.msg, context=base_expr
  2006. )
  2007. # Add 'object' as implicit base if there is no other base class.
  2008. if not base_types and defn.fullname != "builtins.object":
  2009. base_types.append(self.object_type())
  2010. info.bases = base_types
  2011. # Calculate the MRO.
  2012. if not self.verify_base_classes(defn):
  2013. self.set_dummy_mro(defn.info)
  2014. return
  2015. if not self.verify_duplicate_base_classes(defn):
  2016. # We don't want to block the typechecking process,
  2017. # so, we just insert `Any` as the base class and show an error.
  2018. self.set_any_mro(defn.info)
  2019. self.calculate_class_mro(defn, self.object_type)
  2020. def configure_tuple_base_class(self, defn: ClassDef, base: TupleType) -> Instance:
  2021. info = defn.info
  2022. # There may be an existing valid tuple type from previous semanal iterations.
  2023. # Use equality to check if it is the case.
  2024. if info.tuple_type and info.tuple_type != base and not has_placeholder(info.tuple_type):
  2025. self.fail("Class has two incompatible bases derived from tuple", defn)
  2026. defn.has_incompatible_baseclass = True
  2027. if info.special_alias and has_placeholder(info.special_alias.target):
  2028. self.process_placeholder(
  2029. None, "tuple base", defn, force_progress=base != info.tuple_type
  2030. )
  2031. info.update_tuple_type(base)
  2032. self.setup_alias_type_vars(defn)
  2033. if base.partial_fallback.type.fullname == "builtins.tuple" and not has_placeholder(base):
  2034. # Fallback can only be safely calculated after semantic analysis, since base
  2035. # classes may be incomplete. Postpone the calculation.
  2036. self.schedule_patch(PRIORITY_FALLBACKS, lambda: calculate_tuple_fallback(base))
  2037. return base.partial_fallback
  2038. def set_dummy_mro(self, info: TypeInfo) -> None:
  2039. # Give it an MRO consisting of just the class itself and object.
  2040. info.mro = [info, self.object_type().type]
  2041. info.bad_mro = True
  2042. def set_any_mro(self, info: TypeInfo) -> None:
  2043. # Give it an MRO consisting direct `Any` subclass.
  2044. info.fallback_to_any = True
  2045. info.mro = [info, self.object_type().type]
  2046. def calculate_class_mro(
  2047. self, defn: ClassDef, obj_type: Callable[[], Instance] | None = None
  2048. ) -> None:
  2049. """Calculate method resolution order for a class.
  2050. `obj_type` exists just to fill in empty base class list in case of an error.
  2051. """
  2052. try:
  2053. calculate_mro(defn.info, obj_type)
  2054. except MroError:
  2055. self.fail(
  2056. "Cannot determine consistent method resolution "
  2057. 'order (MRO) for "%s"' % defn.name,
  2058. defn,
  2059. )
  2060. self.set_dummy_mro(defn.info)
  2061. # Allow plugins to alter the MRO to handle the fact that `def mro()`
  2062. # on metaclasses permits MRO rewriting.
  2063. if defn.fullname:
  2064. hook = self.plugin.get_customize_class_mro_hook(defn.fullname)
  2065. if hook:
  2066. hook(ClassDefContext(defn, FakeExpression(), self))
  2067. def infer_metaclass_and_bases_from_compat_helpers(self, defn: ClassDef) -> None:
  2068. """Lookup for special metaclass declarations, and update defn fields accordingly.
  2069. * six.with_metaclass(M, B1, B2, ...)
  2070. * @six.add_metaclass(M)
  2071. * future.utils.with_metaclass(M, B1, B2, ...)
  2072. * past.utils.with_metaclass(M, B1, B2, ...)
  2073. """
  2074. # Look for six.with_metaclass(M, B1, B2, ...)
  2075. with_meta_expr: Expression | None = None
  2076. if len(defn.base_type_exprs) == 1:
  2077. base_expr = defn.base_type_exprs[0]
  2078. if isinstance(base_expr, CallExpr) and isinstance(base_expr.callee, RefExpr):
  2079. self.analyze_type_expr(base_expr)
  2080. if (
  2081. base_expr.callee.fullname
  2082. in {
  2083. "six.with_metaclass",
  2084. "future.utils.with_metaclass",
  2085. "past.utils.with_metaclass",
  2086. }
  2087. and len(base_expr.args) >= 1
  2088. and all(kind == ARG_POS for kind in base_expr.arg_kinds)
  2089. ):
  2090. with_meta_expr = base_expr.args[0]
  2091. defn.base_type_exprs = base_expr.args[1:]
  2092. # Look for @six.add_metaclass(M)
  2093. add_meta_expr: Expression | None = None
  2094. for dec_expr in defn.decorators:
  2095. if isinstance(dec_expr, CallExpr) and isinstance(dec_expr.callee, RefExpr):
  2096. dec_expr.callee.accept(self)
  2097. if (
  2098. dec_expr.callee.fullname == "six.add_metaclass"
  2099. and len(dec_expr.args) == 1
  2100. and dec_expr.arg_kinds[0] == ARG_POS
  2101. ):
  2102. add_meta_expr = dec_expr.args[0]
  2103. break
  2104. metas = {defn.metaclass, with_meta_expr, add_meta_expr} - {None}
  2105. if len(metas) == 0:
  2106. return
  2107. if len(metas) > 1:
  2108. self.fail("Multiple metaclass definitions", defn)
  2109. return
  2110. defn.metaclass = metas.pop()
  2111. def verify_base_classes(self, defn: ClassDef) -> bool:
  2112. info = defn.info
  2113. cycle = False
  2114. for base in info.bases:
  2115. baseinfo = base.type
  2116. if self.is_base_class(info, baseinfo):
  2117. self.fail("Cycle in inheritance hierarchy", defn)
  2118. cycle = True
  2119. return not cycle
  2120. def verify_duplicate_base_classes(self, defn: ClassDef) -> bool:
  2121. dup = find_duplicate(defn.info.direct_base_classes())
  2122. if dup:
  2123. self.fail(f'Duplicate base class "{dup.name}"', defn)
  2124. return not dup
  2125. def is_base_class(self, t: TypeInfo, s: TypeInfo) -> bool:
  2126. """Determine if t is a base class of s (but do not use mro)."""
  2127. # Search the base class graph for t, starting from s.
  2128. worklist = [s]
  2129. visited = {s}
  2130. while worklist:
  2131. nxt = worklist.pop()
  2132. if nxt == t:
  2133. return True
  2134. for base in nxt.bases:
  2135. if base.type not in visited:
  2136. worklist.append(base.type)
  2137. visited.add(base.type)
  2138. return False
  2139. def get_declared_metaclass(
  2140. self, name: str, metaclass_expr: Expression | None
  2141. ) -> tuple[Instance | None, bool, bool]:
  2142. """Get declared metaclass from metaclass expression.
  2143. Returns a tuple of three values:
  2144. * A metaclass instance or None
  2145. * A boolean indicating whether we should defer
  2146. * A boolean indicating whether we should set metaclass Any fallback
  2147. (either for Any metaclass or invalid/dynamic metaclass).
  2148. The two boolean flags can only be True if instance is None.
  2149. """
  2150. declared_metaclass = None
  2151. if metaclass_expr:
  2152. metaclass_name = None
  2153. if isinstance(metaclass_expr, NameExpr):
  2154. metaclass_name = metaclass_expr.name
  2155. elif isinstance(metaclass_expr, MemberExpr):
  2156. metaclass_name = get_member_expr_fullname(metaclass_expr)
  2157. if metaclass_name is None:
  2158. self.fail(f'Dynamic metaclass not supported for "{name}"', metaclass_expr)
  2159. return None, False, True
  2160. sym = self.lookup_qualified(metaclass_name, metaclass_expr)
  2161. if sym is None:
  2162. # Probably a name error - it is already handled elsewhere
  2163. return None, False, True
  2164. if isinstance(sym.node, Var) and isinstance(get_proper_type(sym.node.type), AnyType):
  2165. if self.options.disallow_subclassing_any:
  2166. self.fail(
  2167. f'Class cannot use "{sym.node.name}" as a metaclass (has type "Any")',
  2168. metaclass_expr,
  2169. )
  2170. return None, False, True
  2171. if isinstance(sym.node, PlaceholderNode):
  2172. return None, True, False # defer later in the caller
  2173. # Support type aliases, like `_Meta: TypeAlias = type`
  2174. if (
  2175. isinstance(sym.node, TypeAlias)
  2176. and sym.node.no_args
  2177. and isinstance(sym.node.target, ProperType)
  2178. and isinstance(sym.node.target, Instance)
  2179. ):
  2180. metaclass_info: Node | None = sym.node.target.type
  2181. else:
  2182. metaclass_info = sym.node
  2183. if not isinstance(metaclass_info, TypeInfo) or metaclass_info.tuple_type is not None:
  2184. self.fail(f'Invalid metaclass "{metaclass_name}"', metaclass_expr)
  2185. return None, False, False
  2186. if not metaclass_info.is_metaclass():
  2187. self.fail(
  2188. 'Metaclasses not inheriting from "type" are not supported', metaclass_expr
  2189. )
  2190. return None, False, False
  2191. inst = fill_typevars(metaclass_info)
  2192. assert isinstance(inst, Instance)
  2193. declared_metaclass = inst
  2194. return declared_metaclass, False, False
  2195. def recalculate_metaclass(self, defn: ClassDef, declared_metaclass: Instance | None) -> None:
  2196. defn.info.declared_metaclass = declared_metaclass
  2197. defn.info.metaclass_type = defn.info.calculate_metaclass_type()
  2198. if any(info.is_protocol for info in defn.info.mro):
  2199. if (
  2200. not defn.info.metaclass_type
  2201. or defn.info.metaclass_type.type.fullname == "builtins.type"
  2202. ):
  2203. # All protocols and their subclasses have ABCMeta metaclass by default.
  2204. # TODO: add a metaclass conflict check if there is another metaclass.
  2205. abc_meta = self.named_type_or_none("abc.ABCMeta", [])
  2206. if abc_meta is not None: # May be None in tests with incomplete lib-stub.
  2207. defn.info.metaclass_type = abc_meta
  2208. if defn.info.metaclass_type and defn.info.metaclass_type.type.has_base("enum.EnumMeta"):
  2209. defn.info.is_enum = True
  2210. if defn.type_vars:
  2211. self.fail("Enum class cannot be generic", defn)
  2212. #
  2213. # Imports
  2214. #
  2215. def visit_import(self, i: Import) -> None:
  2216. self.statement = i
  2217. for id, as_id in i.ids:
  2218. # Modules imported in a stub file without using 'import X as X' won't get exported
  2219. # When implicit re-exporting is disabled, we have the same behavior as stubs.
  2220. use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
  2221. if as_id is not None:
  2222. base_id = id
  2223. imported_id = as_id
  2224. module_public = use_implicit_reexport or id.split(".")[-1] == as_id
  2225. else:
  2226. base_id = id.split(".")[0]
  2227. imported_id = base_id
  2228. module_public = use_implicit_reexport
  2229. if base_id in self.modules:
  2230. node = self.modules[base_id]
  2231. if self.is_func_scope():
  2232. kind = LDEF
  2233. elif self.type is not None:
  2234. kind = MDEF
  2235. else:
  2236. kind = GDEF
  2237. symbol = SymbolTableNode(
  2238. kind, node, module_public=module_public, module_hidden=not module_public
  2239. )
  2240. self.add_imported_symbol(
  2241. imported_id,
  2242. symbol,
  2243. context=i,
  2244. module_public=module_public,
  2245. module_hidden=not module_public,
  2246. )
  2247. else:
  2248. self.add_unknown_imported_symbol(
  2249. imported_id,
  2250. context=i,
  2251. target_name=base_id,
  2252. module_public=module_public,
  2253. module_hidden=not module_public,
  2254. )
  2255. def visit_import_from(self, imp: ImportFrom) -> None:
  2256. self.statement = imp
  2257. module_id = self.correct_relative_import(imp)
  2258. module = self.modules.get(module_id)
  2259. for id, as_id in imp.names:
  2260. fullname = module_id + "." + id
  2261. self.set_future_import_flags(fullname)
  2262. if module is None:
  2263. node = None
  2264. elif module_id == self.cur_mod_id and fullname in self.modules:
  2265. # Submodule takes precedence over definition in surround package, for
  2266. # compatibility with runtime semantics in typical use cases. This
  2267. # could more precisely model runtime semantics by taking into account
  2268. # the line number beyond which the local definition should take
  2269. # precedence, but doesn't seem to be important in most use cases.
  2270. node = SymbolTableNode(GDEF, self.modules[fullname])
  2271. else:
  2272. if id == as_id == "__all__" and module_id in self.export_map:
  2273. self.all_exports[:] = self.export_map[module_id]
  2274. node = module.names.get(id)
  2275. missing_submodule = False
  2276. imported_id = as_id or id
  2277. # Modules imported in a stub file without using 'from Y import X as X' will
  2278. # not get exported.
  2279. # When implicit re-exporting is disabled, we have the same behavior as stubs.
  2280. use_implicit_reexport = not self.is_stub_file and self.options.implicit_reexport
  2281. module_public = use_implicit_reexport or (as_id is not None and id == as_id)
  2282. # If the module does not contain a symbol with the name 'id',
  2283. # try checking if it's a module instead.
  2284. if not node:
  2285. mod = self.modules.get(fullname)
  2286. if mod is not None:
  2287. kind = self.current_symbol_kind()
  2288. node = SymbolTableNode(kind, mod)
  2289. elif fullname in self.missing_modules:
  2290. missing_submodule = True
  2291. # If it is still not resolved, check for a module level __getattr__
  2292. if (
  2293. module
  2294. and not node
  2295. and (module.is_stub or self.options.python_version >= (3, 7))
  2296. and "__getattr__" in module.names
  2297. ):
  2298. # We store the fullname of the original definition so that we can
  2299. # detect whether two imported names refer to the same thing.
  2300. fullname = module_id + "." + id
  2301. gvar = self.create_getattr_var(module.names["__getattr__"], imported_id, fullname)
  2302. if gvar:
  2303. self.add_symbol(
  2304. imported_id,
  2305. gvar,
  2306. imp,
  2307. module_public=module_public,
  2308. module_hidden=not module_public,
  2309. )
  2310. continue
  2311. if node:
  2312. self.process_imported_symbol(
  2313. node, module_id, id, imported_id, fullname, module_public, context=imp
  2314. )
  2315. if node.module_hidden:
  2316. self.report_missing_module_attribute(
  2317. module_id,
  2318. id,
  2319. imported_id,
  2320. module_public=module_public,
  2321. module_hidden=not module_public,
  2322. context=imp,
  2323. add_unknown_imported_symbol=False,
  2324. )
  2325. elif module and not missing_submodule:
  2326. # Target module exists but the imported name is missing or hidden.
  2327. self.report_missing_module_attribute(
  2328. module_id,
  2329. id,
  2330. imported_id,
  2331. module_public=module_public,
  2332. module_hidden=not module_public,
  2333. context=imp,
  2334. )
  2335. else:
  2336. # Import of a missing (sub)module.
  2337. self.add_unknown_imported_symbol(
  2338. imported_id,
  2339. imp,
  2340. target_name=fullname,
  2341. module_public=module_public,
  2342. module_hidden=not module_public,
  2343. )
  2344. def process_imported_symbol(
  2345. self,
  2346. node: SymbolTableNode,
  2347. module_id: str,
  2348. id: str,
  2349. imported_id: str,
  2350. fullname: str,
  2351. module_public: bool,
  2352. context: ImportBase,
  2353. ) -> None:
  2354. module_hidden = not module_public and (
  2355. # `from package import submodule` should work regardless of whether package
  2356. # re-exports submodule, so we shouldn't hide it
  2357. not isinstance(node.node, MypyFile)
  2358. or fullname not in self.modules
  2359. # but given `from somewhere import random_unrelated_module` we should hide
  2360. # random_unrelated_module
  2361. or not fullname.startswith(self.cur_mod_id + ".")
  2362. )
  2363. if isinstance(node.node, PlaceholderNode):
  2364. if self.final_iteration:
  2365. self.report_missing_module_attribute(
  2366. module_id,
  2367. id,
  2368. imported_id,
  2369. module_public=module_public,
  2370. module_hidden=module_hidden,
  2371. context=context,
  2372. )
  2373. return
  2374. else:
  2375. # This might become a type.
  2376. self.mark_incomplete(
  2377. imported_id,
  2378. node.node,
  2379. module_public=module_public,
  2380. module_hidden=module_hidden,
  2381. becomes_typeinfo=True,
  2382. )
  2383. # NOTE: we take the original node even for final `Var`s. This is to support
  2384. # a common pattern when constants are re-exported (same applies to import *).
  2385. self.add_imported_symbol(
  2386. imported_id, node, context, module_public=module_public, module_hidden=module_hidden
  2387. )
  2388. def report_missing_module_attribute(
  2389. self,
  2390. import_id: str,
  2391. source_id: str,
  2392. imported_id: str,
  2393. module_public: bool,
  2394. module_hidden: bool,
  2395. context: Node,
  2396. add_unknown_imported_symbol: bool = True,
  2397. ) -> None:
  2398. # Missing attribute.
  2399. if self.is_incomplete_namespace(import_id):
  2400. # We don't know whether the name will be there, since the namespace
  2401. # is incomplete. Defer the current target.
  2402. self.mark_incomplete(
  2403. imported_id, context, module_public=module_public, module_hidden=module_hidden
  2404. )
  2405. return
  2406. message = f'Module "{import_id}" has no attribute "{source_id}"'
  2407. # Suggest alternatives, if any match is found.
  2408. module = self.modules.get(import_id)
  2409. if module:
  2410. if source_id in module.names.keys() and not module.names[source_id].module_public:
  2411. message = (
  2412. f'Module "{import_id}" does not explicitly export attribute "{source_id}"'
  2413. )
  2414. else:
  2415. alternatives = set(module.names.keys()).difference({source_id})
  2416. matches = best_matches(source_id, alternatives, n=3)
  2417. if matches:
  2418. suggestion = f"; maybe {pretty_seq(matches, 'or')}?"
  2419. message += f"{suggestion}"
  2420. self.fail(message, context, code=codes.ATTR_DEFINED)
  2421. if add_unknown_imported_symbol:
  2422. self.add_unknown_imported_symbol(
  2423. imported_id,
  2424. context,
  2425. target_name=None,
  2426. module_public=module_public,
  2427. module_hidden=not module_public,
  2428. )
  2429. if import_id == "typing":
  2430. # The user probably has a missing definition in a test fixture. Let's verify.
  2431. fullname = f"builtins.{source_id.lower()}"
  2432. if (
  2433. self.lookup_fully_qualified_or_none(fullname) is None
  2434. and fullname in SUGGESTED_TEST_FIXTURES
  2435. ):
  2436. # Yes. Generate a helpful note.
  2437. self.msg.add_fixture_note(fullname, context)
  2438. else:
  2439. typing_extensions = self.modules.get("typing_extensions")
  2440. if typing_extensions and source_id in typing_extensions.names:
  2441. self.msg.note(
  2442. f"Use `from typing_extensions import {source_id}` instead",
  2443. context,
  2444. code=codes.ATTR_DEFINED,
  2445. )
  2446. self.msg.note(
  2447. "See https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-new-additions-to-the-typing-module",
  2448. context,
  2449. code=codes.ATTR_DEFINED,
  2450. )
  2451. def process_import_over_existing_name(
  2452. self,
  2453. imported_id: str,
  2454. existing_symbol: SymbolTableNode,
  2455. module_symbol: SymbolTableNode,
  2456. import_node: ImportBase,
  2457. ) -> bool:
  2458. if existing_symbol.node is module_symbol.node:
  2459. # We added this symbol on previous iteration.
  2460. return False
  2461. if existing_symbol.kind in (LDEF, GDEF, MDEF) and isinstance(
  2462. existing_symbol.node, (Var, FuncDef, TypeInfo, Decorator, TypeAlias)
  2463. ):
  2464. # This is a valid import over an existing definition in the file. Construct a dummy
  2465. # assignment that we'll use to type check the import.
  2466. lvalue = NameExpr(imported_id)
  2467. lvalue.kind = existing_symbol.kind
  2468. lvalue.node = existing_symbol.node
  2469. rvalue = NameExpr(imported_id)
  2470. rvalue.kind = module_symbol.kind
  2471. rvalue.node = module_symbol.node
  2472. if isinstance(rvalue.node, TypeAlias):
  2473. # Suppress bogus errors from the dummy assignment if rvalue is an alias.
  2474. # Otherwise mypy may complain that alias is invalid in runtime context.
  2475. rvalue.is_alias_rvalue = True
  2476. assignment = AssignmentStmt([lvalue], rvalue)
  2477. for node in assignment, lvalue, rvalue:
  2478. node.set_line(import_node)
  2479. import_node.assignments.append(assignment)
  2480. return True
  2481. return False
  2482. def correct_relative_import(self, node: ImportFrom | ImportAll) -> str:
  2483. import_id, ok = correct_relative_import(
  2484. self.cur_mod_id, node.relative, node.id, self.cur_mod_node.is_package_init_file()
  2485. )
  2486. if not ok:
  2487. self.fail("Relative import climbs too many namespaces", node)
  2488. return import_id
  2489. def visit_import_all(self, i: ImportAll) -> None:
  2490. i_id = self.correct_relative_import(i)
  2491. if i_id in self.modules:
  2492. m = self.modules[i_id]
  2493. if self.is_incomplete_namespace(i_id):
  2494. # Any names could be missing from the current namespace if the target module
  2495. # namespace is incomplete.
  2496. self.mark_incomplete("*", i)
  2497. for name, node in m.names.items():
  2498. fullname = i_id + "." + name
  2499. self.set_future_import_flags(fullname)
  2500. if node is None:
  2501. continue
  2502. # if '__all__' exists, all nodes not included have had module_public set to
  2503. # False, and we can skip checking '_' because it's been explicitly included.
  2504. if node.module_public and (not name.startswith("_") or "__all__" in m.names):
  2505. if isinstance(node.node, MypyFile):
  2506. # Star import of submodule from a package, add it as a dependency.
  2507. self.imports.add(node.node.fullname)
  2508. # `from x import *` always reexports symbols
  2509. self.add_imported_symbol(
  2510. name, node, context=i, module_public=True, module_hidden=False
  2511. )
  2512. else:
  2513. # Don't add any dummy symbols for 'from x import *' if 'x' is unknown.
  2514. pass
  2515. #
  2516. # Assignment
  2517. #
  2518. def visit_assignment_expr(self, s: AssignmentExpr) -> None:
  2519. s.value.accept(self)
  2520. if self.is_func_scope():
  2521. if not self.check_valid_comprehension(s):
  2522. return
  2523. self.analyze_lvalue(s.target, escape_comprehensions=True, has_explicit_value=True)
  2524. def check_valid_comprehension(self, s: AssignmentExpr) -> bool:
  2525. """Check that assignment expression is not nested within comprehension at class scope.
  2526. class C:
  2527. [(j := i) for i in [1, 2, 3]]
  2528. is a syntax error that is not enforced by Python parser, but at later steps.
  2529. """
  2530. for i, is_comprehension in enumerate(reversed(self.is_comprehension_stack)):
  2531. if not is_comprehension and i < len(self.locals) - 1:
  2532. if self.locals[-1 - i] is None:
  2533. self.fail(
  2534. "Assignment expression within a comprehension"
  2535. " cannot be used in a class body",
  2536. s,
  2537. code=codes.SYNTAX,
  2538. serious=True,
  2539. blocker=True,
  2540. )
  2541. return False
  2542. break
  2543. return True
  2544. def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
  2545. self.statement = s
  2546. # Special case assignment like X = X.
  2547. if self.analyze_identity_global_assignment(s):
  2548. return
  2549. tag = self.track_incomplete_refs()
  2550. # Here we have a chicken and egg problem: at this stage we can't call
  2551. # can_be_type_alias(), because we have not enough information about rvalue.
  2552. # But we can't use a full visit because it may emit extra incomplete refs (namely
  2553. # when analysing any type applications there) thus preventing the further analysis.
  2554. # To break the tie, we first analyse rvalue partially, if it can be a type alias.
  2555. if self.can_possibly_be_type_form(s):
  2556. old_basic_type_applications = self.basic_type_applications
  2557. self.basic_type_applications = True
  2558. with self.allow_unbound_tvars_set():
  2559. s.rvalue.accept(self)
  2560. self.basic_type_applications = old_basic_type_applications
  2561. else:
  2562. s.rvalue.accept(self)
  2563. if self.found_incomplete_ref(tag) or self.should_wait_rhs(s.rvalue):
  2564. # Initializer couldn't be fully analyzed. Defer the current node and give up.
  2565. # Make sure that if we skip the definition of some local names, they can't be
  2566. # added later in this scope, since an earlier definition should take precedence.
  2567. for expr in names_modified_by_assignment(s):
  2568. self.mark_incomplete(expr.name, expr)
  2569. return
  2570. if self.can_possibly_be_type_form(s):
  2571. # Now re-visit those rvalues that were we skipped type applications above.
  2572. # This should be safe as generally semantic analyzer is idempotent.
  2573. with self.allow_unbound_tvars_set():
  2574. s.rvalue.accept(self)
  2575. # The r.h.s. is now ready to be classified, first check if it is a special form:
  2576. special_form = False
  2577. # * type alias
  2578. if self.check_and_set_up_type_alias(s):
  2579. s.is_alias_def = True
  2580. special_form = True
  2581. # * type variable definition
  2582. elif self.process_typevar_declaration(s):
  2583. special_form = True
  2584. elif self.process_paramspec_declaration(s):
  2585. special_form = True
  2586. elif self.process_typevartuple_declaration(s):
  2587. special_form = True
  2588. # * type constructors
  2589. elif self.analyze_namedtuple_assign(s):
  2590. special_form = True
  2591. elif self.analyze_typeddict_assign(s):
  2592. special_form = True
  2593. elif self.newtype_analyzer.process_newtype_declaration(s):
  2594. special_form = True
  2595. elif self.analyze_enum_assign(s):
  2596. special_form = True
  2597. if special_form:
  2598. self.record_special_form_lvalue(s)
  2599. return
  2600. # Clear the alias flag if assignment turns out not a special form after all. It
  2601. # may be set to True while there were still placeholders due to forward refs.
  2602. s.is_alias_def = False
  2603. # OK, this is a regular assignment, perform the necessary analysis steps.
  2604. s.is_final_def = self.unwrap_final(s)
  2605. self.analyze_lvalues(s)
  2606. self.check_final_implicit_def(s)
  2607. self.store_final_status(s)
  2608. self.check_classvar(s)
  2609. self.process_type_annotation(s)
  2610. self.apply_dynamic_class_hook(s)
  2611. if not s.type:
  2612. self.process_module_assignment(s.lvalues, s.rvalue, s)
  2613. self.process__all__(s)
  2614. self.process__deletable__(s)
  2615. self.process__slots__(s)
  2616. def analyze_identity_global_assignment(self, s: AssignmentStmt) -> bool:
  2617. """Special case 'X = X' in global scope.
  2618. This allows supporting some important use cases.
  2619. Return true if special casing was applied.
  2620. """
  2621. if not isinstance(s.rvalue, NameExpr) or len(s.lvalues) != 1:
  2622. # Not of form 'X = X'
  2623. return False
  2624. lvalue = s.lvalues[0]
  2625. if not isinstance(lvalue, NameExpr) or s.rvalue.name != lvalue.name:
  2626. # Not of form 'X = X'
  2627. return False
  2628. if self.type is not None or self.is_func_scope():
  2629. # Not in global scope
  2630. return False
  2631. # It's an assignment like 'X = X' in the global scope.
  2632. name = lvalue.name
  2633. sym = self.lookup(name, s)
  2634. if sym is None:
  2635. if self.final_iteration:
  2636. # Fall back to normal assignment analysis.
  2637. return False
  2638. else:
  2639. self.defer()
  2640. return True
  2641. else:
  2642. if sym.node is None:
  2643. # Something special -- fall back to normal assignment analysis.
  2644. return False
  2645. if name not in self.globals:
  2646. # The name is from builtins. Add an alias to the current module.
  2647. self.add_symbol(name, sym.node, s)
  2648. if not isinstance(sym.node, PlaceholderNode):
  2649. for node in s.rvalue, lvalue:
  2650. node.node = sym.node
  2651. node.kind = GDEF
  2652. node.fullname = sym.node.fullname
  2653. return True
  2654. def should_wait_rhs(self, rv: Expression) -> bool:
  2655. """Can we already classify this r.h.s. of an assignment or should we wait?
  2656. This returns True if we don't have enough information to decide whether
  2657. an assignment is just a normal variable definition or a special form.
  2658. Always return False if this is a final iteration. This will typically cause
  2659. the lvalue to be classified as a variable plus emit an error.
  2660. """
  2661. if self.final_iteration:
  2662. # No chance, nothing has changed.
  2663. return False
  2664. if isinstance(rv, NameExpr):
  2665. n = self.lookup(rv.name, rv)
  2666. if n and isinstance(n.node, PlaceholderNode) and not n.node.becomes_typeinfo:
  2667. return True
  2668. elif isinstance(rv, MemberExpr):
  2669. fname = get_member_expr_fullname(rv)
  2670. if fname:
  2671. n = self.lookup_qualified(fname, rv, suppress_errors=True)
  2672. if n and isinstance(n.node, PlaceholderNode) and not n.node.becomes_typeinfo:
  2673. return True
  2674. elif isinstance(rv, IndexExpr) and isinstance(rv.base, RefExpr):
  2675. return self.should_wait_rhs(rv.base)
  2676. elif isinstance(rv, CallExpr) and isinstance(rv.callee, RefExpr):
  2677. # This is only relevant for builtin SCC where things like 'TypeVar'
  2678. # may be not ready.
  2679. return self.should_wait_rhs(rv.callee)
  2680. return False
  2681. def can_be_type_alias(self, rv: Expression, allow_none: bool = False) -> bool:
  2682. """Is this a valid r.h.s. for an alias definition?
  2683. Note: this function should be only called for expressions where self.should_wait_rhs()
  2684. returns False.
  2685. """
  2686. if isinstance(rv, RefExpr) and self.is_type_ref(rv, bare=True):
  2687. return True
  2688. if isinstance(rv, IndexExpr) and self.is_type_ref(rv.base, bare=False):
  2689. return True
  2690. if self.is_none_alias(rv):
  2691. return True
  2692. if allow_none and isinstance(rv, NameExpr) and rv.fullname == "builtins.None":
  2693. return True
  2694. if isinstance(rv, OpExpr) and rv.op == "|":
  2695. if self.is_stub_file:
  2696. return True
  2697. if self.can_be_type_alias(rv.left, allow_none=True) and self.can_be_type_alias(
  2698. rv.right, allow_none=True
  2699. ):
  2700. return True
  2701. return False
  2702. def can_possibly_be_type_form(self, s: AssignmentStmt) -> bool:
  2703. """Like can_be_type_alias(), but simpler and doesn't require fully analyzed rvalue.
  2704. Instead, use lvalues/annotations structure to figure out whether this can potentially be
  2705. a type alias definition, NamedTuple, or TypedDict. Another difference from above function
  2706. is that we are only interested IndexExpr, CallExpr and OpExpr rvalues, since only those
  2707. can be potentially recursive (things like `A = A` are never valid).
  2708. """
  2709. if len(s.lvalues) > 1:
  2710. return False
  2711. if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.callee, RefExpr):
  2712. ref = s.rvalue.callee.fullname
  2713. return ref in TPDICT_NAMES or ref in TYPED_NAMEDTUPLE_NAMES
  2714. if not isinstance(s.lvalues[0], NameExpr):
  2715. return False
  2716. if s.unanalyzed_type is not None and not self.is_pep_613(s):
  2717. return False
  2718. if not isinstance(s.rvalue, (IndexExpr, OpExpr)):
  2719. return False
  2720. # Something that looks like Foo = Bar[Baz, ...]
  2721. return True
  2722. def is_type_ref(self, rv: Expression, bare: bool = False) -> bool:
  2723. """Does this expression refer to a type?
  2724. This includes:
  2725. * Special forms, like Any or Union
  2726. * Classes (except subscripted enums)
  2727. * Other type aliases
  2728. * PlaceholderNodes with becomes_typeinfo=True (these can be not ready class
  2729. definitions, and not ready aliases).
  2730. If bare is True, this is not a base of an index expression, so some special
  2731. forms are not valid (like a bare Union).
  2732. Note: This method should be only used in context of a type alias definition.
  2733. This method can only return True for RefExprs, to check if C[int] is a valid
  2734. target for type alias call this method on expr.base (i.e. on C in C[int]).
  2735. See also can_be_type_alias().
  2736. """
  2737. if not isinstance(rv, RefExpr):
  2738. return False
  2739. if isinstance(rv.node, TypeVarLikeExpr):
  2740. self.fail(f'Type variable "{rv.fullname}" is invalid as target for type alias', rv)
  2741. return False
  2742. if bare:
  2743. # These three are valid even if bare, for example
  2744. # A = Tuple is just equivalent to A = Tuple[Any, ...].
  2745. valid_refs = {"typing.Any", "typing.Tuple", "typing.Callable"}
  2746. else:
  2747. valid_refs = type_constructors
  2748. if isinstance(rv.node, TypeAlias) or rv.fullname in valid_refs:
  2749. return True
  2750. if isinstance(rv.node, TypeInfo):
  2751. if bare:
  2752. return True
  2753. # Assignment color = Color['RED'] defines a variable, not an alias.
  2754. return not rv.node.is_enum
  2755. if isinstance(rv.node, Var):
  2756. return rv.node.fullname in NEVER_NAMES
  2757. if isinstance(rv, NameExpr):
  2758. n = self.lookup(rv.name, rv)
  2759. if n and isinstance(n.node, PlaceholderNode) and n.node.becomes_typeinfo:
  2760. return True
  2761. elif isinstance(rv, MemberExpr):
  2762. fname = get_member_expr_fullname(rv)
  2763. if fname:
  2764. # The r.h.s. for variable definitions may not be a type reference but just
  2765. # an instance attribute, so suppress the errors.
  2766. n = self.lookup_qualified(fname, rv, suppress_errors=True)
  2767. if n and isinstance(n.node, PlaceholderNode) and n.node.becomes_typeinfo:
  2768. return True
  2769. return False
  2770. def is_none_alias(self, node: Expression) -> bool:
  2771. """Is this a r.h.s. for a None alias?
  2772. We special case the assignments like Void = type(None), to allow using
  2773. Void in type annotations.
  2774. """
  2775. if isinstance(node, CallExpr):
  2776. if (
  2777. isinstance(node.callee, NameExpr)
  2778. and len(node.args) == 1
  2779. and isinstance(node.args[0], NameExpr)
  2780. ):
  2781. call = self.lookup_qualified(node.callee.name, node.callee)
  2782. arg = self.lookup_qualified(node.args[0].name, node.args[0])
  2783. if (
  2784. call is not None
  2785. and call.node
  2786. and call.node.fullname == "builtins.type"
  2787. and arg is not None
  2788. and arg.node
  2789. and arg.node.fullname == "builtins.None"
  2790. ):
  2791. return True
  2792. return False
  2793. def record_special_form_lvalue(self, s: AssignmentStmt) -> None:
  2794. """Record minimal necessary information about l.h.s. of a special form.
  2795. This exists mostly for compatibility with the old semantic analyzer.
  2796. """
  2797. lvalue = s.lvalues[0]
  2798. assert isinstance(lvalue, NameExpr)
  2799. lvalue.is_special_form = True
  2800. if self.current_symbol_kind() == GDEF:
  2801. lvalue.fullname = self.qualified_name(lvalue.name)
  2802. lvalue.kind = self.current_symbol_kind()
  2803. def analyze_enum_assign(self, s: AssignmentStmt) -> bool:
  2804. """Check if s defines an Enum."""
  2805. if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, EnumCallExpr):
  2806. # Already analyzed enum -- nothing to do here.
  2807. return True
  2808. return self.enum_call_analyzer.process_enum_call(s, self.is_func_scope())
  2809. def analyze_namedtuple_assign(self, s: AssignmentStmt) -> bool:
  2810. """Check if s defines a namedtuple."""
  2811. if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, NamedTupleExpr):
  2812. if s.rvalue.analyzed.info.tuple_type and not has_placeholder(
  2813. s.rvalue.analyzed.info.tuple_type
  2814. ):
  2815. return True # This is a valid and analyzed named tuple definition, nothing to do here.
  2816. if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], (NameExpr, MemberExpr)):
  2817. return False
  2818. lvalue = s.lvalues[0]
  2819. if isinstance(lvalue, MemberExpr):
  2820. if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.callee, RefExpr):
  2821. fullname = s.rvalue.callee.fullname
  2822. if fullname == "collections.namedtuple" or fullname in TYPED_NAMEDTUPLE_NAMES:
  2823. self.fail("NamedTuple type as an attribute is not supported", lvalue)
  2824. return False
  2825. name = lvalue.name
  2826. namespace = self.qualified_name(name)
  2827. with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)):
  2828. internal_name, info, tvar_defs = self.named_tuple_analyzer.check_namedtuple(
  2829. s.rvalue, name, self.is_func_scope()
  2830. )
  2831. if internal_name is None:
  2832. return False
  2833. if internal_name != name:
  2834. self.fail(
  2835. 'First argument to namedtuple() should be "{}", not "{}"'.format(
  2836. name, internal_name
  2837. ),
  2838. s.rvalue,
  2839. code=codes.NAME_MATCH,
  2840. )
  2841. return True
  2842. # Yes, it's a valid namedtuple, but defer if it is not ready.
  2843. if not info:
  2844. self.mark_incomplete(name, lvalue, becomes_typeinfo=True)
  2845. else:
  2846. self.setup_type_vars(info.defn, tvar_defs)
  2847. self.setup_alias_type_vars(info.defn)
  2848. return True
  2849. def analyze_typeddict_assign(self, s: AssignmentStmt) -> bool:
  2850. """Check if s defines a typed dict."""
  2851. if isinstance(s.rvalue, CallExpr) and isinstance(s.rvalue.analyzed, TypedDictExpr):
  2852. if s.rvalue.analyzed.info.typeddict_type and not has_placeholder(
  2853. s.rvalue.analyzed.info.typeddict_type
  2854. ):
  2855. # This is a valid and analyzed typed dict definition, nothing to do here.
  2856. return True
  2857. if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], (NameExpr, MemberExpr)):
  2858. return False
  2859. lvalue = s.lvalues[0]
  2860. name = lvalue.name
  2861. namespace = self.qualified_name(name)
  2862. with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)):
  2863. is_typed_dict, info, tvar_defs = self.typed_dict_analyzer.check_typeddict(
  2864. s.rvalue, name, self.is_func_scope()
  2865. )
  2866. if not is_typed_dict:
  2867. return False
  2868. if isinstance(lvalue, MemberExpr):
  2869. self.fail("TypedDict type as attribute is not supported", lvalue)
  2870. return False
  2871. # Yes, it's a valid typed dict, but defer if it is not ready.
  2872. if not info:
  2873. self.mark_incomplete(name, lvalue, becomes_typeinfo=True)
  2874. else:
  2875. defn = info.defn
  2876. self.setup_type_vars(defn, tvar_defs)
  2877. self.setup_alias_type_vars(defn)
  2878. return True
  2879. def analyze_lvalues(self, s: AssignmentStmt) -> None:
  2880. # We cannot use s.type, because analyze_simple_literal_type() will set it.
  2881. explicit = s.unanalyzed_type is not None
  2882. if self.is_final_type(s.unanalyzed_type):
  2883. # We need to exclude bare Final.
  2884. assert isinstance(s.unanalyzed_type, UnboundType)
  2885. if not s.unanalyzed_type.args:
  2886. explicit = False
  2887. if s.rvalue:
  2888. if isinstance(s.rvalue, TempNode):
  2889. has_explicit_value = not s.rvalue.no_rhs
  2890. else:
  2891. has_explicit_value = True
  2892. else:
  2893. has_explicit_value = False
  2894. for lval in s.lvalues:
  2895. self.analyze_lvalue(
  2896. lval,
  2897. explicit_type=explicit,
  2898. is_final=s.is_final_def,
  2899. has_explicit_value=has_explicit_value,
  2900. )
  2901. def apply_dynamic_class_hook(self, s: AssignmentStmt) -> None:
  2902. if not isinstance(s.rvalue, CallExpr):
  2903. return
  2904. fname = ""
  2905. call = s.rvalue
  2906. while True:
  2907. if isinstance(call.callee, RefExpr):
  2908. fname = call.callee.fullname
  2909. # check if method call
  2910. if not fname and isinstance(call.callee, MemberExpr):
  2911. callee_expr = call.callee.expr
  2912. if isinstance(callee_expr, RefExpr) and callee_expr.fullname:
  2913. method_name = call.callee.name
  2914. fname = callee_expr.fullname + "." + method_name
  2915. elif isinstance(callee_expr, CallExpr):
  2916. # check if chain call
  2917. call = callee_expr
  2918. continue
  2919. break
  2920. if not fname:
  2921. return
  2922. hook = self.plugin.get_dynamic_class_hook(fname)
  2923. if not hook:
  2924. return
  2925. for lval in s.lvalues:
  2926. if not isinstance(lval, NameExpr):
  2927. continue
  2928. hook(DynamicClassDefContext(call, lval.name, self))
  2929. def unwrap_final(self, s: AssignmentStmt) -> bool:
  2930. """Strip Final[...] if present in an assignment.
  2931. This is done to invoke type inference during type checking phase for this
  2932. assignment. Also, Final[...] doesn't affect type in any way -- it is rather an
  2933. access qualifier for given `Var`.
  2934. Also perform various consistency checks.
  2935. Returns True if Final[...] was present.
  2936. """
  2937. if not s.unanalyzed_type or not self.is_final_type(s.unanalyzed_type):
  2938. return False
  2939. assert isinstance(s.unanalyzed_type, UnboundType)
  2940. if len(s.unanalyzed_type.args) > 1:
  2941. self.fail("Final[...] takes at most one type argument", s.unanalyzed_type)
  2942. invalid_bare_final = False
  2943. if not s.unanalyzed_type.args:
  2944. s.type = None
  2945. if isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs:
  2946. invalid_bare_final = True
  2947. self.fail("Type in Final[...] can only be omitted if there is an initializer", s)
  2948. else:
  2949. s.type = s.unanalyzed_type.args[0]
  2950. if s.type is not None and self.is_classvar(s.type):
  2951. self.fail("Variable should not be annotated with both ClassVar and Final", s)
  2952. return False
  2953. if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], RefExpr):
  2954. self.fail("Invalid final declaration", s)
  2955. return False
  2956. lval = s.lvalues[0]
  2957. assert isinstance(lval, RefExpr)
  2958. # Reset inferred status if it was set due to simple literal rvalue on previous iteration.
  2959. # TODO: this is a best-effort quick fix, we should avoid the need to manually sync this,
  2960. # see https://github.com/python/mypy/issues/6458.
  2961. if lval.is_new_def:
  2962. lval.is_inferred_def = s.type is None
  2963. if self.loop_depth[-1] > 0:
  2964. self.fail("Cannot use Final inside a loop", s)
  2965. if self.type and self.type.is_protocol:
  2966. self.msg.protocol_members_cant_be_final(s)
  2967. if (
  2968. isinstance(s.rvalue, TempNode)
  2969. and s.rvalue.no_rhs
  2970. and not self.is_stub_file
  2971. and not self.is_class_scope()
  2972. ):
  2973. if not invalid_bare_final: # Skip extra error messages.
  2974. self.msg.final_without_value(s)
  2975. return True
  2976. def check_final_implicit_def(self, s: AssignmentStmt) -> None:
  2977. """Do basic checks for final declaration on self in __init__.
  2978. Additional re-definition checks are performed by `analyze_lvalue`.
  2979. """
  2980. if not s.is_final_def:
  2981. return
  2982. lval = s.lvalues[0]
  2983. assert isinstance(lval, RefExpr)
  2984. if isinstance(lval, MemberExpr):
  2985. if not self.is_self_member_ref(lval):
  2986. self.fail("Final can be only applied to a name or an attribute on self", s)
  2987. s.is_final_def = False
  2988. return
  2989. else:
  2990. assert self.function_stack
  2991. if self.function_stack[-1].name != "__init__":
  2992. self.fail("Can only declare a final attribute in class body or __init__", s)
  2993. s.is_final_def = False
  2994. return
  2995. def store_final_status(self, s: AssignmentStmt) -> None:
  2996. """If this is a locally valid final declaration, set the corresponding flag on `Var`."""
  2997. if s.is_final_def:
  2998. if len(s.lvalues) == 1 and isinstance(s.lvalues[0], RefExpr):
  2999. node = s.lvalues[0].node
  3000. if isinstance(node, Var):
  3001. node.is_final = True
  3002. if s.type:
  3003. node.final_value = constant_fold_expr(s.rvalue, self.cur_mod_id)
  3004. if self.is_class_scope() and (
  3005. isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs
  3006. ):
  3007. node.final_unset_in_class = True
  3008. else:
  3009. for lval in self.flatten_lvalues(s.lvalues):
  3010. # Special case: we are working with an `Enum`:
  3011. #
  3012. # class MyEnum(Enum):
  3013. # key = 'some value'
  3014. #
  3015. # Here `key` is implicitly final. In runtime, code like
  3016. #
  3017. # MyEnum.key = 'modified'
  3018. #
  3019. # will fail with `AttributeError: Cannot reassign members.`
  3020. # That's why we need to replicate this.
  3021. if (
  3022. isinstance(lval, NameExpr)
  3023. and isinstance(self.type, TypeInfo)
  3024. and self.type.is_enum
  3025. ):
  3026. cur_node = self.type.names.get(lval.name, None)
  3027. if (
  3028. cur_node
  3029. and isinstance(cur_node.node, Var)
  3030. and not (isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs)
  3031. ):
  3032. # Double underscored members are writable on an `Enum`.
  3033. # (Except read-only `__members__` but that is handled in type checker)
  3034. cur_node.node.is_final = s.is_final_def = not is_dunder(cur_node.node.name)
  3035. # Special case: deferred initialization of a final attribute in __init__.
  3036. # In this case we just pretend this is a valid final definition to suppress
  3037. # errors about assigning to final attribute.
  3038. if isinstance(lval, MemberExpr) and self.is_self_member_ref(lval):
  3039. assert self.type, "Self member outside a class"
  3040. cur_node = self.type.names.get(lval.name, None)
  3041. if cur_node and isinstance(cur_node.node, Var) and cur_node.node.is_final:
  3042. assert self.function_stack
  3043. top_function = self.function_stack[-1]
  3044. if (
  3045. top_function.name == "__init__"
  3046. and cur_node.node.final_unset_in_class
  3047. and not cur_node.node.final_set_in_init
  3048. and not (isinstance(s.rvalue, TempNode) and s.rvalue.no_rhs)
  3049. ):
  3050. cur_node.node.final_set_in_init = True
  3051. s.is_final_def = True
  3052. def flatten_lvalues(self, lvalues: list[Expression]) -> list[Expression]:
  3053. res: list[Expression] = []
  3054. for lv in lvalues:
  3055. if isinstance(lv, (TupleExpr, ListExpr)):
  3056. res.extend(self.flatten_lvalues(lv.items))
  3057. else:
  3058. res.append(lv)
  3059. return res
  3060. def process_type_annotation(self, s: AssignmentStmt) -> None:
  3061. """Analyze type annotation or infer simple literal type."""
  3062. if s.type:
  3063. lvalue = s.lvalues[-1]
  3064. allow_tuple_literal = isinstance(lvalue, TupleExpr)
  3065. analyzed = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
  3066. # Don't store not ready types (including placeholders).
  3067. if analyzed is None or has_placeholder(analyzed):
  3068. self.defer(s)
  3069. return
  3070. s.type = analyzed
  3071. if (
  3072. self.type
  3073. and self.type.is_protocol
  3074. and isinstance(lvalue, NameExpr)
  3075. and isinstance(s.rvalue, TempNode)
  3076. and s.rvalue.no_rhs
  3077. ):
  3078. if isinstance(lvalue.node, Var):
  3079. lvalue.node.is_abstract_var = True
  3080. else:
  3081. if (
  3082. self.type
  3083. and self.type.is_protocol
  3084. and self.is_annotated_protocol_member(s)
  3085. and not self.is_func_scope()
  3086. ):
  3087. self.fail("All protocol members must have explicitly declared types", s)
  3088. # Set the type if the rvalue is a simple literal (even if the above error occurred).
  3089. if len(s.lvalues) == 1 and isinstance(s.lvalues[0], RefExpr):
  3090. ref_expr = s.lvalues[0]
  3091. safe_literal_inference = True
  3092. if self.type and isinstance(ref_expr, NameExpr) and len(self.type.mro) > 1:
  3093. # Check if there is a definition in supertype. If yes, we can't safely
  3094. # decide here what to infer: int or Literal[42].
  3095. safe_literal_inference = self.type.mro[1].get(ref_expr.name) is None
  3096. if safe_literal_inference and ref_expr.is_inferred_def:
  3097. s.type = self.analyze_simple_literal_type(s.rvalue, s.is_final_def)
  3098. if s.type:
  3099. # Store type into nodes.
  3100. for lvalue in s.lvalues:
  3101. self.store_declared_types(lvalue, s.type)
  3102. def is_annotated_protocol_member(self, s: AssignmentStmt) -> bool:
  3103. """Check whether a protocol member is annotated.
  3104. There are some exceptions that can be left unannotated, like ``__slots__``."""
  3105. return any(
  3106. (isinstance(lv, NameExpr) and lv.name != "__slots__" and lv.is_inferred_def)
  3107. for lv in s.lvalues
  3108. )
  3109. def analyze_simple_literal_type(self, rvalue: Expression, is_final: bool) -> Type | None:
  3110. """Return builtins.int if rvalue is an int literal, etc.
  3111. If this is a 'Final' context, we return "Literal[...]" instead.
  3112. """
  3113. if self.function_stack:
  3114. # Skip inside a function; this is to avoid confusing
  3115. # the code that handles dead code due to isinstance()
  3116. # inside type variables with value restrictions (like
  3117. # AnyStr).
  3118. return None
  3119. value = constant_fold_expr(rvalue, self.cur_mod_id)
  3120. if value is None or isinstance(value, complex):
  3121. return None
  3122. if isinstance(value, bool):
  3123. type_name = "builtins.bool"
  3124. elif isinstance(value, int):
  3125. type_name = "builtins.int"
  3126. elif isinstance(value, str):
  3127. type_name = "builtins.str"
  3128. elif isinstance(value, float):
  3129. type_name = "builtins.float"
  3130. typ = self.named_type_or_none(type_name)
  3131. if typ and is_final:
  3132. return typ.copy_modified(last_known_value=LiteralType(value=value, fallback=typ))
  3133. return typ
  3134. def analyze_alias(
  3135. self, name: str, rvalue: Expression, allow_placeholder: bool = False
  3136. ) -> tuple[Type | None, list[TypeVarLikeType], set[str], list[str]]:
  3137. """Check if 'rvalue' is a valid type allowed for aliasing (e.g. not a type variable).
  3138. If yes, return the corresponding type, a list of
  3139. qualified type variable names for generic aliases, a set of names the alias depends on,
  3140. and a list of type variables if the alias is generic.
  3141. A schematic example for the dependencies:
  3142. A = int
  3143. B = str
  3144. analyze_alias(Dict[A, B])[2] == {'__main__.A', '__main__.B'}
  3145. """
  3146. dynamic = bool(self.function_stack and self.function_stack[-1].is_dynamic())
  3147. global_scope = not self.type and not self.function_stack
  3148. try:
  3149. typ = expr_to_unanalyzed_type(rvalue, self.options, self.is_stub_file)
  3150. except TypeTranslationError:
  3151. self.fail(
  3152. "Invalid type alias: expression is not a valid type", rvalue, code=codes.VALID_TYPE
  3153. )
  3154. return None, [], set(), []
  3155. found_type_vars = typ.accept(TypeVarLikeQuery(self, self.tvar_scope))
  3156. tvar_defs: list[TypeVarLikeType] = []
  3157. namespace = self.qualified_name(name)
  3158. with self.tvar_scope_frame(self.tvar_scope.class_frame(namespace)):
  3159. for name, tvar_expr in found_type_vars:
  3160. tvar_def = self.tvar_scope.bind_new(name, tvar_expr)
  3161. tvar_defs.append(tvar_def)
  3162. analyzed, depends_on = analyze_type_alias(
  3163. typ,
  3164. self,
  3165. self.tvar_scope,
  3166. self.plugin,
  3167. self.options,
  3168. self.is_typeshed_stub_file,
  3169. allow_placeholder=allow_placeholder,
  3170. in_dynamic_func=dynamic,
  3171. global_scope=global_scope,
  3172. allowed_alias_tvars=tvar_defs,
  3173. )
  3174. # There can be only one variadic variable at most, the error is reported elsewhere.
  3175. new_tvar_defs = []
  3176. variadic = False
  3177. for td in tvar_defs:
  3178. if isinstance(td, TypeVarTupleType):
  3179. if variadic:
  3180. continue
  3181. variadic = True
  3182. new_tvar_defs.append(td)
  3183. qualified_tvars = [node.fullname for _name, node in found_type_vars]
  3184. return analyzed, new_tvar_defs, depends_on, qualified_tvars
  3185. def is_pep_613(self, s: AssignmentStmt) -> bool:
  3186. if s.unanalyzed_type is not None and isinstance(s.unanalyzed_type, UnboundType):
  3187. lookup = self.lookup_qualified(s.unanalyzed_type.name, s, suppress_errors=True)
  3188. if lookup and lookup.fullname in TYPE_ALIAS_NAMES:
  3189. return True
  3190. return False
  3191. def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
  3192. """Check if assignment creates a type alias and set it up as needed.
  3193. Return True if it is a type alias (even if the target is not ready),
  3194. or False otherwise.
  3195. Note: the resulting types for subscripted (including generic) aliases
  3196. are also stored in rvalue.analyzed.
  3197. """
  3198. if s.invalid_recursive_alias:
  3199. return True
  3200. lvalue = s.lvalues[0]
  3201. if len(s.lvalues) > 1 or not isinstance(lvalue, NameExpr):
  3202. # First rule: Only simple assignments like Alias = ... create aliases.
  3203. return False
  3204. pep_613 = self.is_pep_613(s)
  3205. if not pep_613 and s.unanalyzed_type is not None:
  3206. # Second rule: Explicit type (cls: Type[A] = A) always creates variable, not alias.
  3207. # unless using PEP 613 `cls: TypeAlias = A`
  3208. return False
  3209. if isinstance(s.rvalue, CallExpr) and s.rvalue.analyzed:
  3210. return False
  3211. existing = self.current_symbol_table().get(lvalue.name)
  3212. # Third rule: type aliases can't be re-defined. For example:
  3213. # A: Type[float] = int
  3214. # A = float # OK, but this doesn't define an alias
  3215. # B = int
  3216. # B = float # Error!
  3217. # Don't create an alias in these cases:
  3218. if existing and (
  3219. isinstance(existing.node, Var) # existing variable
  3220. or (isinstance(existing.node, TypeAlias) and not s.is_alias_def) # existing alias
  3221. or (isinstance(existing.node, PlaceholderNode) and existing.node.node.line < s.line)
  3222. ): # previous incomplete definition
  3223. # TODO: find a more robust way to track the order of definitions.
  3224. # Note: if is_alias_def=True, this is just a node from previous iteration.
  3225. if isinstance(existing.node, TypeAlias) and not s.is_alias_def:
  3226. self.fail(
  3227. 'Cannot assign multiple types to name "{}"'
  3228. ' without an explicit "Type[...]" annotation'.format(lvalue.name),
  3229. lvalue,
  3230. )
  3231. return False
  3232. non_global_scope = self.type or self.is_func_scope()
  3233. if not pep_613 and isinstance(s.rvalue, RefExpr) and non_global_scope:
  3234. # Fourth rule (special case): Non-subscripted right hand side creates a variable
  3235. # at class and function scopes. For example:
  3236. #
  3237. # class Model:
  3238. # ...
  3239. # class C:
  3240. # model = Model # this is automatically a variable with type 'Type[Model]'
  3241. #
  3242. # without this rule, this typical use case will require a lot of explicit
  3243. # annotations (see the second rule).
  3244. return False
  3245. rvalue = s.rvalue
  3246. if not pep_613 and not self.can_be_type_alias(rvalue):
  3247. return False
  3248. if existing and not isinstance(existing.node, (PlaceholderNode, TypeAlias)):
  3249. # Cannot redefine existing node as type alias.
  3250. return False
  3251. res: Type | None = None
  3252. if self.is_none_alias(rvalue):
  3253. res = NoneType()
  3254. alias_tvars: list[TypeVarLikeType] = []
  3255. depends_on: set[str] = set()
  3256. qualified_tvars: list[str] = []
  3257. else:
  3258. tag = self.track_incomplete_refs()
  3259. res, alias_tvars, depends_on, qualified_tvars = self.analyze_alias(
  3260. lvalue.name, rvalue, allow_placeholder=True
  3261. )
  3262. if not res:
  3263. return False
  3264. if not self.options.disable_recursive_aliases and not self.is_func_scope():
  3265. # Only marking incomplete for top-level placeholders makes recursive aliases like
  3266. # `A = Sequence[str | A]` valid here, similar to how we treat base classes in class
  3267. # definitions, allowing `class str(Sequence[str]): ...`
  3268. incomplete_target = isinstance(res, ProperType) and isinstance(
  3269. res, PlaceholderType
  3270. )
  3271. else:
  3272. incomplete_target = has_placeholder(res)
  3273. if self.found_incomplete_ref(tag) or incomplete_target:
  3274. # Since we have got here, we know this must be a type alias (incomplete refs
  3275. # may appear in nested positions), therefore use becomes_typeinfo=True.
  3276. self.mark_incomplete(lvalue.name, rvalue, becomes_typeinfo=True)
  3277. return True
  3278. self.add_type_alias_deps(depends_on)
  3279. # In addition to the aliases used, we add deps on unbound
  3280. # type variables, since they are erased from target type.
  3281. self.add_type_alias_deps(qualified_tvars)
  3282. # The above are only direct deps on other aliases.
  3283. # For subscripted aliases, type deps from expansion are added in deps.py
  3284. # (because the type is stored).
  3285. check_for_explicit_any(res, self.options, self.is_typeshed_stub_file, self.msg, context=s)
  3286. # When this type alias gets "inlined", the Any is not explicit anymore,
  3287. # so we need to replace it with non-explicit Anys.
  3288. res = make_any_non_explicit(res)
  3289. # Note: with the new (lazy) type alias representation we only need to set no_args to True
  3290. # if the expected number of arguments is non-zero, so that aliases like A = List work.
  3291. # However, eagerly expanding aliases like Text = str is a nice performance optimization.
  3292. no_args = isinstance(res, Instance) and not res.args # type: ignore[misc]
  3293. fix_instance_types(res, self.fail, self.note, self.options)
  3294. # Aliases defined within functions can't be accessed outside
  3295. # the function, since the symbol table will no longer
  3296. # exist. Work around by expanding them eagerly when used.
  3297. eager = self.is_func_scope()
  3298. alias_node = TypeAlias(
  3299. res,
  3300. self.qualified_name(lvalue.name),
  3301. s.line,
  3302. s.column,
  3303. alias_tvars=alias_tvars,
  3304. no_args=no_args,
  3305. eager=eager,
  3306. )
  3307. if isinstance(s.rvalue, (IndexExpr, CallExpr, OpExpr)) and (
  3308. not isinstance(rvalue, OpExpr)
  3309. or (self.options.python_version >= (3, 10) or self.is_stub_file)
  3310. ):
  3311. # Note: CallExpr is for "void = type(None)" and OpExpr is for "X | Y" union syntax.
  3312. s.rvalue.analyzed = TypeAliasExpr(alias_node)
  3313. s.rvalue.analyzed.line = s.line
  3314. # we use the column from resulting target, to get better location for errors
  3315. s.rvalue.analyzed.column = res.column
  3316. elif isinstance(s.rvalue, RefExpr):
  3317. s.rvalue.is_alias_rvalue = True
  3318. if existing:
  3319. # An alias gets updated.
  3320. updated = False
  3321. if isinstance(existing.node, TypeAlias):
  3322. if existing.node.target != res:
  3323. # Copy expansion to the existing alias, this matches how we update base classes
  3324. # for a TypeInfo _in place_ if there are nested placeholders.
  3325. existing.node.target = res
  3326. existing.node.alias_tvars = alias_tvars
  3327. existing.node.no_args = no_args
  3328. updated = True
  3329. else:
  3330. # Otherwise just replace existing placeholder with type alias.
  3331. existing.node = alias_node
  3332. updated = True
  3333. if updated:
  3334. if self.final_iteration:
  3335. self.cannot_resolve_name(lvalue.name, "name", s)
  3336. return True
  3337. else:
  3338. # We need to defer so that this change can get propagated to base classes.
  3339. self.defer(s, force_progress=True)
  3340. else:
  3341. self.add_symbol(lvalue.name, alias_node, s)
  3342. if isinstance(rvalue, RefExpr) and isinstance(rvalue.node, TypeAlias):
  3343. alias_node.normalized = rvalue.node.normalized
  3344. current_node = existing.node if existing else alias_node
  3345. assert isinstance(current_node, TypeAlias)
  3346. self.disable_invalid_recursive_aliases(s, current_node)
  3347. if self.is_class_scope():
  3348. assert self.type is not None
  3349. if self.type.is_protocol:
  3350. self.fail("Type aliases are prohibited in protocol bodies", s)
  3351. if not lvalue.name[0].isupper():
  3352. self.note("Use variable annotation syntax to define protocol members", s)
  3353. return True
  3354. def disable_invalid_recursive_aliases(
  3355. self, s: AssignmentStmt, current_node: TypeAlias
  3356. ) -> None:
  3357. """Prohibit and fix recursive type aliases that are invalid/unsupported."""
  3358. messages = []
  3359. if is_invalid_recursive_alias({current_node}, current_node.target):
  3360. messages.append("Invalid recursive alias: a union item of itself")
  3361. if detect_diverging_alias(
  3362. current_node, current_node.target, self.lookup_qualified, self.tvar_scope
  3363. ):
  3364. messages.append("Invalid recursive alias: type variable nesting on right hand side")
  3365. if messages:
  3366. current_node.target = AnyType(TypeOfAny.from_error)
  3367. s.invalid_recursive_alias = True
  3368. for msg in messages:
  3369. self.fail(msg, s.rvalue)
  3370. def analyze_lvalue(
  3371. self,
  3372. lval: Lvalue,
  3373. nested: bool = False,
  3374. explicit_type: bool = False,
  3375. is_final: bool = False,
  3376. escape_comprehensions: bool = False,
  3377. has_explicit_value: bool = False,
  3378. ) -> None:
  3379. """Analyze an lvalue or assignment target.
  3380. Args:
  3381. lval: The target lvalue
  3382. nested: If true, the lvalue is within a tuple or list lvalue expression
  3383. explicit_type: Assignment has type annotation
  3384. escape_comprehensions: If we are inside a comprehension, set the variable
  3385. in the enclosing scope instead. This implements
  3386. https://www.python.org/dev/peps/pep-0572/#scope-of-the-target
  3387. """
  3388. if escape_comprehensions:
  3389. assert isinstance(lval, NameExpr), "assignment expression target must be NameExpr"
  3390. if isinstance(lval, NameExpr):
  3391. self.analyze_name_lvalue(
  3392. lval,
  3393. explicit_type,
  3394. is_final,
  3395. escape_comprehensions,
  3396. has_explicit_value=has_explicit_value,
  3397. )
  3398. elif isinstance(lval, MemberExpr):
  3399. self.analyze_member_lvalue(lval, explicit_type, is_final, has_explicit_value)
  3400. if explicit_type and not self.is_self_member_ref(lval):
  3401. self.fail("Type cannot be declared in assignment to non-self attribute", lval)
  3402. elif isinstance(lval, IndexExpr):
  3403. if explicit_type:
  3404. self.fail("Unexpected type declaration", lval)
  3405. lval.accept(self)
  3406. elif isinstance(lval, TupleExpr):
  3407. self.analyze_tuple_or_list_lvalue(lval, explicit_type)
  3408. elif isinstance(lval, StarExpr):
  3409. if nested:
  3410. self.analyze_lvalue(lval.expr, nested, explicit_type)
  3411. else:
  3412. self.fail("Starred assignment target must be in a list or tuple", lval)
  3413. else:
  3414. self.fail("Invalid assignment target", lval)
  3415. def analyze_name_lvalue(
  3416. self,
  3417. lvalue: NameExpr,
  3418. explicit_type: bool,
  3419. is_final: bool,
  3420. escape_comprehensions: bool,
  3421. has_explicit_value: bool,
  3422. ) -> None:
  3423. """Analyze an lvalue that targets a name expression.
  3424. Arguments are similar to "analyze_lvalue".
  3425. """
  3426. if lvalue.node:
  3427. # This has been bound already in a previous iteration.
  3428. return
  3429. name = lvalue.name
  3430. if self.is_alias_for_final_name(name):
  3431. if is_final:
  3432. self.fail("Cannot redefine an existing name as final", lvalue)
  3433. else:
  3434. self.msg.cant_assign_to_final(name, self.type is not None, lvalue)
  3435. kind = self.current_symbol_kind()
  3436. names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
  3437. existing = names.get(name)
  3438. outer = self.is_global_or_nonlocal(name)
  3439. if kind == MDEF and isinstance(self.type, TypeInfo) and self.type.is_enum:
  3440. # Special case: we need to be sure that `Enum` keys are unique.
  3441. if existing is not None and not isinstance(existing.node, PlaceholderNode):
  3442. self.fail(
  3443. 'Attempted to reuse member name "{}" in Enum definition "{}"'.format(
  3444. name, self.type.name
  3445. ),
  3446. lvalue,
  3447. )
  3448. if (not existing or isinstance(existing.node, PlaceholderNode)) and not outer:
  3449. # Define new variable.
  3450. var = self.make_name_lvalue_var(lvalue, kind, not explicit_type, has_explicit_value)
  3451. added = self.add_symbol(name, var, lvalue, escape_comprehensions=escape_comprehensions)
  3452. # Only bind expression if we successfully added name to symbol table.
  3453. if added:
  3454. lvalue.is_new_def = True
  3455. lvalue.is_inferred_def = True
  3456. lvalue.kind = kind
  3457. lvalue.node = var
  3458. if kind == GDEF:
  3459. lvalue.fullname = var._fullname
  3460. else:
  3461. lvalue.fullname = lvalue.name
  3462. if self.is_func_scope():
  3463. if unmangle(name) == "_":
  3464. # Special case for assignment to local named '_': always infer 'Any'.
  3465. typ = AnyType(TypeOfAny.special_form)
  3466. self.store_declared_types(lvalue, typ)
  3467. if is_final and self.is_final_redefinition(kind, name):
  3468. self.fail("Cannot redefine an existing name as final", lvalue)
  3469. else:
  3470. self.make_name_lvalue_point_to_existing_def(lvalue, explicit_type, is_final)
  3471. def is_final_redefinition(self, kind: int, name: str) -> bool:
  3472. if kind == GDEF:
  3473. return self.is_mangled_global(name) and not self.is_initial_mangled_global(name)
  3474. elif kind == MDEF and self.type:
  3475. return unmangle(name) + "'" in self.type.names
  3476. return False
  3477. def is_alias_for_final_name(self, name: str) -> bool:
  3478. if self.is_func_scope():
  3479. if not name.endswith("'"):
  3480. # Not a mangled name -- can't be an alias
  3481. return False
  3482. name = unmangle(name)
  3483. assert self.locals[-1] is not None, "No locals at function scope"
  3484. existing = self.locals[-1].get(name)
  3485. return existing is not None and is_final_node(existing.node)
  3486. elif self.type is not None:
  3487. orig_name = unmangle(name) + "'"
  3488. if name == orig_name:
  3489. return False
  3490. existing = self.type.names.get(orig_name)
  3491. return existing is not None and is_final_node(existing.node)
  3492. else:
  3493. orig_name = unmangle(name) + "'"
  3494. if name == orig_name:
  3495. return False
  3496. existing = self.globals.get(orig_name)
  3497. return existing is not None and is_final_node(existing.node)
  3498. def make_name_lvalue_var(
  3499. self, lvalue: NameExpr, kind: int, inferred: bool, has_explicit_value: bool
  3500. ) -> Var:
  3501. """Return a Var node for an lvalue that is a name expression."""
  3502. name = lvalue.name
  3503. v = Var(name)
  3504. v.set_line(lvalue)
  3505. v.is_inferred = inferred
  3506. if kind == MDEF:
  3507. assert self.type is not None
  3508. v.info = self.type
  3509. v.is_initialized_in_class = True
  3510. v.allow_incompatible_override = name in ALLOW_INCOMPATIBLE_OVERRIDE
  3511. if kind != LDEF:
  3512. v._fullname = self.qualified_name(name)
  3513. else:
  3514. # fullanme should never stay None
  3515. v._fullname = name
  3516. v.is_ready = False # Type not inferred yet
  3517. v.has_explicit_value = has_explicit_value
  3518. return v
  3519. def make_name_lvalue_point_to_existing_def(
  3520. self, lval: NameExpr, explicit_type: bool, is_final: bool
  3521. ) -> None:
  3522. """Update an lvalue to point to existing definition in the same scope.
  3523. Arguments are similar to "analyze_lvalue".
  3524. Assume that an existing name exists.
  3525. """
  3526. if is_final:
  3527. # Redefining an existing name with final is always an error.
  3528. self.fail("Cannot redefine an existing name as final", lval)
  3529. original_def = self.lookup(lval.name, lval, suppress_errors=True)
  3530. if original_def is None and self.type and not self.is_func_scope():
  3531. # Workaround to allow "x, x = ..." in class body.
  3532. original_def = self.type.get(lval.name)
  3533. if explicit_type:
  3534. # Don't re-bind if there is a type annotation.
  3535. self.name_already_defined(lval.name, lval, original_def)
  3536. else:
  3537. # Bind to an existing name.
  3538. if original_def:
  3539. self.bind_name_expr(lval, original_def)
  3540. else:
  3541. self.name_not_defined(lval.name, lval)
  3542. self.check_lvalue_validity(lval.node, lval)
  3543. def analyze_tuple_or_list_lvalue(self, lval: TupleExpr, explicit_type: bool = False) -> None:
  3544. """Analyze an lvalue or assignment target that is a list or tuple."""
  3545. items = lval.items
  3546. star_exprs = [item for item in items if isinstance(item, StarExpr)]
  3547. if len(star_exprs) > 1:
  3548. self.fail("Two starred expressions in assignment", lval)
  3549. else:
  3550. if len(star_exprs) == 1:
  3551. star_exprs[0].valid = True
  3552. for i in items:
  3553. self.analyze_lvalue(
  3554. lval=i,
  3555. nested=True,
  3556. explicit_type=explicit_type,
  3557. # Lists and tuples always have explicit values defined:
  3558. # `a, b, c = value`
  3559. has_explicit_value=True,
  3560. )
  3561. def analyze_member_lvalue(
  3562. self, lval: MemberExpr, explicit_type: bool, is_final: bool, has_explicit_value: bool
  3563. ) -> None:
  3564. """Analyze lvalue that is a member expression.
  3565. Arguments:
  3566. lval: The target lvalue
  3567. explicit_type: Assignment has type annotation
  3568. is_final: Is the target final
  3569. """
  3570. if lval.node:
  3571. # This has been bound already in a previous iteration.
  3572. return
  3573. lval.accept(self)
  3574. if self.is_self_member_ref(lval):
  3575. assert self.type, "Self member outside a class"
  3576. cur_node = self.type.names.get(lval.name)
  3577. node = self.type.get(lval.name)
  3578. if cur_node and is_final:
  3579. # Overrides will be checked in type checker.
  3580. self.fail("Cannot redefine an existing name as final", lval)
  3581. # On first encounter with this definition, if this attribute was defined before
  3582. # with an inferred type and it's marked with an explicit type now, give an error.
  3583. if (
  3584. not lval.node
  3585. and cur_node
  3586. and isinstance(cur_node.node, Var)
  3587. and cur_node.node.is_inferred
  3588. and explicit_type
  3589. ):
  3590. self.attribute_already_defined(lval.name, lval, cur_node)
  3591. if self.type.is_protocol and has_explicit_value and cur_node is not None:
  3592. # Make this variable non-abstract, it would be safer to do this only if we
  3593. # are inside __init__, but we do this always to preserve historical behaviour.
  3594. if isinstance(cur_node.node, Var):
  3595. cur_node.node.is_abstract_var = False
  3596. if (
  3597. # If the attribute of self is not defined, create a new Var, ...
  3598. node is None
  3599. # ... or if it is defined as abstract in a *superclass*.
  3600. or (cur_node is None and isinstance(node.node, Var) and node.node.is_abstract_var)
  3601. # ... also an explicit declaration on self also creates a new Var.
  3602. # Note that `explicit_type` might have been erased for bare `Final`,
  3603. # so we also check if `is_final` is passed.
  3604. or (cur_node is None and (explicit_type or is_final))
  3605. ):
  3606. if self.type.is_protocol and node is None:
  3607. self.fail("Protocol members cannot be defined via assignment to self", lval)
  3608. else:
  3609. # Implicit attribute definition in __init__.
  3610. lval.is_new_def = True
  3611. lval.is_inferred_def = True
  3612. v = Var(lval.name)
  3613. v.set_line(lval)
  3614. v._fullname = self.qualified_name(lval.name)
  3615. v.info = self.type
  3616. v.is_ready = False
  3617. v.explicit_self_type = explicit_type or is_final
  3618. lval.def_var = v
  3619. lval.node = v
  3620. # TODO: should we also set lval.kind = MDEF?
  3621. self.type.names[lval.name] = SymbolTableNode(MDEF, v, implicit=True)
  3622. self.check_lvalue_validity(lval.node, lval)
  3623. def is_self_member_ref(self, memberexpr: MemberExpr) -> bool:
  3624. """Does memberexpr to refer to an attribute of self?"""
  3625. if not isinstance(memberexpr.expr, NameExpr):
  3626. return False
  3627. node = memberexpr.expr.node
  3628. return isinstance(node, Var) and node.is_self
  3629. def check_lvalue_validity(self, node: Expression | SymbolNode | None, ctx: Context) -> None:
  3630. if isinstance(node, TypeVarExpr):
  3631. self.fail("Invalid assignment target", ctx)
  3632. elif isinstance(node, TypeInfo):
  3633. self.fail(message_registry.CANNOT_ASSIGN_TO_TYPE, ctx)
  3634. def store_declared_types(self, lvalue: Lvalue, typ: Type) -> None:
  3635. if isinstance(lvalue, RefExpr):
  3636. lvalue.is_inferred_def = False
  3637. if isinstance(lvalue.node, Var):
  3638. var = lvalue.node
  3639. var.type = typ
  3640. var.is_ready = True
  3641. typ = get_proper_type(typ)
  3642. if (
  3643. var.is_final
  3644. and isinstance(typ, Instance)
  3645. and typ.last_known_value
  3646. and (not self.type or not self.type.is_enum)
  3647. ):
  3648. var.final_value = typ.last_known_value.value
  3649. # If node is not a variable, we'll catch it elsewhere.
  3650. elif isinstance(lvalue, TupleExpr):
  3651. typ = get_proper_type(typ)
  3652. if isinstance(typ, TupleType):
  3653. if len(lvalue.items) != len(typ.items):
  3654. self.fail("Incompatible number of tuple items", lvalue)
  3655. return
  3656. for item, itemtype in zip(lvalue.items, typ.items):
  3657. self.store_declared_types(item, itemtype)
  3658. else:
  3659. self.fail("Tuple type expected for multiple variables", lvalue)
  3660. elif isinstance(lvalue, StarExpr):
  3661. # Historical behavior for the old parser
  3662. self.store_declared_types(lvalue.expr, typ)
  3663. else:
  3664. # This has been flagged elsewhere as an error, so just ignore here.
  3665. pass
  3666. def process_typevar_declaration(self, s: AssignmentStmt) -> bool:
  3667. """Check if s declares a TypeVar; it yes, store it in symbol table.
  3668. Return True if this looks like a type variable declaration (but maybe
  3669. with errors), otherwise return False.
  3670. """
  3671. call = self.get_typevarlike_declaration(s, ("typing.TypeVar", "typing_extensions.TypeVar"))
  3672. if not call:
  3673. return False
  3674. name = self.extract_typevarlike_name(s, call)
  3675. if name is None:
  3676. return False
  3677. # Constraining types
  3678. n_values = call.arg_kinds[1:].count(ARG_POS)
  3679. values = self.analyze_value_types(call.args[1 : 1 + n_values])
  3680. res = self.process_typevar_parameters(
  3681. call.args[1 + n_values :],
  3682. call.arg_names[1 + n_values :],
  3683. call.arg_kinds[1 + n_values :],
  3684. n_values,
  3685. s,
  3686. )
  3687. if res is None:
  3688. return False
  3689. variance, upper_bound, default = res
  3690. existing = self.current_symbol_table().get(name)
  3691. if existing and not (
  3692. isinstance(existing.node, PlaceholderNode)
  3693. or
  3694. # Also give error for another type variable with the same name.
  3695. (isinstance(existing.node, TypeVarExpr) and existing.node is call.analyzed)
  3696. ):
  3697. self.fail(f'Cannot redefine "{name}" as a type variable', s)
  3698. return False
  3699. if self.options.disallow_any_unimported:
  3700. for idx, constraint in enumerate(values, start=1):
  3701. if has_any_from_unimported_type(constraint):
  3702. prefix = f"Constraint {idx}"
  3703. self.msg.unimported_type_becomes_any(prefix, constraint, s)
  3704. if has_any_from_unimported_type(upper_bound):
  3705. prefix = "Upper bound of type variable"
  3706. self.msg.unimported_type_becomes_any(prefix, upper_bound, s)
  3707. for t in values + [upper_bound, default]:
  3708. check_for_explicit_any(
  3709. t, self.options, self.is_typeshed_stub_file, self.msg, context=s
  3710. )
  3711. # mypyc suppresses making copies of a function to check each
  3712. # possible type, so set the upper bound to Any to prevent that
  3713. # from causing errors.
  3714. if values and self.options.mypyc:
  3715. upper_bound = AnyType(TypeOfAny.implementation_artifact)
  3716. # Yes, it's a valid type variable definition! Add it to the symbol table.
  3717. if not call.analyzed:
  3718. type_var = TypeVarExpr(
  3719. name, self.qualified_name(name), values, upper_bound, default, variance
  3720. )
  3721. type_var.line = call.line
  3722. call.analyzed = type_var
  3723. updated = True
  3724. else:
  3725. assert isinstance(call.analyzed, TypeVarExpr)
  3726. updated = (
  3727. values != call.analyzed.values
  3728. or upper_bound != call.analyzed.upper_bound
  3729. or default != call.analyzed.default
  3730. )
  3731. call.analyzed.upper_bound = upper_bound
  3732. call.analyzed.values = values
  3733. call.analyzed.default = default
  3734. if any(has_placeholder(v) for v in values):
  3735. self.process_placeholder(None, "TypeVar values", s, force_progress=updated)
  3736. elif has_placeholder(upper_bound):
  3737. self.process_placeholder(None, "TypeVar upper bound", s, force_progress=updated)
  3738. elif has_placeholder(default):
  3739. self.process_placeholder(None, "TypeVar default", s, force_progress=updated)
  3740. self.add_symbol(name, call.analyzed, s)
  3741. return True
  3742. def check_typevarlike_name(self, call: CallExpr, name: str, context: Context) -> bool:
  3743. """Checks that the name of a TypeVar or ParamSpec matches its variable."""
  3744. name = unmangle(name)
  3745. assert isinstance(call.callee, RefExpr)
  3746. typevarlike_type = (
  3747. call.callee.name if isinstance(call.callee, NameExpr) else call.callee.fullname
  3748. )
  3749. if len(call.args) < 1:
  3750. self.fail(f"Too few arguments for {typevarlike_type}()", context)
  3751. return False
  3752. if not isinstance(call.args[0], StrExpr) or not call.arg_kinds[0] == ARG_POS:
  3753. self.fail(f"{typevarlike_type}() expects a string literal as first argument", context)
  3754. return False
  3755. elif call.args[0].value != name:
  3756. msg = 'String argument 1 "{}" to {}(...) does not match variable name "{}"'
  3757. self.fail(msg.format(call.args[0].value, typevarlike_type, name), context)
  3758. return False
  3759. return True
  3760. def get_typevarlike_declaration(
  3761. self, s: AssignmentStmt, typevarlike_types: tuple[str, ...]
  3762. ) -> CallExpr | None:
  3763. """Returns the call expression if `s` is a declaration of `typevarlike_type`
  3764. (TypeVar or ParamSpec), or None otherwise.
  3765. """
  3766. if len(s.lvalues) != 1 or not isinstance(s.lvalues[0], NameExpr):
  3767. return None
  3768. if not isinstance(s.rvalue, CallExpr):
  3769. return None
  3770. call = s.rvalue
  3771. callee = call.callee
  3772. if not isinstance(callee, RefExpr):
  3773. return None
  3774. if callee.fullname not in typevarlike_types:
  3775. return None
  3776. return call
  3777. def process_typevar_parameters(
  3778. self,
  3779. args: list[Expression],
  3780. names: list[str | None],
  3781. kinds: list[ArgKind],
  3782. num_values: int,
  3783. context: Context,
  3784. ) -> tuple[int, Type, Type] | None:
  3785. has_values = num_values > 0
  3786. covariant = False
  3787. contravariant = False
  3788. upper_bound: Type = self.object_type()
  3789. default: Type = AnyType(TypeOfAny.from_omitted_generics)
  3790. for param_value, param_name, param_kind in zip(args, names, kinds):
  3791. if not param_kind.is_named():
  3792. self.fail(message_registry.TYPEVAR_UNEXPECTED_ARGUMENT, context)
  3793. return None
  3794. if param_name == "covariant":
  3795. if isinstance(param_value, NameExpr) and param_value.name in ("True", "False"):
  3796. covariant = param_value.name == "True"
  3797. else:
  3798. self.fail(message_registry.TYPEVAR_VARIANCE_DEF.format("covariant"), context)
  3799. return None
  3800. elif param_name == "contravariant":
  3801. if isinstance(param_value, NameExpr) and param_value.name in ("True", "False"):
  3802. contravariant = param_value.name == "True"
  3803. else:
  3804. self.fail(
  3805. message_registry.TYPEVAR_VARIANCE_DEF.format("contravariant"), context
  3806. )
  3807. return None
  3808. elif param_name == "bound":
  3809. if has_values:
  3810. self.fail("TypeVar cannot have both values and an upper bound", context)
  3811. return None
  3812. tv_arg = self.get_typevarlike_argument("TypeVar", param_name, param_value, context)
  3813. if tv_arg is None:
  3814. return None
  3815. upper_bound = tv_arg
  3816. elif param_name == "default":
  3817. tv_arg = self.get_typevarlike_argument(
  3818. "TypeVar", param_name, param_value, context, allow_unbound_tvars=True
  3819. )
  3820. default = tv_arg or AnyType(TypeOfAny.from_error)
  3821. elif param_name == "values":
  3822. # Probably using obsolete syntax with values=(...). Explain the current syntax.
  3823. self.fail('TypeVar "values" argument not supported', context)
  3824. self.fail(
  3825. "Use TypeVar('T', t, ...) instead of TypeVar('T', values=(t, ...))", context
  3826. )
  3827. return None
  3828. else:
  3829. self.fail(
  3830. f'{message_registry.TYPEVAR_UNEXPECTED_ARGUMENT}: "{param_name}"', context
  3831. )
  3832. return None
  3833. if covariant and contravariant:
  3834. self.fail("TypeVar cannot be both covariant and contravariant", context)
  3835. return None
  3836. elif num_values == 1:
  3837. self.fail("TypeVar cannot have only a single constraint", context)
  3838. return None
  3839. elif covariant:
  3840. variance = COVARIANT
  3841. elif contravariant:
  3842. variance = CONTRAVARIANT
  3843. else:
  3844. variance = INVARIANT
  3845. return variance, upper_bound, default
  3846. def get_typevarlike_argument(
  3847. self,
  3848. typevarlike_name: str,
  3849. param_name: str,
  3850. param_value: Expression,
  3851. context: Context,
  3852. *,
  3853. allow_unbound_tvars: bool = False,
  3854. allow_param_spec_literals: bool = False,
  3855. report_invalid_typevar_arg: bool = True,
  3856. ) -> ProperType | None:
  3857. try:
  3858. # We want to use our custom error message below, so we suppress
  3859. # the default error message for invalid types here.
  3860. analyzed = self.expr_to_analyzed_type(
  3861. param_value,
  3862. allow_placeholder=True,
  3863. report_invalid_types=False,
  3864. allow_unbound_tvars=allow_unbound_tvars,
  3865. allow_param_spec_literals=allow_param_spec_literals,
  3866. )
  3867. if analyzed is None:
  3868. # Type variables are special: we need to place them in the symbol table
  3869. # soon, even if upper bound is not ready yet. Otherwise avoiding
  3870. # a "deadlock" in this common pattern would be tricky:
  3871. # T = TypeVar('T', bound=Custom[Any])
  3872. # class Custom(Generic[T]):
  3873. # ...
  3874. analyzed = PlaceholderType(None, [], context.line)
  3875. typ = get_proper_type(analyzed)
  3876. if report_invalid_typevar_arg and isinstance(typ, AnyType) and typ.is_from_error:
  3877. self.fail(
  3878. message_registry.TYPEVAR_ARG_MUST_BE_TYPE.format(typevarlike_name, param_name),
  3879. param_value,
  3880. )
  3881. # Note: we do not return 'None' here -- we want to continue
  3882. # using the AnyType.
  3883. return typ
  3884. except TypeTranslationError:
  3885. if report_invalid_typevar_arg:
  3886. self.fail(
  3887. message_registry.TYPEVAR_ARG_MUST_BE_TYPE.format(typevarlike_name, param_name),
  3888. param_value,
  3889. )
  3890. return None
  3891. def extract_typevarlike_name(self, s: AssignmentStmt, call: CallExpr) -> str | None:
  3892. if not call:
  3893. return None
  3894. lvalue = s.lvalues[0]
  3895. assert isinstance(lvalue, NameExpr)
  3896. if s.type:
  3897. self.fail("Cannot declare the type of a TypeVar or similar construct", s)
  3898. return None
  3899. if not self.check_typevarlike_name(call, lvalue.name, s):
  3900. return None
  3901. return lvalue.name
  3902. def process_paramspec_declaration(self, s: AssignmentStmt) -> bool:
  3903. """Checks if s declares a ParamSpec; if yes, store it in symbol table.
  3904. Return True if this looks like a ParamSpec (maybe with errors), otherwise return False.
  3905. In the future, ParamSpec may accept bounds and variance arguments, in which
  3906. case more aggressive sharing of code with process_typevar_declaration should be pursued.
  3907. """
  3908. call = self.get_typevarlike_declaration(
  3909. s, ("typing_extensions.ParamSpec", "typing.ParamSpec")
  3910. )
  3911. if not call:
  3912. return False
  3913. name = self.extract_typevarlike_name(s, call)
  3914. if name is None:
  3915. return False
  3916. n_values = call.arg_kinds[1:].count(ARG_POS)
  3917. if n_values != 0:
  3918. self.fail('Too many positional arguments for "ParamSpec"', s)
  3919. default: Type = AnyType(TypeOfAny.from_omitted_generics)
  3920. for param_value, param_name in zip(
  3921. call.args[1 + n_values :], call.arg_names[1 + n_values :]
  3922. ):
  3923. if param_name == "default":
  3924. tv_arg = self.get_typevarlike_argument(
  3925. "ParamSpec",
  3926. param_name,
  3927. param_value,
  3928. s,
  3929. allow_unbound_tvars=True,
  3930. allow_param_spec_literals=True,
  3931. report_invalid_typevar_arg=False,
  3932. )
  3933. default = tv_arg or AnyType(TypeOfAny.from_error)
  3934. if isinstance(tv_arg, Parameters):
  3935. for i, arg_type in enumerate(tv_arg.arg_types):
  3936. typ = get_proper_type(arg_type)
  3937. if isinstance(typ, AnyType) and typ.is_from_error:
  3938. self.fail(
  3939. f"Argument {i} of ParamSpec default must be a type", param_value
  3940. )
  3941. elif (
  3942. isinstance(default, AnyType)
  3943. and default.is_from_error
  3944. or not isinstance(default, (AnyType, UnboundType))
  3945. ):
  3946. self.fail(
  3947. "The default argument to ParamSpec must be a list expression, ellipsis, or a ParamSpec",
  3948. param_value,
  3949. )
  3950. default = AnyType(TypeOfAny.from_error)
  3951. else:
  3952. # ParamSpec is different from a regular TypeVar:
  3953. # arguments are not semantically valid. But, allowed in runtime.
  3954. # So, we need to warn users about possible invalid usage.
  3955. self.fail(
  3956. "The variance and bound arguments to ParamSpec do not have defined semantics yet",
  3957. s,
  3958. )
  3959. # PEP 612 reserves the right to define bound, covariant and contravariant arguments to
  3960. # ParamSpec in a later PEP. If and when that happens, we should do something
  3961. # on the lines of process_typevar_parameters
  3962. if not call.analyzed:
  3963. paramspec_var = ParamSpecExpr(
  3964. name, self.qualified_name(name), self.object_type(), default, INVARIANT
  3965. )
  3966. paramspec_var.line = call.line
  3967. call.analyzed = paramspec_var
  3968. updated = True
  3969. else:
  3970. assert isinstance(call.analyzed, ParamSpecExpr)
  3971. updated = default != call.analyzed.default
  3972. call.analyzed.default = default
  3973. if has_placeholder(default):
  3974. self.process_placeholder(None, "ParamSpec default", s, force_progress=updated)
  3975. self.add_symbol(name, call.analyzed, s)
  3976. return True
  3977. def process_typevartuple_declaration(self, s: AssignmentStmt) -> bool:
  3978. """Checks if s declares a TypeVarTuple; if yes, store it in symbol table.
  3979. Return True if this looks like a TypeVarTuple (maybe with errors), otherwise return False.
  3980. """
  3981. call = self.get_typevarlike_declaration(
  3982. s, ("typing_extensions.TypeVarTuple", "typing.TypeVarTuple")
  3983. )
  3984. if not call:
  3985. return False
  3986. n_values = call.arg_kinds[1:].count(ARG_POS)
  3987. if n_values != 0:
  3988. self.fail('Too many positional arguments for "TypeVarTuple"', s)
  3989. default: Type = AnyType(TypeOfAny.from_omitted_generics)
  3990. for param_value, param_name in zip(
  3991. call.args[1 + n_values :], call.arg_names[1 + n_values :]
  3992. ):
  3993. if param_name == "default":
  3994. tv_arg = self.get_typevarlike_argument(
  3995. "TypeVarTuple",
  3996. param_name,
  3997. param_value,
  3998. s,
  3999. allow_unbound_tvars=True,
  4000. report_invalid_typevar_arg=False,
  4001. )
  4002. default = tv_arg or AnyType(TypeOfAny.from_error)
  4003. if not isinstance(default, UnpackType):
  4004. self.fail(
  4005. "The default argument to TypeVarTuple must be an Unpacked tuple",
  4006. param_value,
  4007. )
  4008. default = AnyType(TypeOfAny.from_error)
  4009. else:
  4010. self.fail(f'Unexpected keyword argument "{param_name}" for "TypeVarTuple"', s)
  4011. if not self.incomplete_feature_enabled(TYPE_VAR_TUPLE, s):
  4012. return False
  4013. name = self.extract_typevarlike_name(s, call)
  4014. if name is None:
  4015. return False
  4016. # PEP 646 does not specify the behavior of variance, constraints, or bounds.
  4017. if not call.analyzed:
  4018. tuple_fallback = self.named_type("builtins.tuple", [self.object_type()])
  4019. typevartuple_var = TypeVarTupleExpr(
  4020. name,
  4021. self.qualified_name(name),
  4022. self.object_type(),
  4023. tuple_fallback,
  4024. default,
  4025. INVARIANT,
  4026. )
  4027. typevartuple_var.line = call.line
  4028. call.analyzed = typevartuple_var
  4029. updated = True
  4030. else:
  4031. assert isinstance(call.analyzed, TypeVarTupleExpr)
  4032. updated = default != call.analyzed.default
  4033. call.analyzed.default = default
  4034. if has_placeholder(default):
  4035. self.process_placeholder(None, "TypeVarTuple default", s, force_progress=updated)
  4036. self.add_symbol(name, call.analyzed, s)
  4037. return True
  4038. def basic_new_typeinfo(self, name: str, basetype_or_fallback: Instance, line: int) -> TypeInfo:
  4039. if self.is_func_scope() and not self.type and "@" not in name:
  4040. name += "@" + str(line)
  4041. class_def = ClassDef(name, Block([]))
  4042. if self.is_func_scope() and not self.type:
  4043. # Full names of generated classes should always be prefixed with the module names
  4044. # even if they are nested in a function, since these classes will be (de-)serialized.
  4045. # (Note that the caller should append @line to the name to avoid collisions.)
  4046. # TODO: clean this up, see #6422.
  4047. class_def.fullname = self.cur_mod_id + "." + self.qualified_name(name)
  4048. else:
  4049. class_def.fullname = self.qualified_name(name)
  4050. info = TypeInfo(SymbolTable(), class_def, self.cur_mod_id)
  4051. class_def.info = info
  4052. mro = basetype_or_fallback.type.mro
  4053. if not mro:
  4054. # Probably an error, we should not crash so generate something meaningful.
  4055. mro = [basetype_or_fallback.type, self.object_type().type]
  4056. info.mro = [info] + mro
  4057. info.bases = [basetype_or_fallback]
  4058. return info
  4059. def analyze_value_types(self, items: list[Expression]) -> list[Type]:
  4060. """Analyze types from values expressions in type variable definition."""
  4061. result: list[Type] = []
  4062. for node in items:
  4063. try:
  4064. analyzed = self.anal_type(
  4065. self.expr_to_unanalyzed_type(node), allow_placeholder=True
  4066. )
  4067. if analyzed is None:
  4068. # Type variables are special: we need to place them in the symbol table
  4069. # soon, even if some value is not ready yet, see process_typevar_parameters()
  4070. # for an example.
  4071. analyzed = PlaceholderType(None, [], node.line)
  4072. result.append(analyzed)
  4073. except TypeTranslationError:
  4074. self.fail("Type expected", node)
  4075. result.append(AnyType(TypeOfAny.from_error))
  4076. return result
  4077. def check_classvar(self, s: AssignmentStmt) -> None:
  4078. """Check if assignment defines a class variable."""
  4079. lvalue = s.lvalues[0]
  4080. if len(s.lvalues) != 1 or not isinstance(lvalue, RefExpr):
  4081. return
  4082. if not s.type or not self.is_classvar(s.type):
  4083. return
  4084. if self.is_class_scope() and isinstance(lvalue, NameExpr):
  4085. node = lvalue.node
  4086. if isinstance(node, Var):
  4087. node.is_classvar = True
  4088. analyzed = self.anal_type(s.type)
  4089. assert self.type is not None
  4090. if analyzed is not None and set(get_type_vars(analyzed)) & set(
  4091. self.type.defn.type_vars
  4092. ):
  4093. # This means that we have a type var defined inside of a ClassVar.
  4094. # This is not allowed by PEP526.
  4095. # See https://github.com/python/mypy/issues/11538
  4096. self.fail(message_registry.CLASS_VAR_WITH_TYPEVARS, s)
  4097. if (
  4098. analyzed is not None
  4099. and self.type.self_type in get_type_vars(analyzed)
  4100. and self.type.defn.type_vars
  4101. ):
  4102. self.fail(message_registry.CLASS_VAR_WITH_GENERIC_SELF, s)
  4103. elif not isinstance(lvalue, MemberExpr) or self.is_self_member_ref(lvalue):
  4104. # In case of member access, report error only when assigning to self
  4105. # Other kinds of member assignments should be already reported
  4106. self.fail_invalid_classvar(lvalue)
  4107. def is_classvar(self, typ: Type) -> bool:
  4108. if not isinstance(typ, UnboundType):
  4109. return False
  4110. sym = self.lookup_qualified(typ.name, typ)
  4111. if not sym or not sym.node:
  4112. return False
  4113. return sym.node.fullname == "typing.ClassVar"
  4114. def is_final_type(self, typ: Type | None) -> bool:
  4115. if not isinstance(typ, UnboundType):
  4116. return False
  4117. sym = self.lookup_qualified(typ.name, typ)
  4118. if not sym or not sym.node:
  4119. return False
  4120. return sym.node.fullname in FINAL_TYPE_NAMES
  4121. def fail_invalid_classvar(self, context: Context) -> None:
  4122. self.fail(message_registry.CLASS_VAR_OUTSIDE_OF_CLASS, context)
  4123. def process_module_assignment(
  4124. self, lvals: list[Lvalue], rval: Expression, ctx: AssignmentStmt
  4125. ) -> None:
  4126. """Propagate module references across assignments.
  4127. Recursively handles the simple form of iterable unpacking; doesn't
  4128. handle advanced unpacking with *rest, dictionary unpacking, etc.
  4129. In an expression like x = y = z, z is the rval and lvals will be [x,
  4130. y].
  4131. """
  4132. if isinstance(rval, (TupleExpr, ListExpr)) and all(
  4133. isinstance(v, TupleExpr) for v in lvals
  4134. ):
  4135. # rval and all lvals are either list or tuple, so we are dealing
  4136. # with unpacking assignment like `x, y = a, b`. Mypy didn't
  4137. # understand our all(isinstance(...)), so cast them as TupleExpr
  4138. # so mypy knows it is safe to access their .items attribute.
  4139. seq_lvals = cast(List[TupleExpr], lvals)
  4140. # given an assignment like:
  4141. # (x, y) = (m, n) = (a, b)
  4142. # we now have:
  4143. # seq_lvals = [(x, y), (m, n)]
  4144. # seq_rval = (a, b)
  4145. # We now zip this into:
  4146. # elementwise_assignments = [(a, x, m), (b, y, n)]
  4147. # where each elementwise assignment includes one element of rval and the
  4148. # corresponding element of each lval. Basically we unpack
  4149. # (x, y) = (m, n) = (a, b)
  4150. # into elementwise assignments
  4151. # x = m = a
  4152. # y = n = b
  4153. # and then we recursively call this method for each of those assignments.
  4154. # If the rval and all lvals are not all of the same length, zip will just ignore
  4155. # extra elements, so no error will be raised here; mypy will later complain
  4156. # about the length mismatch in type-checking.
  4157. elementwise_assignments = zip(rval.items, *[v.items for v in seq_lvals])
  4158. for rv, *lvs in elementwise_assignments:
  4159. self.process_module_assignment(lvs, rv, ctx)
  4160. elif isinstance(rval, RefExpr):
  4161. rnode = self.lookup_type_node(rval)
  4162. if rnode and isinstance(rnode.node, MypyFile):
  4163. for lval in lvals:
  4164. if not isinstance(lval, RefExpr):
  4165. continue
  4166. # respect explicitly annotated type
  4167. if isinstance(lval.node, Var) and lval.node.type is not None:
  4168. continue
  4169. # We can handle these assignments to locals and to self
  4170. if isinstance(lval, NameExpr):
  4171. lnode = self.current_symbol_table().get(lval.name)
  4172. elif isinstance(lval, MemberExpr) and self.is_self_member_ref(lval):
  4173. assert self.type is not None
  4174. lnode = self.type.names.get(lval.name)
  4175. else:
  4176. continue
  4177. if lnode:
  4178. if isinstance(lnode.node, MypyFile) and lnode.node is not rnode.node:
  4179. assert isinstance(lval, (NameExpr, MemberExpr))
  4180. self.fail(
  4181. 'Cannot assign multiple modules to name "{}" '
  4182. 'without explicit "types.ModuleType" annotation'.format(lval.name),
  4183. ctx,
  4184. )
  4185. # never create module alias except on initial var definition
  4186. elif lval.is_inferred_def:
  4187. assert rnode.node is not None
  4188. lnode.node = rnode.node
  4189. def process__all__(self, s: AssignmentStmt) -> None:
  4190. """Export names if argument is a __all__ assignment."""
  4191. if (
  4192. len(s.lvalues) == 1
  4193. and isinstance(s.lvalues[0], NameExpr)
  4194. and s.lvalues[0].name == "__all__"
  4195. and s.lvalues[0].kind == GDEF
  4196. and isinstance(s.rvalue, (ListExpr, TupleExpr))
  4197. ):
  4198. self.add_exports(s.rvalue.items)
  4199. def process__deletable__(self, s: AssignmentStmt) -> None:
  4200. if not self.options.mypyc:
  4201. return
  4202. if (
  4203. len(s.lvalues) == 1
  4204. and isinstance(s.lvalues[0], NameExpr)
  4205. and s.lvalues[0].name == "__deletable__"
  4206. and s.lvalues[0].kind == MDEF
  4207. ):
  4208. rvalue = s.rvalue
  4209. if not isinstance(rvalue, (ListExpr, TupleExpr)):
  4210. self.fail('"__deletable__" must be initialized with a list or tuple expression', s)
  4211. return
  4212. items = rvalue.items
  4213. attrs = []
  4214. for item in items:
  4215. if not isinstance(item, StrExpr):
  4216. self.fail('Invalid "__deletable__" item; string literal expected', item)
  4217. else:
  4218. attrs.append(item.value)
  4219. assert self.type
  4220. self.type.deletable_attributes = attrs
  4221. def process__slots__(self, s: AssignmentStmt) -> None:
  4222. """
  4223. Processing ``__slots__`` if defined in type.
  4224. See: https://docs.python.org/3/reference/datamodel.html#slots
  4225. """
  4226. # Later we can support `__slots__` defined as `__slots__ = other = ('a', 'b')`
  4227. if (
  4228. isinstance(self.type, TypeInfo)
  4229. and len(s.lvalues) == 1
  4230. and isinstance(s.lvalues[0], NameExpr)
  4231. and s.lvalues[0].name == "__slots__"
  4232. and s.lvalues[0].kind == MDEF
  4233. ):
  4234. # We understand `__slots__` defined as string, tuple, list, set, and dict:
  4235. if not isinstance(s.rvalue, (StrExpr, ListExpr, TupleExpr, SetExpr, DictExpr)):
  4236. # For example, `__slots__` can be defined as a variable,
  4237. # we don't support it for now.
  4238. return
  4239. if any(p.slots is None for p in self.type.mro[1:-1]):
  4240. # At least one type in mro (excluding `self` and `object`)
  4241. # does not have concrete `__slots__` defined. Ignoring.
  4242. return
  4243. concrete_slots = True
  4244. rvalue: list[Expression] = []
  4245. if isinstance(s.rvalue, StrExpr):
  4246. rvalue.append(s.rvalue)
  4247. elif isinstance(s.rvalue, (ListExpr, TupleExpr, SetExpr)):
  4248. rvalue.extend(s.rvalue.items)
  4249. else:
  4250. # We have a special treatment of `dict` with possible `{**kwargs}` usage.
  4251. # In this case we consider all `__slots__` to be non-concrete.
  4252. for key, _ in s.rvalue.items:
  4253. if concrete_slots and key is not None:
  4254. rvalue.append(key)
  4255. else:
  4256. concrete_slots = False
  4257. slots = []
  4258. for item in rvalue:
  4259. # Special case for `'__dict__'` value:
  4260. # when specified it will still allow any attribute assignment.
  4261. if isinstance(item, StrExpr) and item.value != "__dict__":
  4262. slots.append(item.value)
  4263. else:
  4264. concrete_slots = False
  4265. if not concrete_slots:
  4266. # Some slot items are dynamic, we don't want any false positives,
  4267. # so, we just pretend that this type does not have any slots at all.
  4268. return
  4269. # We need to copy all slots from super types:
  4270. for super_type in self.type.mro[1:-1]:
  4271. assert super_type.slots is not None
  4272. slots.extend(super_type.slots)
  4273. self.type.slots = set(slots)
  4274. #
  4275. # Misc statements
  4276. #
  4277. def visit_block(self, b: Block) -> None:
  4278. if b.is_unreachable:
  4279. return
  4280. self.block_depth[-1] += 1
  4281. for s in b.body:
  4282. self.accept(s)
  4283. self.block_depth[-1] -= 1
  4284. def visit_block_maybe(self, b: Block | None) -> None:
  4285. if b:
  4286. self.visit_block(b)
  4287. def visit_expression_stmt(self, s: ExpressionStmt) -> None:
  4288. self.statement = s
  4289. s.expr.accept(self)
  4290. def visit_return_stmt(self, s: ReturnStmt) -> None:
  4291. self.statement = s
  4292. if not self.is_func_scope():
  4293. self.fail('"return" outside function', s)
  4294. if s.expr:
  4295. s.expr.accept(self)
  4296. def visit_raise_stmt(self, s: RaiseStmt) -> None:
  4297. self.statement = s
  4298. if s.expr:
  4299. s.expr.accept(self)
  4300. if s.from_expr:
  4301. s.from_expr.accept(self)
  4302. def visit_assert_stmt(self, s: AssertStmt) -> None:
  4303. self.statement = s
  4304. if s.expr:
  4305. s.expr.accept(self)
  4306. if s.msg:
  4307. s.msg.accept(self)
  4308. def visit_operator_assignment_stmt(self, s: OperatorAssignmentStmt) -> None:
  4309. self.statement = s
  4310. s.lvalue.accept(self)
  4311. s.rvalue.accept(self)
  4312. if (
  4313. isinstance(s.lvalue, NameExpr)
  4314. and s.lvalue.name == "__all__"
  4315. and s.lvalue.kind == GDEF
  4316. and isinstance(s.rvalue, (ListExpr, TupleExpr))
  4317. ):
  4318. self.add_exports(s.rvalue.items)
  4319. def visit_while_stmt(self, s: WhileStmt) -> None:
  4320. self.statement = s
  4321. s.expr.accept(self)
  4322. self.loop_depth[-1] += 1
  4323. s.body.accept(self)
  4324. self.loop_depth[-1] -= 1
  4325. self.visit_block_maybe(s.else_body)
  4326. def visit_for_stmt(self, s: ForStmt) -> None:
  4327. if s.is_async:
  4328. if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
  4329. self.fail(message_registry.ASYNC_FOR_OUTSIDE_COROUTINE, s, code=codes.SYNTAX)
  4330. self.statement = s
  4331. s.expr.accept(self)
  4332. # Bind index variables and check if they define new names.
  4333. self.analyze_lvalue(s.index, explicit_type=s.index_type is not None)
  4334. if s.index_type:
  4335. if self.is_classvar(s.index_type):
  4336. self.fail_invalid_classvar(s.index)
  4337. allow_tuple_literal = isinstance(s.index, TupleExpr)
  4338. analyzed = self.anal_type(s.index_type, allow_tuple_literal=allow_tuple_literal)
  4339. if analyzed is not None:
  4340. self.store_declared_types(s.index, analyzed)
  4341. s.index_type = analyzed
  4342. self.loop_depth[-1] += 1
  4343. self.visit_block(s.body)
  4344. self.loop_depth[-1] -= 1
  4345. self.visit_block_maybe(s.else_body)
  4346. def visit_break_stmt(self, s: BreakStmt) -> None:
  4347. self.statement = s
  4348. if self.loop_depth[-1] == 0:
  4349. self.fail('"break" outside loop', s, serious=True, blocker=True)
  4350. def visit_continue_stmt(self, s: ContinueStmt) -> None:
  4351. self.statement = s
  4352. if self.loop_depth[-1] == 0:
  4353. self.fail('"continue" outside loop', s, serious=True, blocker=True)
  4354. def visit_if_stmt(self, s: IfStmt) -> None:
  4355. self.statement = s
  4356. infer_reachability_of_if_statement(s, self.options)
  4357. for i in range(len(s.expr)):
  4358. s.expr[i].accept(self)
  4359. self.visit_block(s.body[i])
  4360. self.visit_block_maybe(s.else_body)
  4361. def visit_try_stmt(self, s: TryStmt) -> None:
  4362. self.statement = s
  4363. self.analyze_try_stmt(s, self)
  4364. def analyze_try_stmt(self, s: TryStmt, visitor: NodeVisitor[None]) -> None:
  4365. s.body.accept(visitor)
  4366. for type, var, handler in zip(s.types, s.vars, s.handlers):
  4367. if type:
  4368. type.accept(visitor)
  4369. if var:
  4370. self.analyze_lvalue(var)
  4371. handler.accept(visitor)
  4372. if s.else_body:
  4373. s.else_body.accept(visitor)
  4374. if s.finally_body:
  4375. s.finally_body.accept(visitor)
  4376. def visit_with_stmt(self, s: WithStmt) -> None:
  4377. self.statement = s
  4378. types: list[Type] = []
  4379. if s.is_async:
  4380. if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
  4381. self.fail(message_registry.ASYNC_WITH_OUTSIDE_COROUTINE, s, code=codes.SYNTAX)
  4382. if s.unanalyzed_type:
  4383. assert isinstance(s.unanalyzed_type, ProperType)
  4384. actual_targets = [t for t in s.target if t is not None]
  4385. if len(actual_targets) == 0:
  4386. # We have a type for no targets
  4387. self.fail('Invalid type comment: "with" statement has no targets', s)
  4388. elif len(actual_targets) == 1:
  4389. # We have one target and one type
  4390. types = [s.unanalyzed_type]
  4391. elif isinstance(s.unanalyzed_type, TupleType):
  4392. # We have multiple targets and multiple types
  4393. if len(actual_targets) == len(s.unanalyzed_type.items):
  4394. types = s.unanalyzed_type.items.copy()
  4395. else:
  4396. # But it's the wrong number of items
  4397. self.fail('Incompatible number of types for "with" targets', s)
  4398. else:
  4399. # We have multiple targets and one type
  4400. self.fail('Multiple types expected for multiple "with" targets', s)
  4401. new_types: list[Type] = []
  4402. for e, n in zip(s.expr, s.target):
  4403. e.accept(self)
  4404. if n:
  4405. self.analyze_lvalue(n, explicit_type=s.unanalyzed_type is not None)
  4406. # Since we have a target, pop the next type from types
  4407. if types:
  4408. t = types.pop(0)
  4409. if self.is_classvar(t):
  4410. self.fail_invalid_classvar(n)
  4411. allow_tuple_literal = isinstance(n, TupleExpr)
  4412. analyzed = self.anal_type(t, allow_tuple_literal=allow_tuple_literal)
  4413. if analyzed is not None:
  4414. # TODO: Deal with this better
  4415. new_types.append(analyzed)
  4416. self.store_declared_types(n, analyzed)
  4417. s.analyzed_types = new_types
  4418. self.visit_block(s.body)
  4419. def visit_del_stmt(self, s: DelStmt) -> None:
  4420. self.statement = s
  4421. s.expr.accept(self)
  4422. if not self.is_valid_del_target(s.expr):
  4423. self.fail("Invalid delete target", s)
  4424. def is_valid_del_target(self, s: Expression) -> bool:
  4425. if isinstance(s, (IndexExpr, NameExpr, MemberExpr)):
  4426. return True
  4427. elif isinstance(s, (TupleExpr, ListExpr)):
  4428. return all(self.is_valid_del_target(item) for item in s.items)
  4429. else:
  4430. return False
  4431. def visit_global_decl(self, g: GlobalDecl) -> None:
  4432. self.statement = g
  4433. for name in g.names:
  4434. if name in self.nonlocal_decls[-1]:
  4435. self.fail(f'Name "{name}" is nonlocal and global', g)
  4436. self.global_decls[-1].add(name)
  4437. def visit_nonlocal_decl(self, d: NonlocalDecl) -> None:
  4438. self.statement = d
  4439. if self.is_module_scope():
  4440. self.fail("nonlocal declaration not allowed at module level", d)
  4441. else:
  4442. for name in d.names:
  4443. for table in reversed(self.locals[:-1]):
  4444. if table is not None and name in table:
  4445. break
  4446. else:
  4447. self.fail(f'No binding for nonlocal "{name}" found', d)
  4448. if self.locals[-1] is not None and name in self.locals[-1]:
  4449. self.fail(
  4450. 'Name "{}" is already defined in local '
  4451. "scope before nonlocal declaration".format(name),
  4452. d,
  4453. )
  4454. if name in self.global_decls[-1]:
  4455. self.fail(f'Name "{name}" is nonlocal and global', d)
  4456. self.nonlocal_decls[-1].add(name)
  4457. def visit_match_stmt(self, s: MatchStmt) -> None:
  4458. self.statement = s
  4459. infer_reachability_of_match_statement(s, self.options)
  4460. s.subject.accept(self)
  4461. for i in range(len(s.patterns)):
  4462. s.patterns[i].accept(self)
  4463. guard = s.guards[i]
  4464. if guard is not None:
  4465. guard.accept(self)
  4466. self.visit_block(s.bodies[i])
  4467. #
  4468. # Expressions
  4469. #
  4470. def visit_name_expr(self, expr: NameExpr) -> None:
  4471. n = self.lookup(expr.name, expr)
  4472. if n:
  4473. self.bind_name_expr(expr, n)
  4474. def bind_name_expr(self, expr: NameExpr, sym: SymbolTableNode) -> None:
  4475. """Bind name expression to a symbol table node."""
  4476. if isinstance(sym.node, TypeVarExpr) and self.tvar_scope.get_binding(sym):
  4477. self.fail(
  4478. '"{}" is a type variable and only valid in type ' "context".format(expr.name), expr
  4479. )
  4480. elif isinstance(sym.node, PlaceholderNode):
  4481. self.process_placeholder(expr.name, "name", expr)
  4482. else:
  4483. expr.kind = sym.kind
  4484. expr.node = sym.node
  4485. expr.fullname = sym.fullname or ""
  4486. def visit_super_expr(self, expr: SuperExpr) -> None:
  4487. if not self.type and not expr.call.args:
  4488. self.fail('"super" used outside class', expr)
  4489. return
  4490. expr.info = self.type
  4491. for arg in expr.call.args:
  4492. arg.accept(self)
  4493. def visit_tuple_expr(self, expr: TupleExpr) -> None:
  4494. for item in expr.items:
  4495. if isinstance(item, StarExpr):
  4496. item.valid = True
  4497. item.accept(self)
  4498. def visit_list_expr(self, expr: ListExpr) -> None:
  4499. for item in expr.items:
  4500. if isinstance(item, StarExpr):
  4501. item.valid = True
  4502. item.accept(self)
  4503. def visit_set_expr(self, expr: SetExpr) -> None:
  4504. for item in expr.items:
  4505. if isinstance(item, StarExpr):
  4506. item.valid = True
  4507. item.accept(self)
  4508. def visit_dict_expr(self, expr: DictExpr) -> None:
  4509. for key, value in expr.items:
  4510. if key is not None:
  4511. key.accept(self)
  4512. value.accept(self)
  4513. def visit_star_expr(self, expr: StarExpr) -> None:
  4514. if not expr.valid:
  4515. self.fail("Can use starred expression only as assignment target", expr, blocker=True)
  4516. else:
  4517. expr.expr.accept(self)
  4518. def visit_yield_from_expr(self, e: YieldFromExpr) -> None:
  4519. if not self.is_func_scope():
  4520. self.fail('"yield from" outside function', e, serious=True, blocker=True)
  4521. elif self.is_comprehension_stack[-1]:
  4522. self.fail(
  4523. '"yield from" inside comprehension or generator expression',
  4524. e,
  4525. serious=True,
  4526. blocker=True,
  4527. )
  4528. elif self.function_stack[-1].is_coroutine:
  4529. self.fail('"yield from" in async function', e, serious=True, blocker=True)
  4530. else:
  4531. self.function_stack[-1].is_generator = True
  4532. if e.expr:
  4533. e.expr.accept(self)
  4534. def visit_call_expr(self, expr: CallExpr) -> None:
  4535. """Analyze a call expression.
  4536. Some call expressions are recognized as special forms, including
  4537. cast(...).
  4538. """
  4539. expr.callee.accept(self)
  4540. if refers_to_fullname(expr.callee, "typing.cast"):
  4541. # Special form cast(...).
  4542. if not self.check_fixed_args(expr, 2, "cast"):
  4543. return
  4544. # Translate first argument to an unanalyzed type.
  4545. try:
  4546. target = self.expr_to_unanalyzed_type(expr.args[0])
  4547. except TypeTranslationError:
  4548. self.fail("Cast target is not a type", expr)
  4549. return
  4550. # Piggyback CastExpr object to the CallExpr object; it takes
  4551. # precedence over the CallExpr semantics.
  4552. expr.analyzed = CastExpr(expr.args[1], target)
  4553. expr.analyzed.line = expr.line
  4554. expr.analyzed.column = expr.column
  4555. expr.analyzed.accept(self)
  4556. elif refers_to_fullname(expr.callee, ASSERT_TYPE_NAMES):
  4557. if not self.check_fixed_args(expr, 2, "assert_type"):
  4558. return
  4559. # Translate second argument to an unanalyzed type.
  4560. try:
  4561. target = self.expr_to_unanalyzed_type(expr.args[1])
  4562. except TypeTranslationError:
  4563. self.fail("assert_type() type is not a type", expr)
  4564. return
  4565. expr.analyzed = AssertTypeExpr(expr.args[0], target)
  4566. expr.analyzed.line = expr.line
  4567. expr.analyzed.column = expr.column
  4568. expr.analyzed.accept(self)
  4569. elif refers_to_fullname(expr.callee, REVEAL_TYPE_NAMES):
  4570. if not self.check_fixed_args(expr, 1, "reveal_type"):
  4571. return
  4572. expr.analyzed = RevealExpr(kind=REVEAL_TYPE, expr=expr.args[0])
  4573. expr.analyzed.line = expr.line
  4574. expr.analyzed.column = expr.column
  4575. expr.analyzed.accept(self)
  4576. elif refers_to_fullname(expr.callee, "builtins.reveal_locals"):
  4577. # Store the local variable names into the RevealExpr for use in the
  4578. # type checking pass
  4579. local_nodes: list[Var] = []
  4580. if self.is_module_scope():
  4581. # try to determine just the variable declarations in module scope
  4582. # self.globals.values() contains SymbolTableNode's
  4583. # Each SymbolTableNode has an attribute node that is nodes.Var
  4584. # look for variable nodes that marked as is_inferred
  4585. # Each symboltable node has a Var node as .node
  4586. local_nodes = [
  4587. n.node
  4588. for name, n in self.globals.items()
  4589. if getattr(n.node, "is_inferred", False) and isinstance(n.node, Var)
  4590. ]
  4591. elif self.is_class_scope():
  4592. # type = None # type: Optional[TypeInfo]
  4593. if self.type is not None:
  4594. local_nodes = [
  4595. st.node for st in self.type.names.values() if isinstance(st.node, Var)
  4596. ]
  4597. elif self.is_func_scope():
  4598. # locals = None # type: List[Optional[SymbolTable]]
  4599. if self.locals is not None:
  4600. symbol_table = self.locals[-1]
  4601. if symbol_table is not None:
  4602. local_nodes = [
  4603. st.node for st in symbol_table.values() if isinstance(st.node, Var)
  4604. ]
  4605. expr.analyzed = RevealExpr(kind=REVEAL_LOCALS, local_nodes=local_nodes)
  4606. expr.analyzed.line = expr.line
  4607. expr.analyzed.column = expr.column
  4608. expr.analyzed.accept(self)
  4609. elif refers_to_fullname(expr.callee, "typing.Any"):
  4610. # Special form Any(...) no longer supported.
  4611. self.fail("Any(...) is no longer supported. Use cast(Any, ...) instead", expr)
  4612. elif refers_to_fullname(expr.callee, "typing._promote"):
  4613. # Special form _promote(...).
  4614. if not self.check_fixed_args(expr, 1, "_promote"):
  4615. return
  4616. # Translate first argument to an unanalyzed type.
  4617. try:
  4618. target = self.expr_to_unanalyzed_type(expr.args[0])
  4619. except TypeTranslationError:
  4620. self.fail("Argument 1 to _promote is not a type", expr)
  4621. return
  4622. expr.analyzed = PromoteExpr(target)
  4623. expr.analyzed.line = expr.line
  4624. expr.analyzed.accept(self)
  4625. elif refers_to_fullname(expr.callee, "builtins.dict"):
  4626. expr.analyzed = self.translate_dict_call(expr)
  4627. elif refers_to_fullname(expr.callee, "builtins.divmod"):
  4628. if not self.check_fixed_args(expr, 2, "divmod"):
  4629. return
  4630. expr.analyzed = OpExpr("divmod", expr.args[0], expr.args[1])
  4631. expr.analyzed.line = expr.line
  4632. expr.analyzed.accept(self)
  4633. else:
  4634. # Normal call expression.
  4635. for a in expr.args:
  4636. a.accept(self)
  4637. if (
  4638. isinstance(expr.callee, MemberExpr)
  4639. and isinstance(expr.callee.expr, NameExpr)
  4640. and expr.callee.expr.name == "__all__"
  4641. and expr.callee.expr.kind == GDEF
  4642. and expr.callee.name in ("append", "extend", "remove")
  4643. ):
  4644. if expr.callee.name == "append" and expr.args:
  4645. self.add_exports(expr.args[0])
  4646. elif (
  4647. expr.callee.name == "extend"
  4648. and expr.args
  4649. and isinstance(expr.args[0], (ListExpr, TupleExpr))
  4650. ):
  4651. self.add_exports(expr.args[0].items)
  4652. elif (
  4653. expr.callee.name == "remove"
  4654. and expr.args
  4655. and isinstance(expr.args[0], StrExpr)
  4656. ):
  4657. self.all_exports = [n for n in self.all_exports if n != expr.args[0].value]
  4658. def translate_dict_call(self, call: CallExpr) -> DictExpr | None:
  4659. """Translate 'dict(x=y, ...)' to {'x': y, ...} and 'dict()' to {}.
  4660. For other variants of dict(...), return None.
  4661. """
  4662. if not all(kind in (ARG_NAMED, ARG_STAR2) for kind in call.arg_kinds):
  4663. # Must still accept those args.
  4664. for a in call.args:
  4665. a.accept(self)
  4666. return None
  4667. expr = DictExpr(
  4668. [
  4669. (StrExpr(key) if key is not None else None, value)
  4670. for key, value in zip(call.arg_names, call.args)
  4671. ]
  4672. )
  4673. expr.set_line(call)
  4674. expr.accept(self)
  4675. return expr
  4676. def check_fixed_args(self, expr: CallExpr, numargs: int, name: str) -> bool:
  4677. """Verify that expr has specified number of positional args.
  4678. Return True if the arguments are valid.
  4679. """
  4680. s = "s"
  4681. if numargs == 1:
  4682. s = ""
  4683. if len(expr.args) != numargs:
  4684. self.fail('"%s" expects %d argument%s' % (name, numargs, s), expr)
  4685. return False
  4686. if expr.arg_kinds != [ARG_POS] * numargs:
  4687. self.fail(f'"{name}" must be called with {numargs} positional argument{s}', expr)
  4688. return False
  4689. return True
  4690. def visit_member_expr(self, expr: MemberExpr) -> None:
  4691. base = expr.expr
  4692. base.accept(self)
  4693. if isinstance(base, RefExpr) and isinstance(base.node, MypyFile):
  4694. # Handle module attribute.
  4695. sym = self.get_module_symbol(base.node, expr.name)
  4696. if sym:
  4697. if isinstance(sym.node, PlaceholderNode):
  4698. self.process_placeholder(expr.name, "attribute", expr)
  4699. return
  4700. expr.kind = sym.kind
  4701. expr.fullname = sym.fullname or ""
  4702. expr.node = sym.node
  4703. elif isinstance(base, RefExpr):
  4704. # This branch handles the case C.bar (or cls.bar or self.bar inside
  4705. # a classmethod/method), where C is a class and bar is a type
  4706. # definition or a module resulting from `import bar` (or a module
  4707. # assignment) inside class C. We look up bar in the class' TypeInfo
  4708. # namespace. This is done only when bar is a module or a type;
  4709. # other things (e.g. methods) are handled by other code in
  4710. # checkmember.
  4711. type_info = None
  4712. if isinstance(base.node, TypeInfo):
  4713. # C.bar where C is a class
  4714. type_info = base.node
  4715. elif isinstance(base.node, Var) and self.type and self.function_stack:
  4716. # check for self.bar or cls.bar in method/classmethod
  4717. func_def = self.function_stack[-1]
  4718. if not func_def.is_static and isinstance(func_def.type, CallableType):
  4719. formal_arg = func_def.type.argument_by_name(base.node.name)
  4720. if formal_arg and formal_arg.pos == 0:
  4721. type_info = self.type
  4722. elif isinstance(base.node, TypeAlias) and base.node.no_args:
  4723. assert isinstance(base.node.target, ProperType)
  4724. if isinstance(base.node.target, Instance):
  4725. type_info = base.node.target.type
  4726. if type_info:
  4727. n = type_info.names.get(expr.name)
  4728. if n is not None and isinstance(n.node, (MypyFile, TypeInfo, TypeAlias)):
  4729. if not n:
  4730. return
  4731. expr.kind = n.kind
  4732. expr.fullname = n.fullname or ""
  4733. expr.node = n.node
  4734. def visit_op_expr(self, expr: OpExpr) -> None:
  4735. expr.left.accept(self)
  4736. if expr.op in ("and", "or"):
  4737. inferred = infer_condition_value(expr.left, self.options)
  4738. if (inferred in (ALWAYS_FALSE, MYPY_FALSE) and expr.op == "and") or (
  4739. inferred in (ALWAYS_TRUE, MYPY_TRUE) and expr.op == "or"
  4740. ):
  4741. expr.right_unreachable = True
  4742. return
  4743. elif (inferred in (ALWAYS_TRUE, MYPY_TRUE) and expr.op == "and") or (
  4744. inferred in (ALWAYS_FALSE, MYPY_FALSE) and expr.op == "or"
  4745. ):
  4746. expr.right_always = True
  4747. expr.right.accept(self)
  4748. def visit_comparison_expr(self, expr: ComparisonExpr) -> None:
  4749. for operand in expr.operands:
  4750. operand.accept(self)
  4751. def visit_unary_expr(self, expr: UnaryExpr) -> None:
  4752. expr.expr.accept(self)
  4753. def visit_index_expr(self, expr: IndexExpr) -> None:
  4754. base = expr.base
  4755. base.accept(self)
  4756. if (
  4757. isinstance(base, RefExpr)
  4758. and isinstance(base.node, TypeInfo)
  4759. and not base.node.is_generic()
  4760. ):
  4761. expr.index.accept(self)
  4762. elif (
  4763. isinstance(base, RefExpr) and isinstance(base.node, TypeAlias)
  4764. ) or refers_to_class_or_function(base):
  4765. # We need to do full processing on every iteration, since some type
  4766. # arguments may contain placeholder types.
  4767. self.analyze_type_application(expr)
  4768. else:
  4769. expr.index.accept(self)
  4770. def analyze_type_application(self, expr: IndexExpr) -> None:
  4771. """Analyze special form -- type application (either direct or via type aliasing)."""
  4772. types = self.analyze_type_application_args(expr)
  4773. if types is None:
  4774. return
  4775. base = expr.base
  4776. expr.analyzed = TypeApplication(base, types)
  4777. expr.analyzed.line = expr.line
  4778. expr.analyzed.column = expr.column
  4779. # Types list, dict, set are not subscriptable, prohibit this if
  4780. # subscripted either via type alias...
  4781. if isinstance(base, RefExpr) and isinstance(base.node, TypeAlias):
  4782. alias = base.node
  4783. target = get_proper_type(alias.target)
  4784. if isinstance(target, Instance):
  4785. name = target.type.fullname
  4786. if (
  4787. alias.no_args
  4788. and name # this avoids bogus errors for already reported aliases
  4789. in get_nongen_builtins(self.options.python_version)
  4790. and not self.is_stub_file
  4791. and not alias.normalized
  4792. ):
  4793. self.fail(no_subscript_builtin_alias(name, propose_alt=False), expr)
  4794. # ...or directly.
  4795. else:
  4796. n = self.lookup_type_node(base)
  4797. if (
  4798. n
  4799. and n.fullname in get_nongen_builtins(self.options.python_version)
  4800. and not self.is_stub_file
  4801. ):
  4802. self.fail(no_subscript_builtin_alias(n.fullname, propose_alt=False), expr)
  4803. def analyze_type_application_args(self, expr: IndexExpr) -> list[Type] | None:
  4804. """Analyze type arguments (index) in a type application.
  4805. Return None if anything was incomplete.
  4806. """
  4807. index = expr.index
  4808. tag = self.track_incomplete_refs()
  4809. self.analyze_type_expr(index)
  4810. if self.found_incomplete_ref(tag):
  4811. return None
  4812. if self.basic_type_applications:
  4813. # Postpone the rest until we have more information (for r.h.s. of an assignment)
  4814. return None
  4815. types: list[Type] = []
  4816. if isinstance(index, TupleExpr):
  4817. items = index.items
  4818. is_tuple = isinstance(expr.base, RefExpr) and expr.base.fullname == "builtins.tuple"
  4819. if is_tuple and len(items) == 2 and isinstance(items[-1], EllipsisExpr):
  4820. items = items[:-1]
  4821. else:
  4822. items = [index]
  4823. # whether param spec literals be allowed here
  4824. # TODO: should this be computed once and passed in?
  4825. # or is there a better way to do this?
  4826. base = expr.base
  4827. if isinstance(base, RefExpr) and isinstance(base.node, TypeAlias):
  4828. alias = base.node
  4829. target = get_proper_type(alias.target)
  4830. if isinstance(target, Instance):
  4831. has_param_spec = target.type.has_param_spec_type
  4832. num_args = len(target.type.type_vars)
  4833. else:
  4834. has_param_spec = False
  4835. num_args = -1
  4836. elif isinstance(base, NameExpr) and isinstance(base.node, TypeInfo):
  4837. has_param_spec = base.node.has_param_spec_type
  4838. num_args = len(base.node.type_vars)
  4839. else:
  4840. has_param_spec = False
  4841. num_args = -1
  4842. for item in items:
  4843. try:
  4844. typearg = self.expr_to_unanalyzed_type(item)
  4845. except TypeTranslationError:
  4846. self.fail("Type expected within [...]", expr)
  4847. return None
  4848. analyzed = self.anal_type(
  4849. typearg,
  4850. # The type application may appear in base class expression,
  4851. # where type variables are not bound yet. Or when accepting
  4852. # r.h.s. of type alias before we figured out it is a type alias.
  4853. allow_unbound_tvars=self.allow_unbound_tvars,
  4854. allow_placeholder=True,
  4855. allow_param_spec_literals=has_param_spec,
  4856. )
  4857. if analyzed is None:
  4858. return None
  4859. types.append(analyzed)
  4860. if has_param_spec and num_args == 1 and types:
  4861. first_arg = get_proper_type(types[0])
  4862. if not (
  4863. len(types) == 1 and isinstance(first_arg, (Parameters, ParamSpecType, AnyType))
  4864. ):
  4865. types = [Parameters(types, [ARG_POS] * len(types), [None] * len(types))]
  4866. return types
  4867. def visit_slice_expr(self, expr: SliceExpr) -> None:
  4868. if expr.begin_index:
  4869. expr.begin_index.accept(self)
  4870. if expr.end_index:
  4871. expr.end_index.accept(self)
  4872. if expr.stride:
  4873. expr.stride.accept(self)
  4874. def visit_cast_expr(self, expr: CastExpr) -> None:
  4875. expr.expr.accept(self)
  4876. analyzed = self.anal_type(expr.type)
  4877. if analyzed is not None:
  4878. expr.type = analyzed
  4879. def visit_assert_type_expr(self, expr: AssertTypeExpr) -> None:
  4880. expr.expr.accept(self)
  4881. analyzed = self.anal_type(expr.type)
  4882. if analyzed is not None:
  4883. expr.type = analyzed
  4884. def visit_reveal_expr(self, expr: RevealExpr) -> None:
  4885. if expr.kind == REVEAL_TYPE:
  4886. if expr.expr is not None:
  4887. expr.expr.accept(self)
  4888. else:
  4889. # Reveal locals doesn't have an inner expression, there's no
  4890. # need to traverse inside it
  4891. pass
  4892. def visit_type_application(self, expr: TypeApplication) -> None:
  4893. expr.expr.accept(self)
  4894. for i in range(len(expr.types)):
  4895. analyzed = self.anal_type(expr.types[i])
  4896. if analyzed is not None:
  4897. expr.types[i] = analyzed
  4898. def visit_list_comprehension(self, expr: ListComprehension) -> None:
  4899. if any(expr.generator.is_async):
  4900. if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
  4901. self.fail(message_registry.ASYNC_FOR_OUTSIDE_COROUTINE, expr, code=codes.SYNTAX)
  4902. expr.generator.accept(self)
  4903. def visit_set_comprehension(self, expr: SetComprehension) -> None:
  4904. if any(expr.generator.is_async):
  4905. if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
  4906. self.fail(message_registry.ASYNC_FOR_OUTSIDE_COROUTINE, expr, code=codes.SYNTAX)
  4907. expr.generator.accept(self)
  4908. def visit_dictionary_comprehension(self, expr: DictionaryComprehension) -> None:
  4909. if any(expr.is_async):
  4910. if not self.is_func_scope() or not self.function_stack[-1].is_coroutine:
  4911. self.fail(message_registry.ASYNC_FOR_OUTSIDE_COROUTINE, expr, code=codes.SYNTAX)
  4912. with self.enter(expr):
  4913. self.analyze_comp_for(expr)
  4914. expr.key.accept(self)
  4915. expr.value.accept(self)
  4916. self.analyze_comp_for_2(expr)
  4917. def visit_generator_expr(self, expr: GeneratorExpr) -> None:
  4918. with self.enter(expr):
  4919. self.analyze_comp_for(expr)
  4920. expr.left_expr.accept(self)
  4921. self.analyze_comp_for_2(expr)
  4922. def analyze_comp_for(self, expr: GeneratorExpr | DictionaryComprehension) -> None:
  4923. """Analyses the 'comp_for' part of comprehensions (part 1).
  4924. That is the part after 'for' in (x for x in l if p). This analyzes
  4925. variables and conditions which are analyzed in a local scope.
  4926. """
  4927. for i, (index, sequence, conditions) in enumerate(
  4928. zip(expr.indices, expr.sequences, expr.condlists)
  4929. ):
  4930. if i > 0:
  4931. sequence.accept(self)
  4932. # Bind index variables.
  4933. self.analyze_lvalue(index)
  4934. for cond in conditions:
  4935. cond.accept(self)
  4936. def analyze_comp_for_2(self, expr: GeneratorExpr | DictionaryComprehension) -> None:
  4937. """Analyses the 'comp_for' part of comprehensions (part 2).
  4938. That is the part after 'for' in (x for x in l if p). This analyzes
  4939. the 'l' part which is analyzed in the surrounding scope.
  4940. """
  4941. expr.sequences[0].accept(self)
  4942. def visit_lambda_expr(self, expr: LambdaExpr) -> None:
  4943. self.analyze_arg_initializers(expr)
  4944. self.analyze_function_body(expr)
  4945. def visit_conditional_expr(self, expr: ConditionalExpr) -> None:
  4946. expr.if_expr.accept(self)
  4947. expr.cond.accept(self)
  4948. expr.else_expr.accept(self)
  4949. def visit__promote_expr(self, expr: PromoteExpr) -> None:
  4950. analyzed = self.anal_type(expr.type)
  4951. if analyzed is not None:
  4952. assert isinstance(analyzed, ProperType), "Cannot use type aliases for promotions"
  4953. expr.type = analyzed
  4954. def visit_yield_expr(self, e: YieldExpr) -> None:
  4955. if not self.is_func_scope():
  4956. self.fail('"yield" outside function', e, serious=True, blocker=True)
  4957. elif self.is_comprehension_stack[-1]:
  4958. self.fail(
  4959. '"yield" inside comprehension or generator expression',
  4960. e,
  4961. serious=True,
  4962. blocker=True,
  4963. )
  4964. elif self.function_stack[-1].is_coroutine:
  4965. if self.options.python_version < (3, 6):
  4966. self.fail('"yield" in async function', e, serious=True, blocker=True)
  4967. else:
  4968. self.function_stack[-1].is_generator = True
  4969. self.function_stack[-1].is_async_generator = True
  4970. else:
  4971. self.function_stack[-1].is_generator = True
  4972. if e.expr:
  4973. e.expr.accept(self)
  4974. def visit_await_expr(self, expr: AwaitExpr) -> None:
  4975. if not self.is_func_scope() or not self.function_stack:
  4976. # We check both because is_function_scope() returns True inside comprehensions.
  4977. # This is not a blocker, because some enviroments (like ipython)
  4978. # support top level awaits.
  4979. self.fail('"await" outside function', expr, serious=True, code=codes.TOP_LEVEL_AWAIT)
  4980. elif not self.function_stack[-1].is_coroutine:
  4981. self.fail('"await" outside coroutine ("async def")', expr, serious=True, blocker=True)
  4982. expr.expr.accept(self)
  4983. #
  4984. # Patterns
  4985. #
  4986. def visit_as_pattern(self, p: AsPattern) -> None:
  4987. if p.pattern is not None:
  4988. p.pattern.accept(self)
  4989. if p.name is not None:
  4990. self.analyze_lvalue(p.name)
  4991. def visit_or_pattern(self, p: OrPattern) -> None:
  4992. for pattern in p.patterns:
  4993. pattern.accept(self)
  4994. def visit_value_pattern(self, p: ValuePattern) -> None:
  4995. p.expr.accept(self)
  4996. def visit_sequence_pattern(self, p: SequencePattern) -> None:
  4997. for pattern in p.patterns:
  4998. pattern.accept(self)
  4999. def visit_starred_pattern(self, p: StarredPattern) -> None:
  5000. if p.capture is not None:
  5001. self.analyze_lvalue(p.capture)
  5002. def visit_mapping_pattern(self, p: MappingPattern) -> None:
  5003. for key in p.keys:
  5004. key.accept(self)
  5005. for value in p.values:
  5006. value.accept(self)
  5007. if p.rest is not None:
  5008. self.analyze_lvalue(p.rest)
  5009. def visit_class_pattern(self, p: ClassPattern) -> None:
  5010. p.class_ref.accept(self)
  5011. for pos in p.positionals:
  5012. pos.accept(self)
  5013. for v in p.keyword_values:
  5014. v.accept(self)
  5015. #
  5016. # Lookup functions
  5017. #
  5018. def lookup(
  5019. self, name: str, ctx: Context, suppress_errors: bool = False
  5020. ) -> SymbolTableNode | None:
  5021. """Look up an unqualified (no dots) name in all active namespaces.
  5022. Note that the result may contain a PlaceholderNode. The caller may
  5023. want to defer in that case.
  5024. Generate an error if the name is not defined unless suppress_errors
  5025. is true or the current namespace is incomplete. In the latter case
  5026. defer.
  5027. """
  5028. implicit_name = False
  5029. # 1a. Name declared using 'global x' takes precedence
  5030. if name in self.global_decls[-1]:
  5031. if name in self.globals:
  5032. return self.globals[name]
  5033. if not suppress_errors:
  5034. self.name_not_defined(name, ctx)
  5035. return None
  5036. # 1b. Name declared using 'nonlocal x' takes precedence
  5037. if name in self.nonlocal_decls[-1]:
  5038. for table in reversed(self.locals[:-1]):
  5039. if table is not None and name in table:
  5040. return table[name]
  5041. if not suppress_errors:
  5042. self.name_not_defined(name, ctx)
  5043. return None
  5044. # 2. Class attributes (if within class definition)
  5045. if self.type and not self.is_func_scope() and name in self.type.names:
  5046. node = self.type.names[name]
  5047. if not node.implicit:
  5048. if self.is_active_symbol_in_class_body(node.node):
  5049. return node
  5050. else:
  5051. # Defined through self.x assignment
  5052. implicit_name = True
  5053. implicit_node = node
  5054. # 3. Local (function) scopes
  5055. for table in reversed(self.locals):
  5056. if table is not None and name in table:
  5057. return table[name]
  5058. # 4. Current file global scope
  5059. if name in self.globals:
  5060. return self.globals[name]
  5061. # 5. Builtins
  5062. b = self.globals.get("__builtins__", None)
  5063. if b:
  5064. assert isinstance(b.node, MypyFile)
  5065. table = b.node.names
  5066. if name in table:
  5067. if len(name) > 1 and name[0] == "_" and name[1] != "_":
  5068. if not suppress_errors:
  5069. self.name_not_defined(name, ctx)
  5070. return None
  5071. node = table[name]
  5072. return node
  5073. # Give up.
  5074. if not implicit_name and not suppress_errors:
  5075. self.name_not_defined(name, ctx)
  5076. else:
  5077. if implicit_name:
  5078. return implicit_node
  5079. return None
  5080. def is_active_symbol_in_class_body(self, node: SymbolNode | None) -> bool:
  5081. """Can a symbol defined in class body accessed at current statement?
  5082. Only allow access to class attributes textually after
  5083. the definition, so that it's possible to fall back to the
  5084. outer scope. Example:
  5085. class X: ...
  5086. class C:
  5087. X = X # Initializer refers to outer scope
  5088. Nested classes are an exception, since we want to support
  5089. arbitrary forward references in type annotations. Also, we
  5090. allow forward references to type aliases to support recursive
  5091. types.
  5092. """
  5093. # TODO: Forward reference to name imported in class body is not
  5094. # caught.
  5095. if self.statement is None:
  5096. # Assume it's fine -- don't have enough context to check
  5097. return True
  5098. return (
  5099. node is None
  5100. or self.is_textually_before_statement(node)
  5101. or not self.is_defined_in_current_module(node.fullname)
  5102. or isinstance(node, (TypeInfo, TypeAlias))
  5103. or (isinstance(node, PlaceholderNode) and node.becomes_typeinfo)
  5104. )
  5105. def is_textually_before_statement(self, node: SymbolNode) -> bool:
  5106. """Check if a node is defined textually before the current statement
  5107. Note that decorated functions' line number are the same as
  5108. the top decorator.
  5109. """
  5110. assert self.statement
  5111. line_diff = self.statement.line - node.line
  5112. # The first branch handles reference an overloaded function variant inside itself,
  5113. # this is a corner case where mypy technically deviates from runtime name resolution,
  5114. # but it is fine because we want an overloaded function to be treated as a single unit.
  5115. if self.is_overloaded_item(node, self.statement):
  5116. return False
  5117. elif isinstance(node, Decorator) and not node.is_overload:
  5118. return line_diff > len(node.original_decorators)
  5119. else:
  5120. return line_diff > 0
  5121. def is_overloaded_item(self, node: SymbolNode, statement: Statement) -> bool:
  5122. """Check whether the function belongs to the overloaded variants"""
  5123. if isinstance(node, OverloadedFuncDef) and isinstance(statement, FuncDef):
  5124. in_items = statement in {
  5125. item.func if isinstance(item, Decorator) else item for item in node.items
  5126. }
  5127. in_impl = node.impl is not None and (
  5128. (isinstance(node.impl, Decorator) and statement is node.impl.func)
  5129. or statement is node.impl
  5130. )
  5131. return in_items or in_impl
  5132. return False
  5133. def is_defined_in_current_module(self, fullname: str | None) -> bool:
  5134. if not fullname:
  5135. return False
  5136. return module_prefix(self.modules, fullname) == self.cur_mod_id
  5137. def lookup_qualified(
  5138. self, name: str, ctx: Context, suppress_errors: bool = False
  5139. ) -> SymbolTableNode | None:
  5140. """Lookup a qualified name in all activate namespaces.
  5141. Note that the result may contain a PlaceholderNode. The caller may
  5142. want to defer in that case.
  5143. Generate an error if the name is not defined unless suppress_errors
  5144. is true or the current namespace is incomplete. In the latter case
  5145. defer.
  5146. """
  5147. if "." not in name:
  5148. # Simple case: look up a short name.
  5149. return self.lookup(name, ctx, suppress_errors=suppress_errors)
  5150. parts = name.split(".")
  5151. namespace = self.cur_mod_id
  5152. sym = self.lookup(parts[0], ctx, suppress_errors=suppress_errors)
  5153. if sym:
  5154. for i in range(1, len(parts)):
  5155. node = sym.node
  5156. part = parts[i]
  5157. if isinstance(node, TypeInfo):
  5158. nextsym = node.get(part)
  5159. elif isinstance(node, MypyFile):
  5160. nextsym = self.get_module_symbol(node, part)
  5161. namespace = node.fullname
  5162. elif isinstance(node, PlaceholderNode):
  5163. return sym
  5164. elif isinstance(node, TypeAlias) and node.no_args:
  5165. assert isinstance(node.target, ProperType)
  5166. if isinstance(node.target, Instance):
  5167. nextsym = node.target.type.get(part)
  5168. else:
  5169. nextsym = None
  5170. else:
  5171. if isinstance(node, Var):
  5172. typ = get_proper_type(node.type)
  5173. if isinstance(typ, AnyType):
  5174. # Allow access through Var with Any type without error.
  5175. return self.implicit_symbol(sym, name, parts[i:], typ)
  5176. # This might be something like valid `P.args` or invalid `P.__bound__` access.
  5177. # Important note that `ParamSpecExpr` is also ignored in other places.
  5178. # See https://github.com/python/mypy/pull/13468
  5179. if isinstance(node, ParamSpecExpr) and part in ("args", "kwargs"):
  5180. return None
  5181. # Lookup through invalid node, such as variable or function
  5182. nextsym = None
  5183. if not nextsym or nextsym.module_hidden:
  5184. if not suppress_errors:
  5185. self.name_not_defined(name, ctx, namespace=namespace)
  5186. return None
  5187. sym = nextsym
  5188. return sym
  5189. def lookup_type_node(self, expr: Expression) -> SymbolTableNode | None:
  5190. try:
  5191. t = self.expr_to_unanalyzed_type(expr)
  5192. except TypeTranslationError:
  5193. return None
  5194. if isinstance(t, UnboundType):
  5195. n = self.lookup_qualified(t.name, expr, suppress_errors=True)
  5196. return n
  5197. return None
  5198. def get_module_symbol(self, node: MypyFile, name: str) -> SymbolTableNode | None:
  5199. """Look up a symbol from a module.
  5200. Return None if no matching symbol could be bound.
  5201. """
  5202. module = node.fullname
  5203. names = node.names
  5204. sym = names.get(name)
  5205. if not sym:
  5206. fullname = module + "." + name
  5207. if fullname in self.modules:
  5208. sym = SymbolTableNode(GDEF, self.modules[fullname])
  5209. elif self.is_incomplete_namespace(module):
  5210. self.record_incomplete_ref()
  5211. elif "__getattr__" in names and (
  5212. node.is_stub or self.options.python_version >= (3, 7)
  5213. ):
  5214. gvar = self.create_getattr_var(names["__getattr__"], name, fullname)
  5215. if gvar:
  5216. sym = SymbolTableNode(GDEF, gvar)
  5217. elif self.is_missing_module(fullname):
  5218. # We use the fullname of the original definition so that we can
  5219. # detect whether two names refer to the same thing.
  5220. var_type = AnyType(TypeOfAny.from_unimported_type)
  5221. v = Var(name, type=var_type)
  5222. v._fullname = fullname
  5223. sym = SymbolTableNode(GDEF, v)
  5224. elif sym.module_hidden:
  5225. sym = None
  5226. return sym
  5227. def is_missing_module(self, module: str) -> bool:
  5228. return module in self.missing_modules
  5229. def implicit_symbol(
  5230. self, sym: SymbolTableNode, name: str, parts: list[str], source_type: AnyType
  5231. ) -> SymbolTableNode:
  5232. """Create symbol for a qualified name reference through Any type."""
  5233. if sym.node is None:
  5234. basename = None
  5235. else:
  5236. basename = sym.node.fullname
  5237. if basename is None:
  5238. fullname = name
  5239. else:
  5240. fullname = basename + "." + ".".join(parts)
  5241. var_type = AnyType(TypeOfAny.from_another_any, source_type)
  5242. var = Var(parts[-1], var_type)
  5243. var._fullname = fullname
  5244. return SymbolTableNode(GDEF, var)
  5245. def create_getattr_var(
  5246. self, getattr_defn: SymbolTableNode, name: str, fullname: str
  5247. ) -> Var | None:
  5248. """Create a dummy variable using module-level __getattr__ return type.
  5249. If not possible, return None.
  5250. Note that multiple Var nodes can be created for a single name. We
  5251. can use the from_module_getattr and the fullname attributes to
  5252. check if two dummy Var nodes refer to the same thing. Reusing Var
  5253. nodes would require non-local mutable state, which we prefer to
  5254. avoid.
  5255. """
  5256. if isinstance(getattr_defn.node, (FuncDef, Var)):
  5257. node_type = get_proper_type(getattr_defn.node.type)
  5258. if isinstance(node_type, CallableType):
  5259. typ = node_type.ret_type
  5260. else:
  5261. typ = AnyType(TypeOfAny.from_error)
  5262. v = Var(name, type=typ)
  5263. v._fullname = fullname
  5264. v.from_module_getattr = True
  5265. return v
  5266. return None
  5267. def lookup_fully_qualified(self, fullname: str) -> SymbolTableNode:
  5268. ret = self.lookup_fully_qualified_or_none(fullname)
  5269. assert ret is not None, fullname
  5270. return ret
  5271. def lookup_fully_qualified_or_none(self, fullname: str) -> SymbolTableNode | None:
  5272. """Lookup a fully qualified name that refers to a module-level definition.
  5273. Don't assume that the name is defined. This happens in the global namespace --
  5274. the local module namespace is ignored. This does not dereference indirect
  5275. refs.
  5276. Note that this can't be used for names nested in class namespaces.
  5277. """
  5278. # TODO: unify/clean-up/simplify lookup methods, see #4157.
  5279. # TODO: support nested classes (but consider performance impact,
  5280. # we might keep the module level only lookup for thing like 'builtins.int').
  5281. assert "." in fullname
  5282. module, name = fullname.rsplit(".", maxsplit=1)
  5283. if module not in self.modules:
  5284. return None
  5285. filenode = self.modules[module]
  5286. result = filenode.names.get(name)
  5287. if result is None and self.is_incomplete_namespace(module):
  5288. # TODO: More explicit handling of incomplete refs?
  5289. self.record_incomplete_ref()
  5290. return result
  5291. def object_type(self) -> Instance:
  5292. return self.named_type("builtins.object")
  5293. def str_type(self) -> Instance:
  5294. return self.named_type("builtins.str")
  5295. def named_type(self, fullname: str, args: list[Type] | None = None) -> Instance:
  5296. sym = self.lookup_fully_qualified(fullname)
  5297. assert sym, "Internal error: attempted to construct unknown type"
  5298. node = sym.node
  5299. assert isinstance(node, TypeInfo)
  5300. if args:
  5301. # TODO: assert len(args) == len(node.defn.type_vars)
  5302. return Instance(node, args)
  5303. return Instance(node, [AnyType(TypeOfAny.special_form)] * len(node.defn.type_vars))
  5304. def named_type_or_none(self, fullname: str, args: list[Type] | None = None) -> Instance | None:
  5305. sym = self.lookup_fully_qualified_or_none(fullname)
  5306. if not sym or isinstance(sym.node, PlaceholderNode):
  5307. return None
  5308. node = sym.node
  5309. if isinstance(node, TypeAlias):
  5310. assert isinstance(node.target, Instance) # type: ignore[misc]
  5311. node = node.target.type
  5312. assert isinstance(node, TypeInfo), node
  5313. if args is not None:
  5314. # TODO: assert len(args) == len(node.defn.type_vars)
  5315. return Instance(node, args)
  5316. return Instance(node, [AnyType(TypeOfAny.unannotated)] * len(node.defn.type_vars))
  5317. def builtin_type(self, fully_qualified_name: str) -> Instance:
  5318. """Legacy function -- use named_type() instead."""
  5319. return self.named_type(fully_qualified_name)
  5320. def lookup_current_scope(self, name: str) -> SymbolTableNode | None:
  5321. if self.locals[-1] is not None:
  5322. return self.locals[-1].get(name)
  5323. elif self.type is not None:
  5324. return self.type.names.get(name)
  5325. else:
  5326. return self.globals.get(name)
  5327. #
  5328. # Adding symbols
  5329. #
  5330. def add_symbol(
  5331. self,
  5332. name: str,
  5333. node: SymbolNode,
  5334. context: Context,
  5335. module_public: bool = True,
  5336. module_hidden: bool = False,
  5337. can_defer: bool = True,
  5338. escape_comprehensions: bool = False,
  5339. ) -> bool:
  5340. """Add symbol to the currently active symbol table.
  5341. Generally additions to symbol table should go through this method or
  5342. one of the methods below so that kinds, redefinitions, conditional
  5343. definitions, and skipped names are handled consistently.
  5344. Return True if we actually added the symbol, or False if we refused to do so
  5345. (because something is not ready).
  5346. If can_defer is True, defer current target if adding a placeholder.
  5347. """
  5348. if self.is_func_scope():
  5349. kind = LDEF
  5350. elif self.type is not None:
  5351. kind = MDEF
  5352. else:
  5353. kind = GDEF
  5354. symbol = SymbolTableNode(
  5355. kind, node, module_public=module_public, module_hidden=module_hidden
  5356. )
  5357. return self.add_symbol_table_node(name, symbol, context, can_defer, escape_comprehensions)
  5358. def add_symbol_skip_local(self, name: str, node: SymbolNode) -> None:
  5359. """Same as above, but skipping the local namespace.
  5360. This doesn't check for previous definition and is only used
  5361. for serialization of method-level classes.
  5362. Classes defined within methods can be exposed through an
  5363. attribute type, but method-level symbol tables aren't serialized.
  5364. This method can be used to add such classes to an enclosing,
  5365. serialized symbol table.
  5366. """
  5367. # TODO: currently this is only used by named tuples and typed dicts.
  5368. # Use this method also by normal classes, see issue #6422.
  5369. if self.type is not None:
  5370. names = self.type.names
  5371. kind = MDEF
  5372. else:
  5373. names = self.globals
  5374. kind = GDEF
  5375. symbol = SymbolTableNode(kind, node)
  5376. names[name] = symbol
  5377. def add_symbol_table_node(
  5378. self,
  5379. name: str,
  5380. symbol: SymbolTableNode,
  5381. context: Context | None = None,
  5382. can_defer: bool = True,
  5383. escape_comprehensions: bool = False,
  5384. ) -> bool:
  5385. """Add symbol table node to the currently active symbol table.
  5386. Return True if we actually added the symbol, or False if we refused
  5387. to do so (because something is not ready or it was a no-op).
  5388. Generate an error if there is an invalid redefinition.
  5389. If context is None, unconditionally add node, since we can't report
  5390. an error. Note that this is used by plugins to forcibly replace nodes!
  5391. TODO: Prevent plugins from replacing nodes, as it could cause problems?
  5392. Args:
  5393. name: short name of symbol
  5394. symbol: Node to add
  5395. can_defer: if True, defer current target if adding a placeholder
  5396. context: error context (see above about None value)
  5397. """
  5398. names = self.current_symbol_table(escape_comprehensions=escape_comprehensions)
  5399. existing = names.get(name)
  5400. if isinstance(symbol.node, PlaceholderNode) and can_defer:
  5401. if context is not None:
  5402. self.process_placeholder(name, "name", context)
  5403. else:
  5404. # see note in docstring describing None contexts
  5405. self.defer()
  5406. if (
  5407. existing is not None
  5408. and context is not None
  5409. and not is_valid_replacement(existing, symbol)
  5410. ):
  5411. # There is an existing node, so this may be a redefinition.
  5412. # If the new node points to the same node as the old one,
  5413. # or if both old and new nodes are placeholders, we don't
  5414. # need to do anything.
  5415. old = existing.node
  5416. new = symbol.node
  5417. if isinstance(new, PlaceholderNode):
  5418. # We don't know whether this is okay. Let's wait until the next iteration.
  5419. return False
  5420. if not is_same_symbol(old, new):
  5421. if isinstance(new, (FuncDef, Decorator, OverloadedFuncDef, TypeInfo)):
  5422. self.add_redefinition(names, name, symbol)
  5423. if not (isinstance(new, (FuncDef, Decorator)) and self.set_original_def(old, new)):
  5424. self.name_already_defined(name, context, existing)
  5425. elif name not in self.missing_names[-1] and "*" not in self.missing_names[-1]:
  5426. names[name] = symbol
  5427. self.progress = True
  5428. return True
  5429. return False
  5430. def add_redefinition(self, names: SymbolTable, name: str, symbol: SymbolTableNode) -> None:
  5431. """Add a symbol table node that reflects a redefinition as a function or a class.
  5432. Redefinitions need to be added to the symbol table so that they can be found
  5433. through AST traversal, but they have dummy names of form 'name-redefinition[N]',
  5434. where N ranges over 2, 3, ... (omitted for the first redefinition).
  5435. Note: we always store redefinitions independently of whether they are valid or not
  5436. (so they will be semantically analyzed), the caller should give an error for invalid
  5437. redefinitions (such as e.g. variable redefined as a class).
  5438. """
  5439. i = 1
  5440. # Don't serialize redefined nodes. They are likely to have
  5441. # busted internal references which can cause problems with
  5442. # serialization and they can't have any external references to
  5443. # them.
  5444. symbol.no_serialize = True
  5445. while True:
  5446. if i == 1:
  5447. new_name = f"{name}-redefinition"
  5448. else:
  5449. new_name = f"{name}-redefinition{i}"
  5450. existing = names.get(new_name)
  5451. if existing is None:
  5452. names[new_name] = symbol
  5453. return
  5454. elif existing.node is symbol.node:
  5455. # Already there
  5456. return
  5457. i += 1
  5458. def add_local(self, node: Var | FuncDef | OverloadedFuncDef, context: Context) -> None:
  5459. """Add local variable or function."""
  5460. assert self.is_func_scope()
  5461. name = node.name
  5462. node._fullname = name
  5463. self.add_symbol(name, node, context)
  5464. def _get_node_for_class_scoped_import(
  5465. self, name: str, symbol_node: SymbolNode | None, context: Context
  5466. ) -> SymbolNode | None:
  5467. if symbol_node is None:
  5468. return None
  5469. # I promise this type checks; I'm just making mypyc issues go away.
  5470. # mypyc is absolutely convinced that `symbol_node` narrows to a Var in the following,
  5471. # when it can also be a FuncBase. Once fixed, `f` in the following can be removed.
  5472. # See also https://github.com/mypyc/mypyc/issues/892
  5473. f: Callable[[object], Any] = lambda x: x
  5474. if isinstance(f(symbol_node), (Decorator, FuncBase, Var)):
  5475. # For imports in class scope, we construct a new node to represent the symbol and
  5476. # set its `info` attribute to `self.type`.
  5477. existing = self.current_symbol_table().get(name)
  5478. if (
  5479. # The redefinition checks in `add_symbol_table_node` don't work for our
  5480. # constructed Var / FuncBase, so check for possible redefinitions here.
  5481. existing is not None
  5482. and isinstance(f(existing.node), (Decorator, FuncBase, Var))
  5483. and (
  5484. isinstance(f(existing.type), f(AnyType))
  5485. or f(existing.type) == f(symbol_node).type
  5486. )
  5487. ):
  5488. return existing.node
  5489. # Construct the new node
  5490. if isinstance(f(symbol_node), (FuncBase, Decorator)):
  5491. # In theory we could construct a new node here as well, but in practice
  5492. # it doesn't work well, see #12197
  5493. typ: Type | None = AnyType(TypeOfAny.from_error)
  5494. self.fail("Unsupported class scoped import", context)
  5495. else:
  5496. typ = f(symbol_node).type
  5497. symbol_node = Var(name, typ)
  5498. symbol_node._fullname = self.qualified_name(name)
  5499. assert self.type is not None # guaranteed by is_class_scope
  5500. symbol_node.info = self.type
  5501. symbol_node.line = context.line
  5502. symbol_node.column = context.column
  5503. return symbol_node
  5504. def add_imported_symbol(
  5505. self,
  5506. name: str,
  5507. node: SymbolTableNode,
  5508. context: ImportBase,
  5509. module_public: bool,
  5510. module_hidden: bool,
  5511. ) -> None:
  5512. """Add an alias to an existing symbol through import."""
  5513. assert not module_hidden or not module_public
  5514. existing_symbol = self.lookup_current_scope(name)
  5515. if (
  5516. existing_symbol
  5517. and not isinstance(existing_symbol.node, PlaceholderNode)
  5518. and not isinstance(node.node, PlaceholderNode)
  5519. ):
  5520. # Import can redefine a variable. They get special treatment.
  5521. if self.process_import_over_existing_name(name, existing_symbol, node, context):
  5522. return
  5523. symbol_node: SymbolNode | None = node.node
  5524. if self.is_class_scope():
  5525. symbol_node = self._get_node_for_class_scoped_import(name, symbol_node, context)
  5526. symbol = SymbolTableNode(
  5527. node.kind, symbol_node, module_public=module_public, module_hidden=module_hidden
  5528. )
  5529. self.add_symbol_table_node(name, symbol, context)
  5530. def add_unknown_imported_symbol(
  5531. self,
  5532. name: str,
  5533. context: Context,
  5534. target_name: str | None,
  5535. module_public: bool,
  5536. module_hidden: bool,
  5537. ) -> None:
  5538. """Add symbol that we don't know what it points to because resolving an import failed.
  5539. This can happen if a module is missing, or it is present, but doesn't have
  5540. the imported attribute. The `target_name` is the name of symbol in the namespace
  5541. it is imported from. For example, for 'from mod import x as y' the target_name is
  5542. 'mod.x'. This is currently used only to track logical dependencies.
  5543. """
  5544. existing = self.current_symbol_table().get(name)
  5545. if existing and isinstance(existing.node, Var) and existing.node.is_suppressed_import:
  5546. # This missing import was already added -- nothing to do here.
  5547. return
  5548. var = Var(name)
  5549. if self.options.logical_deps and target_name is not None:
  5550. # This makes it possible to add logical fine-grained dependencies
  5551. # from a missing module. We can't use this by default, since in a
  5552. # few places we assume that the full name points to a real
  5553. # definition, but this name may point to nothing.
  5554. var._fullname = target_name
  5555. elif self.type:
  5556. var._fullname = self.type.fullname + "." + name
  5557. var.info = self.type
  5558. else:
  5559. var._fullname = self.qualified_name(name)
  5560. var.is_ready = True
  5561. any_type = AnyType(TypeOfAny.from_unimported_type, missing_import_name=var._fullname)
  5562. var.type = any_type
  5563. var.is_suppressed_import = True
  5564. self.add_symbol(
  5565. name, var, context, module_public=module_public, module_hidden=module_hidden
  5566. )
  5567. #
  5568. # Other helpers
  5569. #
  5570. @contextmanager
  5571. def tvar_scope_frame(self, frame: TypeVarLikeScope) -> Iterator[None]:
  5572. old_scope = self.tvar_scope
  5573. self.tvar_scope = frame
  5574. yield
  5575. self.tvar_scope = old_scope
  5576. def defer(self, debug_context: Context | None = None, force_progress: bool = False) -> None:
  5577. """Defer current analysis target to be analyzed again.
  5578. This must be called if something in the current target is
  5579. incomplete or has a placeholder node. However, this must *not*
  5580. be called during the final analysis iteration! Instead, an error
  5581. should be generated. Often 'process_placeholder' is a good
  5582. way to either defer or generate an error.
  5583. NOTE: Some methods, such as 'anal_type', 'mark_incomplete' and
  5584. 'record_incomplete_ref', call this implicitly, or when needed.
  5585. They are usually preferable to a direct defer() call.
  5586. """
  5587. assert not self.final_iteration, "Must not defer during final iteration"
  5588. if force_progress:
  5589. # Usually, we report progress if we have replaced a placeholder node
  5590. # with an actual valid node. However, sometimes we need to update an
  5591. # existing node *in-place*. For example, this is used by type aliases
  5592. # in context of forward references and/or recursive aliases, and in
  5593. # similar situations (recursive named tuples etc).
  5594. self.progress = True
  5595. self.deferred = True
  5596. # Store debug info for this deferral.
  5597. line = (
  5598. debug_context.line if debug_context else self.statement.line if self.statement else -1
  5599. )
  5600. self.deferral_debug_context.append((self.cur_mod_id, line))
  5601. def track_incomplete_refs(self) -> Tag:
  5602. """Return tag that can be used for tracking references to incomplete names."""
  5603. return self.num_incomplete_refs
  5604. def found_incomplete_ref(self, tag: Tag) -> bool:
  5605. """Have we encountered an incomplete reference since starting tracking?"""
  5606. return self.num_incomplete_refs != tag
  5607. def record_incomplete_ref(self) -> None:
  5608. """Record the encounter of an incomplete reference and defer current analysis target."""
  5609. self.defer()
  5610. self.num_incomplete_refs += 1
  5611. def mark_incomplete(
  5612. self,
  5613. name: str,
  5614. node: Node,
  5615. becomes_typeinfo: bool = False,
  5616. module_public: bool = True,
  5617. module_hidden: bool = False,
  5618. ) -> None:
  5619. """Mark a definition as incomplete (and defer current analysis target).
  5620. Also potentially mark the current namespace as incomplete.
  5621. Args:
  5622. name: The name that we weren't able to define (or '*' if the name is unknown)
  5623. node: The node that refers to the name (definition or lvalue)
  5624. becomes_typeinfo: Pass this to PlaceholderNode (used by special forms like
  5625. named tuples that will create TypeInfos).
  5626. """
  5627. self.defer(node)
  5628. if name == "*":
  5629. self.incomplete = True
  5630. elif not self.is_global_or_nonlocal(name):
  5631. fullname = self.qualified_name(name)
  5632. assert self.statement
  5633. placeholder = PlaceholderNode(
  5634. fullname, node, self.statement.line, becomes_typeinfo=becomes_typeinfo
  5635. )
  5636. self.add_symbol(
  5637. name,
  5638. placeholder,
  5639. module_public=module_public,
  5640. module_hidden=module_hidden,
  5641. context=dummy_context(),
  5642. )
  5643. self.missing_names[-1].add(name)
  5644. def is_incomplete_namespace(self, fullname: str) -> bool:
  5645. """Is a module or class namespace potentially missing some definitions?
  5646. If a name is missing from an incomplete namespace, we'll need to defer the
  5647. current analysis target.
  5648. """
  5649. return fullname in self.incomplete_namespaces
  5650. def process_placeholder(
  5651. self, name: str | None, kind: str, ctx: Context, force_progress: bool = False
  5652. ) -> None:
  5653. """Process a reference targeting placeholder node.
  5654. If this is not a final iteration, defer current node,
  5655. otherwise report an error.
  5656. The 'kind' argument indicates if this a name or attribute expression
  5657. (used for better error message).
  5658. """
  5659. if self.final_iteration:
  5660. self.cannot_resolve_name(name, kind, ctx)
  5661. else:
  5662. self.defer(ctx, force_progress=force_progress)
  5663. def cannot_resolve_name(self, name: str | None, kind: str, ctx: Context) -> None:
  5664. name_format = f' "{name}"' if name else ""
  5665. self.fail(f"Cannot resolve {kind}{name_format} (possible cyclic definition)", ctx)
  5666. if not self.options.disable_recursive_aliases and self.is_func_scope():
  5667. self.note("Recursive types are not allowed at function scope", ctx)
  5668. def qualified_name(self, name: str) -> str:
  5669. if self.type is not None:
  5670. return self.type._fullname + "." + name
  5671. elif self.is_func_scope():
  5672. return name
  5673. else:
  5674. return self.cur_mod_id + "." + name
  5675. @contextmanager
  5676. def enter(
  5677. self, function: FuncItem | GeneratorExpr | DictionaryComprehension
  5678. ) -> Iterator[None]:
  5679. """Enter a function, generator or comprehension scope."""
  5680. names = self.saved_locals.setdefault(function, SymbolTable())
  5681. self.locals.append(names)
  5682. is_comprehension = isinstance(function, (GeneratorExpr, DictionaryComprehension))
  5683. self.is_comprehension_stack.append(is_comprehension)
  5684. self.global_decls.append(set())
  5685. self.nonlocal_decls.append(set())
  5686. # -1 since entering block will increment this to 0.
  5687. self.block_depth.append(-1)
  5688. self.loop_depth.append(0)
  5689. self.missing_names.append(set())
  5690. try:
  5691. yield
  5692. finally:
  5693. self.locals.pop()
  5694. self.is_comprehension_stack.pop()
  5695. self.global_decls.pop()
  5696. self.nonlocal_decls.pop()
  5697. self.block_depth.pop()
  5698. self.loop_depth.pop()
  5699. self.missing_names.pop()
  5700. def is_func_scope(self) -> bool:
  5701. return self.locals[-1] is not None
  5702. def is_nested_within_func_scope(self) -> bool:
  5703. """Are we underneath a function scope, even if we are in a nested class also?"""
  5704. return any(l is not None for l in self.locals)
  5705. def is_class_scope(self) -> bool:
  5706. return self.type is not None and not self.is_func_scope()
  5707. def is_module_scope(self) -> bool:
  5708. return not (self.is_class_scope() or self.is_func_scope())
  5709. def current_symbol_kind(self) -> int:
  5710. if self.is_class_scope():
  5711. kind = MDEF
  5712. elif self.is_func_scope():
  5713. kind = LDEF
  5714. else:
  5715. kind = GDEF
  5716. return kind
  5717. def current_symbol_table(self, escape_comprehensions: bool = False) -> SymbolTable:
  5718. if self.is_func_scope():
  5719. assert self.locals[-1] is not None
  5720. if escape_comprehensions:
  5721. assert len(self.locals) == len(self.is_comprehension_stack)
  5722. # Retrieve the symbol table from the enclosing non-comprehension scope.
  5723. for i, is_comprehension in enumerate(reversed(self.is_comprehension_stack)):
  5724. if not is_comprehension:
  5725. if i == len(self.locals) - 1: # The last iteration.
  5726. # The caller of the comprehension is in the global space.
  5727. names = self.globals
  5728. else:
  5729. names_candidate = self.locals[-1 - i]
  5730. assert (
  5731. names_candidate is not None
  5732. ), "Escaping comprehension from invalid scope"
  5733. names = names_candidate
  5734. break
  5735. else:
  5736. assert False, "Should have at least one non-comprehension scope"
  5737. else:
  5738. names = self.locals[-1]
  5739. assert names is not None
  5740. elif self.type is not None:
  5741. names = self.type.names
  5742. else:
  5743. names = self.globals
  5744. return names
  5745. def is_global_or_nonlocal(self, name: str) -> bool:
  5746. return self.is_func_scope() and (
  5747. name in self.global_decls[-1] or name in self.nonlocal_decls[-1]
  5748. )
  5749. def add_exports(self, exp_or_exps: Iterable[Expression] | Expression) -> None:
  5750. exps = [exp_or_exps] if isinstance(exp_or_exps, Expression) else exp_or_exps
  5751. for exp in exps:
  5752. if isinstance(exp, StrExpr):
  5753. self.all_exports.append(exp.value)
  5754. def name_not_defined(self, name: str, ctx: Context, namespace: str | None = None) -> None:
  5755. incomplete = self.is_incomplete_namespace(namespace or self.cur_mod_id)
  5756. if (
  5757. namespace is None
  5758. and self.type
  5759. and not self.is_func_scope()
  5760. and self.incomplete_type_stack[-1]
  5761. and not self.final_iteration
  5762. ):
  5763. # We are processing a class body for the first time, so it is incomplete.
  5764. incomplete = True
  5765. if incomplete:
  5766. # Target namespace is incomplete, so it's possible that the name will be defined
  5767. # later on. Defer current target.
  5768. self.record_incomplete_ref()
  5769. return
  5770. message = f'Name "{name}" is not defined'
  5771. self.fail(message, ctx, code=codes.NAME_DEFINED)
  5772. if f"builtins.{name}" in SUGGESTED_TEST_FIXTURES:
  5773. # The user probably has a missing definition in a test fixture. Let's verify.
  5774. fullname = f"builtins.{name}"
  5775. if self.lookup_fully_qualified_or_none(fullname) is None:
  5776. # Yes. Generate a helpful note.
  5777. self.msg.add_fixture_note(fullname, ctx)
  5778. modules_with_unimported_hints = {
  5779. name.split(".", 1)[0] for name in TYPES_FOR_UNIMPORTED_HINTS
  5780. }
  5781. lowercased = {name.lower(): name for name in TYPES_FOR_UNIMPORTED_HINTS}
  5782. for module in modules_with_unimported_hints:
  5783. fullname = f"{module}.{name}".lower()
  5784. if fullname not in lowercased:
  5785. continue
  5786. # User probably forgot to import these types.
  5787. hint = (
  5788. 'Did you forget to import it from "{module}"?'
  5789. ' (Suggestion: "from {module} import {name}")'
  5790. ).format(module=module, name=lowercased[fullname].rsplit(".", 1)[-1])
  5791. self.note(hint, ctx, code=codes.NAME_DEFINED)
  5792. def already_defined(
  5793. self, name: str, ctx: Context, original_ctx: SymbolTableNode | SymbolNode | None, noun: str
  5794. ) -> None:
  5795. if isinstance(original_ctx, SymbolTableNode):
  5796. node: SymbolNode | None = original_ctx.node
  5797. elif isinstance(original_ctx, SymbolNode):
  5798. node = original_ctx
  5799. else:
  5800. node = None
  5801. if isinstance(original_ctx, SymbolTableNode) and isinstance(original_ctx.node, MypyFile):
  5802. # Since this is an import, original_ctx.node points to the module definition.
  5803. # Therefore its line number is always 1, which is not useful for this
  5804. # error message.
  5805. extra_msg = " (by an import)"
  5806. elif node and node.line != -1 and self.is_local_name(node.fullname):
  5807. # TODO: Using previous symbol node may give wrong line. We should use
  5808. # the line number where the binding was established instead.
  5809. extra_msg = f" on line {node.line}"
  5810. else:
  5811. extra_msg = " (possibly by an import)"
  5812. self.fail(
  5813. f'{noun} "{unmangle(name)}" already defined{extra_msg}', ctx, code=codes.NO_REDEF
  5814. )
  5815. def name_already_defined(
  5816. self, name: str, ctx: Context, original_ctx: SymbolTableNode | SymbolNode | None = None
  5817. ) -> None:
  5818. self.already_defined(name, ctx, original_ctx, noun="Name")
  5819. def attribute_already_defined(
  5820. self, name: str, ctx: Context, original_ctx: SymbolTableNode | SymbolNode | None = None
  5821. ) -> None:
  5822. self.already_defined(name, ctx, original_ctx, noun="Attribute")
  5823. def is_local_name(self, name: str) -> bool:
  5824. """Does name look like reference to a definition in the current module?"""
  5825. return self.is_defined_in_current_module(name) or "." not in name
  5826. def in_checked_function(self) -> bool:
  5827. """Should we type-check the current function?
  5828. - Yes if --check-untyped-defs is set.
  5829. - Yes outside functions.
  5830. - Yes in annotated functions.
  5831. - No otherwise.
  5832. """
  5833. if self.options.check_untyped_defs or not self.function_stack:
  5834. return True
  5835. current_index = len(self.function_stack) - 1
  5836. while current_index >= 0:
  5837. current_func = self.function_stack[current_index]
  5838. if not isinstance(current_func, LambdaExpr):
  5839. return not current_func.is_dynamic()
  5840. # Special case, `lambda` inherits the "checked" state from its parent.
  5841. # Because `lambda` itself cannot be annotated.
  5842. # `lambdas` can be deeply nested, so we try to find at least one other parent.
  5843. current_index -= 1
  5844. # This means that we only have a stack of `lambda` functions,
  5845. # no regular functions.
  5846. return True
  5847. def fail(
  5848. self,
  5849. msg: str,
  5850. ctx: Context,
  5851. serious: bool = False,
  5852. *,
  5853. code: ErrorCode | None = None,
  5854. blocker: bool = False,
  5855. ) -> None:
  5856. if not serious and not self.in_checked_function():
  5857. return
  5858. # In case it's a bug and we don't really have context
  5859. assert ctx is not None, msg
  5860. self.errors.report(ctx.line, ctx.column, msg, blocker=blocker, code=code)
  5861. def note(self, msg: str, ctx: Context, code: ErrorCode | None = None) -> None:
  5862. if not self.in_checked_function():
  5863. return
  5864. self.errors.report(ctx.line, ctx.column, msg, severity="note", code=code)
  5865. def incomplete_feature_enabled(self, feature: str, ctx: Context) -> bool:
  5866. if feature not in self.options.enable_incomplete_feature:
  5867. self.fail(
  5868. f'"{feature}" support is experimental,'
  5869. f" use --enable-incomplete-feature={feature} to enable",
  5870. ctx,
  5871. )
  5872. return False
  5873. return True
  5874. def accept(self, node: Node) -> None:
  5875. try:
  5876. node.accept(self)
  5877. except Exception as err:
  5878. report_internal_error(err, self.errors.file, node.line, self.errors, self.options)
  5879. def expr_to_analyzed_type(
  5880. self,
  5881. expr: Expression,
  5882. report_invalid_types: bool = True,
  5883. allow_placeholder: bool = False,
  5884. allow_type_any: bool = False,
  5885. allow_unbound_tvars: bool = False,
  5886. allow_param_spec_literals: bool = False,
  5887. ) -> Type | None:
  5888. if isinstance(expr, CallExpr):
  5889. # This is a legacy syntax intended mostly for Python 2, we keep it for
  5890. # backwards compatibility, but new features like generic named tuples
  5891. # and recursive named tuples will be not supported.
  5892. expr.accept(self)
  5893. internal_name, info, tvar_defs = self.named_tuple_analyzer.check_namedtuple(
  5894. expr, None, self.is_func_scope()
  5895. )
  5896. if tvar_defs:
  5897. self.fail("Generic named tuples are not supported for legacy class syntax", expr)
  5898. self.note("Use either Python 3 class syntax, or the assignment syntax", expr)
  5899. if internal_name is None:
  5900. # Some form of namedtuple is the only valid type that looks like a call
  5901. # expression. This isn't a valid type.
  5902. raise TypeTranslationError()
  5903. elif not info:
  5904. self.defer(expr)
  5905. return None
  5906. assert info.tuple_type, "NamedTuple without tuple type"
  5907. fallback = Instance(info, [])
  5908. return TupleType(info.tuple_type.items, fallback=fallback)
  5909. typ = self.expr_to_unanalyzed_type(expr)
  5910. return self.anal_type(
  5911. typ,
  5912. report_invalid_types=report_invalid_types,
  5913. allow_placeholder=allow_placeholder,
  5914. allow_type_any=allow_type_any,
  5915. allow_unbound_tvars=allow_unbound_tvars,
  5916. allow_param_spec_literals=allow_param_spec_literals,
  5917. )
  5918. def analyze_type_expr(self, expr: Expression) -> None:
  5919. # There are certain expressions that mypy does not need to semantically analyze,
  5920. # since they analyzed solely as type. (For example, indexes in type alias definitions
  5921. # and base classes in class defs). External consumers of the mypy AST may need
  5922. # them semantically analyzed, however, if they need to treat it as an expression
  5923. # and not a type. (Which is to say, mypyc needs to do this.) Do the analysis
  5924. # in a fresh tvar scope in order to suppress any errors about using type variables.
  5925. with self.tvar_scope_frame(TypeVarLikeScope()), self.allow_unbound_tvars_set():
  5926. expr.accept(self)
  5927. def type_analyzer(
  5928. self,
  5929. *,
  5930. tvar_scope: TypeVarLikeScope | None = None,
  5931. allow_tuple_literal: bool = False,
  5932. allow_unbound_tvars: bool = False,
  5933. allow_placeholder: bool = False,
  5934. allow_required: bool = False,
  5935. allow_param_spec_literals: bool = False,
  5936. report_invalid_types: bool = True,
  5937. prohibit_self_type: str | None = None,
  5938. allow_type_any: bool = False,
  5939. ) -> TypeAnalyser:
  5940. if tvar_scope is None:
  5941. tvar_scope = self.tvar_scope
  5942. tpan = TypeAnalyser(
  5943. self,
  5944. tvar_scope,
  5945. self.plugin,
  5946. self.options,
  5947. self.is_typeshed_stub_file,
  5948. allow_unbound_tvars=allow_unbound_tvars,
  5949. allow_tuple_literal=allow_tuple_literal,
  5950. report_invalid_types=report_invalid_types,
  5951. allow_placeholder=allow_placeholder,
  5952. allow_required=allow_required,
  5953. allow_param_spec_literals=allow_param_spec_literals,
  5954. prohibit_self_type=prohibit_self_type,
  5955. allow_type_any=allow_type_any,
  5956. )
  5957. tpan.in_dynamic_func = bool(self.function_stack and self.function_stack[-1].is_dynamic())
  5958. tpan.global_scope = not self.type and not self.function_stack
  5959. return tpan
  5960. def expr_to_unanalyzed_type(self, node: Expression) -> ProperType:
  5961. return expr_to_unanalyzed_type(node, self.options, self.is_stub_file)
  5962. def anal_type(
  5963. self,
  5964. typ: Type,
  5965. *,
  5966. tvar_scope: TypeVarLikeScope | None = None,
  5967. allow_tuple_literal: bool = False,
  5968. allow_unbound_tvars: bool = False,
  5969. allow_placeholder: bool = False,
  5970. allow_required: bool = False,
  5971. allow_param_spec_literals: bool = False,
  5972. report_invalid_types: bool = True,
  5973. prohibit_self_type: str | None = None,
  5974. allow_type_any: bool = False,
  5975. third_pass: bool = False,
  5976. ) -> Type | None:
  5977. """Semantically analyze a type.
  5978. Args:
  5979. typ: Type to analyze (if already analyzed, this is a no-op)
  5980. allow_placeholder: If True, may return PlaceholderType if
  5981. encountering an incomplete definition
  5982. third_pass: Unused; only for compatibility with old semantic
  5983. analyzer
  5984. Return None only if some part of the type couldn't be bound *and* it
  5985. referred to an incomplete namespace or definition. In this case also
  5986. defer as needed. During a final iteration this won't return None;
  5987. instead report an error if the type can't be analyzed and return
  5988. AnyType.
  5989. In case of other errors, report an error message and return AnyType.
  5990. NOTE: The caller shouldn't defer even if this returns None or a
  5991. placeholder type.
  5992. """
  5993. has_self_type = find_self_type(
  5994. typ, lambda name: self.lookup_qualified(name, typ, suppress_errors=True)
  5995. )
  5996. if has_self_type and self.type and prohibit_self_type is None:
  5997. self.setup_self_type()
  5998. a = self.type_analyzer(
  5999. tvar_scope=tvar_scope,
  6000. allow_unbound_tvars=allow_unbound_tvars,
  6001. allow_tuple_literal=allow_tuple_literal,
  6002. allow_placeholder=allow_placeholder,
  6003. allow_required=allow_required,
  6004. allow_param_spec_literals=allow_param_spec_literals,
  6005. report_invalid_types=report_invalid_types,
  6006. prohibit_self_type=prohibit_self_type,
  6007. allow_type_any=allow_type_any,
  6008. )
  6009. tag = self.track_incomplete_refs()
  6010. typ = typ.accept(a)
  6011. if self.found_incomplete_ref(tag):
  6012. # Something could not be bound yet.
  6013. return None
  6014. self.add_type_alias_deps(a.aliases_used)
  6015. return typ
  6016. def class_type(self, self_type: Type) -> Type:
  6017. return TypeType.make_normalized(self_type)
  6018. def schedule_patch(self, priority: int, patch: Callable[[], None]) -> None:
  6019. self.patches.append((priority, patch))
  6020. def report_hang(self) -> None:
  6021. print("Deferral trace:")
  6022. for mod, line in self.deferral_debug_context:
  6023. print(f" {mod}:{line}")
  6024. self.errors.report(
  6025. -1,
  6026. -1,
  6027. "INTERNAL ERROR: maximum semantic analysis iteration count reached",
  6028. blocker=True,
  6029. )
  6030. def add_plugin_dependency(self, trigger: str, target: str | None = None) -> None:
  6031. """Add dependency from trigger to a target.
  6032. If the target is not given explicitly, use the current target.
  6033. """
  6034. if target is None:
  6035. target = self.scope.current_target()
  6036. self.cur_mod_node.plugin_deps.setdefault(trigger, set()).add(target)
  6037. def add_type_alias_deps(
  6038. self, aliases_used: Collection[str], target: str | None = None
  6039. ) -> None:
  6040. """Add full names of type aliases on which the current node depends.
  6041. This is used by fine-grained incremental mode to re-check the corresponding nodes.
  6042. If `target` is None, then the target node used will be the current scope.
  6043. """
  6044. if not aliases_used:
  6045. # A basic optimization to avoid adding targets with no dependencies to
  6046. # the `alias_deps` dict.
  6047. return
  6048. if target is None:
  6049. target = self.scope.current_target()
  6050. self.cur_mod_node.alias_deps[target].update(aliases_used)
  6051. def is_mangled_global(self, name: str) -> bool:
  6052. # A global is mangled if there exists at least one renamed variant.
  6053. return unmangle(name) + "'" in self.globals
  6054. def is_initial_mangled_global(self, name: str) -> bool:
  6055. # If there are renamed definitions for a global, the first one has exactly one prime.
  6056. return name == unmangle(name) + "'"
  6057. def parse_bool(self, expr: Expression) -> bool | None:
  6058. # This wrapper is preserved for plugins.
  6059. return parse_bool(expr)
  6060. def parse_str_literal(self, expr: Expression) -> str | None:
  6061. """Attempt to find the string literal value of the given expression. Returns `None` if no
  6062. literal value can be found."""
  6063. if isinstance(expr, StrExpr):
  6064. return expr.value
  6065. if isinstance(expr, RefExpr) and isinstance(expr.node, Var) and expr.node.type is not None:
  6066. values = try_getting_str_literals_from_type(expr.node.type)
  6067. if values is not None and len(values) == 1:
  6068. return values[0]
  6069. return None
  6070. def set_future_import_flags(self, module_name: str) -> None:
  6071. if module_name in FUTURE_IMPORTS:
  6072. self.modules[self.cur_mod_id].future_import_flags.add(FUTURE_IMPORTS[module_name])
  6073. def is_future_flag_set(self, flag: str) -> bool:
  6074. return self.modules[self.cur_mod_id].is_future_flag_set(flag)
  6075. def parse_dataclass_transform_spec(self, call: CallExpr) -> DataclassTransformSpec:
  6076. """Build a DataclassTransformSpec from the arguments passed to the given call to
  6077. typing.dataclass_transform."""
  6078. parameters = DataclassTransformSpec()
  6079. for name, value in zip(call.arg_names, call.args):
  6080. # Skip any positional args. Note that any such args are invalid, but we can rely on
  6081. # typeshed to enforce this and don't need an additional error here.
  6082. if name is None:
  6083. continue
  6084. # field_specifiers is currently the only non-boolean argument; check for it first so
  6085. # so the rest of the block can fail through to handling booleans
  6086. if name == "field_specifiers":
  6087. parameters.field_specifiers = self.parse_dataclass_transform_field_specifiers(
  6088. value
  6089. )
  6090. continue
  6091. boolean = require_bool_literal_argument(self, value, name)
  6092. if boolean is None:
  6093. continue
  6094. if name == "eq_default":
  6095. parameters.eq_default = boolean
  6096. elif name == "order_default":
  6097. parameters.order_default = boolean
  6098. elif name == "kw_only_default":
  6099. parameters.kw_only_default = boolean
  6100. elif name == "frozen_default":
  6101. parameters.frozen_default = boolean
  6102. else:
  6103. self.fail(f'Unrecognized dataclass_transform parameter "{name}"', call)
  6104. return parameters
  6105. def parse_dataclass_transform_field_specifiers(self, arg: Expression) -> tuple[str, ...]:
  6106. if not isinstance(arg, TupleExpr):
  6107. self.fail('"field_specifiers" argument must be a tuple literal', arg)
  6108. return tuple()
  6109. names = []
  6110. for specifier in arg.items:
  6111. if not isinstance(specifier, RefExpr):
  6112. self.fail('"field_specifiers" must only contain identifiers', specifier)
  6113. return tuple()
  6114. names.append(specifier.fullname)
  6115. return tuple(names)
  6116. def replace_implicit_first_type(sig: FunctionLike, new: Type) -> FunctionLike:
  6117. if isinstance(sig, CallableType):
  6118. if len(sig.arg_types) == 0:
  6119. return sig
  6120. return sig.copy_modified(arg_types=[new] + sig.arg_types[1:])
  6121. elif isinstance(sig, Overloaded):
  6122. return Overloaded(
  6123. [cast(CallableType, replace_implicit_first_type(i, new)) for i in sig.items]
  6124. )
  6125. else:
  6126. assert False
  6127. def refers_to_fullname(node: Expression, fullnames: str | tuple[str, ...]) -> bool:
  6128. """Is node a name or member expression with the given full name?"""
  6129. if not isinstance(fullnames, tuple):
  6130. fullnames = (fullnames,)
  6131. if not isinstance(node, RefExpr):
  6132. return False
  6133. if node.fullname in fullnames:
  6134. return True
  6135. if isinstance(node.node, TypeAlias):
  6136. return is_named_instance(node.node.target, fullnames)
  6137. return False
  6138. def refers_to_class_or_function(node: Expression) -> bool:
  6139. """Does semantically analyzed node refer to a class?"""
  6140. return isinstance(node, RefExpr) and isinstance(
  6141. node.node, (TypeInfo, FuncDef, OverloadedFuncDef)
  6142. )
  6143. def find_duplicate(list: list[T]) -> T | None:
  6144. """If the list has duplicates, return one of the duplicates.
  6145. Otherwise, return None.
  6146. """
  6147. for i in range(1, len(list)):
  6148. if list[i] in list[:i]:
  6149. return list[i]
  6150. return None
  6151. def remove_imported_names_from_symtable(names: SymbolTable, module: str) -> None:
  6152. """Remove all imported names from the symbol table of a module."""
  6153. removed: list[str] = []
  6154. for name, node in names.items():
  6155. if node.node is None:
  6156. continue
  6157. fullname = node.node.fullname
  6158. prefix = fullname[: fullname.rfind(".")]
  6159. if prefix != module:
  6160. removed.append(name)
  6161. for name in removed:
  6162. del names[name]
  6163. def make_any_non_explicit(t: Type) -> Type:
  6164. """Replace all Any types within in with Any that has attribute 'explicit' set to False"""
  6165. return t.accept(MakeAnyNonExplicit())
  6166. class MakeAnyNonExplicit(TrivialSyntheticTypeTranslator):
  6167. def visit_any(self, t: AnyType) -> Type:
  6168. if t.type_of_any == TypeOfAny.explicit:
  6169. return t.copy_modified(TypeOfAny.special_form)
  6170. return t
  6171. def visit_type_alias_type(self, t: TypeAliasType) -> Type:
  6172. return t.copy_modified(args=[a.accept(self) for a in t.args])
  6173. def apply_semantic_analyzer_patches(patches: list[tuple[int, Callable[[], None]]]) -> None:
  6174. """Call patch callbacks in the right order.
  6175. This should happen after semantic analyzer pass 3.
  6176. """
  6177. patches_by_priority = sorted(patches, key=lambda x: x[0])
  6178. for priority, patch_func in patches_by_priority:
  6179. patch_func()
  6180. def names_modified_by_assignment(s: AssignmentStmt) -> list[NameExpr]:
  6181. """Return all unqualified (short) names assigned to in an assignment statement."""
  6182. result: list[NameExpr] = []
  6183. for lvalue in s.lvalues:
  6184. result += names_modified_in_lvalue(lvalue)
  6185. return result
  6186. def names_modified_in_lvalue(lvalue: Lvalue) -> list[NameExpr]:
  6187. """Return all NameExpr assignment targets in an Lvalue."""
  6188. if isinstance(lvalue, NameExpr):
  6189. return [lvalue]
  6190. elif isinstance(lvalue, StarExpr):
  6191. return names_modified_in_lvalue(lvalue.expr)
  6192. elif isinstance(lvalue, (ListExpr, TupleExpr)):
  6193. result: list[NameExpr] = []
  6194. for item in lvalue.items:
  6195. result += names_modified_in_lvalue(item)
  6196. return result
  6197. return []
  6198. def is_same_var_from_getattr(n1: SymbolNode | None, n2: SymbolNode | None) -> bool:
  6199. """Do n1 and n2 refer to the same Var derived from module-level __getattr__?"""
  6200. return (
  6201. isinstance(n1, Var)
  6202. and n1.from_module_getattr
  6203. and isinstance(n2, Var)
  6204. and n2.from_module_getattr
  6205. and n1.fullname == n2.fullname
  6206. )
  6207. def dummy_context() -> Context:
  6208. return TempNode(AnyType(TypeOfAny.special_form))
  6209. def is_valid_replacement(old: SymbolTableNode, new: SymbolTableNode) -> bool:
  6210. """Can symbol table node replace an existing one?
  6211. These are the only valid cases:
  6212. 1. Placeholder gets replaced with a non-placeholder
  6213. 2. Placeholder that isn't known to become type replaced with a
  6214. placeholder that can become a type
  6215. """
  6216. if isinstance(old.node, PlaceholderNode):
  6217. if isinstance(new.node, PlaceholderNode):
  6218. return not old.node.becomes_typeinfo and new.node.becomes_typeinfo
  6219. else:
  6220. return True
  6221. return False
  6222. def is_same_symbol(a: SymbolNode | None, b: SymbolNode | None) -> bool:
  6223. return (
  6224. a == b
  6225. or (isinstance(a, PlaceholderNode) and isinstance(b, PlaceholderNode))
  6226. or is_same_var_from_getattr(a, b)
  6227. )
  6228. def is_trivial_body(block: Block) -> bool:
  6229. """Returns 'true' if the given body is "trivial" -- if it contains just a "pass",
  6230. "..." (ellipsis), or "raise NotImplementedError()". A trivial body may also
  6231. start with a statement containing just a string (e.g. a docstring).
  6232. Note: Functions that raise other kinds of exceptions do not count as
  6233. "trivial". We use this function to help us determine when it's ok to
  6234. relax certain checks on body, but functions that raise arbitrary exceptions
  6235. are more likely to do non-trivial work. For example:
  6236. def halt(self, reason: str = ...) -> NoReturn:
  6237. raise MyCustomError("Fatal error: " + reason, self.line, self.context)
  6238. A function that raises just NotImplementedError is much less likely to be
  6239. this complex.
  6240. Note: If you update this, you may also need to update
  6241. mypy.fastparse.is_possible_trivial_body!
  6242. """
  6243. body = block.body
  6244. if not body:
  6245. # Functions have empty bodies only if the body is stripped or the function is
  6246. # generated or deserialized. In these cases the body is unknown.
  6247. return False
  6248. # Skip a docstring
  6249. if isinstance(body[0], ExpressionStmt) and isinstance(body[0].expr, StrExpr):
  6250. body = block.body[1:]
  6251. if len(body) == 0:
  6252. # There's only a docstring (or no body at all).
  6253. return True
  6254. elif len(body) > 1:
  6255. return False
  6256. stmt = body[0]
  6257. if isinstance(stmt, RaiseStmt):
  6258. expr = stmt.expr
  6259. if expr is None:
  6260. return False
  6261. if isinstance(expr, CallExpr):
  6262. expr = expr.callee
  6263. return isinstance(expr, NameExpr) and expr.fullname == "builtins.NotImplementedError"
  6264. return isinstance(stmt, PassStmt) or (
  6265. isinstance(stmt, ExpressionStmt) and isinstance(stmt.expr, EllipsisExpr)
  6266. )