checkexpr.py 251 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972
  1. """Expression type checker. This file is conceptually part of TypeChecker."""
  2. from __future__ import annotations
  3. import itertools
  4. import time
  5. from collections import defaultdict
  6. from contextlib import contextmanager
  7. from typing import Callable, ClassVar, Final, Iterable, Iterator, List, Optional, Sequence, cast
  8. from typing_extensions import TypeAlias as _TypeAlias, overload
  9. import mypy.checker
  10. import mypy.errorcodes as codes
  11. from mypy import applytype, erasetype, join, message_registry, nodes, operators, types
  12. from mypy.argmap import ArgTypeExpander, map_actuals_to_formals, map_formals_to_actuals
  13. from mypy.checkmember import analyze_member_access, freeze_all_type_vars, type_object_type
  14. from mypy.checkstrformat import StringFormatterChecker
  15. from mypy.erasetype import erase_type, remove_instance_last_known_values, replace_meta_vars
  16. from mypy.errors import ErrorWatcher, report_internal_error
  17. from mypy.expandtype import expand_type, expand_type_by_instance, freshen_function_type_vars
  18. from mypy.infer import ArgumentInferContext, infer_function_type_arguments, infer_type_arguments
  19. from mypy.literals import literal
  20. from mypy.maptype import map_instance_to_supertype
  21. from mypy.meet import is_overlapping_types, narrow_declared_type
  22. from mypy.message_registry import ErrorMessage
  23. from mypy.messages import MessageBuilder
  24. from mypy.nodes import (
  25. ARG_NAMED,
  26. ARG_POS,
  27. ARG_STAR,
  28. ARG_STAR2,
  29. IMPLICITLY_ABSTRACT,
  30. LITERAL_TYPE,
  31. REVEAL_TYPE,
  32. ArgKind,
  33. AssertTypeExpr,
  34. AssignmentExpr,
  35. AwaitExpr,
  36. BytesExpr,
  37. CallExpr,
  38. CastExpr,
  39. ComparisonExpr,
  40. ComplexExpr,
  41. ConditionalExpr,
  42. Context,
  43. Decorator,
  44. DictExpr,
  45. DictionaryComprehension,
  46. EllipsisExpr,
  47. EnumCallExpr,
  48. Expression,
  49. FloatExpr,
  50. FuncDef,
  51. GeneratorExpr,
  52. IndexExpr,
  53. IntExpr,
  54. LambdaExpr,
  55. ListComprehension,
  56. ListExpr,
  57. MemberExpr,
  58. MypyFile,
  59. NamedTupleExpr,
  60. NameExpr,
  61. NewTypeExpr,
  62. OpExpr,
  63. OverloadedFuncDef,
  64. ParamSpecExpr,
  65. PlaceholderNode,
  66. PromoteExpr,
  67. RefExpr,
  68. RevealExpr,
  69. SetComprehension,
  70. SetExpr,
  71. SliceExpr,
  72. StarExpr,
  73. StrExpr,
  74. SuperExpr,
  75. SymbolNode,
  76. TempNode,
  77. TupleExpr,
  78. TypeAlias,
  79. TypeAliasExpr,
  80. TypeApplication,
  81. TypedDictExpr,
  82. TypeInfo,
  83. TypeVarExpr,
  84. TypeVarTupleExpr,
  85. UnaryExpr,
  86. Var,
  87. YieldExpr,
  88. YieldFromExpr,
  89. )
  90. from mypy.plugin import (
  91. FunctionContext,
  92. FunctionSigContext,
  93. MethodContext,
  94. MethodSigContext,
  95. Plugin,
  96. )
  97. from mypy.semanal_enum import ENUM_BASES
  98. from mypy.state import state
  99. from mypy.subtypes import (
  100. find_member,
  101. is_equivalent,
  102. is_same_type,
  103. is_subtype,
  104. non_method_protocol_members,
  105. )
  106. from mypy.traverser import has_await_expression
  107. from mypy.type_visitor import TypeTranslator
  108. from mypy.typeanal import (
  109. check_for_explicit_any,
  110. has_any_from_unimported_type,
  111. instantiate_type_alias,
  112. make_optional_type,
  113. set_any_tvars,
  114. )
  115. from mypy.typeops import (
  116. callable_type,
  117. custom_special_method,
  118. erase_to_union_or_bound,
  119. false_only,
  120. fixup_partial_type,
  121. function_type,
  122. get_type_vars,
  123. is_literal_type_like,
  124. make_simplified_union,
  125. simple_literal_type,
  126. true_only,
  127. try_expanding_sum_type_to_union,
  128. try_getting_str_literals,
  129. tuple_fallback,
  130. )
  131. from mypy.types import (
  132. LITERAL_TYPE_NAMES,
  133. TUPLE_LIKE_INSTANCE_NAMES,
  134. AnyType,
  135. CallableType,
  136. DeletedType,
  137. ErasedType,
  138. ExtraAttrs,
  139. FunctionLike,
  140. Instance,
  141. LiteralType,
  142. LiteralValue,
  143. NoneType,
  144. Overloaded,
  145. ParamSpecFlavor,
  146. ParamSpecType,
  147. PartialType,
  148. ProperType,
  149. TupleType,
  150. Type,
  151. TypeAliasType,
  152. TypedDictType,
  153. TypeOfAny,
  154. TypeType,
  155. TypeVarLikeType,
  156. TypeVarTupleType,
  157. TypeVarType,
  158. UninhabitedType,
  159. UnionType,
  160. UnpackType,
  161. flatten_nested_tuples,
  162. flatten_nested_unions,
  163. get_proper_type,
  164. get_proper_types,
  165. has_recursive_types,
  166. is_named_instance,
  167. split_with_prefix_and_suffix,
  168. )
  169. from mypy.types_utils import is_generic_instance, is_optional, is_self_type_like, remove_optional
  170. from mypy.typestate import type_state
  171. from mypy.typevars import fill_typevars
  172. from mypy.typevartuples import find_unpack_in_list
  173. from mypy.util import split_module_names
  174. from mypy.visitor import ExpressionVisitor
  175. # Type of callback user for checking individual function arguments. See
  176. # check_args() below for details.
  177. ArgChecker: _TypeAlias = Callable[
  178. [Type, Type, ArgKind, Type, int, int, CallableType, Optional[Type], Context, Context], None
  179. ]
  180. # Maximum nesting level for math union in overloads, setting this to large values
  181. # may cause performance issues. The reason is that although union math algorithm we use
  182. # nicely captures most corner cases, its worst case complexity is exponential,
  183. # see https://github.com/python/mypy/pull/5255#discussion_r196896335 for discussion.
  184. MAX_UNIONS: Final = 5
  185. # Types considered safe for comparisons with --strict-equality due to known behaviour of __eq__.
  186. # NOTE: All these types are subtypes of AbstractSet.
  187. OVERLAPPING_TYPES_ALLOWLIST: Final = [
  188. "builtins.set",
  189. "builtins.frozenset",
  190. "typing.KeysView",
  191. "typing.ItemsView",
  192. "builtins._dict_keys",
  193. "builtins._dict_items",
  194. "_collections_abc.dict_keys",
  195. "_collections_abc.dict_items",
  196. ]
  197. OVERLAPPING_BYTES_ALLOWLIST: Final = {
  198. "builtins.bytes",
  199. "builtins.bytearray",
  200. "builtins.memoryview",
  201. }
  202. class TooManyUnions(Exception):
  203. """Indicates that we need to stop splitting unions in an attempt
  204. to match an overload in order to save performance.
  205. """
  206. def allow_fast_container_literal(t: Type) -> bool:
  207. if isinstance(t, TypeAliasType) and t.is_recursive:
  208. return False
  209. t = get_proper_type(t)
  210. return isinstance(t, Instance) or (
  211. isinstance(t, TupleType) and all(allow_fast_container_literal(it) for it in t.items)
  212. )
  213. def extract_refexpr_names(expr: RefExpr) -> set[str]:
  214. """Recursively extracts all module references from a reference expression.
  215. Note that currently, the only two subclasses of RefExpr are NameExpr and
  216. MemberExpr."""
  217. output: set[str] = set()
  218. while isinstance(expr.node, MypyFile) or expr.fullname:
  219. if isinstance(expr.node, MypyFile) and expr.fullname:
  220. # If it's None, something's wrong (perhaps due to an
  221. # import cycle or a suppressed error). For now we just
  222. # skip it.
  223. output.add(expr.fullname)
  224. if isinstance(expr, NameExpr):
  225. is_suppressed_import = isinstance(expr.node, Var) and expr.node.is_suppressed_import
  226. if isinstance(expr.node, TypeInfo):
  227. # Reference to a class or a nested class
  228. output.update(split_module_names(expr.node.module_name))
  229. elif "." in expr.fullname and not is_suppressed_import:
  230. # Everything else (that is not a silenced import within a class)
  231. output.add(expr.fullname.rsplit(".", 1)[0])
  232. break
  233. elif isinstance(expr, MemberExpr):
  234. if isinstance(expr.expr, RefExpr):
  235. expr = expr.expr
  236. else:
  237. break
  238. else:
  239. raise AssertionError(f"Unknown RefExpr subclass: {type(expr)}")
  240. return output
  241. class Finished(Exception):
  242. """Raised if we can terminate overload argument check early (no match)."""
  243. class ExpressionChecker(ExpressionVisitor[Type]):
  244. """Expression type checker.
  245. This class works closely together with checker.TypeChecker.
  246. """
  247. # Some services are provided by a TypeChecker instance.
  248. chk: mypy.checker.TypeChecker
  249. # This is shared with TypeChecker, but stored also here for convenience.
  250. msg: MessageBuilder
  251. # Type context for type inference
  252. type_context: list[Type | None]
  253. # cache resolved types in some cases
  254. resolved_type: dict[Expression, ProperType]
  255. strfrm_checker: StringFormatterChecker
  256. plugin: Plugin
  257. def __init__(
  258. self,
  259. chk: mypy.checker.TypeChecker,
  260. msg: MessageBuilder,
  261. plugin: Plugin,
  262. per_line_checking_time_ns: dict[int, int],
  263. ) -> None:
  264. """Construct an expression type checker."""
  265. self.chk = chk
  266. self.msg = msg
  267. self.plugin = plugin
  268. self.per_line_checking_time_ns = per_line_checking_time_ns
  269. self.collect_line_checking_stats = chk.options.line_checking_stats is not None
  270. # Are we already visiting some expression? This is used to avoid double counting
  271. # time for nested expressions.
  272. self.in_expression = False
  273. self.type_context = [None]
  274. # Temporary overrides for expression types. This is currently
  275. # used by the union math in overloads.
  276. # TODO: refactor this to use a pattern similar to one in
  277. # multiassign_from_union, or maybe even combine the two?
  278. self.type_overrides: dict[Expression, Type] = {}
  279. self.strfrm_checker = StringFormatterChecker(self, self.chk, self.msg)
  280. self.resolved_type = {}
  281. # Callee in a call expression is in some sense both runtime context and
  282. # type context, because we support things like C[int](...). Store information
  283. # on whether current expression is a callee, to give better error messages
  284. # related to type context.
  285. self.is_callee = False
  286. type_state.infer_polymorphic = self.chk.options.new_type_inference
  287. def reset(self) -> None:
  288. self.resolved_type = {}
  289. def visit_name_expr(self, e: NameExpr) -> Type:
  290. """Type check a name expression.
  291. It can be of any kind: local, member or global.
  292. """
  293. self.chk.module_refs.update(extract_refexpr_names(e))
  294. result = self.analyze_ref_expr(e)
  295. return self.narrow_type_from_binder(e, result)
  296. def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
  297. result: Type | None = None
  298. node = e.node
  299. if isinstance(e, NameExpr) and e.is_special_form:
  300. # A special form definition, nothing to check here.
  301. return AnyType(TypeOfAny.special_form)
  302. if isinstance(node, Var):
  303. # Variable reference.
  304. result = self.analyze_var_ref(node, e)
  305. if isinstance(result, PartialType):
  306. result = self.chk.handle_partial_var_type(result, lvalue, node, e)
  307. elif isinstance(node, FuncDef):
  308. # Reference to a global function.
  309. result = function_type(node, self.named_type("builtins.function"))
  310. elif isinstance(node, OverloadedFuncDef) and node.type is not None:
  311. # node.type is None when there are multiple definitions of a function
  312. # and it's decorated by something that is not typing.overload
  313. # TODO: use a dummy Overloaded instead of AnyType in this case
  314. # like we do in mypy.types.function_type()?
  315. result = node.type
  316. elif isinstance(node, TypeInfo):
  317. # Reference to a type object.
  318. if node.typeddict_type:
  319. # We special-case TypedDict, because they don't define any constructor.
  320. result = self.typeddict_callable(node)
  321. else:
  322. result = type_object_type(node, self.named_type)
  323. if isinstance(result, CallableType) and isinstance( # type: ignore[misc]
  324. result.ret_type, Instance
  325. ):
  326. # We need to set correct line and column
  327. # TODO: always do this in type_object_type by passing the original context
  328. result.ret_type.line = e.line
  329. result.ret_type.column = e.column
  330. if isinstance(get_proper_type(self.type_context[-1]), TypeType):
  331. # This is the type in a Type[] expression, so substitute type
  332. # variables with Any.
  333. result = erasetype.erase_typevars(result)
  334. elif isinstance(node, MypyFile):
  335. # Reference to a module object.
  336. result = self.module_type(node)
  337. elif isinstance(node, Decorator):
  338. result = self.analyze_var_ref(node.var, e)
  339. elif isinstance(node, TypeAlias):
  340. # Something that refers to a type alias appears in runtime context.
  341. # Note that we suppress bogus errors for alias redefinitions,
  342. # they are already reported in semanal.py.
  343. result = self.alias_type_in_runtime_context(
  344. node, ctx=e, alias_definition=e.is_alias_rvalue or lvalue
  345. )
  346. elif isinstance(node, (TypeVarExpr, ParamSpecExpr)):
  347. result = self.object_type()
  348. else:
  349. if isinstance(node, PlaceholderNode):
  350. assert False, f"PlaceholderNode {node.fullname!r} leaked to checker"
  351. # Unknown reference; use any type implicitly to avoid
  352. # generating extra type errors.
  353. result = AnyType(TypeOfAny.from_error)
  354. assert result is not None
  355. return result
  356. def analyze_var_ref(self, var: Var, context: Context) -> Type:
  357. if var.type:
  358. var_type = get_proper_type(var.type)
  359. if isinstance(var_type, Instance):
  360. if self.is_literal_context() and var_type.last_known_value is not None:
  361. return var_type.last_known_value
  362. if var.name in {"True", "False"}:
  363. return self.infer_literal_expr_type(var.name == "True", "builtins.bool")
  364. return var.type
  365. else:
  366. if not var.is_ready and self.chk.in_checked_function():
  367. self.chk.handle_cannot_determine_type(var.name, context)
  368. # Implicit 'Any' type.
  369. return AnyType(TypeOfAny.special_form)
  370. def module_type(self, node: MypyFile) -> Instance:
  371. try:
  372. result = self.named_type("types.ModuleType")
  373. except KeyError:
  374. # In test cases might 'types' may not be available.
  375. # Fall back to a dummy 'object' type instead to
  376. # avoid a crash.
  377. result = self.named_type("builtins.object")
  378. module_attrs = {}
  379. immutable = set()
  380. for name, n in node.names.items():
  381. if not n.module_public:
  382. continue
  383. if isinstance(n.node, Var) and n.node.is_final:
  384. immutable.add(name)
  385. typ = self.chk.determine_type_of_member(n)
  386. if typ:
  387. module_attrs[name] = typ
  388. else:
  389. # TODO: what to do about nested module references?
  390. # They are non-trivial because there may be import cycles.
  391. module_attrs[name] = AnyType(TypeOfAny.special_form)
  392. result.extra_attrs = ExtraAttrs(module_attrs, immutable, node.fullname)
  393. return result
  394. def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
  395. """Type check a call expression."""
  396. if e.analyzed:
  397. if isinstance(e.analyzed, NamedTupleExpr) and not e.analyzed.is_typed:
  398. # Type check the arguments, but ignore the results. This relies
  399. # on the typeshed stubs to type check the arguments.
  400. self.visit_call_expr_inner(e)
  401. # It's really a special form that only looks like a call.
  402. return self.accept(e.analyzed, self.type_context[-1])
  403. return self.visit_call_expr_inner(e, allow_none_return=allow_none_return)
  404. def refers_to_typeddict(self, base: Expression) -> bool:
  405. if not isinstance(base, RefExpr):
  406. return False
  407. if isinstance(base.node, TypeInfo) and base.node.typeddict_type is not None:
  408. # Direct reference.
  409. return True
  410. return isinstance(base.node, TypeAlias) and isinstance(
  411. get_proper_type(base.node.target), TypedDictType
  412. )
  413. def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> Type:
  414. if (
  415. self.refers_to_typeddict(e.callee)
  416. or isinstance(e.callee, IndexExpr)
  417. and self.refers_to_typeddict(e.callee.base)
  418. ):
  419. typeddict_callable = get_proper_type(self.accept(e.callee, is_callee=True))
  420. if isinstance(typeddict_callable, CallableType):
  421. typeddict_type = get_proper_type(typeddict_callable.ret_type)
  422. assert isinstance(typeddict_type, TypedDictType)
  423. return self.check_typeddict_call(
  424. typeddict_type, e.arg_kinds, e.arg_names, e.args, e, typeddict_callable
  425. )
  426. if (
  427. isinstance(e.callee, NameExpr)
  428. and e.callee.name in ("isinstance", "issubclass")
  429. and len(e.args) == 2
  430. ):
  431. for typ in mypy.checker.flatten(e.args[1]):
  432. node = None
  433. if isinstance(typ, NameExpr):
  434. try:
  435. node = self.chk.lookup_qualified(typ.name)
  436. except KeyError:
  437. # Undefined names should already be reported in semantic analysis.
  438. pass
  439. if is_expr_literal_type(typ):
  440. self.msg.cannot_use_function_with_type(e.callee.name, "Literal", e)
  441. continue
  442. if (
  443. node
  444. and isinstance(node.node, TypeAlias)
  445. and isinstance(get_proper_type(node.node.target), AnyType)
  446. ):
  447. self.msg.cannot_use_function_with_type(e.callee.name, "Any", e)
  448. continue
  449. if (
  450. isinstance(typ, IndexExpr)
  451. and isinstance(typ.analyzed, (TypeApplication, TypeAliasExpr))
  452. ) or (
  453. isinstance(typ, NameExpr)
  454. and node
  455. and isinstance(node.node, TypeAlias)
  456. and not node.node.no_args
  457. ):
  458. self.msg.type_arguments_not_allowed(e)
  459. if isinstance(typ, RefExpr) and isinstance(typ.node, TypeInfo):
  460. if typ.node.typeddict_type:
  461. self.msg.cannot_use_function_with_type(e.callee.name, "TypedDict", e)
  462. elif typ.node.is_newtype:
  463. self.msg.cannot_use_function_with_type(e.callee.name, "NewType", e)
  464. self.try_infer_partial_type(e)
  465. type_context = None
  466. if isinstance(e.callee, LambdaExpr):
  467. formal_to_actual = map_actuals_to_formals(
  468. e.arg_kinds,
  469. e.arg_names,
  470. e.callee.arg_kinds,
  471. e.callee.arg_names,
  472. lambda i: self.accept(e.args[i]),
  473. )
  474. arg_types = [
  475. join.join_type_list([self.accept(e.args[j]) for j in formal_to_actual[i]])
  476. for i in range(len(e.callee.arg_kinds))
  477. ]
  478. type_context = CallableType(
  479. arg_types,
  480. e.callee.arg_kinds,
  481. e.callee.arg_names,
  482. ret_type=self.object_type(),
  483. fallback=self.named_type("builtins.function"),
  484. )
  485. callee_type = get_proper_type(
  486. self.accept(e.callee, type_context, always_allow_any=True, is_callee=True)
  487. )
  488. if (
  489. self.chk.options.disallow_untyped_calls
  490. and self.chk.in_checked_function()
  491. and isinstance(callee_type, CallableType)
  492. and callee_type.implicit
  493. ):
  494. self.msg.untyped_function_call(callee_type, e)
  495. # Figure out the full name of the callee for plugin lookup.
  496. object_type = None
  497. member = None
  498. fullname = None
  499. if isinstance(e.callee, RefExpr):
  500. # There are two special cases where plugins might act:
  501. # * A "static" reference/alias to a class or function;
  502. # get_function_hook() will be invoked for these.
  503. fullname = e.callee.fullname or None
  504. if isinstance(e.callee.node, TypeAlias):
  505. target = get_proper_type(e.callee.node.target)
  506. if isinstance(target, Instance):
  507. fullname = target.type.fullname
  508. # * Call to a method on object that has a full name (see
  509. # method_fullname() for details on supported objects);
  510. # get_method_hook() and get_method_signature_hook() will
  511. # be invoked for these.
  512. if (
  513. not fullname
  514. and isinstance(e.callee, MemberExpr)
  515. and self.chk.has_type(e.callee.expr)
  516. ):
  517. member = e.callee.name
  518. object_type = self.chk.lookup_type(e.callee.expr)
  519. ret_type = self.check_call_expr_with_callee_type(
  520. callee_type, e, fullname, object_type, member
  521. )
  522. if isinstance(e.callee, RefExpr) and len(e.args) == 2:
  523. if e.callee.fullname in ("builtins.isinstance", "builtins.issubclass"):
  524. self.check_runtime_protocol_test(e)
  525. if e.callee.fullname == "builtins.issubclass":
  526. self.check_protocol_issubclass(e)
  527. if isinstance(e.callee, MemberExpr) and e.callee.name == "format":
  528. self.check_str_format_call(e)
  529. ret_type = get_proper_type(ret_type)
  530. if isinstance(ret_type, UnionType):
  531. ret_type = make_simplified_union(ret_type.items)
  532. if isinstance(ret_type, UninhabitedType) and not ret_type.ambiguous:
  533. self.chk.binder.unreachable()
  534. # Warn on calls to functions that always return None. The check
  535. # of ret_type is both a common-case optimization and prevents reporting
  536. # the error in dynamic functions (where it will be Any).
  537. if (
  538. not allow_none_return
  539. and isinstance(ret_type, NoneType)
  540. and self.always_returns_none(e.callee)
  541. ):
  542. self.chk.msg.does_not_return_value(callee_type, e)
  543. return AnyType(TypeOfAny.from_error)
  544. return ret_type
  545. def check_str_format_call(self, e: CallExpr) -> None:
  546. """More precise type checking for str.format() calls on literals."""
  547. assert isinstance(e.callee, MemberExpr)
  548. format_value = None
  549. if isinstance(e.callee.expr, StrExpr):
  550. format_value = e.callee.expr.value
  551. elif self.chk.has_type(e.callee.expr):
  552. base_typ = try_getting_literal(self.chk.lookup_type(e.callee.expr))
  553. if isinstance(base_typ, LiteralType) and isinstance(base_typ.value, str):
  554. format_value = base_typ.value
  555. if format_value is not None:
  556. self.strfrm_checker.check_str_format_call(e, format_value)
  557. def method_fullname(self, object_type: Type, method_name: str) -> str | None:
  558. """Convert a method name to a fully qualified name, based on the type of the object that
  559. it is invoked on. Return `None` if the name of `object_type` cannot be determined.
  560. """
  561. object_type = get_proper_type(object_type)
  562. if isinstance(object_type, CallableType) and object_type.is_type_obj():
  563. # For class method calls, object_type is a callable representing the class object.
  564. # We "unwrap" it to a regular type, as the class/instance method difference doesn't
  565. # affect the fully qualified name.
  566. object_type = get_proper_type(object_type.ret_type)
  567. elif isinstance(object_type, TypeType):
  568. object_type = object_type.item
  569. type_name = None
  570. if isinstance(object_type, Instance):
  571. type_name = object_type.type.fullname
  572. elif isinstance(object_type, (TypedDictType, LiteralType)):
  573. info = object_type.fallback.type.get_containing_type_info(method_name)
  574. type_name = info.fullname if info is not None else None
  575. elif isinstance(object_type, TupleType):
  576. type_name = tuple_fallback(object_type).type.fullname
  577. if type_name:
  578. return f"{type_name}.{method_name}"
  579. else:
  580. return None
  581. def always_returns_none(self, node: Expression) -> bool:
  582. """Check if `node` refers to something explicitly annotated as only returning None."""
  583. if isinstance(node, RefExpr):
  584. if self.defn_returns_none(node.node):
  585. return True
  586. if isinstance(node, MemberExpr) and node.node is None: # instance or class attribute
  587. typ = get_proper_type(self.chk.lookup_type(node.expr))
  588. if isinstance(typ, Instance):
  589. info = typ.type
  590. elif isinstance(typ, CallableType) and typ.is_type_obj():
  591. ret_type = get_proper_type(typ.ret_type)
  592. if isinstance(ret_type, Instance):
  593. info = ret_type.type
  594. else:
  595. return False
  596. else:
  597. return False
  598. sym = info.get(node.name)
  599. if sym and self.defn_returns_none(sym.node):
  600. return True
  601. return False
  602. def defn_returns_none(self, defn: SymbolNode | None) -> bool:
  603. """Check if `defn` can _only_ return None."""
  604. if isinstance(defn, FuncDef):
  605. return isinstance(defn.type, CallableType) and isinstance(
  606. get_proper_type(defn.type.ret_type), NoneType
  607. )
  608. if isinstance(defn, OverloadedFuncDef):
  609. return all(self.defn_returns_none(item) for item in defn.items)
  610. if isinstance(defn, Var):
  611. typ = get_proper_type(defn.type)
  612. if (
  613. not defn.is_inferred
  614. and isinstance(typ, CallableType)
  615. and isinstance(get_proper_type(typ.ret_type), NoneType)
  616. ):
  617. return True
  618. if isinstance(typ, Instance):
  619. sym = typ.type.get("__call__")
  620. if sym and self.defn_returns_none(sym.node):
  621. return True
  622. return False
  623. def check_runtime_protocol_test(self, e: CallExpr) -> None:
  624. for expr in mypy.checker.flatten(e.args[1]):
  625. tp = get_proper_type(self.chk.lookup_type(expr))
  626. if (
  627. isinstance(tp, CallableType)
  628. and tp.is_type_obj()
  629. and tp.type_object().is_protocol
  630. and not tp.type_object().runtime_protocol
  631. ):
  632. self.chk.fail(message_registry.RUNTIME_PROTOCOL_EXPECTED, e)
  633. def check_protocol_issubclass(self, e: CallExpr) -> None:
  634. for expr in mypy.checker.flatten(e.args[1]):
  635. tp = get_proper_type(self.chk.lookup_type(expr))
  636. if isinstance(tp, CallableType) and tp.is_type_obj() and tp.type_object().is_protocol:
  637. attr_members = non_method_protocol_members(tp.type_object())
  638. if attr_members:
  639. self.chk.msg.report_non_method_protocol(tp.type_object(), attr_members, e)
  640. def check_typeddict_call(
  641. self,
  642. callee: TypedDictType,
  643. arg_kinds: list[ArgKind],
  644. arg_names: Sequence[str | None],
  645. args: list[Expression],
  646. context: Context,
  647. orig_callee: Type | None,
  648. ) -> Type:
  649. if args and all([ak in (ARG_NAMED, ARG_STAR2) for ak in arg_kinds]):
  650. # ex: Point(x=42, y=1337, **extras)
  651. # This is a bit ugly, but this is a price for supporting all possible syntax
  652. # variants for TypedDict constructors.
  653. kwargs = zip([StrExpr(n) if n is not None else None for n in arg_names], args)
  654. result = self.validate_typeddict_kwargs(kwargs=kwargs, callee=callee)
  655. if result is not None:
  656. validated_kwargs, always_present_keys = result
  657. return self.check_typeddict_call_with_kwargs(
  658. callee, validated_kwargs, context, orig_callee, always_present_keys
  659. )
  660. return AnyType(TypeOfAny.from_error)
  661. if len(args) == 1 and arg_kinds[0] == ARG_POS:
  662. unique_arg = args[0]
  663. if isinstance(unique_arg, DictExpr):
  664. # ex: Point({'x': 42, 'y': 1337, **extras})
  665. return self.check_typeddict_call_with_dict(
  666. callee, unique_arg.items, context, orig_callee
  667. )
  668. if isinstance(unique_arg, CallExpr) and isinstance(unique_arg.analyzed, DictExpr):
  669. # ex: Point(dict(x=42, y=1337, **extras))
  670. return self.check_typeddict_call_with_dict(
  671. callee, unique_arg.analyzed.items, context, orig_callee
  672. )
  673. if not args:
  674. # ex: EmptyDict()
  675. return self.check_typeddict_call_with_kwargs(callee, {}, context, orig_callee, set())
  676. self.chk.fail(message_registry.INVALID_TYPEDDICT_ARGS, context)
  677. return AnyType(TypeOfAny.from_error)
  678. def validate_typeddict_kwargs(
  679. self, kwargs: Iterable[tuple[Expression | None, Expression]], callee: TypedDictType
  680. ) -> tuple[dict[str, list[Expression]], set[str]] | None:
  681. # All (actual or mapped from ** unpacks) expressions that can match given key.
  682. result = defaultdict(list)
  683. # Keys that are guaranteed to be present no matter what (e.g. for all items of a union)
  684. always_present_keys = set()
  685. # Indicates latest encountered ** unpack among items.
  686. last_star_found = None
  687. for item_name_expr, item_arg in kwargs:
  688. if item_name_expr:
  689. key_type = self.accept(item_name_expr)
  690. values = try_getting_str_literals(item_name_expr, key_type)
  691. literal_value = None
  692. if values and len(values) == 1:
  693. literal_value = values[0]
  694. if literal_value is None:
  695. key_context = item_name_expr or item_arg
  696. self.chk.fail(
  697. message_registry.TYPEDDICT_KEY_MUST_BE_STRING_LITERAL,
  698. key_context,
  699. code=codes.LITERAL_REQ,
  700. )
  701. return None
  702. else:
  703. # A directly present key unconditionally shadows all previously found
  704. # values from ** items.
  705. # TODO: for duplicate keys, type-check all values.
  706. result[literal_value] = [item_arg]
  707. always_present_keys.add(literal_value)
  708. else:
  709. last_star_found = item_arg
  710. if not self.validate_star_typeddict_item(
  711. item_arg, callee, result, always_present_keys
  712. ):
  713. return None
  714. if self.chk.options.extra_checks and last_star_found is not None:
  715. absent_keys = []
  716. for key in callee.items:
  717. if key not in callee.required_keys and key not in result:
  718. absent_keys.append(key)
  719. if absent_keys:
  720. # Having an optional key not explicitly declared by a ** unpacked
  721. # TypedDict is unsafe, it may be an (incompatible) subtype at runtime.
  722. # TODO: catch the cases where a declared key is overridden by a subsequent
  723. # ** item without it (and not again overriden with complete ** item).
  724. self.msg.non_required_keys_absent_with_star(absent_keys, last_star_found)
  725. return result, always_present_keys
  726. def validate_star_typeddict_item(
  727. self,
  728. item_arg: Expression,
  729. callee: TypedDictType,
  730. result: dict[str, list[Expression]],
  731. always_present_keys: set[str],
  732. ) -> bool:
  733. """Update keys/expressions from a ** expression in TypedDict constructor.
  734. Note `result` and `always_present_keys` are updated in place. Return true if the
  735. expression `item_arg` may valid in `callee` TypedDict context.
  736. """
  737. with self.chk.local_type_map(), self.msg.filter_errors():
  738. inferred = get_proper_type(self.accept(item_arg, type_context=callee))
  739. possible_tds = []
  740. if isinstance(inferred, TypedDictType):
  741. possible_tds = [inferred]
  742. elif isinstance(inferred, UnionType):
  743. for item in get_proper_types(inferred.relevant_items()):
  744. if isinstance(item, TypedDictType):
  745. possible_tds.append(item)
  746. elif not self.valid_unpack_fallback_item(item):
  747. self.msg.unsupported_target_for_star_typeddict(item, item_arg)
  748. return False
  749. elif not self.valid_unpack_fallback_item(inferred):
  750. self.msg.unsupported_target_for_star_typeddict(inferred, item_arg)
  751. return False
  752. all_keys: set[str] = set()
  753. for td in possible_tds:
  754. all_keys |= td.items.keys()
  755. for key in all_keys:
  756. arg = TempNode(
  757. UnionType.make_union([td.items[key] for td in possible_tds if key in td.items])
  758. )
  759. arg.set_line(item_arg)
  760. if all(key in td.required_keys for td in possible_tds):
  761. always_present_keys.add(key)
  762. # Always present keys override previously found values. This is done
  763. # to support use cases like `Config({**defaults, **overrides})`, where
  764. # some `overrides` types are narrower that types in `defaults`, and
  765. # former are too wide for `Config`.
  766. if result[key]:
  767. first = result[key][0]
  768. if not isinstance(first, TempNode):
  769. # We must always preserve any non-synthetic values, so that
  770. # we will accept them even if they are shadowed.
  771. result[key] = [first, arg]
  772. else:
  773. result[key] = [arg]
  774. else:
  775. result[key] = [arg]
  776. else:
  777. # If this key is not required at least in some item of a union
  778. # it may not shadow previous item, so we need to type check both.
  779. result[key].append(arg)
  780. return True
  781. def valid_unpack_fallback_item(self, typ: ProperType) -> bool:
  782. if isinstance(typ, AnyType):
  783. return True
  784. if not isinstance(typ, Instance) or not typ.type.has_base("typing.Mapping"):
  785. return False
  786. mapped = map_instance_to_supertype(typ, self.chk.lookup_typeinfo("typing.Mapping"))
  787. return all(isinstance(a, AnyType) for a in get_proper_types(mapped.args))
  788. def match_typeddict_call_with_dict(
  789. self,
  790. callee: TypedDictType,
  791. kwargs: list[tuple[Expression | None, Expression]],
  792. context: Context,
  793. ) -> bool:
  794. result = self.validate_typeddict_kwargs(kwargs=kwargs, callee=callee)
  795. if result is not None:
  796. validated_kwargs, _ = result
  797. return callee.required_keys <= set(validated_kwargs.keys()) <= set(callee.items.keys())
  798. else:
  799. return False
  800. def check_typeddict_call_with_dict(
  801. self,
  802. callee: TypedDictType,
  803. kwargs: list[tuple[Expression | None, Expression]],
  804. context: Context,
  805. orig_callee: Type | None,
  806. ) -> Type:
  807. result = self.validate_typeddict_kwargs(kwargs=kwargs, callee=callee)
  808. if result is not None:
  809. validated_kwargs, always_present_keys = result
  810. return self.check_typeddict_call_with_kwargs(
  811. callee,
  812. kwargs=validated_kwargs,
  813. context=context,
  814. orig_callee=orig_callee,
  815. always_present_keys=always_present_keys,
  816. )
  817. else:
  818. return AnyType(TypeOfAny.from_error)
  819. def typeddict_callable(self, info: TypeInfo) -> CallableType:
  820. """Construct a reasonable type for a TypedDict type in runtime context.
  821. If it appears as a callee, it will be special-cased anyway, e.g. it is
  822. also allowed to accept a single positional argument if it is a dict literal.
  823. Note it is not safe to move this to type_object_type() since it will crash
  824. on plugin-generated TypedDicts, that may not have the special_alias.
  825. """
  826. assert info.special_alias is not None
  827. target = info.special_alias.target
  828. assert isinstance(target, ProperType) and isinstance(target, TypedDictType)
  829. expected_types = list(target.items.values())
  830. kinds = [ArgKind.ARG_NAMED] * len(expected_types)
  831. names = list(target.items.keys())
  832. return CallableType(
  833. expected_types,
  834. kinds,
  835. names,
  836. target,
  837. self.named_type("builtins.type"),
  838. variables=info.defn.type_vars,
  839. )
  840. def typeddict_callable_from_context(self, callee: TypedDictType) -> CallableType:
  841. return CallableType(
  842. list(callee.items.values()),
  843. [ArgKind.ARG_NAMED] * len(callee.items),
  844. list(callee.items.keys()),
  845. callee,
  846. self.named_type("builtins.type"),
  847. )
  848. def check_typeddict_call_with_kwargs(
  849. self,
  850. callee: TypedDictType,
  851. kwargs: dict[str, list[Expression]],
  852. context: Context,
  853. orig_callee: Type | None,
  854. always_present_keys: set[str],
  855. ) -> Type:
  856. actual_keys = kwargs.keys()
  857. if not (
  858. callee.required_keys <= always_present_keys and actual_keys <= callee.items.keys()
  859. ):
  860. if not (actual_keys <= callee.items.keys()):
  861. self.msg.unexpected_typeddict_keys(
  862. callee,
  863. expected_keys=[
  864. key
  865. for key in callee.items.keys()
  866. if key in callee.required_keys or key in actual_keys
  867. ],
  868. actual_keys=list(actual_keys),
  869. context=context,
  870. )
  871. if not (callee.required_keys <= always_present_keys):
  872. self.msg.unexpected_typeddict_keys(
  873. callee,
  874. expected_keys=[
  875. key for key in callee.items.keys() if key in callee.required_keys
  876. ],
  877. actual_keys=[
  878. key for key in always_present_keys if key in callee.required_keys
  879. ],
  880. context=context,
  881. )
  882. if callee.required_keys > actual_keys:
  883. # found_set is a sub-set of the required_keys
  884. # This means we're missing some keys and as such, we can't
  885. # properly type the object
  886. return AnyType(TypeOfAny.from_error)
  887. orig_callee = get_proper_type(orig_callee)
  888. if isinstance(orig_callee, CallableType):
  889. infer_callee = orig_callee
  890. else:
  891. # Try reconstructing from type context.
  892. if callee.fallback.type.special_alias is not None:
  893. infer_callee = self.typeddict_callable(callee.fallback.type)
  894. else:
  895. # Likely a TypedDict type generated by a plugin.
  896. infer_callee = self.typeddict_callable_from_context(callee)
  897. # We don't show any errors, just infer types in a generic TypedDict type,
  898. # a custom error message will be given below, if there are errors.
  899. with self.msg.filter_errors(), self.chk.local_type_map():
  900. orig_ret_type, _ = self.check_callable_call(
  901. infer_callee,
  902. # We use first expression for each key to infer type variables of a generic
  903. # TypedDict. This is a bit arbitrary, but in most cases will work better than
  904. # trying to infer a union or a join.
  905. [args[0] for args in kwargs.values()],
  906. [ArgKind.ARG_NAMED] * len(kwargs),
  907. context,
  908. list(kwargs.keys()),
  909. None,
  910. None,
  911. None,
  912. )
  913. ret_type = get_proper_type(orig_ret_type)
  914. if not isinstance(ret_type, TypedDictType):
  915. # If something went really wrong, type-check call with original type,
  916. # this may give a better error message.
  917. ret_type = callee
  918. for item_name, item_expected_type in ret_type.items.items():
  919. if item_name in kwargs:
  920. item_values = kwargs[item_name]
  921. for item_value in item_values:
  922. self.chk.check_simple_assignment(
  923. lvalue_type=item_expected_type,
  924. rvalue=item_value,
  925. context=item_value,
  926. msg=ErrorMessage(
  927. message_registry.INCOMPATIBLE_TYPES.value, code=codes.TYPEDDICT_ITEM
  928. ),
  929. lvalue_name=f'TypedDict item "{item_name}"',
  930. rvalue_name="expression",
  931. )
  932. return orig_ret_type
  933. def get_partial_self_var(self, expr: MemberExpr) -> Var | None:
  934. """Get variable node for a partial self attribute.
  935. If the expression is not a self attribute, or attribute is not variable,
  936. or variable is not partial, return None.
  937. """
  938. if not (
  939. isinstance(expr.expr, NameExpr)
  940. and isinstance(expr.expr.node, Var)
  941. and expr.expr.node.is_self
  942. ):
  943. # Not a self.attr expression.
  944. return None
  945. info = self.chk.scope.enclosing_class()
  946. if not info or expr.name not in info.names:
  947. # Don't mess with partial types in superclasses.
  948. return None
  949. sym = info.names[expr.name]
  950. if isinstance(sym.node, Var) and isinstance(sym.node.type, PartialType):
  951. return sym.node
  952. return None
  953. # Types and methods that can be used to infer partial types.
  954. item_args: ClassVar[dict[str, list[str]]] = {
  955. "builtins.list": ["append"],
  956. "builtins.set": ["add", "discard"],
  957. }
  958. container_args: ClassVar[dict[str, dict[str, list[str]]]] = {
  959. "builtins.list": {"extend": ["builtins.list"]},
  960. "builtins.dict": {"update": ["builtins.dict"]},
  961. "collections.OrderedDict": {"update": ["builtins.dict"]},
  962. "builtins.set": {"update": ["builtins.set", "builtins.list"]},
  963. }
  964. def try_infer_partial_type(self, e: CallExpr) -> None:
  965. """Try to make partial type precise from a call."""
  966. if not isinstance(e.callee, MemberExpr):
  967. return
  968. callee = e.callee
  969. if isinstance(callee.expr, RefExpr):
  970. # Call a method with a RefExpr callee, such as 'x.method(...)'.
  971. ret = self.get_partial_var(callee.expr)
  972. if ret is None:
  973. return
  974. var, partial_types = ret
  975. typ = self.try_infer_partial_value_type_from_call(e, callee.name, var)
  976. # Var may be deleted from partial_types in try_infer_partial_value_type_from_call
  977. if typ is not None and var in partial_types:
  978. var.type = typ
  979. del partial_types[var]
  980. elif isinstance(callee.expr, IndexExpr) and isinstance(callee.expr.base, RefExpr):
  981. # Call 'x[y].method(...)'; may infer type of 'x' if it's a partial defaultdict.
  982. if callee.expr.analyzed is not None:
  983. return # A special form
  984. base = callee.expr.base
  985. index = callee.expr.index
  986. ret = self.get_partial_var(base)
  987. if ret is None:
  988. return
  989. var, partial_types = ret
  990. partial_type = get_partial_instance_type(var.type)
  991. if partial_type is None or partial_type.value_type is None:
  992. return
  993. value_type = self.try_infer_partial_value_type_from_call(e, callee.name, var)
  994. if value_type is not None:
  995. # Infer key type.
  996. key_type = self.accept(index)
  997. if mypy.checker.is_valid_inferred_type(key_type):
  998. # Store inferred partial type.
  999. assert partial_type.type is not None
  1000. typename = partial_type.type.fullname
  1001. var.type = self.chk.named_generic_type(typename, [key_type, value_type])
  1002. del partial_types[var]
  1003. def get_partial_var(self, ref: RefExpr) -> tuple[Var, dict[Var, Context]] | None:
  1004. var = ref.node
  1005. if var is None and isinstance(ref, MemberExpr):
  1006. var = self.get_partial_self_var(ref)
  1007. if not isinstance(var, Var):
  1008. return None
  1009. partial_types = self.chk.find_partial_types(var)
  1010. if partial_types is None:
  1011. return None
  1012. return var, partial_types
  1013. def try_infer_partial_value_type_from_call(
  1014. self, e: CallExpr, methodname: str, var: Var
  1015. ) -> Instance | None:
  1016. """Try to make partial type precise from a call such as 'x.append(y)'."""
  1017. if self.chk.current_node_deferred:
  1018. return None
  1019. partial_type = get_partial_instance_type(var.type)
  1020. if partial_type is None:
  1021. return None
  1022. if partial_type.value_type:
  1023. typename = partial_type.value_type.type.fullname
  1024. else:
  1025. assert partial_type.type is not None
  1026. typename = partial_type.type.fullname
  1027. # Sometimes we can infer a full type for a partial List, Dict or Set type.
  1028. # TODO: Don't infer argument expression twice.
  1029. if (
  1030. typename in self.item_args
  1031. and methodname in self.item_args[typename]
  1032. and e.arg_kinds == [ARG_POS]
  1033. ):
  1034. item_type = self.accept(e.args[0])
  1035. if mypy.checker.is_valid_inferred_type(item_type):
  1036. return self.chk.named_generic_type(typename, [item_type])
  1037. elif (
  1038. typename in self.container_args
  1039. and methodname in self.container_args[typename]
  1040. and e.arg_kinds == [ARG_POS]
  1041. ):
  1042. arg_type = get_proper_type(self.accept(e.args[0]))
  1043. if isinstance(arg_type, Instance):
  1044. arg_typename = arg_type.type.fullname
  1045. if arg_typename in self.container_args[typename][methodname]:
  1046. if all(
  1047. mypy.checker.is_valid_inferred_type(item_type)
  1048. for item_type in arg_type.args
  1049. ):
  1050. return self.chk.named_generic_type(typename, list(arg_type.args))
  1051. elif isinstance(arg_type, AnyType):
  1052. return self.chk.named_type(typename)
  1053. return None
  1054. def apply_function_plugin(
  1055. self,
  1056. callee: CallableType,
  1057. arg_kinds: list[ArgKind],
  1058. arg_types: list[Type],
  1059. arg_names: Sequence[str | None] | None,
  1060. formal_to_actual: list[list[int]],
  1061. args: list[Expression],
  1062. fullname: str,
  1063. object_type: Type | None,
  1064. context: Context,
  1065. ) -> Type:
  1066. """Use special case logic to infer the return type of a specific named function/method.
  1067. Caller must ensure that a plugin hook exists. There are two different cases:
  1068. - If object_type is None, the caller must ensure that a function hook exists
  1069. for fullname.
  1070. - If object_type is not None, the caller must ensure that a method hook exists
  1071. for fullname.
  1072. Return the inferred return type.
  1073. """
  1074. num_formals = len(callee.arg_types)
  1075. formal_arg_types: list[list[Type]] = [[] for _ in range(num_formals)]
  1076. formal_arg_exprs: list[list[Expression]] = [[] for _ in range(num_formals)]
  1077. formal_arg_names: list[list[str | None]] = [[] for _ in range(num_formals)]
  1078. formal_arg_kinds: list[list[ArgKind]] = [[] for _ in range(num_formals)]
  1079. for formal, actuals in enumerate(formal_to_actual):
  1080. for actual in actuals:
  1081. formal_arg_types[formal].append(arg_types[actual])
  1082. formal_arg_exprs[formal].append(args[actual])
  1083. if arg_names:
  1084. formal_arg_names[formal].append(arg_names[actual])
  1085. formal_arg_kinds[formal].append(arg_kinds[actual])
  1086. if object_type is None:
  1087. # Apply function plugin
  1088. callback = self.plugin.get_function_hook(fullname)
  1089. assert callback is not None # Assume that caller ensures this
  1090. return callback(
  1091. FunctionContext(
  1092. formal_arg_types,
  1093. formal_arg_kinds,
  1094. callee.arg_names,
  1095. formal_arg_names,
  1096. callee.ret_type,
  1097. formal_arg_exprs,
  1098. context,
  1099. self.chk,
  1100. )
  1101. )
  1102. else:
  1103. # Apply method plugin
  1104. method_callback = self.plugin.get_method_hook(fullname)
  1105. assert method_callback is not None # Assume that caller ensures this
  1106. object_type = get_proper_type(object_type)
  1107. return method_callback(
  1108. MethodContext(
  1109. object_type,
  1110. formal_arg_types,
  1111. formal_arg_kinds,
  1112. callee.arg_names,
  1113. formal_arg_names,
  1114. callee.ret_type,
  1115. formal_arg_exprs,
  1116. context,
  1117. self.chk,
  1118. )
  1119. )
  1120. def apply_signature_hook(
  1121. self,
  1122. callee: FunctionLike,
  1123. args: list[Expression],
  1124. arg_kinds: list[ArgKind],
  1125. arg_names: Sequence[str | None] | None,
  1126. hook: Callable[[list[list[Expression]], CallableType], FunctionLike],
  1127. ) -> FunctionLike:
  1128. """Helper to apply a signature hook for either a function or method"""
  1129. if isinstance(callee, CallableType):
  1130. num_formals = len(callee.arg_kinds)
  1131. formal_to_actual = map_actuals_to_formals(
  1132. arg_kinds,
  1133. arg_names,
  1134. callee.arg_kinds,
  1135. callee.arg_names,
  1136. lambda i: self.accept(args[i]),
  1137. )
  1138. formal_arg_exprs: list[list[Expression]] = [[] for _ in range(num_formals)]
  1139. for formal, actuals in enumerate(formal_to_actual):
  1140. for actual in actuals:
  1141. formal_arg_exprs[formal].append(args[actual])
  1142. return hook(formal_arg_exprs, callee)
  1143. else:
  1144. assert isinstance(callee, Overloaded)
  1145. items = []
  1146. for item in callee.items:
  1147. adjusted = self.apply_signature_hook(item, args, arg_kinds, arg_names, hook)
  1148. assert isinstance(adjusted, CallableType)
  1149. items.append(adjusted)
  1150. return Overloaded(items)
  1151. def apply_function_signature_hook(
  1152. self,
  1153. callee: FunctionLike,
  1154. args: list[Expression],
  1155. arg_kinds: list[ArgKind],
  1156. context: Context,
  1157. arg_names: Sequence[str | None] | None,
  1158. signature_hook: Callable[[FunctionSigContext], FunctionLike],
  1159. ) -> FunctionLike:
  1160. """Apply a plugin hook that may infer a more precise signature for a function."""
  1161. return self.apply_signature_hook(
  1162. callee,
  1163. args,
  1164. arg_kinds,
  1165. arg_names,
  1166. (lambda args, sig: signature_hook(FunctionSigContext(args, sig, context, self.chk))),
  1167. )
  1168. def apply_method_signature_hook(
  1169. self,
  1170. callee: FunctionLike,
  1171. args: list[Expression],
  1172. arg_kinds: list[ArgKind],
  1173. context: Context,
  1174. arg_names: Sequence[str | None] | None,
  1175. object_type: Type,
  1176. signature_hook: Callable[[MethodSigContext], FunctionLike],
  1177. ) -> FunctionLike:
  1178. """Apply a plugin hook that may infer a more precise signature for a method."""
  1179. pobject_type = get_proper_type(object_type)
  1180. return self.apply_signature_hook(
  1181. callee,
  1182. args,
  1183. arg_kinds,
  1184. arg_names,
  1185. (
  1186. lambda args, sig: signature_hook(
  1187. MethodSigContext(pobject_type, args, sig, context, self.chk)
  1188. )
  1189. ),
  1190. )
  1191. def transform_callee_type(
  1192. self,
  1193. callable_name: str | None,
  1194. callee: Type,
  1195. args: list[Expression],
  1196. arg_kinds: list[ArgKind],
  1197. context: Context,
  1198. arg_names: Sequence[str | None] | None = None,
  1199. object_type: Type | None = None,
  1200. ) -> Type:
  1201. """Attempt to determine a more accurate signature for a method call.
  1202. This is done by looking up and applying a method signature hook (if one exists for the
  1203. given method name).
  1204. If no matching method signature hook is found, callee is returned unmodified. The same
  1205. happens if the arguments refer to a non-method callable (this is allowed so that the code
  1206. calling transform_callee_type needs to perform fewer boilerplate checks).
  1207. Note: this method is *not* called automatically as part of check_call, because in some
  1208. cases check_call is called multiple times while checking a single call (for example when
  1209. dealing with overloads). Instead, this method needs to be called explicitly
  1210. (if appropriate) before the signature is passed to check_call.
  1211. """
  1212. callee = get_proper_type(callee)
  1213. if callable_name is not None and isinstance(callee, FunctionLike):
  1214. if object_type is not None:
  1215. method_sig_hook = self.plugin.get_method_signature_hook(callable_name)
  1216. if method_sig_hook:
  1217. return self.apply_method_signature_hook(
  1218. callee, args, arg_kinds, context, arg_names, object_type, method_sig_hook
  1219. )
  1220. else:
  1221. function_sig_hook = self.plugin.get_function_signature_hook(callable_name)
  1222. if function_sig_hook:
  1223. return self.apply_function_signature_hook(
  1224. callee, args, arg_kinds, context, arg_names, function_sig_hook
  1225. )
  1226. return callee
  1227. def check_call_expr_with_callee_type(
  1228. self,
  1229. callee_type: Type,
  1230. e: CallExpr,
  1231. callable_name: str | None,
  1232. object_type: Type | None,
  1233. member: str | None = None,
  1234. ) -> Type:
  1235. """Type check call expression.
  1236. The callee_type should be used as the type of callee expression. In particular,
  1237. in case of a union type this can be a particular item of the union, so that we can
  1238. apply plugin hooks to each item.
  1239. The 'member', 'callable_name' and 'object_type' are only used to call plugin hooks.
  1240. If 'callable_name' is None but 'member' is not None (member call), try constructing
  1241. 'callable_name' using 'object_type' (the base type on which the method is called),
  1242. for example 'typing.Mapping.get'.
  1243. """
  1244. if callable_name is None and member is not None:
  1245. assert object_type is not None
  1246. callable_name = self.method_fullname(object_type, member)
  1247. object_type = get_proper_type(object_type)
  1248. if callable_name:
  1249. # Try to refine the call signature using plugin hooks before checking the call.
  1250. callee_type = self.transform_callee_type(
  1251. callable_name, callee_type, e.args, e.arg_kinds, e, e.arg_names, object_type
  1252. )
  1253. # Unions are special-cased to allow plugins to act on each item in the union.
  1254. elif member is not None and isinstance(object_type, UnionType):
  1255. return self.check_union_call_expr(e, object_type, member)
  1256. ret_type, callee_type = self.check_call(
  1257. callee_type,
  1258. e.args,
  1259. e.arg_kinds,
  1260. e,
  1261. e.arg_names,
  1262. callable_node=e.callee,
  1263. callable_name=callable_name,
  1264. object_type=object_type,
  1265. )
  1266. proper_callee = get_proper_type(callee_type)
  1267. if (
  1268. isinstance(e.callee, RefExpr)
  1269. and isinstance(proper_callee, CallableType)
  1270. and proper_callee.type_guard is not None
  1271. ):
  1272. # Cache it for find_isinstance_check()
  1273. e.callee.type_guard = proper_callee.type_guard
  1274. return ret_type
  1275. def check_union_call_expr(self, e: CallExpr, object_type: UnionType, member: str) -> Type:
  1276. """Type check calling a member expression where the base type is a union."""
  1277. res: list[Type] = []
  1278. for typ in object_type.relevant_items():
  1279. # Member access errors are already reported when visiting the member expression.
  1280. with self.msg.filter_errors():
  1281. item = analyze_member_access(
  1282. member,
  1283. typ,
  1284. e,
  1285. False,
  1286. False,
  1287. False,
  1288. self.msg,
  1289. original_type=object_type,
  1290. chk=self.chk,
  1291. in_literal_context=self.is_literal_context(),
  1292. self_type=typ,
  1293. )
  1294. narrowed = self.narrow_type_from_binder(e.callee, item, skip_non_overlapping=True)
  1295. if narrowed is None:
  1296. continue
  1297. callable_name = self.method_fullname(typ, member)
  1298. item_object_type = typ if callable_name else None
  1299. res.append(
  1300. self.check_call_expr_with_callee_type(narrowed, e, callable_name, item_object_type)
  1301. )
  1302. return make_simplified_union(res)
  1303. def check_call(
  1304. self,
  1305. callee: Type,
  1306. args: list[Expression],
  1307. arg_kinds: list[ArgKind],
  1308. context: Context,
  1309. arg_names: Sequence[str | None] | None = None,
  1310. callable_node: Expression | None = None,
  1311. callable_name: str | None = None,
  1312. object_type: Type | None = None,
  1313. ) -> tuple[Type, Type]:
  1314. """Type check a call.
  1315. Also infer type arguments if the callee is a generic function.
  1316. Return (result type, inferred callee type).
  1317. Arguments:
  1318. callee: type of the called value
  1319. args: actual argument expressions
  1320. arg_kinds: contains nodes.ARG_* constant for each argument in args
  1321. describing whether the argument is positional, *arg, etc.
  1322. context: current expression context, used for inference.
  1323. arg_names: names of arguments (optional)
  1324. callable_node: associate the inferred callable type to this node,
  1325. if specified
  1326. callable_name: Fully-qualified name of the function/method to call,
  1327. or None if unavailable (examples: 'builtins.open', 'typing.Mapping.get')
  1328. object_type: If callable_name refers to a method, the type of the object
  1329. on which the method is being called
  1330. """
  1331. callee = get_proper_type(callee)
  1332. if isinstance(callee, CallableType):
  1333. return self.check_callable_call(
  1334. callee,
  1335. args,
  1336. arg_kinds,
  1337. context,
  1338. arg_names,
  1339. callable_node,
  1340. callable_name,
  1341. object_type,
  1342. )
  1343. elif isinstance(callee, Overloaded):
  1344. return self.check_overload_call(
  1345. callee, args, arg_kinds, arg_names, callable_name, object_type, context
  1346. )
  1347. elif isinstance(callee, AnyType) or not self.chk.in_checked_function():
  1348. return self.check_any_type_call(args, callee)
  1349. elif isinstance(callee, UnionType):
  1350. return self.check_union_call(callee, args, arg_kinds, arg_names, context)
  1351. elif isinstance(callee, Instance):
  1352. call_function = analyze_member_access(
  1353. "__call__",
  1354. callee,
  1355. context,
  1356. is_lvalue=False,
  1357. is_super=False,
  1358. is_operator=True,
  1359. msg=self.msg,
  1360. original_type=callee,
  1361. chk=self.chk,
  1362. in_literal_context=self.is_literal_context(),
  1363. )
  1364. callable_name = callee.type.fullname + ".__call__"
  1365. # Apply method signature hook, if one exists
  1366. call_function = self.transform_callee_type(
  1367. callable_name, call_function, args, arg_kinds, context, arg_names, callee
  1368. )
  1369. result = self.check_call(
  1370. call_function,
  1371. args,
  1372. arg_kinds,
  1373. context,
  1374. arg_names,
  1375. callable_node,
  1376. callable_name,
  1377. callee,
  1378. )
  1379. if callable_node:
  1380. # check_call() stored "call_function" as the type, which is incorrect.
  1381. # Override the type.
  1382. self.chk.store_type(callable_node, callee)
  1383. return result
  1384. elif isinstance(callee, TypeVarType):
  1385. return self.check_call(
  1386. callee.upper_bound, args, arg_kinds, context, arg_names, callable_node
  1387. )
  1388. elif isinstance(callee, TypeType):
  1389. item = self.analyze_type_type_callee(callee.item, context)
  1390. return self.check_call(item, args, arg_kinds, context, arg_names, callable_node)
  1391. elif isinstance(callee, TupleType):
  1392. return self.check_call(
  1393. tuple_fallback(callee),
  1394. args,
  1395. arg_kinds,
  1396. context,
  1397. arg_names,
  1398. callable_node,
  1399. callable_name,
  1400. object_type,
  1401. )
  1402. else:
  1403. return self.msg.not_callable(callee, context), AnyType(TypeOfAny.from_error)
  1404. def check_callable_call(
  1405. self,
  1406. callee: CallableType,
  1407. args: list[Expression],
  1408. arg_kinds: list[ArgKind],
  1409. context: Context,
  1410. arg_names: Sequence[str | None] | None,
  1411. callable_node: Expression | None,
  1412. callable_name: str | None,
  1413. object_type: Type | None,
  1414. ) -> tuple[Type, Type]:
  1415. """Type check a call that targets a callable value.
  1416. See the docstring of check_call for more information.
  1417. """
  1418. # Always unpack **kwargs before checking a call.
  1419. callee = callee.with_unpacked_kwargs()
  1420. if callable_name is None and callee.name:
  1421. callable_name = callee.name
  1422. ret_type = get_proper_type(callee.ret_type)
  1423. if callee.is_type_obj() and isinstance(ret_type, Instance):
  1424. callable_name = ret_type.type.fullname
  1425. if isinstance(callable_node, RefExpr) and callable_node.fullname in ENUM_BASES:
  1426. # An Enum() call that failed SemanticAnalyzerPass2.check_enum_call().
  1427. return callee.ret_type, callee
  1428. if (
  1429. callee.is_type_obj()
  1430. and callee.type_object().is_protocol
  1431. # Exception for Type[...]
  1432. and not callee.from_type_type
  1433. ):
  1434. self.chk.fail(
  1435. message_registry.CANNOT_INSTANTIATE_PROTOCOL.format(callee.type_object().name),
  1436. context,
  1437. )
  1438. elif (
  1439. callee.is_type_obj()
  1440. and callee.type_object().is_abstract
  1441. # Exception for Type[...]
  1442. and not callee.from_type_type
  1443. and not callee.type_object().fallback_to_any
  1444. ):
  1445. type = callee.type_object()
  1446. # Determine whether the implicitly abstract attributes are functions with
  1447. # None-compatible return types.
  1448. abstract_attributes: dict[str, bool] = {}
  1449. for attr_name, abstract_status in type.abstract_attributes:
  1450. if abstract_status == IMPLICITLY_ABSTRACT:
  1451. abstract_attributes[attr_name] = self.can_return_none(type, attr_name)
  1452. else:
  1453. abstract_attributes[attr_name] = False
  1454. self.msg.cannot_instantiate_abstract_class(
  1455. callee.type_object().name, abstract_attributes, context
  1456. )
  1457. formal_to_actual = map_actuals_to_formals(
  1458. arg_kinds,
  1459. arg_names,
  1460. callee.arg_kinds,
  1461. callee.arg_names,
  1462. lambda i: self.accept(args[i]),
  1463. )
  1464. if callee.is_generic():
  1465. need_refresh = any(
  1466. isinstance(v, (ParamSpecType, TypeVarTupleType)) for v in callee.variables
  1467. )
  1468. callee = freshen_function_type_vars(callee)
  1469. callee = self.infer_function_type_arguments_using_context(callee, context)
  1470. if need_refresh:
  1471. # Argument kinds etc. may have changed due to
  1472. # ParamSpec or TypeVarTuple variables being replaced with an arbitrary
  1473. # number of arguments; recalculate actual-to-formal map
  1474. formal_to_actual = map_actuals_to_formals(
  1475. arg_kinds,
  1476. arg_names,
  1477. callee.arg_kinds,
  1478. callee.arg_names,
  1479. lambda i: self.accept(args[i]),
  1480. )
  1481. callee = self.infer_function_type_arguments(
  1482. callee, args, arg_kinds, formal_to_actual, context
  1483. )
  1484. if need_refresh:
  1485. formal_to_actual = map_actuals_to_formals(
  1486. arg_kinds,
  1487. arg_names,
  1488. callee.arg_kinds,
  1489. callee.arg_names,
  1490. lambda i: self.accept(args[i]),
  1491. )
  1492. param_spec = callee.param_spec()
  1493. if param_spec is not None and arg_kinds == [ARG_STAR, ARG_STAR2]:
  1494. arg1 = self.accept(args[0])
  1495. arg2 = self.accept(args[1])
  1496. if (
  1497. isinstance(arg1, ParamSpecType)
  1498. and isinstance(arg2, ParamSpecType)
  1499. and arg1.flavor == ParamSpecFlavor.ARGS
  1500. and arg2.flavor == ParamSpecFlavor.KWARGS
  1501. and arg1.id == arg2.id == param_spec.id
  1502. ):
  1503. return callee.ret_type, callee
  1504. arg_types = self.infer_arg_types_in_context(callee, args, arg_kinds, formal_to_actual)
  1505. self.check_argument_count(
  1506. callee,
  1507. arg_types,
  1508. arg_kinds,
  1509. arg_names,
  1510. formal_to_actual,
  1511. context,
  1512. object_type,
  1513. callable_name,
  1514. )
  1515. self.check_argument_types(
  1516. arg_types, arg_kinds, args, callee, formal_to_actual, context, object_type=object_type
  1517. )
  1518. if (
  1519. callee.is_type_obj()
  1520. and (len(arg_types) == 1)
  1521. and is_equivalent(callee.ret_type, self.named_type("builtins.type"))
  1522. ):
  1523. callee = callee.copy_modified(ret_type=TypeType.make_normalized(arg_types[0]))
  1524. if callable_node:
  1525. # Store the inferred callable type.
  1526. self.chk.store_type(callable_node, callee)
  1527. if callable_name and (
  1528. (object_type is None and self.plugin.get_function_hook(callable_name))
  1529. or (object_type is not None and self.plugin.get_method_hook(callable_name))
  1530. ):
  1531. new_ret_type = self.apply_function_plugin(
  1532. callee,
  1533. arg_kinds,
  1534. arg_types,
  1535. arg_names,
  1536. formal_to_actual,
  1537. args,
  1538. callable_name,
  1539. object_type,
  1540. context,
  1541. )
  1542. callee = callee.copy_modified(ret_type=new_ret_type)
  1543. return callee.ret_type, callee
  1544. def can_return_none(self, type: TypeInfo, attr_name: str) -> bool:
  1545. """Is the given attribute a method with a None-compatible return type?
  1546. Overloads are only checked if there is an implementation.
  1547. """
  1548. if not state.strict_optional:
  1549. # If strict-optional is not set, is_subtype(NoneType(), T) is always True.
  1550. # So, we cannot do anything useful here in that case.
  1551. return False
  1552. for base in type.mro:
  1553. symnode = base.names.get(attr_name)
  1554. if symnode is None:
  1555. continue
  1556. node = symnode.node
  1557. if isinstance(node, OverloadedFuncDef):
  1558. node = node.impl
  1559. if isinstance(node, Decorator):
  1560. node = node.func
  1561. if isinstance(node, FuncDef):
  1562. if node.type is not None:
  1563. assert isinstance(node.type, CallableType)
  1564. return is_subtype(NoneType(), node.type.ret_type)
  1565. return False
  1566. def analyze_type_type_callee(self, item: ProperType, context: Context) -> Type:
  1567. """Analyze the callee X in X(...) where X is Type[item].
  1568. Return a Y that we can pass to check_call(Y, ...).
  1569. """
  1570. if isinstance(item, AnyType):
  1571. return AnyType(TypeOfAny.from_another_any, source_any=item)
  1572. if isinstance(item, Instance):
  1573. res = type_object_type(item.type, self.named_type)
  1574. if isinstance(res, CallableType):
  1575. res = res.copy_modified(from_type_type=True)
  1576. expanded = expand_type_by_instance(res, item)
  1577. if isinstance(expanded, CallableType):
  1578. # Callee of the form Type[...] should never be generic, only
  1579. # proper class objects can be.
  1580. expanded = expanded.copy_modified(variables=[])
  1581. return expanded
  1582. if isinstance(item, UnionType):
  1583. return UnionType(
  1584. [
  1585. self.analyze_type_type_callee(get_proper_type(tp), context)
  1586. for tp in item.relevant_items()
  1587. ],
  1588. item.line,
  1589. )
  1590. if isinstance(item, TypeVarType):
  1591. # Pretend we're calling the typevar's upper bound,
  1592. # i.e. its constructor (a poor approximation for reality,
  1593. # but better than AnyType...), but replace the return type
  1594. # with typevar.
  1595. callee = self.analyze_type_type_callee(get_proper_type(item.upper_bound), context)
  1596. callee = get_proper_type(callee)
  1597. if isinstance(callee, CallableType):
  1598. callee = callee.copy_modified(ret_type=item)
  1599. elif isinstance(callee, Overloaded):
  1600. callee = Overloaded([c.copy_modified(ret_type=item) for c in callee.items])
  1601. return callee
  1602. # We support Type of namedtuples but not of tuples in general
  1603. if isinstance(item, TupleType) and tuple_fallback(item).type.fullname != "builtins.tuple":
  1604. return self.analyze_type_type_callee(tuple_fallback(item), context)
  1605. self.msg.unsupported_type_type(item, context)
  1606. return AnyType(TypeOfAny.from_error)
  1607. def infer_arg_types_in_empty_context(self, args: list[Expression]) -> list[Type]:
  1608. """Infer argument expression types in an empty context.
  1609. In short, we basically recurse on each argument without considering
  1610. in what context the argument was called.
  1611. """
  1612. res: list[Type] = []
  1613. for arg in args:
  1614. arg_type = self.accept(arg)
  1615. if has_erased_component(arg_type):
  1616. res.append(NoneType())
  1617. else:
  1618. res.append(arg_type)
  1619. return res
  1620. def infer_more_unions_for_recursive_type(self, type_context: Type) -> bool:
  1621. """Adjust type inference of unions if type context has a recursive type.
  1622. Return the old state. The caller must assign it to type_state.infer_unions
  1623. afterwards.
  1624. This is a hack to better support inference for recursive types.
  1625. Note: This is performance-sensitive and must not be a context manager
  1626. until mypyc supports them better.
  1627. """
  1628. old = type_state.infer_unions
  1629. if has_recursive_types(type_context):
  1630. type_state.infer_unions = True
  1631. return old
  1632. def infer_arg_types_in_context(
  1633. self,
  1634. callee: CallableType,
  1635. args: list[Expression],
  1636. arg_kinds: list[ArgKind],
  1637. formal_to_actual: list[list[int]],
  1638. ) -> list[Type]:
  1639. """Infer argument expression types using a callable type as context.
  1640. For example, if callee argument 2 has type List[int], infer the
  1641. argument expression with List[int] type context.
  1642. Returns the inferred types of *actual arguments*.
  1643. """
  1644. res: list[Type | None] = [None] * len(args)
  1645. for i, actuals in enumerate(formal_to_actual):
  1646. for ai in actuals:
  1647. if not arg_kinds[ai].is_star():
  1648. arg_type = callee.arg_types[i]
  1649. # When the outer context for a function call is known to be recursive,
  1650. # we solve type constraints inferred from arguments using unions instead
  1651. # of joins. This is a bit arbitrary, but in practice it works for most
  1652. # cases. A cleaner alternative would be to switch to single bin type
  1653. # inference, but this is a lot of work.
  1654. old = self.infer_more_unions_for_recursive_type(arg_type)
  1655. res[ai] = self.accept(args[ai], arg_type)
  1656. # We need to manually restore union inference state, ugh.
  1657. type_state.infer_unions = old
  1658. # Fill in the rest of the argument types.
  1659. for i, t in enumerate(res):
  1660. if not t:
  1661. res[i] = self.accept(args[i])
  1662. assert all(tp is not None for tp in res)
  1663. return cast(List[Type], res)
  1664. def infer_function_type_arguments_using_context(
  1665. self, callable: CallableType, error_context: Context
  1666. ) -> CallableType:
  1667. """Unify callable return type to type context to infer type vars.
  1668. For example, if the return type is set[t] where 't' is a type variable
  1669. of callable, and if the context is set[int], return callable modified
  1670. by substituting 't' with 'int'.
  1671. """
  1672. ctx = self.type_context[-1]
  1673. if not ctx:
  1674. return callable
  1675. # The return type may have references to type metavariables that
  1676. # we are inferring right now. We must consider them as indeterminate
  1677. # and they are not potential results; thus we replace them with the
  1678. # special ErasedType type. On the other hand, class type variables are
  1679. # valid results.
  1680. erased_ctx = replace_meta_vars(ctx, ErasedType())
  1681. ret_type = callable.ret_type
  1682. if is_optional(ret_type) and is_optional(ctx):
  1683. # If both the context and the return type are optional, unwrap the optional,
  1684. # since in 99% cases this is what a user expects. In other words, we replace
  1685. # Optional[T] <: Optional[int]
  1686. # with
  1687. # T <: int
  1688. # while the former would infer T <: Optional[int].
  1689. ret_type = remove_optional(ret_type)
  1690. erased_ctx = remove_optional(erased_ctx)
  1691. #
  1692. # TODO: Instead of this hack and the one below, we need to use outer and
  1693. # inner contexts at the same time. This is however not easy because of two
  1694. # reasons:
  1695. # * We need to support constraints like [1 <: 2, 2 <: X], i.e. with variables
  1696. # on both sides. (This is not too hard.)
  1697. # * We need to update all the inference "infrastructure", so that all
  1698. # variables in an expression are inferred at the same time.
  1699. # (And this is hard, also we need to be careful with lambdas that require
  1700. # two passes.)
  1701. if isinstance(ret_type, TypeVarType):
  1702. # Another special case: the return type is a type variable. If it's unrestricted,
  1703. # we could infer a too general type for the type variable if we use context,
  1704. # and this could result in confusing and spurious type errors elsewhere.
  1705. #
  1706. # So we give up and just use function arguments for type inference, with just two
  1707. # exceptions:
  1708. #
  1709. # 1. If the context is a generic instance type, actually use it as context, as
  1710. # this *seems* to usually be the reasonable thing to do.
  1711. #
  1712. # See also github issues #462 and #360.
  1713. #
  1714. # 2. If the context is some literal type, we want to "propagate" that information
  1715. # down so that we infer a more precise type for literal expressions. For example,
  1716. # the expression `3` normally has an inferred type of `builtins.int`: but if it's
  1717. # in a literal context like below, we want it to infer `Literal[3]` instead.
  1718. #
  1719. # def expects_literal(x: Literal[3]) -> None: pass
  1720. # def identity(x: T) -> T: return x
  1721. #
  1722. # expects_literal(identity(3)) # Should type-check
  1723. if not is_generic_instance(ctx) and not is_literal_type_like(ctx):
  1724. return callable.copy_modified()
  1725. args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
  1726. # Only substitute non-Uninhabited and non-erased types.
  1727. new_args: list[Type | None] = []
  1728. for arg in args:
  1729. if has_uninhabited_component(arg) or has_erased_component(arg):
  1730. new_args.append(None)
  1731. else:
  1732. new_args.append(arg)
  1733. # Don't show errors after we have only used the outer context for inference.
  1734. # We will use argument context to infer more variables.
  1735. return self.apply_generic_arguments(
  1736. callable, new_args, error_context, skip_unsatisfied=True
  1737. )
  1738. def infer_function_type_arguments(
  1739. self,
  1740. callee_type: CallableType,
  1741. args: list[Expression],
  1742. arg_kinds: list[ArgKind],
  1743. formal_to_actual: list[list[int]],
  1744. context: Context,
  1745. ) -> CallableType:
  1746. """Infer the type arguments for a generic callee type.
  1747. Infer based on the types of arguments.
  1748. Return a derived callable type that has the arguments applied.
  1749. """
  1750. if self.chk.in_checked_function():
  1751. # Disable type errors during type inference. There may be errors
  1752. # due to partial available context information at this time, but
  1753. # these errors can be safely ignored as the arguments will be
  1754. # inferred again later.
  1755. with self.msg.filter_errors():
  1756. arg_types = self.infer_arg_types_in_context(
  1757. callee_type, args, arg_kinds, formal_to_actual
  1758. )
  1759. arg_pass_nums = self.get_arg_infer_passes(
  1760. callee_type.arg_types, formal_to_actual, len(args)
  1761. )
  1762. pass1_args: list[Type | None] = []
  1763. for i, arg in enumerate(arg_types):
  1764. if arg_pass_nums[i] > 1:
  1765. pass1_args.append(None)
  1766. else:
  1767. pass1_args.append(arg)
  1768. inferred_args = infer_function_type_arguments(
  1769. callee_type,
  1770. pass1_args,
  1771. arg_kinds,
  1772. formal_to_actual,
  1773. context=self.argument_infer_context(),
  1774. strict=self.chk.in_checked_function(),
  1775. )
  1776. if 2 in arg_pass_nums:
  1777. # Second pass of type inference.
  1778. (callee_type, inferred_args) = self.infer_function_type_arguments_pass2(
  1779. callee_type, args, arg_kinds, formal_to_actual, inferred_args, context
  1780. )
  1781. if (
  1782. callee_type.special_sig == "dict"
  1783. and len(inferred_args) == 2
  1784. and (ARG_NAMED in arg_kinds or ARG_STAR2 in arg_kinds)
  1785. ):
  1786. # HACK: Infer str key type for dict(...) with keyword args. The type system
  1787. # can't represent this so we special case it, as this is a pretty common
  1788. # thing. This doesn't quite work with all possible subclasses of dict
  1789. # if they shuffle type variables around, as we assume that there is a 1-1
  1790. # correspondence with dict type variables. This is a marginal issue and
  1791. # a little tricky to fix so it's left unfixed for now.
  1792. first_arg = get_proper_type(inferred_args[0])
  1793. if isinstance(first_arg, (NoneType, UninhabitedType)):
  1794. inferred_args[0] = self.named_type("builtins.str")
  1795. elif not first_arg or not is_subtype(self.named_type("builtins.str"), first_arg):
  1796. self.chk.fail(message_registry.KEYWORD_ARGUMENT_REQUIRES_STR_KEY_TYPE, context)
  1797. if self.chk.options.new_type_inference and any(
  1798. a is None
  1799. or isinstance(get_proper_type(a), UninhabitedType)
  1800. or set(get_type_vars(a)) & set(callee_type.variables)
  1801. for a in inferred_args
  1802. ):
  1803. # If the regular two-phase inference didn't work, try inferring type
  1804. # variables while allowing for polymorphic solutions, i.e. for solutions
  1805. # potentially involving free variables.
  1806. # TODO: support the similar inference for return type context.
  1807. poly_inferred_args = infer_function_type_arguments(
  1808. callee_type,
  1809. arg_types,
  1810. arg_kinds,
  1811. formal_to_actual,
  1812. context=self.argument_infer_context(),
  1813. strict=self.chk.in_checked_function(),
  1814. allow_polymorphic=True,
  1815. )
  1816. for i, pa in enumerate(get_proper_types(poly_inferred_args)):
  1817. if isinstance(pa, (NoneType, UninhabitedType)) or has_erased_component(pa):
  1818. # Indicate that free variables should not be applied in the call below.
  1819. poly_inferred_args[i] = None
  1820. poly_callee_type = self.apply_generic_arguments(
  1821. callee_type, poly_inferred_args, context
  1822. )
  1823. yes_vars = poly_callee_type.variables
  1824. no_vars = {v for v in callee_type.variables if v not in poly_callee_type.variables}
  1825. if not set(get_type_vars(poly_callee_type)) & no_vars:
  1826. # Try applying inferred polymorphic type if possible, e.g. Callable[[T], T] can
  1827. # be interpreted as def [T] (T) -> T, but dict[T, T] cannot be expressed.
  1828. applied = apply_poly(poly_callee_type, yes_vars)
  1829. if applied is not None and poly_inferred_args != [UninhabitedType()] * len(
  1830. poly_inferred_args
  1831. ):
  1832. freeze_all_type_vars(applied)
  1833. return applied
  1834. # If it didn't work, erase free variables as <nothing>, to avoid confusing errors.
  1835. inferred_args = [
  1836. expand_type(a, {v.id: UninhabitedType() for v in callee_type.variables})
  1837. if a is not None
  1838. else None
  1839. for a in inferred_args
  1840. ]
  1841. else:
  1842. # In dynamically typed functions use implicit 'Any' types for
  1843. # type variables.
  1844. inferred_args = [AnyType(TypeOfAny.unannotated)] * len(callee_type.variables)
  1845. return self.apply_inferred_arguments(callee_type, inferred_args, context)
  1846. def infer_function_type_arguments_pass2(
  1847. self,
  1848. callee_type: CallableType,
  1849. args: list[Expression],
  1850. arg_kinds: list[ArgKind],
  1851. formal_to_actual: list[list[int]],
  1852. old_inferred_args: Sequence[Type | None],
  1853. context: Context,
  1854. ) -> tuple[CallableType, list[Type | None]]:
  1855. """Perform second pass of generic function type argument inference.
  1856. The second pass is needed for arguments with types such as Callable[[T], S],
  1857. where both T and S are type variables, when the actual argument is a
  1858. lambda with inferred types. The idea is to infer the type variable T
  1859. in the first pass (based on the types of other arguments). This lets
  1860. us infer the argument and return type of the lambda expression and
  1861. thus also the type variable S in this second pass.
  1862. Return (the callee with type vars applied, inferred actual arg types).
  1863. """
  1864. # None or erased types in inferred types mean that there was not enough
  1865. # information to infer the argument. Replace them with None values so
  1866. # that they are not applied yet below.
  1867. inferred_args = list(old_inferred_args)
  1868. for i, arg in enumerate(get_proper_types(inferred_args)):
  1869. if isinstance(arg, (NoneType, UninhabitedType)) or has_erased_component(arg):
  1870. inferred_args[i] = None
  1871. callee_type = self.apply_generic_arguments(callee_type, inferred_args, context)
  1872. arg_types = self.infer_arg_types_in_context(callee_type, args, arg_kinds, formal_to_actual)
  1873. inferred_args = infer_function_type_arguments(
  1874. callee_type,
  1875. arg_types,
  1876. arg_kinds,
  1877. formal_to_actual,
  1878. context=self.argument_infer_context(),
  1879. )
  1880. return callee_type, inferred_args
  1881. def argument_infer_context(self) -> ArgumentInferContext:
  1882. return ArgumentInferContext(
  1883. self.chk.named_type("typing.Mapping"), self.chk.named_type("typing.Iterable")
  1884. )
  1885. def get_arg_infer_passes(
  1886. self, arg_types: list[Type], formal_to_actual: list[list[int]], num_actuals: int
  1887. ) -> list[int]:
  1888. """Return pass numbers for args for two-pass argument type inference.
  1889. For each actual, the pass number is either 1 (first pass) or 2 (second
  1890. pass).
  1891. Two-pass argument type inference primarily lets us infer types of
  1892. lambdas more effectively.
  1893. """
  1894. res = [1] * num_actuals
  1895. for i, arg in enumerate(arg_types):
  1896. if arg.accept(ArgInferSecondPassQuery()):
  1897. for j in formal_to_actual[i]:
  1898. res[j] = 2
  1899. return res
  1900. def apply_inferred_arguments(
  1901. self, callee_type: CallableType, inferred_args: Sequence[Type | None], context: Context
  1902. ) -> CallableType:
  1903. """Apply inferred values of type arguments to a generic function.
  1904. Inferred_args contains the values of function type arguments.
  1905. """
  1906. # Report error if some of the variables could not be solved. In that
  1907. # case assume that all variables have type Any to avoid extra
  1908. # bogus error messages.
  1909. for i, inferred_type in enumerate(inferred_args):
  1910. if not inferred_type or has_erased_component(inferred_type):
  1911. # Could not infer a non-trivial type for a type variable.
  1912. self.msg.could_not_infer_type_arguments(callee_type, i + 1, context)
  1913. inferred_args = [AnyType(TypeOfAny.from_error)] * len(inferred_args)
  1914. # Apply the inferred types to the function type. In this case the
  1915. # return type must be CallableType, since we give the right number of type
  1916. # arguments.
  1917. return self.apply_generic_arguments(callee_type, inferred_args, context)
  1918. def check_argument_count(
  1919. self,
  1920. callee: CallableType,
  1921. actual_types: list[Type],
  1922. actual_kinds: list[ArgKind],
  1923. actual_names: Sequence[str | None] | None,
  1924. formal_to_actual: list[list[int]],
  1925. context: Context | None,
  1926. object_type: Type | None = None,
  1927. callable_name: str | None = None,
  1928. ) -> bool:
  1929. """Check that there is a value for all required arguments to a function.
  1930. Also check that there are no duplicate values for arguments. Report found errors
  1931. using 'messages' if it's not None. If 'messages' is given, 'context' must also be given.
  1932. Return False if there were any errors. Otherwise return True
  1933. """
  1934. if context is None:
  1935. # Avoid "is None" checks
  1936. context = TempNode(AnyType(TypeOfAny.special_form))
  1937. # TODO(jukka): We could return as soon as we find an error if messages is None.
  1938. # Collect dict of all actual arguments matched to formal arguments, with occurrence count
  1939. all_actuals: dict[int, int] = {}
  1940. for actuals in formal_to_actual:
  1941. for a in actuals:
  1942. all_actuals[a] = all_actuals.get(a, 0) + 1
  1943. ok, is_unexpected_arg_error = self.check_for_extra_actual_arguments(
  1944. callee, actual_types, actual_kinds, actual_names, all_actuals, context
  1945. )
  1946. # Check for too many or few values for formals.
  1947. for i, kind in enumerate(callee.arg_kinds):
  1948. if kind.is_required() and not formal_to_actual[i] and not is_unexpected_arg_error:
  1949. # No actual for a mandatory formal
  1950. if kind.is_positional():
  1951. self.msg.too_few_arguments(callee, context, actual_names)
  1952. if object_type and callable_name and "." in callable_name:
  1953. self.missing_classvar_callable_note(object_type, callable_name, context)
  1954. else:
  1955. argname = callee.arg_names[i] or "?"
  1956. self.msg.missing_named_argument(callee, context, argname)
  1957. ok = False
  1958. elif not kind.is_star() and is_duplicate_mapping(
  1959. formal_to_actual[i], actual_types, actual_kinds
  1960. ):
  1961. if self.chk.in_checked_function() or isinstance(
  1962. get_proper_type(actual_types[formal_to_actual[i][0]]), TupleType
  1963. ):
  1964. self.msg.duplicate_argument_value(callee, i, context)
  1965. ok = False
  1966. elif (
  1967. kind.is_named()
  1968. and formal_to_actual[i]
  1969. and actual_kinds[formal_to_actual[i][0]] not in [nodes.ARG_NAMED, nodes.ARG_STAR2]
  1970. ):
  1971. # Positional argument when expecting a keyword argument.
  1972. self.msg.too_many_positional_arguments(callee, context)
  1973. ok = False
  1974. return ok
  1975. def check_for_extra_actual_arguments(
  1976. self,
  1977. callee: CallableType,
  1978. actual_types: list[Type],
  1979. actual_kinds: list[ArgKind],
  1980. actual_names: Sequence[str | None] | None,
  1981. all_actuals: dict[int, int],
  1982. context: Context,
  1983. ) -> tuple[bool, bool]:
  1984. """Check for extra actual arguments.
  1985. Return tuple (was everything ok,
  1986. was there an extra keyword argument error [used to avoid duplicate errors]).
  1987. """
  1988. is_unexpected_arg_error = False # Keep track of errors to avoid duplicate errors
  1989. ok = True # False if we've found any error
  1990. for i, kind in enumerate(actual_kinds):
  1991. if (
  1992. i not in all_actuals
  1993. and
  1994. # We accept the other iterables than tuple (including Any)
  1995. # as star arguments because they could be empty, resulting no arguments.
  1996. (kind != nodes.ARG_STAR or is_non_empty_tuple(actual_types[i]))
  1997. and
  1998. # Accept all types for double-starred arguments, because they could be empty
  1999. # dictionaries and we can't tell it from their types
  2000. kind != nodes.ARG_STAR2
  2001. ):
  2002. # Extra actual: not matched by a formal argument.
  2003. ok = False
  2004. if kind != nodes.ARG_NAMED:
  2005. self.msg.too_many_arguments(callee, context)
  2006. else:
  2007. assert actual_names, "Internal error: named kinds without names given"
  2008. act_name = actual_names[i]
  2009. assert act_name is not None
  2010. act_type = actual_types[i]
  2011. self.msg.unexpected_keyword_argument(callee, act_name, act_type, context)
  2012. is_unexpected_arg_error = True
  2013. elif (
  2014. kind == nodes.ARG_STAR and nodes.ARG_STAR not in callee.arg_kinds
  2015. ) or kind == nodes.ARG_STAR2:
  2016. actual_type = get_proper_type(actual_types[i])
  2017. if isinstance(actual_type, (TupleType, TypedDictType)):
  2018. if all_actuals.get(i, 0) < len(actual_type.items):
  2019. # Too many tuple/dict items as some did not match.
  2020. if kind != nodes.ARG_STAR2 or not isinstance(actual_type, TypedDictType):
  2021. self.msg.too_many_arguments(callee, context)
  2022. else:
  2023. self.msg.too_many_arguments_from_typed_dict(
  2024. callee, actual_type, context
  2025. )
  2026. is_unexpected_arg_error = True
  2027. ok = False
  2028. # *args/**kwargs can be applied even if the function takes a fixed
  2029. # number of positional arguments. This may succeed at runtime.
  2030. return ok, is_unexpected_arg_error
  2031. def missing_classvar_callable_note(
  2032. self, object_type: Type, callable_name: str, context: Context
  2033. ) -> None:
  2034. if isinstance(object_type, ProperType) and isinstance(object_type, Instance):
  2035. _, var_name = callable_name.rsplit(".", maxsplit=1)
  2036. node = object_type.type.get(var_name)
  2037. if node is not None and isinstance(node.node, Var):
  2038. if not node.node.is_inferred and not node.node.is_classvar:
  2039. self.msg.note(
  2040. f'"{var_name}" is considered instance variable,'
  2041. " to make it class variable use ClassVar[...]",
  2042. context,
  2043. )
  2044. def check_argument_types(
  2045. self,
  2046. arg_types: list[Type],
  2047. arg_kinds: list[ArgKind],
  2048. args: list[Expression],
  2049. callee: CallableType,
  2050. formal_to_actual: list[list[int]],
  2051. context: Context,
  2052. check_arg: ArgChecker | None = None,
  2053. object_type: Type | None = None,
  2054. ) -> None:
  2055. """Check argument types against a callable type.
  2056. Report errors if the argument types are not compatible.
  2057. The check_call docstring describes some of the arguments.
  2058. """
  2059. check_arg = check_arg or self.check_arg
  2060. # Keep track of consumed tuple *arg items.
  2061. mapper = ArgTypeExpander(self.argument_infer_context())
  2062. for i, actuals in enumerate(formal_to_actual):
  2063. orig_callee_arg_type = get_proper_type(callee.arg_types[i])
  2064. # Checking the case that we have more than one item but the first argument
  2065. # is an unpack, so this would be something like:
  2066. # [Tuple[Unpack[Ts]], int]
  2067. #
  2068. # In this case we have to check everything together, we do this by re-unifying
  2069. # the suffices to the tuple, e.g. a single actual like
  2070. # Tuple[Unpack[Ts], int]
  2071. expanded_tuple = False
  2072. if len(actuals) > 1:
  2073. first_actual_arg_type = get_proper_type(arg_types[actuals[0]])
  2074. if (
  2075. isinstance(first_actual_arg_type, TupleType)
  2076. and len(first_actual_arg_type.items) == 1
  2077. and isinstance(first_actual_arg_type.items[0], UnpackType)
  2078. ):
  2079. # TODO: use walrus operator
  2080. actual_types = [first_actual_arg_type.items[0]] + [
  2081. arg_types[a] for a in actuals[1:]
  2082. ]
  2083. actual_kinds = [nodes.ARG_STAR] + [nodes.ARG_POS] * (len(actuals) - 1)
  2084. assert isinstance(orig_callee_arg_type, TupleType)
  2085. assert orig_callee_arg_type.items
  2086. callee_arg_types = orig_callee_arg_type.items
  2087. callee_arg_kinds = [nodes.ARG_STAR] + [nodes.ARG_POS] * (
  2088. len(orig_callee_arg_type.items) - 1
  2089. )
  2090. expanded_tuple = True
  2091. if not expanded_tuple:
  2092. actual_types = [arg_types[a] for a in actuals]
  2093. actual_kinds = [arg_kinds[a] for a in actuals]
  2094. if isinstance(orig_callee_arg_type, UnpackType):
  2095. unpacked_type = get_proper_type(orig_callee_arg_type.type)
  2096. if isinstance(unpacked_type, TupleType):
  2097. inner_unpack_index = find_unpack_in_list(unpacked_type.items)
  2098. if inner_unpack_index is None:
  2099. callee_arg_types = unpacked_type.items
  2100. callee_arg_kinds = [ARG_POS] * len(actuals)
  2101. else:
  2102. inner_unpack = unpacked_type.items[inner_unpack_index]
  2103. assert isinstance(inner_unpack, UnpackType)
  2104. inner_unpacked_type = get_proper_type(inner_unpack.type)
  2105. # We assume heterogenous tuples are desugared earlier
  2106. assert isinstance(inner_unpacked_type, Instance)
  2107. assert inner_unpacked_type.type.fullname == "builtins.tuple"
  2108. callee_arg_types = (
  2109. unpacked_type.items[:inner_unpack_index]
  2110. + [inner_unpacked_type.args[0]]
  2111. * (len(actuals) - len(unpacked_type.items) + 1)
  2112. + unpacked_type.items[inner_unpack_index + 1 :]
  2113. )
  2114. callee_arg_kinds = [ARG_POS] * len(actuals)
  2115. else:
  2116. assert isinstance(unpacked_type, Instance)
  2117. assert unpacked_type.type.fullname == "builtins.tuple"
  2118. callee_arg_types = [unpacked_type.args[0]] * len(actuals)
  2119. callee_arg_kinds = [ARG_POS] * len(actuals)
  2120. else:
  2121. callee_arg_types = [orig_callee_arg_type] * len(actuals)
  2122. callee_arg_kinds = [callee.arg_kinds[i]] * len(actuals)
  2123. assert len(actual_types) == len(actuals) == len(actual_kinds)
  2124. if len(callee_arg_types) != len(actual_types):
  2125. # TODO: Improve error message
  2126. self.chk.fail("Invalid number of arguments", context)
  2127. continue
  2128. assert len(callee_arg_types) == len(actual_types)
  2129. assert len(callee_arg_types) == len(callee_arg_kinds)
  2130. for actual, actual_type, actual_kind, callee_arg_type, callee_arg_kind in zip(
  2131. actuals, actual_types, actual_kinds, callee_arg_types, callee_arg_kinds
  2132. ):
  2133. if actual_type is None:
  2134. continue # Some kind of error was already reported.
  2135. # Check that a *arg is valid as varargs.
  2136. if actual_kind == nodes.ARG_STAR and not self.is_valid_var_arg(actual_type):
  2137. self.msg.invalid_var_arg(actual_type, context)
  2138. if actual_kind == nodes.ARG_STAR2 and not self.is_valid_keyword_var_arg(
  2139. actual_type
  2140. ):
  2141. is_mapping = is_subtype(
  2142. actual_type, self.chk.named_type("_typeshed.SupportsKeysAndGetItem")
  2143. )
  2144. self.msg.invalid_keyword_var_arg(actual_type, is_mapping, context)
  2145. expanded_actual = mapper.expand_actual_type(
  2146. actual_type, actual_kind, callee.arg_names[i], callee_arg_kind
  2147. )
  2148. check_arg(
  2149. expanded_actual,
  2150. actual_type,
  2151. actual_kind,
  2152. callee_arg_type,
  2153. actual + 1,
  2154. i + 1,
  2155. callee,
  2156. object_type,
  2157. args[actual],
  2158. context,
  2159. )
  2160. def check_arg(
  2161. self,
  2162. caller_type: Type,
  2163. original_caller_type: Type,
  2164. caller_kind: ArgKind,
  2165. callee_type: Type,
  2166. n: int,
  2167. m: int,
  2168. callee: CallableType,
  2169. object_type: Type | None,
  2170. context: Context,
  2171. outer_context: Context,
  2172. ) -> None:
  2173. """Check the type of a single argument in a call."""
  2174. caller_type = get_proper_type(caller_type)
  2175. original_caller_type = get_proper_type(original_caller_type)
  2176. callee_type = get_proper_type(callee_type)
  2177. if isinstance(caller_type, DeletedType):
  2178. self.msg.deleted_as_rvalue(caller_type, context)
  2179. # Only non-abstract non-protocol class can be given where Type[...] is expected...
  2180. elif self.has_abstract_type_part(caller_type, callee_type):
  2181. self.msg.concrete_only_call(callee_type, context)
  2182. elif not is_subtype(caller_type, callee_type, options=self.chk.options):
  2183. code = self.msg.incompatible_argument(
  2184. n,
  2185. m,
  2186. callee,
  2187. original_caller_type,
  2188. caller_kind,
  2189. object_type=object_type,
  2190. context=context,
  2191. outer_context=outer_context,
  2192. )
  2193. self.msg.incompatible_argument_note(
  2194. original_caller_type, callee_type, context, code=code
  2195. )
  2196. if not self.msg.prefer_simple_messages():
  2197. self.chk.check_possible_missing_await(caller_type, callee_type, context)
  2198. def check_overload_call(
  2199. self,
  2200. callee: Overloaded,
  2201. args: list[Expression],
  2202. arg_kinds: list[ArgKind],
  2203. arg_names: Sequence[str | None] | None,
  2204. callable_name: str | None,
  2205. object_type: Type | None,
  2206. context: Context,
  2207. ) -> tuple[Type, Type]:
  2208. """Checks a call to an overloaded function."""
  2209. # Normalize unpacked kwargs before checking the call.
  2210. callee = callee.with_unpacked_kwargs()
  2211. arg_types = self.infer_arg_types_in_empty_context(args)
  2212. # Step 1: Filter call targets to remove ones where the argument counts don't match
  2213. plausible_targets = self.plausible_overload_call_targets(
  2214. arg_types, arg_kinds, arg_names, callee
  2215. )
  2216. # Step 2: If the arguments contain a union, we try performing union math first,
  2217. # instead of picking the first matching overload.
  2218. # This is because picking the first overload often ends up being too greedy:
  2219. # for example, when we have a fallback alternative that accepts an unrestricted
  2220. # typevar. See https://github.com/python/mypy/issues/4063 for related discussion.
  2221. erased_targets: list[CallableType] | None = None
  2222. unioned_result: tuple[Type, Type] | None = None
  2223. union_interrupted = False # did we try all union combinations?
  2224. if any(self.real_union(arg) for arg in arg_types):
  2225. try:
  2226. with self.msg.filter_errors():
  2227. unioned_return = self.union_overload_result(
  2228. plausible_targets,
  2229. args,
  2230. arg_types,
  2231. arg_kinds,
  2232. arg_names,
  2233. callable_name,
  2234. object_type,
  2235. context,
  2236. )
  2237. except TooManyUnions:
  2238. union_interrupted = True
  2239. else:
  2240. # Record if we succeeded. Next we need to see if maybe normal procedure
  2241. # gives a narrower type.
  2242. if unioned_return:
  2243. returns, inferred_types = zip(*unioned_return)
  2244. # Note that we use `combine_function_signatures` instead of just returning
  2245. # a union of inferred callables because for example a call
  2246. # Union[int -> int, str -> str](Union[int, str]) is invalid and
  2247. # we don't want to introduce internal inconsistencies.
  2248. unioned_result = (
  2249. make_simplified_union(list(returns), context.line, context.column),
  2250. self.combine_function_signatures(get_proper_types(inferred_types)),
  2251. )
  2252. # Step 3: We try checking each branch one-by-one.
  2253. inferred_result = self.infer_overload_return_type(
  2254. plausible_targets,
  2255. args,
  2256. arg_types,
  2257. arg_kinds,
  2258. arg_names,
  2259. callable_name,
  2260. object_type,
  2261. context,
  2262. )
  2263. # If any of checks succeed, stop early.
  2264. if inferred_result is not None and unioned_result is not None:
  2265. # Both unioned and direct checks succeeded, choose the more precise type.
  2266. if is_subtype(inferred_result[0], unioned_result[0]) and not isinstance(
  2267. get_proper_type(inferred_result[0]), AnyType
  2268. ):
  2269. return inferred_result
  2270. return unioned_result
  2271. elif unioned_result is not None:
  2272. return unioned_result
  2273. elif inferred_result is not None:
  2274. return inferred_result
  2275. # Step 4: Failure. At this point, we know there is no match. We fall back to trying
  2276. # to find a somewhat plausible overload target using the erased types
  2277. # so we can produce a nice error message.
  2278. #
  2279. # For example, suppose the user passes a value of type 'List[str]' into an
  2280. # overload with signatures f(x: int) -> int and f(x: List[int]) -> List[int].
  2281. #
  2282. # Neither alternative matches, but we can guess the user probably wants the
  2283. # second one.
  2284. erased_targets = self.overload_erased_call_targets(
  2285. plausible_targets, arg_types, arg_kinds, arg_names, args, context
  2286. )
  2287. # Step 5: We try and infer a second-best alternative if possible. If not, fall back
  2288. # to using 'Any'.
  2289. if len(erased_targets) > 0:
  2290. # Pick the first plausible erased target as the fallback
  2291. # TODO: Adjust the error message here to make it clear there was no match.
  2292. # In order to do this, we need to find a clean way of associating
  2293. # a note with whatever error message 'self.check_call' will generate.
  2294. # In particular, the note's line and column numbers need to be the same
  2295. # as the error's.
  2296. target: Type = erased_targets[0]
  2297. else:
  2298. # There was no plausible match: give up
  2299. target = AnyType(TypeOfAny.from_error)
  2300. if not is_operator_method(callable_name):
  2301. code = None
  2302. else:
  2303. code = codes.OPERATOR
  2304. self.msg.no_variant_matches_arguments(callee, arg_types, context, code=code)
  2305. result = self.check_call(
  2306. target,
  2307. args,
  2308. arg_kinds,
  2309. context,
  2310. arg_names,
  2311. callable_name=callable_name,
  2312. object_type=object_type,
  2313. )
  2314. if union_interrupted:
  2315. self.chk.fail(message_registry.TOO_MANY_UNION_COMBINATIONS, context)
  2316. return result
  2317. def plausible_overload_call_targets(
  2318. self,
  2319. arg_types: list[Type],
  2320. arg_kinds: list[ArgKind],
  2321. arg_names: Sequence[str | None] | None,
  2322. overload: Overloaded,
  2323. ) -> list[CallableType]:
  2324. """Returns all overload call targets that having matching argument counts.
  2325. If the given args contains a star-arg (*arg or **kwarg argument), this method
  2326. will ensure all star-arg overloads appear at the start of the list, instead
  2327. of their usual location.
  2328. The only exception is if the starred argument is something like a Tuple or a
  2329. NamedTuple, which has a definitive "shape". If so, we don't move the corresponding
  2330. alternative to the front since we can infer a more precise match using the original
  2331. order."""
  2332. def has_shape(typ: Type) -> bool:
  2333. typ = get_proper_type(typ)
  2334. return isinstance(typ, (TupleType, TypedDictType)) or (
  2335. isinstance(typ, Instance) and typ.type.is_named_tuple
  2336. )
  2337. matches: list[CallableType] = []
  2338. star_matches: list[CallableType] = []
  2339. args_have_var_arg = False
  2340. args_have_kw_arg = False
  2341. for kind, typ in zip(arg_kinds, arg_types):
  2342. if kind == ARG_STAR and not has_shape(typ):
  2343. args_have_var_arg = True
  2344. if kind == ARG_STAR2 and not has_shape(typ):
  2345. args_have_kw_arg = True
  2346. for typ in overload.items:
  2347. formal_to_actual = map_actuals_to_formals(
  2348. arg_kinds, arg_names, typ.arg_kinds, typ.arg_names, lambda i: arg_types[i]
  2349. )
  2350. with self.msg.filter_errors():
  2351. if self.check_argument_count(
  2352. typ, arg_types, arg_kinds, arg_names, formal_to_actual, None
  2353. ):
  2354. if args_have_var_arg and typ.is_var_arg:
  2355. star_matches.append(typ)
  2356. elif args_have_kw_arg and typ.is_kw_arg:
  2357. star_matches.append(typ)
  2358. else:
  2359. matches.append(typ)
  2360. return star_matches + matches
  2361. def infer_overload_return_type(
  2362. self,
  2363. plausible_targets: list[CallableType],
  2364. args: list[Expression],
  2365. arg_types: list[Type],
  2366. arg_kinds: list[ArgKind],
  2367. arg_names: Sequence[str | None] | None,
  2368. callable_name: str | None,
  2369. object_type: Type | None,
  2370. context: Context,
  2371. ) -> tuple[Type, Type] | None:
  2372. """Attempts to find the first matching callable from the given list.
  2373. If a match is found, returns a tuple containing the result type and the inferred
  2374. callee type. (This tuple is meant to be eventually returned by check_call.)
  2375. If multiple targets match due to ambiguous Any parameters, returns (AnyType, AnyType).
  2376. If no targets match, returns None.
  2377. Assumes all of the given targets have argument counts compatible with the caller.
  2378. """
  2379. matches: list[CallableType] = []
  2380. return_types: list[Type] = []
  2381. inferred_types: list[Type] = []
  2382. args_contain_any = any(map(has_any_type, arg_types))
  2383. type_maps: list[dict[Expression, Type]] = []
  2384. for typ in plausible_targets:
  2385. assert self.msg is self.chk.msg
  2386. with self.msg.filter_errors() as w:
  2387. with self.chk.local_type_map() as m:
  2388. ret_type, infer_type = self.check_call(
  2389. callee=typ,
  2390. args=args,
  2391. arg_kinds=arg_kinds,
  2392. arg_names=arg_names,
  2393. context=context,
  2394. callable_name=callable_name,
  2395. object_type=object_type,
  2396. )
  2397. is_match = not w.has_new_errors()
  2398. if is_match:
  2399. # Return early if possible; otherwise record info so we can
  2400. # check for ambiguity due to 'Any' below.
  2401. if not args_contain_any:
  2402. return ret_type, infer_type
  2403. matches.append(typ)
  2404. return_types.append(ret_type)
  2405. inferred_types.append(infer_type)
  2406. type_maps.append(m)
  2407. if not matches:
  2408. return None
  2409. elif any_causes_overload_ambiguity(matches, return_types, arg_types, arg_kinds, arg_names):
  2410. # An argument of type or containing the type 'Any' caused ambiguity.
  2411. # We try returning a precise type if we can. If not, we give up and just return 'Any'.
  2412. if all_same_types(return_types):
  2413. self.chk.store_types(type_maps[0])
  2414. return return_types[0], inferred_types[0]
  2415. elif all_same_types([erase_type(typ) for typ in return_types]):
  2416. self.chk.store_types(type_maps[0])
  2417. return erase_type(return_types[0]), erase_type(inferred_types[0])
  2418. else:
  2419. return self.check_call(
  2420. callee=AnyType(TypeOfAny.special_form),
  2421. args=args,
  2422. arg_kinds=arg_kinds,
  2423. arg_names=arg_names,
  2424. context=context,
  2425. callable_name=callable_name,
  2426. object_type=object_type,
  2427. )
  2428. else:
  2429. # Success! No ambiguity; return the first match.
  2430. self.chk.store_types(type_maps[0])
  2431. return return_types[0], inferred_types[0]
  2432. def overload_erased_call_targets(
  2433. self,
  2434. plausible_targets: list[CallableType],
  2435. arg_types: list[Type],
  2436. arg_kinds: list[ArgKind],
  2437. arg_names: Sequence[str | None] | None,
  2438. args: list[Expression],
  2439. context: Context,
  2440. ) -> list[CallableType]:
  2441. """Returns a list of all targets that match the caller after erasing types.
  2442. Assumes all of the given targets have argument counts compatible with the caller.
  2443. """
  2444. matches: list[CallableType] = []
  2445. for typ in plausible_targets:
  2446. if self.erased_signature_similarity(
  2447. arg_types, arg_kinds, arg_names, args, typ, context
  2448. ):
  2449. matches.append(typ)
  2450. return matches
  2451. def union_overload_result(
  2452. self,
  2453. plausible_targets: list[CallableType],
  2454. args: list[Expression],
  2455. arg_types: list[Type],
  2456. arg_kinds: list[ArgKind],
  2457. arg_names: Sequence[str | None] | None,
  2458. callable_name: str | None,
  2459. object_type: Type | None,
  2460. context: Context,
  2461. level: int = 0,
  2462. ) -> list[tuple[Type, Type]] | None:
  2463. """Accepts a list of overload signatures and attempts to match calls by destructuring
  2464. the first union.
  2465. Return a list of (<return type>, <inferred variant type>) if call succeeds for every
  2466. item of the desctructured union. Returns None if there is no match.
  2467. """
  2468. # Step 1: If we are already too deep, then stop immediately. Otherwise mypy might
  2469. # hang for long time because of a weird overload call. The caller will get
  2470. # the exception and generate an appropriate note message, if needed.
  2471. if level >= MAX_UNIONS:
  2472. raise TooManyUnions
  2473. # Step 2: Find position of the first union in arguments. Return the normal inferred
  2474. # type if no more unions left.
  2475. for idx, typ in enumerate(arg_types):
  2476. if self.real_union(typ):
  2477. break
  2478. else:
  2479. # No unions in args, just fall back to normal inference
  2480. with self.type_overrides_set(args, arg_types):
  2481. res = self.infer_overload_return_type(
  2482. plausible_targets,
  2483. args,
  2484. arg_types,
  2485. arg_kinds,
  2486. arg_names,
  2487. callable_name,
  2488. object_type,
  2489. context,
  2490. )
  2491. if res is not None:
  2492. return [res]
  2493. return None
  2494. # Step 3: Try a direct match before splitting to avoid unnecessary union splits
  2495. # and save performance.
  2496. with self.type_overrides_set(args, arg_types):
  2497. direct = self.infer_overload_return_type(
  2498. plausible_targets,
  2499. args,
  2500. arg_types,
  2501. arg_kinds,
  2502. arg_names,
  2503. callable_name,
  2504. object_type,
  2505. context,
  2506. )
  2507. if direct is not None and not isinstance(get_proper_type(direct[0]), (UnionType, AnyType)):
  2508. # We only return non-unions soon, to avoid greedy match.
  2509. return [direct]
  2510. # Step 4: Split the first remaining union type in arguments into items and
  2511. # try to match each item individually (recursive).
  2512. first_union = get_proper_type(arg_types[idx])
  2513. assert isinstance(first_union, UnionType)
  2514. res_items = []
  2515. for item in first_union.relevant_items():
  2516. new_arg_types = arg_types.copy()
  2517. new_arg_types[idx] = item
  2518. sub_result = self.union_overload_result(
  2519. plausible_targets,
  2520. args,
  2521. new_arg_types,
  2522. arg_kinds,
  2523. arg_names,
  2524. callable_name,
  2525. object_type,
  2526. context,
  2527. level + 1,
  2528. )
  2529. if sub_result is not None:
  2530. res_items.extend(sub_result)
  2531. else:
  2532. # Some item doesn't match, return soon.
  2533. return None
  2534. # Step 5: If splitting succeeded, then filter out duplicate items before returning.
  2535. seen: set[tuple[Type, Type]] = set()
  2536. result = []
  2537. for pair in res_items:
  2538. if pair not in seen:
  2539. seen.add(pair)
  2540. result.append(pair)
  2541. return result
  2542. def real_union(self, typ: Type) -> bool:
  2543. typ = get_proper_type(typ)
  2544. return isinstance(typ, UnionType) and len(typ.relevant_items()) > 1
  2545. @contextmanager
  2546. def type_overrides_set(
  2547. self, exprs: Sequence[Expression], overrides: Sequence[Type]
  2548. ) -> Iterator[None]:
  2549. """Set _temporary_ type overrides for given expressions."""
  2550. assert len(exprs) == len(overrides)
  2551. for expr, typ in zip(exprs, overrides):
  2552. self.type_overrides[expr] = typ
  2553. try:
  2554. yield
  2555. finally:
  2556. for expr in exprs:
  2557. del self.type_overrides[expr]
  2558. def combine_function_signatures(self, types: list[ProperType]) -> AnyType | CallableType:
  2559. """Accepts a list of function signatures and attempts to combine them together into a
  2560. new CallableType consisting of the union of all of the given arguments and return types.
  2561. If there is at least one non-callable type, return Any (this can happen if there is
  2562. an ambiguity because of Any in arguments).
  2563. """
  2564. assert types, "Trying to merge no callables"
  2565. if not all(isinstance(c, CallableType) for c in types):
  2566. return AnyType(TypeOfAny.special_form)
  2567. callables = cast("list[CallableType]", types)
  2568. if len(callables) == 1:
  2569. return callables[0]
  2570. # Note: we are assuming here that if a user uses some TypeVar 'T' in
  2571. # two different functions, they meant for that TypeVar to mean the
  2572. # same thing.
  2573. #
  2574. # This function will make sure that all instances of that TypeVar 'T'
  2575. # refer to the same underlying TypeVarType objects to simplify the union-ing
  2576. # logic below.
  2577. #
  2578. # (If the user did *not* mean for 'T' to be consistently bound to the
  2579. # same type in their overloads, well, their code is probably too
  2580. # confusing and ought to be re-written anyways.)
  2581. callables, variables = merge_typevars_in_callables_by_name(callables)
  2582. new_args: list[list[Type]] = [[] for _ in range(len(callables[0].arg_types))]
  2583. new_kinds = list(callables[0].arg_kinds)
  2584. new_returns: list[Type] = []
  2585. too_complex = False
  2586. for target in callables:
  2587. # We fall back to Callable[..., Union[<returns>]] if the functions do not have
  2588. # the exact same signature. The only exception is if one arg is optional and
  2589. # the other is positional: in that case, we continue unioning (and expect a
  2590. # positional arg).
  2591. # TODO: Enhance the merging logic to handle a wider variety of signatures.
  2592. if len(new_kinds) != len(target.arg_kinds):
  2593. too_complex = True
  2594. break
  2595. for i, (new_kind, target_kind) in enumerate(zip(new_kinds, target.arg_kinds)):
  2596. if new_kind == target_kind:
  2597. continue
  2598. elif new_kind.is_positional() and target_kind.is_positional():
  2599. new_kinds[i] = ARG_POS
  2600. else:
  2601. too_complex = True
  2602. break
  2603. if too_complex:
  2604. break # outer loop
  2605. for i, arg in enumerate(target.arg_types):
  2606. new_args[i].append(arg)
  2607. new_returns.append(target.ret_type)
  2608. union_return = make_simplified_union(new_returns)
  2609. if too_complex:
  2610. any = AnyType(TypeOfAny.special_form)
  2611. return callables[0].copy_modified(
  2612. arg_types=[any, any],
  2613. arg_kinds=[ARG_STAR, ARG_STAR2],
  2614. arg_names=[None, None],
  2615. ret_type=union_return,
  2616. variables=variables,
  2617. implicit=True,
  2618. )
  2619. final_args = []
  2620. for args_list in new_args:
  2621. new_type = make_simplified_union(args_list)
  2622. final_args.append(new_type)
  2623. return callables[0].copy_modified(
  2624. arg_types=final_args,
  2625. arg_kinds=new_kinds,
  2626. ret_type=union_return,
  2627. variables=variables,
  2628. implicit=True,
  2629. )
  2630. def erased_signature_similarity(
  2631. self,
  2632. arg_types: list[Type],
  2633. arg_kinds: list[ArgKind],
  2634. arg_names: Sequence[str | None] | None,
  2635. args: list[Expression],
  2636. callee: CallableType,
  2637. context: Context,
  2638. ) -> bool:
  2639. """Determine whether arguments could match the signature at runtime, after
  2640. erasing types."""
  2641. formal_to_actual = map_actuals_to_formals(
  2642. arg_kinds, arg_names, callee.arg_kinds, callee.arg_names, lambda i: arg_types[i]
  2643. )
  2644. with self.msg.filter_errors():
  2645. if not self.check_argument_count(
  2646. callee, arg_types, arg_kinds, arg_names, formal_to_actual, None
  2647. ):
  2648. # Too few or many arguments -> no match.
  2649. return False
  2650. def check_arg(
  2651. caller_type: Type,
  2652. original_ccaller_type: Type,
  2653. caller_kind: ArgKind,
  2654. callee_type: Type,
  2655. n: int,
  2656. m: int,
  2657. callee: CallableType,
  2658. object_type: Type | None,
  2659. context: Context,
  2660. outer_context: Context,
  2661. ) -> None:
  2662. if not arg_approximate_similarity(caller_type, callee_type):
  2663. # No match -- exit early since none of the remaining work can change
  2664. # the result.
  2665. raise Finished
  2666. try:
  2667. self.check_argument_types(
  2668. arg_types,
  2669. arg_kinds,
  2670. args,
  2671. callee,
  2672. formal_to_actual,
  2673. context=context,
  2674. check_arg=check_arg,
  2675. )
  2676. return True
  2677. except Finished:
  2678. return False
  2679. def apply_generic_arguments(
  2680. self,
  2681. callable: CallableType,
  2682. types: Sequence[Type | None],
  2683. context: Context,
  2684. skip_unsatisfied: bool = False,
  2685. ) -> CallableType:
  2686. """Simple wrapper around mypy.applytype.apply_generic_arguments."""
  2687. return applytype.apply_generic_arguments(
  2688. callable,
  2689. types,
  2690. self.msg.incompatible_typevar_value,
  2691. context,
  2692. skip_unsatisfied=skip_unsatisfied,
  2693. )
  2694. def check_any_type_call(self, args: list[Expression], callee: Type) -> tuple[Type, Type]:
  2695. self.infer_arg_types_in_empty_context(args)
  2696. callee = get_proper_type(callee)
  2697. if isinstance(callee, AnyType):
  2698. return (
  2699. AnyType(TypeOfAny.from_another_any, source_any=callee),
  2700. AnyType(TypeOfAny.from_another_any, source_any=callee),
  2701. )
  2702. else:
  2703. return AnyType(TypeOfAny.special_form), AnyType(TypeOfAny.special_form)
  2704. def check_union_call(
  2705. self,
  2706. callee: UnionType,
  2707. args: list[Expression],
  2708. arg_kinds: list[ArgKind],
  2709. arg_names: Sequence[str | None] | None,
  2710. context: Context,
  2711. ) -> tuple[Type, Type]:
  2712. with self.msg.disable_type_names():
  2713. results = [
  2714. self.check_call(subtype, args, arg_kinds, context, arg_names)
  2715. for subtype in callee.relevant_items()
  2716. ]
  2717. return (make_simplified_union([res[0] for res in results]), callee)
  2718. def visit_member_expr(self, e: MemberExpr, is_lvalue: bool = False) -> Type:
  2719. """Visit member expression (of form e.id)."""
  2720. self.chk.module_refs.update(extract_refexpr_names(e))
  2721. result = self.analyze_ordinary_member_access(e, is_lvalue)
  2722. return self.narrow_type_from_binder(e, result)
  2723. def analyze_ordinary_member_access(self, e: MemberExpr, is_lvalue: bool) -> Type:
  2724. """Analyse member expression or member lvalue."""
  2725. if e.kind is not None:
  2726. # This is a reference to a module attribute.
  2727. return self.analyze_ref_expr(e)
  2728. else:
  2729. # This is a reference to a non-module attribute.
  2730. original_type = self.accept(e.expr, is_callee=self.is_callee)
  2731. base = e.expr
  2732. module_symbol_table = None
  2733. if isinstance(base, RefExpr) and isinstance(base.node, MypyFile):
  2734. module_symbol_table = base.node.names
  2735. if isinstance(base, RefExpr) and isinstance(base.node, Var):
  2736. is_self = base.node.is_self
  2737. else:
  2738. is_self = False
  2739. member_type = analyze_member_access(
  2740. e.name,
  2741. original_type,
  2742. e,
  2743. is_lvalue,
  2744. False,
  2745. False,
  2746. self.msg,
  2747. original_type=original_type,
  2748. chk=self.chk,
  2749. in_literal_context=self.is_literal_context(),
  2750. module_symbol_table=module_symbol_table,
  2751. is_self=is_self,
  2752. )
  2753. return member_type
  2754. def analyze_external_member_access(
  2755. self, member: str, base_type: Type, context: Context
  2756. ) -> Type:
  2757. """Analyse member access that is external, i.e. it cannot
  2758. refer to private definitions. Return the result type.
  2759. """
  2760. # TODO remove; no private definitions in mypy
  2761. return analyze_member_access(
  2762. member,
  2763. base_type,
  2764. context,
  2765. False,
  2766. False,
  2767. False,
  2768. self.msg,
  2769. original_type=base_type,
  2770. chk=self.chk,
  2771. in_literal_context=self.is_literal_context(),
  2772. )
  2773. def is_literal_context(self) -> bool:
  2774. return is_literal_type_like(self.type_context[-1])
  2775. def infer_literal_expr_type(self, value: LiteralValue, fallback_name: str) -> Type:
  2776. """Analyzes the given literal expression and determines if we should be
  2777. inferring an Instance type, a Literal[...] type, or an Instance that
  2778. remembers the original literal. We...
  2779. 1. ...Infer a normal Instance in most circumstances.
  2780. 2. ...Infer a Literal[...] if we're in a literal context. For example, if we
  2781. were analyzing the "3" in "foo(3)" where "foo" has a signature of
  2782. "def foo(Literal[3]) -> None", we'd want to infer that the "3" has a
  2783. type of Literal[3] instead of Instance.
  2784. 3. ...Infer an Instance that remembers the original Literal if we're declaring
  2785. a Final variable with an inferred type -- for example, "bar" in "bar: Final = 3"
  2786. would be assigned an Instance that remembers it originated from a '3'. See
  2787. the comments in Instance's constructor for more details.
  2788. """
  2789. typ = self.named_type(fallback_name)
  2790. if self.is_literal_context():
  2791. return LiteralType(value=value, fallback=typ)
  2792. else:
  2793. return typ.copy_modified(
  2794. last_known_value=LiteralType(
  2795. value=value, fallback=typ, line=typ.line, column=typ.column
  2796. )
  2797. )
  2798. def concat_tuples(self, left: TupleType, right: TupleType) -> TupleType:
  2799. """Concatenate two fixed length tuples."""
  2800. return TupleType(
  2801. items=left.items + right.items, fallback=self.named_type("builtins.tuple")
  2802. )
  2803. def visit_int_expr(self, e: IntExpr) -> Type:
  2804. """Type check an integer literal (trivial)."""
  2805. return self.infer_literal_expr_type(e.value, "builtins.int")
  2806. def visit_str_expr(self, e: StrExpr) -> Type:
  2807. """Type check a string literal (trivial)."""
  2808. return self.infer_literal_expr_type(e.value, "builtins.str")
  2809. def visit_bytes_expr(self, e: BytesExpr) -> Type:
  2810. """Type check a bytes literal (trivial)."""
  2811. return self.infer_literal_expr_type(e.value, "builtins.bytes")
  2812. def visit_float_expr(self, e: FloatExpr) -> Type:
  2813. """Type check a float literal (trivial)."""
  2814. return self.named_type("builtins.float")
  2815. def visit_complex_expr(self, e: ComplexExpr) -> Type:
  2816. """Type check a complex literal."""
  2817. return self.named_type("builtins.complex")
  2818. def visit_ellipsis(self, e: EllipsisExpr) -> Type:
  2819. """Type check '...'."""
  2820. return self.named_type("builtins.ellipsis")
  2821. def visit_op_expr(self, e: OpExpr) -> Type:
  2822. """Type check a binary operator expression."""
  2823. if e.analyzed:
  2824. # It's actually a type expression X | Y.
  2825. return self.accept(e.analyzed)
  2826. if e.op == "and" or e.op == "or":
  2827. return self.check_boolean_op(e, e)
  2828. if e.op == "*" and isinstance(e.left, ListExpr):
  2829. # Expressions of form [...] * e get special type inference.
  2830. return self.check_list_multiply(e)
  2831. if e.op == "%":
  2832. if isinstance(e.left, BytesExpr) and self.chk.options.python_version >= (3, 5):
  2833. return self.strfrm_checker.check_str_interpolation(e.left, e.right)
  2834. if isinstance(e.left, StrExpr):
  2835. return self.strfrm_checker.check_str_interpolation(e.left, e.right)
  2836. left_type = self.accept(e.left)
  2837. proper_left_type = get_proper_type(left_type)
  2838. if isinstance(proper_left_type, TupleType) and e.op == "+":
  2839. left_add_method = proper_left_type.partial_fallback.type.get("__add__")
  2840. if left_add_method and left_add_method.fullname == "builtins.tuple.__add__":
  2841. proper_right_type = get_proper_type(self.accept(e.right))
  2842. if isinstance(proper_right_type, TupleType):
  2843. right_radd_method = proper_right_type.partial_fallback.type.get("__radd__")
  2844. if right_radd_method is None:
  2845. return self.concat_tuples(proper_left_type, proper_right_type)
  2846. if e.op in operators.op_methods:
  2847. method = operators.op_methods[e.op]
  2848. result, method_type = self.check_op(method, left_type, e.right, e, allow_reverse=True)
  2849. e.method_type = method_type
  2850. return result
  2851. else:
  2852. raise RuntimeError(f"Unknown operator {e.op}")
  2853. def visit_comparison_expr(self, e: ComparisonExpr) -> Type:
  2854. """Type check a comparison expression.
  2855. Comparison expressions are type checked consecutive-pair-wise
  2856. That is, 'a < b > c == d' is check as 'a < b and b > c and c == d'
  2857. """
  2858. result: Type | None = None
  2859. sub_result: Type
  2860. # Check each consecutive operand pair and their operator
  2861. for left, right, operator in zip(e.operands, e.operands[1:], e.operators):
  2862. left_type = self.accept(left)
  2863. if operator == "in" or operator == "not in":
  2864. # This case covers both iterables and containers, which have different meanings.
  2865. # For a container, the in operator calls the __contains__ method.
  2866. # For an iterable, the in operator iterates over the iterable, and compares each item one-by-one.
  2867. # We allow `in` for a union of containers and iterables as long as at least one of them matches the
  2868. # type of the left operand, as the operation will simply return False if the union's container/iterator
  2869. # type doesn't match the left operand.
  2870. # If the right operand has partial type, look it up without triggering
  2871. # a "Need type annotation ..." message, as it would be noise.
  2872. right_type = self.find_partial_type_ref_fast_path(right)
  2873. if right_type is None:
  2874. right_type = self.accept(right) # Validate the right operand
  2875. right_type = get_proper_type(right_type)
  2876. item_types: Sequence[Type] = [right_type]
  2877. if isinstance(right_type, UnionType):
  2878. item_types = list(right_type.relevant_items())
  2879. sub_result = self.bool_type()
  2880. container_types: list[Type] = []
  2881. iterable_types: list[Type] = []
  2882. failed_out = False
  2883. encountered_partial_type = False
  2884. for item_type in item_types:
  2885. # Keep track of whether we get type check errors (these won't be reported, they
  2886. # are just to verify whether something is valid typing wise).
  2887. with self.msg.filter_errors(save_filtered_errors=True) as container_errors:
  2888. _, method_type = self.check_method_call_by_name(
  2889. method="__contains__",
  2890. base_type=item_type,
  2891. args=[left],
  2892. arg_kinds=[ARG_POS],
  2893. context=e,
  2894. original_type=right_type,
  2895. )
  2896. # Container item type for strict type overlap checks. Note: we need to only
  2897. # check for nominal type, because a usual "Unsupported operands for in"
  2898. # will be reported for types incompatible with __contains__().
  2899. # See testCustomContainsCheckStrictEquality for an example.
  2900. cont_type = self.chk.analyze_container_item_type(item_type)
  2901. if isinstance(item_type, PartialType):
  2902. # We don't really know if this is an error or not, so just shut up.
  2903. encountered_partial_type = True
  2904. pass
  2905. elif (
  2906. container_errors.has_new_errors()
  2907. and
  2908. # is_valid_var_arg is True for any Iterable
  2909. self.is_valid_var_arg(item_type)
  2910. ):
  2911. # it's not a container, but it is an iterable
  2912. with self.msg.filter_errors(save_filtered_errors=True) as iterable_errors:
  2913. _, itertype = self.chk.analyze_iterable_item_type_without_expression(
  2914. item_type, e
  2915. )
  2916. if iterable_errors.has_new_errors():
  2917. self.msg.add_errors(iterable_errors.filtered_errors())
  2918. failed_out = True
  2919. else:
  2920. method_type = CallableType(
  2921. [left_type],
  2922. [nodes.ARG_POS],
  2923. [None],
  2924. self.bool_type(),
  2925. self.named_type("builtins.function"),
  2926. )
  2927. e.method_types.append(method_type)
  2928. iterable_types.append(itertype)
  2929. elif not container_errors.has_new_errors() and cont_type:
  2930. container_types.append(cont_type)
  2931. e.method_types.append(method_type)
  2932. else:
  2933. self.msg.add_errors(container_errors.filtered_errors())
  2934. failed_out = True
  2935. if not encountered_partial_type and not failed_out:
  2936. iterable_type = UnionType.make_union(iterable_types)
  2937. if not is_subtype(left_type, iterable_type):
  2938. if not container_types:
  2939. self.msg.unsupported_operand_types("in", left_type, right_type, e)
  2940. else:
  2941. container_type = UnionType.make_union(container_types)
  2942. if self.dangerous_comparison(
  2943. left_type,
  2944. container_type,
  2945. original_container=right_type,
  2946. prefer_literal=False,
  2947. ):
  2948. self.msg.dangerous_comparison(
  2949. left_type, container_type, "container", e
  2950. )
  2951. elif operator in operators.op_methods:
  2952. method = operators.op_methods[operator]
  2953. with ErrorWatcher(self.msg.errors) as w:
  2954. sub_result, method_type = self.check_op(
  2955. method, left_type, right, e, allow_reverse=True
  2956. )
  2957. e.method_types.append(method_type)
  2958. # Only show dangerous overlap if there are no other errors. See
  2959. # testCustomEqCheckStrictEquality for an example.
  2960. if not w.has_new_errors() and operator in ("==", "!="):
  2961. right_type = self.accept(right)
  2962. if self.dangerous_comparison(left_type, right_type):
  2963. # Show the most specific literal types possible
  2964. left_type = try_getting_literal(left_type)
  2965. right_type = try_getting_literal(right_type)
  2966. self.msg.dangerous_comparison(left_type, right_type, "equality", e)
  2967. elif operator == "is" or operator == "is not":
  2968. right_type = self.accept(right) # validate the right operand
  2969. sub_result = self.bool_type()
  2970. if self.dangerous_comparison(left_type, right_type):
  2971. # Show the most specific literal types possible
  2972. left_type = try_getting_literal(left_type)
  2973. right_type = try_getting_literal(right_type)
  2974. self.msg.dangerous_comparison(left_type, right_type, "identity", e)
  2975. e.method_types.append(None)
  2976. else:
  2977. raise RuntimeError(f"Unknown comparison operator {operator}")
  2978. # Determine type of boolean-and of result and sub_result
  2979. if result is None:
  2980. result = sub_result
  2981. else:
  2982. result = join.join_types(result, sub_result)
  2983. assert result is not None
  2984. return result
  2985. def find_partial_type_ref_fast_path(self, expr: Expression) -> Type | None:
  2986. """If expression has a partial generic type, return it without additional checks.
  2987. In particular, this does not generate an error about a missing annotation.
  2988. Otherwise, return None.
  2989. """
  2990. if not isinstance(expr, RefExpr):
  2991. return None
  2992. if isinstance(expr.node, Var):
  2993. result = self.analyze_var_ref(expr.node, expr)
  2994. if isinstance(result, PartialType) and result.type is not None:
  2995. self.chk.store_type(expr, fixup_partial_type(result))
  2996. return result
  2997. return None
  2998. def dangerous_comparison(
  2999. self,
  3000. left: Type,
  3001. right: Type,
  3002. original_container: Type | None = None,
  3003. *,
  3004. prefer_literal: bool = True,
  3005. ) -> bool:
  3006. """Check for dangerous non-overlapping comparisons like 42 == 'no'.
  3007. The original_container is the original container type for 'in' checks
  3008. (and None for equality checks).
  3009. Rules:
  3010. * X and None are overlapping even in strict-optional mode. This is to allow
  3011. 'assert x is not None' for x defined as 'x = None # type: str' in class body
  3012. (otherwise mypy itself would have couple dozen errors because of this).
  3013. * Optional[X] and Optional[Y] are non-overlapping if X and Y are
  3014. non-overlapping, although technically None is overlap, it is most
  3015. likely an error.
  3016. * Any overlaps with everything, i.e. always safe.
  3017. * Special case: b'abc' in b'cde' is safe.
  3018. """
  3019. if not self.chk.options.strict_equality:
  3020. return False
  3021. left, right = get_proper_types((left, right))
  3022. # We suppress the error if there is a custom __eq__() method on either
  3023. # side. User defined (or even standard library) classes can define this
  3024. # to return True for comparisons between non-overlapping types.
  3025. if custom_special_method(left, "__eq__") or custom_special_method(right, "__eq__"):
  3026. return False
  3027. if prefer_literal:
  3028. # Also flag non-overlapping literals in situations like:
  3029. # x: Literal['a', 'b']
  3030. # if x == 'c':
  3031. # ...
  3032. left = try_getting_literal(left)
  3033. right = try_getting_literal(right)
  3034. if self.chk.binder.is_unreachable_warning_suppressed():
  3035. # We are inside a function that contains type variables with value restrictions in
  3036. # its signature. In this case we just suppress all strict-equality checks to avoid
  3037. # false positives for code like:
  3038. #
  3039. # T = TypeVar('T', str, int)
  3040. # def f(x: T) -> T:
  3041. # if x == 0:
  3042. # ...
  3043. # return x
  3044. #
  3045. # TODO: find a way of disabling the check only for types resulted from the expansion.
  3046. return False
  3047. if isinstance(left, NoneType) or isinstance(right, NoneType):
  3048. return False
  3049. if isinstance(left, UnionType) and isinstance(right, UnionType):
  3050. left = remove_optional(left)
  3051. right = remove_optional(right)
  3052. left, right = get_proper_types((left, right))
  3053. if (
  3054. original_container
  3055. and has_bytes_component(original_container)
  3056. and has_bytes_component(left)
  3057. ):
  3058. # We need to special case bytes and bytearray, because 97 in b'abc', b'a' in b'abc',
  3059. # b'a' in bytearray(b'abc') etc. all return True (and we want to show the error only
  3060. # if the check can _never_ be True).
  3061. return False
  3062. if isinstance(left, Instance) and isinstance(right, Instance):
  3063. # Special case some builtin implementations of AbstractSet.
  3064. left_name = left.type.fullname
  3065. right_name = right.type.fullname
  3066. if (
  3067. left_name in OVERLAPPING_TYPES_ALLOWLIST
  3068. and right_name in OVERLAPPING_TYPES_ALLOWLIST
  3069. ):
  3070. abstract_set = self.chk.lookup_typeinfo("typing.AbstractSet")
  3071. left = map_instance_to_supertype(left, abstract_set)
  3072. right = map_instance_to_supertype(right, abstract_set)
  3073. return self.dangerous_comparison(left.args[0], right.args[0])
  3074. elif left_name in ("builtins.list", "builtins.tuple") and right_name == left_name:
  3075. return self.dangerous_comparison(left.args[0], right.args[0])
  3076. elif left_name in OVERLAPPING_BYTES_ALLOWLIST and right_name in (
  3077. OVERLAPPING_BYTES_ALLOWLIST
  3078. ):
  3079. return False
  3080. if isinstance(left, LiteralType) and isinstance(right, LiteralType):
  3081. if isinstance(left.value, bool) and isinstance(right.value, bool):
  3082. # Comparing different booleans is not dangerous.
  3083. return False
  3084. return not is_overlapping_types(left, right, ignore_promotions=False)
  3085. def check_method_call_by_name(
  3086. self,
  3087. method: str,
  3088. base_type: Type,
  3089. args: list[Expression],
  3090. arg_kinds: list[ArgKind],
  3091. context: Context,
  3092. original_type: Type | None = None,
  3093. ) -> tuple[Type, Type]:
  3094. """Type check a call to a named method on an object.
  3095. Return tuple (result type, inferred method type). The 'original_type'
  3096. is used for error messages.
  3097. """
  3098. original_type = original_type or base_type
  3099. # Unions are special-cased to allow plugins to act on each element of the union.
  3100. base_type = get_proper_type(base_type)
  3101. if isinstance(base_type, UnionType):
  3102. return self.check_union_method_call_by_name(
  3103. method, base_type, args, arg_kinds, context, original_type
  3104. )
  3105. method_type = analyze_member_access(
  3106. method,
  3107. base_type,
  3108. context,
  3109. False,
  3110. False,
  3111. True,
  3112. self.msg,
  3113. original_type=original_type,
  3114. chk=self.chk,
  3115. in_literal_context=self.is_literal_context(),
  3116. )
  3117. return self.check_method_call(method, base_type, method_type, args, arg_kinds, context)
  3118. def check_union_method_call_by_name(
  3119. self,
  3120. method: str,
  3121. base_type: UnionType,
  3122. args: list[Expression],
  3123. arg_kinds: list[ArgKind],
  3124. context: Context,
  3125. original_type: Type | None = None,
  3126. ) -> tuple[Type, Type]:
  3127. """Type check a call to a named method on an object with union type.
  3128. This essentially checks the call using check_method_call_by_name() for each
  3129. union item and unions the result. We do this to allow plugins to act on
  3130. individual union items.
  3131. """
  3132. res: list[Type] = []
  3133. meth_res: list[Type] = []
  3134. for typ in base_type.relevant_items():
  3135. # Format error messages consistently with
  3136. # mypy.checkmember.analyze_union_member_access().
  3137. with self.msg.disable_type_names():
  3138. item, meth_item = self.check_method_call_by_name(
  3139. method, typ, args, arg_kinds, context, original_type
  3140. )
  3141. res.append(item)
  3142. meth_res.append(meth_item)
  3143. return make_simplified_union(res), make_simplified_union(meth_res)
  3144. def check_method_call(
  3145. self,
  3146. method_name: str,
  3147. base_type: Type,
  3148. method_type: Type,
  3149. args: list[Expression],
  3150. arg_kinds: list[ArgKind],
  3151. context: Context,
  3152. ) -> tuple[Type, Type]:
  3153. """Type check a call to a method with the given name and type on an object.
  3154. Return tuple (result type, inferred method type).
  3155. """
  3156. callable_name = self.method_fullname(base_type, method_name)
  3157. object_type = base_type if callable_name is not None else None
  3158. # Try to refine the method signature using plugin hooks before checking the call.
  3159. method_type = self.transform_callee_type(
  3160. callable_name, method_type, args, arg_kinds, context, object_type=object_type
  3161. )
  3162. return self.check_call(
  3163. method_type,
  3164. args,
  3165. arg_kinds,
  3166. context,
  3167. callable_name=callable_name,
  3168. object_type=base_type,
  3169. )
  3170. def check_op_reversible(
  3171. self,
  3172. op_name: str,
  3173. left_type: Type,
  3174. left_expr: Expression,
  3175. right_type: Type,
  3176. right_expr: Expression,
  3177. context: Context,
  3178. ) -> tuple[Type, Type]:
  3179. def lookup_operator(op_name: str, base_type: Type) -> Type | None:
  3180. """Looks up the given operator and returns the corresponding type,
  3181. if it exists."""
  3182. # This check is an important performance optimization,
  3183. # even though it is mostly a subset of
  3184. # analyze_member_access.
  3185. # TODO: Find a way to remove this call without performance implications.
  3186. if not self.has_member(base_type, op_name):
  3187. return None
  3188. with self.msg.filter_errors() as w:
  3189. member = analyze_member_access(
  3190. name=op_name,
  3191. typ=base_type,
  3192. is_lvalue=False,
  3193. is_super=False,
  3194. is_operator=True,
  3195. original_type=base_type,
  3196. context=context,
  3197. msg=self.msg,
  3198. chk=self.chk,
  3199. in_literal_context=self.is_literal_context(),
  3200. )
  3201. return None if w.has_new_errors() else member
  3202. def lookup_definer(typ: Instance, attr_name: str) -> str | None:
  3203. """Returns the name of the class that contains the actual definition of attr_name.
  3204. So if class A defines foo and class B subclasses A, running
  3205. 'get_class_defined_in(B, "foo")` would return the full name of A.
  3206. However, if B were to override and redefine foo, that method call would
  3207. return the full name of B instead.
  3208. If the attr name is not present in the given class or its MRO, returns None.
  3209. """
  3210. for cls in typ.type.mro:
  3211. if cls.names.get(attr_name):
  3212. return cls.fullname
  3213. return None
  3214. left_type = get_proper_type(left_type)
  3215. right_type = get_proper_type(right_type)
  3216. # If either the LHS or the RHS are Any, we can't really concluding anything
  3217. # about the operation since the Any type may or may not define an
  3218. # __op__ or __rop__ method. So, we punt and return Any instead.
  3219. if isinstance(left_type, AnyType):
  3220. any_type = AnyType(TypeOfAny.from_another_any, source_any=left_type)
  3221. return any_type, any_type
  3222. if isinstance(right_type, AnyType):
  3223. any_type = AnyType(TypeOfAny.from_another_any, source_any=right_type)
  3224. return any_type, any_type
  3225. # STEP 1:
  3226. # We start by getting the __op__ and __rop__ methods, if they exist.
  3227. rev_op_name = operators.reverse_op_methods[op_name]
  3228. left_op = lookup_operator(op_name, left_type)
  3229. right_op = lookup_operator(rev_op_name, right_type)
  3230. # STEP 2a:
  3231. # We figure out in which order Python will call the operator methods. As it
  3232. # turns out, it's not as simple as just trying to call __op__ first and
  3233. # __rop__ second.
  3234. #
  3235. # We store the determined order inside the 'variants_raw' variable,
  3236. # which records tuples containing the method, base type, and the argument.
  3237. if op_name in operators.op_methods_that_shortcut and is_same_type(left_type, right_type):
  3238. # When we do "A() + A()", for example, Python will only call the __add__ method,
  3239. # never the __radd__ method.
  3240. #
  3241. # This is the case even if the __add__ method is completely missing and the __radd__
  3242. # method is defined.
  3243. variants_raw = [(left_op, left_type, right_expr)]
  3244. elif (
  3245. is_subtype(right_type, left_type)
  3246. and isinstance(left_type, Instance)
  3247. and isinstance(right_type, Instance)
  3248. and not (
  3249. left_type.type.alt_promote is not None
  3250. and left_type.type.alt_promote.type is right_type.type
  3251. )
  3252. and lookup_definer(left_type, op_name) != lookup_definer(right_type, rev_op_name)
  3253. ):
  3254. # When we do "A() + B()" where B is a subclass of A, we'll actually try calling
  3255. # B's __radd__ method first, but ONLY if B explicitly defines or overrides the
  3256. # __radd__ method.
  3257. #
  3258. # This mechanism lets subclasses "refine" the expected outcome of the operation, even
  3259. # if they're located on the RHS.
  3260. #
  3261. # As a special case, the alt_promote check makes sure that we don't use the
  3262. # __radd__ method of int if the LHS is a native int type.
  3263. variants_raw = [(right_op, right_type, left_expr), (left_op, left_type, right_expr)]
  3264. else:
  3265. # In all other cases, we do the usual thing and call __add__ first and
  3266. # __radd__ second when doing "A() + B()".
  3267. variants_raw = [(left_op, left_type, right_expr), (right_op, right_type, left_expr)]
  3268. # STEP 3:
  3269. # We now filter out all non-existent operators. The 'variants' list contains
  3270. # all operator methods that are actually present, in the order that Python
  3271. # attempts to invoke them.
  3272. variants = [(op, obj, arg) for (op, obj, arg) in variants_raw if op is not None]
  3273. # STEP 4:
  3274. # We now try invoking each one. If an operation succeeds, end early and return
  3275. # the corresponding result. Otherwise, return the result and errors associated
  3276. # with the first entry.
  3277. errors = []
  3278. results = []
  3279. for method, obj, arg in variants:
  3280. with self.msg.filter_errors(save_filtered_errors=True) as local_errors:
  3281. result = self.check_method_call(op_name, obj, method, [arg], [ARG_POS], context)
  3282. if local_errors.has_new_errors():
  3283. errors.append(local_errors.filtered_errors())
  3284. results.append(result)
  3285. else:
  3286. return result
  3287. # We finish invoking above operators and no early return happens. Therefore,
  3288. # we check if either the LHS or the RHS is Instance and fallbacks to Any,
  3289. # if so, we also return Any
  3290. if (isinstance(left_type, Instance) and left_type.type.fallback_to_any) or (
  3291. isinstance(right_type, Instance) and right_type.type.fallback_to_any
  3292. ):
  3293. any_type = AnyType(TypeOfAny.special_form)
  3294. return any_type, any_type
  3295. # STEP 4b:
  3296. # Sometimes, the variants list is empty. In that case, we fall-back to attempting to
  3297. # call the __op__ method (even though it's missing).
  3298. if not variants:
  3299. with self.msg.filter_errors(save_filtered_errors=True) as local_errors:
  3300. result = self.check_method_call_by_name(
  3301. op_name, left_type, [right_expr], [ARG_POS], context
  3302. )
  3303. if local_errors.has_new_errors():
  3304. errors.append(local_errors.filtered_errors())
  3305. results.append(result)
  3306. else:
  3307. # In theory, we should never enter this case, but it seems
  3308. # we sometimes do, when dealing with Type[...]? E.g. see
  3309. # check-classes.testTypeTypeComparisonWorks.
  3310. #
  3311. # This is probably related to the TODO in lookup_operator(...)
  3312. # up above.
  3313. #
  3314. # TODO: Remove this extra case
  3315. return result
  3316. self.msg.add_errors(errors[0])
  3317. if len(results) == 1:
  3318. return results[0]
  3319. else:
  3320. error_any = AnyType(TypeOfAny.from_error)
  3321. result = error_any, error_any
  3322. return result
  3323. def check_op(
  3324. self,
  3325. method: str,
  3326. base_type: Type,
  3327. arg: Expression,
  3328. context: Context,
  3329. allow_reverse: bool = False,
  3330. ) -> tuple[Type, Type]:
  3331. """Type check a binary operation which maps to a method call.
  3332. Return tuple (result type, inferred operator method type).
  3333. """
  3334. if allow_reverse:
  3335. left_variants = [base_type]
  3336. base_type = get_proper_type(base_type)
  3337. if isinstance(base_type, UnionType):
  3338. left_variants = [
  3339. item for item in flatten_nested_unions(base_type.relevant_items())
  3340. ]
  3341. right_type = self.accept(arg)
  3342. # Step 1: We first try leaving the right arguments alone and destructure
  3343. # just the left ones. (Mypy can sometimes perform some more precise inference
  3344. # if we leave the right operands a union -- see testOperatorWithEmptyListAndSum.)
  3345. all_results = []
  3346. all_inferred = []
  3347. with self.msg.filter_errors() as local_errors:
  3348. for left_possible_type in left_variants:
  3349. result, inferred = self.check_op_reversible(
  3350. op_name=method,
  3351. left_type=left_possible_type,
  3352. left_expr=TempNode(left_possible_type, context=context),
  3353. right_type=right_type,
  3354. right_expr=arg,
  3355. context=context,
  3356. )
  3357. all_results.append(result)
  3358. all_inferred.append(inferred)
  3359. if not local_errors.has_new_errors():
  3360. results_final = make_simplified_union(all_results)
  3361. inferred_final = make_simplified_union(all_inferred)
  3362. return results_final, inferred_final
  3363. # Step 2: If that fails, we try again but also destructure the right argument.
  3364. # This is also necessary to make certain edge cases work -- see
  3365. # testOperatorDoubleUnionInterwovenUnionAdd, for example.
  3366. # Note: We want to pass in the original 'arg' for 'left_expr' and 'right_expr'
  3367. # whenever possible so that plugins and similar things can introspect on the original
  3368. # node if possible.
  3369. #
  3370. # We don't do the same for the base expression because it could lead to weird
  3371. # type inference errors -- e.g. see 'testOperatorDoubleUnionSum'.
  3372. # TODO: Can we use `type_overrides_set()` here?
  3373. right_variants = [(right_type, arg)]
  3374. right_type = get_proper_type(right_type)
  3375. if isinstance(right_type, UnionType):
  3376. right_variants = [
  3377. (item, TempNode(item, context=context))
  3378. for item in flatten_nested_unions(right_type.relevant_items())
  3379. ]
  3380. all_results = []
  3381. all_inferred = []
  3382. with self.msg.filter_errors(save_filtered_errors=True) as local_errors:
  3383. for left_possible_type in left_variants:
  3384. for right_possible_type, right_expr in right_variants:
  3385. result, inferred = self.check_op_reversible(
  3386. op_name=method,
  3387. left_type=left_possible_type,
  3388. left_expr=TempNode(left_possible_type, context=context),
  3389. right_type=right_possible_type,
  3390. right_expr=right_expr,
  3391. context=context,
  3392. )
  3393. all_results.append(result)
  3394. all_inferred.append(inferred)
  3395. if local_errors.has_new_errors():
  3396. self.msg.add_errors(local_errors.filtered_errors())
  3397. # Point any notes to the same location as an existing message.
  3398. err = local_errors.filtered_errors()[-1]
  3399. recent_context = TempNode(NoneType())
  3400. recent_context.line = err.line
  3401. recent_context.column = err.column
  3402. if len(left_variants) >= 2 and len(right_variants) >= 2:
  3403. self.msg.warn_both_operands_are_from_unions(recent_context)
  3404. elif len(left_variants) >= 2:
  3405. self.msg.warn_operand_was_from_union("Left", base_type, context=recent_context)
  3406. elif len(right_variants) >= 2:
  3407. self.msg.warn_operand_was_from_union(
  3408. "Right", right_type, context=recent_context
  3409. )
  3410. # See the comment in 'check_overload_call' for more details on why
  3411. # we call 'combine_function_signature' instead of just unioning the inferred
  3412. # callable types.
  3413. results_final = make_simplified_union(all_results)
  3414. inferred_final = self.combine_function_signatures(get_proper_types(all_inferred))
  3415. return results_final, inferred_final
  3416. else:
  3417. return self.check_method_call_by_name(
  3418. method=method,
  3419. base_type=base_type,
  3420. args=[arg],
  3421. arg_kinds=[ARG_POS],
  3422. context=context,
  3423. )
  3424. def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
  3425. """Type check a boolean operation ('and' or 'or')."""
  3426. # A boolean operation can evaluate to either of the operands.
  3427. # We use the current type context to guide the type inference of of
  3428. # the left operand. We also use the left operand type to guide the type
  3429. # inference of the right operand so that expressions such as
  3430. # '[1] or []' are inferred correctly.
  3431. ctx = self.type_context[-1]
  3432. left_type = self.accept(e.left, ctx)
  3433. expanded_left_type = try_expanding_sum_type_to_union(
  3434. self.accept(e.left, ctx), "builtins.bool"
  3435. )
  3436. assert e.op in ("and", "or") # Checked by visit_op_expr
  3437. if e.right_always:
  3438. left_map: mypy.checker.TypeMap = None
  3439. right_map: mypy.checker.TypeMap = {}
  3440. elif e.right_unreachable:
  3441. left_map, right_map = {}, None
  3442. elif e.op == "and":
  3443. right_map, left_map = self.chk.find_isinstance_check(e.left)
  3444. elif e.op == "or":
  3445. left_map, right_map = self.chk.find_isinstance_check(e.left)
  3446. # If left_map is None then we know mypy considers the left expression
  3447. # to be redundant.
  3448. if (
  3449. codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes
  3450. and left_map is None
  3451. # don't report an error if it's intentional
  3452. and not e.right_always
  3453. ):
  3454. self.msg.redundant_left_operand(e.op, e.left)
  3455. if (
  3456. self.chk.should_report_unreachable_issues()
  3457. and right_map is None
  3458. # don't report an error if it's intentional
  3459. and not e.right_unreachable
  3460. ):
  3461. self.msg.unreachable_right_operand(e.op, e.right)
  3462. # If right_map is None then we know mypy considers the right branch
  3463. # to be unreachable and therefore any errors found in the right branch
  3464. # should be suppressed.
  3465. with self.msg.filter_errors(filter_errors=right_map is None):
  3466. right_type = self.analyze_cond_branch(right_map, e.right, expanded_left_type)
  3467. if left_map is None and right_map is None:
  3468. return UninhabitedType()
  3469. if right_map is None:
  3470. # The boolean expression is statically known to be the left value
  3471. assert left_map is not None
  3472. return left_type
  3473. if left_map is None:
  3474. # The boolean expression is statically known to be the right value
  3475. assert right_map is not None
  3476. return right_type
  3477. if e.op == "and":
  3478. restricted_left_type = false_only(expanded_left_type)
  3479. result_is_left = not expanded_left_type.can_be_true
  3480. elif e.op == "or":
  3481. restricted_left_type = true_only(expanded_left_type)
  3482. result_is_left = not expanded_left_type.can_be_false
  3483. if isinstance(restricted_left_type, UninhabitedType):
  3484. # The left operand can never be the result
  3485. return right_type
  3486. elif result_is_left:
  3487. # The left operand is always the result
  3488. return left_type
  3489. else:
  3490. return make_simplified_union([restricted_left_type, right_type])
  3491. def check_list_multiply(self, e: OpExpr) -> Type:
  3492. """Type check an expression of form '[...] * e'.
  3493. Type inference is special-cased for this common construct.
  3494. """
  3495. right_type = self.accept(e.right)
  3496. if is_subtype(right_type, self.named_type("builtins.int")):
  3497. # Special case: [...] * <int value>. Use the type context of the
  3498. # OpExpr, since the multiplication does not affect the type.
  3499. left_type = self.accept(e.left, type_context=self.type_context[-1])
  3500. else:
  3501. left_type = self.accept(e.left)
  3502. result, method_type = self.check_op("__mul__", left_type, e.right, e)
  3503. e.method_type = method_type
  3504. return result
  3505. def visit_assignment_expr(self, e: AssignmentExpr) -> Type:
  3506. value = self.accept(e.value)
  3507. self.chk.check_assignment(e.target, e.value)
  3508. self.chk.check_final(e)
  3509. self.chk.store_type(e.target, value)
  3510. self.find_partial_type_ref_fast_path(e.target)
  3511. return value
  3512. def visit_unary_expr(self, e: UnaryExpr) -> Type:
  3513. """Type check an unary operation ('not', '-', '+' or '~')."""
  3514. operand_type = self.accept(e.expr)
  3515. op = e.op
  3516. if op == "not":
  3517. result: Type = self.bool_type()
  3518. else:
  3519. method = operators.unary_op_methods[op]
  3520. result, method_type = self.check_method_call_by_name(method, operand_type, [], [], e)
  3521. e.method_type = method_type
  3522. return result
  3523. def visit_index_expr(self, e: IndexExpr) -> Type:
  3524. """Type check an index expression (base[index]).
  3525. It may also represent type application.
  3526. """
  3527. result = self.visit_index_expr_helper(e)
  3528. result = self.narrow_type_from_binder(e, result)
  3529. p_result = get_proper_type(result)
  3530. if (
  3531. self.is_literal_context()
  3532. and isinstance(p_result, Instance)
  3533. and p_result.last_known_value is not None
  3534. ):
  3535. result = p_result.last_known_value
  3536. return result
  3537. def visit_index_expr_helper(self, e: IndexExpr) -> Type:
  3538. if e.analyzed:
  3539. # It's actually a type application.
  3540. return self.accept(e.analyzed)
  3541. left_type = self.accept(e.base)
  3542. return self.visit_index_with_type(left_type, e)
  3543. def visit_index_with_type(
  3544. self, left_type: Type, e: IndexExpr, original_type: ProperType | None = None
  3545. ) -> Type:
  3546. """Analyze type of an index expression for a given type of base expression.
  3547. The 'original_type' is used for error messages (currently used for union types).
  3548. """
  3549. index = e.index
  3550. left_type = get_proper_type(left_type)
  3551. # Visit the index, just to make sure we have a type for it available
  3552. self.accept(index)
  3553. if isinstance(left_type, UnionType):
  3554. original_type = original_type or left_type
  3555. # Don't combine literal types, since we may need them for type narrowing.
  3556. return make_simplified_union(
  3557. [
  3558. self.visit_index_with_type(typ, e, original_type)
  3559. for typ in left_type.relevant_items()
  3560. ],
  3561. contract_literals=False,
  3562. )
  3563. elif isinstance(left_type, TupleType) and self.chk.in_checked_function():
  3564. # Special case for tuples. They return a more specific type when
  3565. # indexed by an integer literal.
  3566. if isinstance(index, SliceExpr):
  3567. return self.visit_tuple_slice_helper(left_type, index)
  3568. ns = self.try_getting_int_literals(index)
  3569. if ns is not None:
  3570. out = []
  3571. for n in ns:
  3572. if n < 0:
  3573. n += len(left_type.items)
  3574. if 0 <= n < len(left_type.items):
  3575. out.append(left_type.items[n])
  3576. else:
  3577. self.chk.fail(message_registry.TUPLE_INDEX_OUT_OF_RANGE, e)
  3578. return AnyType(TypeOfAny.from_error)
  3579. return make_simplified_union(out)
  3580. else:
  3581. return self.nonliteral_tuple_index_helper(left_type, index)
  3582. elif isinstance(left_type, TypedDictType):
  3583. return self.visit_typeddict_index_expr(left_type, e.index)
  3584. elif (
  3585. isinstance(left_type, CallableType)
  3586. and left_type.is_type_obj()
  3587. and left_type.type_object().is_enum
  3588. ):
  3589. return self.visit_enum_index_expr(left_type.type_object(), e.index, e)
  3590. elif isinstance(left_type, TypeVarType) and not self.has_member(
  3591. left_type.upper_bound, "__getitem__"
  3592. ):
  3593. return self.visit_index_with_type(left_type.upper_bound, e, original_type)
  3594. else:
  3595. result, method_type = self.check_method_call_by_name(
  3596. "__getitem__", left_type, [e.index], [ARG_POS], e, original_type=original_type
  3597. )
  3598. e.method_type = method_type
  3599. return result
  3600. def visit_tuple_slice_helper(self, left_type: TupleType, slic: SliceExpr) -> Type:
  3601. begin: Sequence[int | None] = [None]
  3602. end: Sequence[int | None] = [None]
  3603. stride: Sequence[int | None] = [None]
  3604. if slic.begin_index:
  3605. begin_raw = self.try_getting_int_literals(slic.begin_index)
  3606. if begin_raw is None:
  3607. return self.nonliteral_tuple_index_helper(left_type, slic)
  3608. begin = begin_raw
  3609. if slic.end_index:
  3610. end_raw = self.try_getting_int_literals(slic.end_index)
  3611. if end_raw is None:
  3612. return self.nonliteral_tuple_index_helper(left_type, slic)
  3613. end = end_raw
  3614. if slic.stride:
  3615. stride_raw = self.try_getting_int_literals(slic.stride)
  3616. if stride_raw is None:
  3617. return self.nonliteral_tuple_index_helper(left_type, slic)
  3618. stride = stride_raw
  3619. items: list[Type] = []
  3620. for b, e, s in itertools.product(begin, end, stride):
  3621. items.append(left_type.slice(b, e, s))
  3622. return make_simplified_union(items)
  3623. def try_getting_int_literals(self, index: Expression) -> list[int] | None:
  3624. """If the given expression or type corresponds to an int literal
  3625. or a union of int literals, returns a list of the underlying ints.
  3626. Otherwise, returns None.
  3627. Specifically, this function is guaranteed to return a list with
  3628. one or more ints if one one the following is true:
  3629. 1. 'expr' is a IntExpr or a UnaryExpr backed by an IntExpr
  3630. 2. 'typ' is a LiteralType containing an int
  3631. 3. 'typ' is a UnionType containing only LiteralType of ints
  3632. """
  3633. if isinstance(index, IntExpr):
  3634. return [index.value]
  3635. elif isinstance(index, UnaryExpr):
  3636. if index.op == "-":
  3637. operand = index.expr
  3638. if isinstance(operand, IntExpr):
  3639. return [-1 * operand.value]
  3640. typ = get_proper_type(self.accept(index))
  3641. if isinstance(typ, Instance) and typ.last_known_value is not None:
  3642. typ = typ.last_known_value
  3643. if isinstance(typ, LiteralType) and isinstance(typ.value, int):
  3644. return [typ.value]
  3645. if isinstance(typ, UnionType):
  3646. out = []
  3647. for item in get_proper_types(typ.items):
  3648. if isinstance(item, LiteralType) and isinstance(item.value, int):
  3649. out.append(item.value)
  3650. else:
  3651. return None
  3652. return out
  3653. return None
  3654. def nonliteral_tuple_index_helper(self, left_type: TupleType, index: Expression) -> Type:
  3655. self.check_method_call_by_name("__getitem__", left_type, [index], [ARG_POS], context=index)
  3656. # We could return the return type from above, but unions are often better than the join
  3657. union = make_simplified_union(left_type.items)
  3658. if isinstance(index, SliceExpr):
  3659. return self.chk.named_generic_type("builtins.tuple", [union])
  3660. return union
  3661. def visit_typeddict_index_expr(
  3662. self, td_type: TypedDictType, index: Expression, setitem: bool = False
  3663. ) -> Type:
  3664. if isinstance(index, StrExpr):
  3665. key_names = [index.value]
  3666. else:
  3667. typ = get_proper_type(self.accept(index))
  3668. if isinstance(typ, UnionType):
  3669. key_types: list[Type] = list(typ.items)
  3670. else:
  3671. key_types = [typ]
  3672. key_names = []
  3673. for key_type in get_proper_types(key_types):
  3674. if isinstance(key_type, Instance) and key_type.last_known_value is not None:
  3675. key_type = key_type.last_known_value
  3676. if (
  3677. isinstance(key_type, LiteralType)
  3678. and isinstance(key_type.value, str)
  3679. and key_type.fallback.type.fullname != "builtins.bytes"
  3680. ):
  3681. key_names.append(key_type.value)
  3682. else:
  3683. self.msg.typeddict_key_must_be_string_literal(td_type, index)
  3684. return AnyType(TypeOfAny.from_error)
  3685. value_types = []
  3686. for key_name in key_names:
  3687. value_type = td_type.items.get(key_name)
  3688. if value_type is None:
  3689. self.msg.typeddict_key_not_found(td_type, key_name, index, setitem)
  3690. return AnyType(TypeOfAny.from_error)
  3691. else:
  3692. value_types.append(value_type)
  3693. return make_simplified_union(value_types)
  3694. def visit_enum_index_expr(
  3695. self, enum_type: TypeInfo, index: Expression, context: Context
  3696. ) -> Type:
  3697. string_type: Type = self.named_type("builtins.str")
  3698. self.chk.check_subtype(
  3699. self.accept(index),
  3700. string_type,
  3701. context,
  3702. "Enum index should be a string",
  3703. "actual index type",
  3704. )
  3705. return Instance(enum_type, [])
  3706. def visit_cast_expr(self, expr: CastExpr) -> Type:
  3707. """Type check a cast expression."""
  3708. source_type = self.accept(
  3709. expr.expr,
  3710. type_context=AnyType(TypeOfAny.special_form),
  3711. allow_none_return=True,
  3712. always_allow_any=True,
  3713. )
  3714. target_type = expr.type
  3715. options = self.chk.options
  3716. if (
  3717. options.warn_redundant_casts
  3718. and not isinstance(get_proper_type(target_type), AnyType)
  3719. and source_type == target_type
  3720. ):
  3721. self.msg.redundant_cast(target_type, expr)
  3722. if options.disallow_any_unimported and has_any_from_unimported_type(target_type):
  3723. self.msg.unimported_type_becomes_any("Target type of cast", target_type, expr)
  3724. check_for_explicit_any(
  3725. target_type, self.chk.options, self.chk.is_typeshed_stub, self.msg, context=expr
  3726. )
  3727. return target_type
  3728. def visit_assert_type_expr(self, expr: AssertTypeExpr) -> Type:
  3729. source_type = self.accept(
  3730. expr.expr,
  3731. type_context=self.type_context[-1],
  3732. allow_none_return=True,
  3733. always_allow_any=True,
  3734. )
  3735. target_type = expr.type
  3736. proper_source_type = get_proper_type(source_type)
  3737. if (
  3738. isinstance(proper_source_type, mypy.types.Instance)
  3739. and proper_source_type.last_known_value is not None
  3740. ):
  3741. source_type = proper_source_type.last_known_value
  3742. if not is_same_type(source_type, target_type):
  3743. if not self.chk.in_checked_function():
  3744. self.msg.note(
  3745. '"assert_type" expects everything to be "Any" in unchecked functions',
  3746. expr.expr,
  3747. )
  3748. self.msg.assert_type_fail(source_type, target_type, expr)
  3749. return source_type
  3750. def visit_reveal_expr(self, expr: RevealExpr) -> Type:
  3751. """Type check a reveal_type expression."""
  3752. if expr.kind == REVEAL_TYPE:
  3753. assert expr.expr is not None
  3754. revealed_type = self.accept(
  3755. expr.expr, type_context=self.type_context[-1], allow_none_return=True
  3756. )
  3757. if not self.chk.current_node_deferred:
  3758. self.msg.reveal_type(revealed_type, expr.expr)
  3759. if not self.chk.in_checked_function():
  3760. self.msg.note(
  3761. "'reveal_type' always outputs 'Any' in unchecked functions", expr.expr
  3762. )
  3763. return revealed_type
  3764. else:
  3765. # REVEAL_LOCALS
  3766. if not self.chk.current_node_deferred:
  3767. # the RevealExpr contains a local_nodes attribute,
  3768. # calculated at semantic analysis time. Use it to pull out the
  3769. # corresponding subset of variables in self.chk.type_map
  3770. names_to_types = (
  3771. {var_node.name: var_node.type for var_node in expr.local_nodes}
  3772. if expr.local_nodes is not None
  3773. else {}
  3774. )
  3775. self.msg.reveal_locals(names_to_types, expr)
  3776. return NoneType()
  3777. def visit_type_application(self, tapp: TypeApplication) -> Type:
  3778. """Type check a type application (expr[type, ...]).
  3779. There are two different options here, depending on whether expr refers
  3780. to a type alias or directly to a generic class. In the first case we need
  3781. to use a dedicated function typeanal.instantiate_type_alias(). This
  3782. is due to slight differences in how type arguments are applied and checked.
  3783. """
  3784. if isinstance(tapp.expr, RefExpr) and isinstance(tapp.expr.node, TypeAlias):
  3785. # Subscription of a (generic) alias in runtime context, expand the alias.
  3786. item = instantiate_type_alias(
  3787. tapp.expr.node,
  3788. tapp.types,
  3789. self.chk.fail,
  3790. tapp.expr.node.no_args,
  3791. tapp,
  3792. self.chk.options,
  3793. )
  3794. item = get_proper_type(item)
  3795. if isinstance(item, Instance):
  3796. tp = type_object_type(item.type, self.named_type)
  3797. return self.apply_type_arguments_to_callable(tp, item.args, tapp)
  3798. elif isinstance(item, TupleType) and item.partial_fallback.type.is_named_tuple:
  3799. tp = type_object_type(item.partial_fallback.type, self.named_type)
  3800. return self.apply_type_arguments_to_callable(tp, item.partial_fallback.args, tapp)
  3801. elif isinstance(item, TypedDictType):
  3802. return self.typeddict_callable_from_context(item)
  3803. else:
  3804. self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
  3805. return AnyType(TypeOfAny.from_error)
  3806. # Type application of a normal generic class in runtime context.
  3807. # This is typically used as `x = G[int]()`.
  3808. tp = get_proper_type(self.accept(tapp.expr))
  3809. if isinstance(tp, (CallableType, Overloaded)):
  3810. if not tp.is_type_obj():
  3811. self.chk.fail(message_registry.ONLY_CLASS_APPLICATION, tapp)
  3812. return self.apply_type_arguments_to_callable(tp, tapp.types, tapp)
  3813. if isinstance(tp, AnyType):
  3814. return AnyType(TypeOfAny.from_another_any, source_any=tp)
  3815. return AnyType(TypeOfAny.special_form)
  3816. def visit_type_alias_expr(self, alias: TypeAliasExpr) -> Type:
  3817. """Right hand side of a type alias definition.
  3818. It has the same type as if the alias itself was used in a runtime context.
  3819. For example, here:
  3820. A = reveal_type(List[T])
  3821. reveal_type(A)
  3822. both `reveal_type` instances will reveal the same type `def (...) -> builtins.list[Any]`.
  3823. Note that type variables are implicitly substituted with `Any`.
  3824. """
  3825. return self.alias_type_in_runtime_context(alias.node, ctx=alias, alias_definition=True)
  3826. def alias_type_in_runtime_context(
  3827. self, alias: TypeAlias, *, ctx: Context, alias_definition: bool = False
  3828. ) -> Type:
  3829. """Get type of a type alias (could be generic) in a runtime expression.
  3830. Note that this function can be called only if the alias appears _not_
  3831. as a target of type application, which is treated separately in the
  3832. visit_type_application method. Some examples where this method is called are
  3833. casts and instantiation:
  3834. class LongName(Generic[T]): ...
  3835. A = LongName[int]
  3836. x = A()
  3837. y = cast(A, ...)
  3838. """
  3839. if isinstance(alias.target, Instance) and alias.target.invalid: # type: ignore[misc]
  3840. # An invalid alias, error already has been reported
  3841. return AnyType(TypeOfAny.from_error)
  3842. # If this is a generic alias, we set all variables to `Any`.
  3843. # For example:
  3844. # A = List[Tuple[T, T]]
  3845. # x = A() <- same as List[Tuple[Any, Any]], see PEP 484.
  3846. disallow_any = self.chk.options.disallow_any_generics and self.is_callee
  3847. item = get_proper_type(
  3848. set_any_tvars(
  3849. alias,
  3850. ctx.line,
  3851. ctx.column,
  3852. self.chk.options,
  3853. disallow_any=disallow_any,
  3854. fail=self.msg.fail,
  3855. )
  3856. )
  3857. if isinstance(item, Instance):
  3858. # Normally we get a callable type (or overloaded) with .is_type_obj() true
  3859. # representing the class's constructor
  3860. tp = type_object_type(item.type, self.named_type)
  3861. if alias.no_args:
  3862. return tp
  3863. return self.apply_type_arguments_to_callable(tp, item.args, ctx)
  3864. elif (
  3865. isinstance(item, TupleType)
  3866. and
  3867. # Tuple[str, int]() fails at runtime, only named tuples and subclasses work.
  3868. tuple_fallback(item).type.fullname != "builtins.tuple"
  3869. ):
  3870. return type_object_type(tuple_fallback(item).type, self.named_type)
  3871. elif isinstance(item, TypedDictType):
  3872. return self.typeddict_callable_from_context(item)
  3873. elif isinstance(item, AnyType):
  3874. return AnyType(TypeOfAny.from_another_any, source_any=item)
  3875. else:
  3876. if alias_definition:
  3877. return AnyType(TypeOfAny.special_form)
  3878. # The _SpecialForm type can be used in some runtime contexts (e.g. it may have __or__).
  3879. return self.named_type("typing._SpecialForm")
  3880. def split_for_callable(
  3881. self, t: CallableType, args: Sequence[Type], ctx: Context
  3882. ) -> list[Type]:
  3883. """Handle directly applying type arguments to a variadic Callable.
  3884. This is needed in situations where e.g. variadic class object appears in
  3885. runtime context. For example:
  3886. class C(Generic[T, Unpack[Ts]]): ...
  3887. x = C[int, str]()
  3888. We simply group the arguments that need to go into Ts variable into a TupleType,
  3889. similar to how it is done in other places using split_with_prefix_and_suffix().
  3890. """
  3891. vars = t.variables
  3892. if not vars or not any(isinstance(v, TypeVarTupleType) for v in vars):
  3893. return list(args)
  3894. prefix = next(i for (i, v) in enumerate(vars) if isinstance(v, TypeVarTupleType))
  3895. suffix = len(vars) - prefix - 1
  3896. args = flatten_nested_tuples(args)
  3897. if len(args) < len(vars) - 1:
  3898. self.msg.incompatible_type_application(len(vars), len(args), ctx)
  3899. return [AnyType(TypeOfAny.from_error)] * len(vars)
  3900. tvt = vars[prefix]
  3901. assert isinstance(tvt, TypeVarTupleType)
  3902. start, middle, end = split_with_prefix_and_suffix(tuple(args), prefix, suffix)
  3903. return list(start) + [TupleType(list(middle), tvt.tuple_fallback)] + list(end)
  3904. def apply_type_arguments_to_callable(
  3905. self, tp: Type, args: Sequence[Type], ctx: Context
  3906. ) -> Type:
  3907. """Apply type arguments to a generic callable type coming from a type object.
  3908. This will first perform type arguments count checks, report the
  3909. error as needed, and return the correct kind of Any. As a special
  3910. case this returns Any for non-callable types, because if type object type
  3911. is not callable, then an error should be already reported.
  3912. """
  3913. tp = get_proper_type(tp)
  3914. if isinstance(tp, CallableType):
  3915. if len(tp.variables) != len(args) and not any(
  3916. isinstance(v, TypeVarTupleType) for v in tp.variables
  3917. ):
  3918. if tp.is_type_obj() and tp.type_object().fullname == "builtins.tuple":
  3919. # TODO: Specialize the callable for the type arguments
  3920. return tp
  3921. self.msg.incompatible_type_application(len(tp.variables), len(args), ctx)
  3922. return AnyType(TypeOfAny.from_error)
  3923. return self.apply_generic_arguments(tp, self.split_for_callable(tp, args, ctx), ctx)
  3924. if isinstance(tp, Overloaded):
  3925. for it in tp.items:
  3926. if len(it.variables) != len(args) and not any(
  3927. isinstance(v, TypeVarTupleType) for v in it.variables
  3928. ):
  3929. self.msg.incompatible_type_application(len(it.variables), len(args), ctx)
  3930. return AnyType(TypeOfAny.from_error)
  3931. return Overloaded(
  3932. [
  3933. self.apply_generic_arguments(it, self.split_for_callable(it, args, ctx), ctx)
  3934. for it in tp.items
  3935. ]
  3936. )
  3937. return AnyType(TypeOfAny.special_form)
  3938. def visit_list_expr(self, e: ListExpr) -> Type:
  3939. """Type check a list expression [...]."""
  3940. return self.check_lst_expr(e, "builtins.list", "<list>")
  3941. def visit_set_expr(self, e: SetExpr) -> Type:
  3942. return self.check_lst_expr(e, "builtins.set", "<set>")
  3943. def fast_container_type(
  3944. self, e: ListExpr | SetExpr | TupleExpr, container_fullname: str
  3945. ) -> Type | None:
  3946. """
  3947. Fast path to determine the type of a list or set literal,
  3948. based on the list of entries. This mostly impacts large
  3949. module-level constant definitions.
  3950. Limitations:
  3951. - no active type context
  3952. - no star expressions
  3953. - the joined type of all entries must be an Instance or Tuple type
  3954. """
  3955. ctx = self.type_context[-1]
  3956. if ctx:
  3957. return None
  3958. rt = self.resolved_type.get(e, None)
  3959. if rt is not None:
  3960. return rt if isinstance(rt, Instance) else None
  3961. values: list[Type] = []
  3962. for item in e.items:
  3963. if isinstance(item, StarExpr):
  3964. # fallback to slow path
  3965. self.resolved_type[e] = NoneType()
  3966. return None
  3967. values.append(self.accept(item))
  3968. vt = join.join_type_list(values)
  3969. if not allow_fast_container_literal(vt):
  3970. self.resolved_type[e] = NoneType()
  3971. return None
  3972. ct = self.chk.named_generic_type(container_fullname, [vt])
  3973. self.resolved_type[e] = ct
  3974. return ct
  3975. def check_lst_expr(self, e: ListExpr | SetExpr | TupleExpr, fullname: str, tag: str) -> Type:
  3976. # fast path
  3977. t = self.fast_container_type(e, fullname)
  3978. if t:
  3979. return t
  3980. # Translate into type checking a generic function call.
  3981. # Used for list and set expressions, as well as for tuples
  3982. # containing star expressions that don't refer to a
  3983. # Tuple. (Note: "lst" stands for list-set-tuple. :-)
  3984. tv = TypeVarType(
  3985. "T",
  3986. "T",
  3987. id=-1,
  3988. values=[],
  3989. upper_bound=self.object_type(),
  3990. default=AnyType(TypeOfAny.from_omitted_generics),
  3991. )
  3992. constructor = CallableType(
  3993. [tv],
  3994. [nodes.ARG_STAR],
  3995. [None],
  3996. self.chk.named_generic_type(fullname, [tv]),
  3997. self.named_type("builtins.function"),
  3998. name=tag,
  3999. variables=[tv],
  4000. )
  4001. out = self.check_call(
  4002. constructor,
  4003. [(i.expr if isinstance(i, StarExpr) else i) for i in e.items],
  4004. [(nodes.ARG_STAR if isinstance(i, StarExpr) else nodes.ARG_POS) for i in e.items],
  4005. e,
  4006. )[0]
  4007. return remove_instance_last_known_values(out)
  4008. def visit_tuple_expr(self, e: TupleExpr) -> Type:
  4009. """Type check a tuple expression."""
  4010. # Try to determine type context for type inference.
  4011. type_context = get_proper_type(self.type_context[-1])
  4012. type_context_items = None
  4013. if isinstance(type_context, UnionType):
  4014. tuples_in_context = [
  4015. t
  4016. for t in get_proper_types(type_context.items)
  4017. if (isinstance(t, TupleType) and len(t.items) == len(e.items))
  4018. or is_named_instance(t, TUPLE_LIKE_INSTANCE_NAMES)
  4019. ]
  4020. if len(tuples_in_context) == 1:
  4021. type_context = tuples_in_context[0]
  4022. else:
  4023. # There are either no relevant tuples in the Union, or there is
  4024. # more than one. Either way, we can't decide on a context.
  4025. pass
  4026. if isinstance(type_context, TupleType):
  4027. type_context_items = type_context.items
  4028. elif type_context and is_named_instance(type_context, TUPLE_LIKE_INSTANCE_NAMES):
  4029. assert isinstance(type_context, Instance)
  4030. if type_context.args:
  4031. type_context_items = [type_context.args[0]] * len(e.items)
  4032. # NOTE: it's possible for the context to have a different
  4033. # number of items than e. In that case we use those context
  4034. # items that match a position in e, and we'll worry about type
  4035. # mismatches later.
  4036. # Infer item types. Give up if there's a star expression
  4037. # that's not a Tuple.
  4038. items: list[Type] = []
  4039. j = 0 # Index into type_context_items; irrelevant if type_context_items is none
  4040. for i in range(len(e.items)):
  4041. item = e.items[i]
  4042. if isinstance(item, StarExpr):
  4043. # Special handling for star expressions.
  4044. # TODO: If there's a context, and item.expr is a
  4045. # TupleExpr, flatten it, so we can benefit from the
  4046. # context? Counterargument: Why would anyone write
  4047. # (1, *(2, 3)) instead of (1, 2, 3) except in a test?
  4048. tt = self.accept(item.expr)
  4049. tt = get_proper_type(tt)
  4050. if isinstance(tt, TupleType):
  4051. items.extend(tt.items)
  4052. j += len(tt.items)
  4053. else:
  4054. # A star expression that's not a Tuple.
  4055. # Treat the whole thing as a variable-length tuple.
  4056. return self.check_lst_expr(e, "builtins.tuple", "<tuple>")
  4057. else:
  4058. if not type_context_items or j >= len(type_context_items):
  4059. tt = self.accept(item)
  4060. else:
  4061. tt = self.accept(item, type_context_items[j])
  4062. j += 1
  4063. items.append(tt)
  4064. # This is a partial fallback item type. A precise type will be calculated on demand.
  4065. fallback_item = AnyType(TypeOfAny.special_form)
  4066. return TupleType(items, self.chk.named_generic_type("builtins.tuple", [fallback_item]))
  4067. def fast_dict_type(self, e: DictExpr) -> Type | None:
  4068. """
  4069. Fast path to determine the type of a dict literal,
  4070. based on the list of entries. This mostly impacts large
  4071. module-level constant definitions.
  4072. Limitations:
  4073. - no active type context
  4074. - only supported star expressions are other dict instances
  4075. - the joined types of all keys and values must be Instance or Tuple types
  4076. """
  4077. ctx = self.type_context[-1]
  4078. if ctx:
  4079. return None
  4080. rt = self.resolved_type.get(e, None)
  4081. if rt is not None:
  4082. return rt if isinstance(rt, Instance) else None
  4083. keys: list[Type] = []
  4084. values: list[Type] = []
  4085. stargs: tuple[Type, Type] | None = None
  4086. for key, value in e.items:
  4087. if key is None:
  4088. st = get_proper_type(self.accept(value))
  4089. if (
  4090. isinstance(st, Instance)
  4091. and st.type.fullname == "builtins.dict"
  4092. and len(st.args) == 2
  4093. ):
  4094. stargs = (st.args[0], st.args[1])
  4095. else:
  4096. self.resolved_type[e] = NoneType()
  4097. return None
  4098. else:
  4099. keys.append(self.accept(key))
  4100. values.append(self.accept(value))
  4101. kt = join.join_type_list(keys)
  4102. vt = join.join_type_list(values)
  4103. if not (allow_fast_container_literal(kt) and allow_fast_container_literal(vt)):
  4104. self.resolved_type[e] = NoneType()
  4105. return None
  4106. if stargs and (stargs[0] != kt or stargs[1] != vt):
  4107. self.resolved_type[e] = NoneType()
  4108. return None
  4109. dt = self.chk.named_generic_type("builtins.dict", [kt, vt])
  4110. self.resolved_type[e] = dt
  4111. return dt
  4112. def check_typeddict_literal_in_context(
  4113. self, e: DictExpr, typeddict_context: TypedDictType
  4114. ) -> Type:
  4115. orig_ret_type = self.check_typeddict_call_with_dict(
  4116. callee=typeddict_context, kwargs=e.items, context=e, orig_callee=None
  4117. )
  4118. ret_type = get_proper_type(orig_ret_type)
  4119. if isinstance(ret_type, TypedDictType):
  4120. return ret_type.copy_modified()
  4121. return typeddict_context.copy_modified()
  4122. def visit_dict_expr(self, e: DictExpr) -> Type:
  4123. """Type check a dict expression.
  4124. Translate it into a call to dict(), with provisions for **expr.
  4125. """
  4126. # if the dict literal doesn't match TypedDict, check_typeddict_call_with_dict reports
  4127. # an error, but returns the TypedDict type that matches the literal it found
  4128. # that would cause a second error when that TypedDict type is returned upstream
  4129. # to avoid the second error, we always return TypedDict type that was requested
  4130. typeddict_contexts = self.find_typeddict_context(self.type_context[-1], e)
  4131. if typeddict_contexts:
  4132. if len(typeddict_contexts) == 1:
  4133. return self.check_typeddict_literal_in_context(e, typeddict_contexts[0])
  4134. # Multiple items union, check if at least one of them matches cleanly.
  4135. for typeddict_context in typeddict_contexts:
  4136. with self.msg.filter_errors() as err, self.chk.local_type_map() as tmap:
  4137. ret_type = self.check_typeddict_literal_in_context(e, typeddict_context)
  4138. if err.has_new_errors():
  4139. continue
  4140. self.chk.store_types(tmap)
  4141. return ret_type
  4142. # No item matched without an error, so we can't unambiguously choose the item.
  4143. self.msg.typeddict_context_ambiguous(typeddict_contexts, e)
  4144. # fast path attempt
  4145. dt = self.fast_dict_type(e)
  4146. if dt:
  4147. return dt
  4148. # Define type variables (used in constructors below).
  4149. kt = TypeVarType(
  4150. "KT",
  4151. "KT",
  4152. id=-1,
  4153. values=[],
  4154. upper_bound=self.object_type(),
  4155. default=AnyType(TypeOfAny.from_omitted_generics),
  4156. )
  4157. vt = TypeVarType(
  4158. "VT",
  4159. "VT",
  4160. id=-2,
  4161. values=[],
  4162. upper_bound=self.object_type(),
  4163. default=AnyType(TypeOfAny.from_omitted_generics),
  4164. )
  4165. # Collect function arguments, watching out for **expr.
  4166. args: list[Expression] = []
  4167. expected_types: list[Type] = []
  4168. for key, value in e.items:
  4169. if key is None:
  4170. args.append(value)
  4171. expected_types.append(
  4172. self.chk.named_generic_type("_typeshed.SupportsKeysAndGetItem", [kt, vt])
  4173. )
  4174. else:
  4175. tup = TupleExpr([key, value])
  4176. if key.line >= 0:
  4177. tup.line = key.line
  4178. tup.column = key.column
  4179. else:
  4180. tup.line = value.line
  4181. tup.column = value.column
  4182. tup.end_line = value.end_line
  4183. tup.end_column = value.end_column
  4184. args.append(tup)
  4185. expected_types.append(TupleType([kt, vt], self.named_type("builtins.tuple")))
  4186. # The callable type represents a function like this (except we adjust for **expr):
  4187. # def <dict>(*v: Tuple[kt, vt]) -> Dict[kt, vt]: ...
  4188. constructor = CallableType(
  4189. expected_types,
  4190. [nodes.ARG_POS] * len(expected_types),
  4191. [None] * len(expected_types),
  4192. self.chk.named_generic_type("builtins.dict", [kt, vt]),
  4193. self.named_type("builtins.function"),
  4194. name="<dict>",
  4195. variables=[kt, vt],
  4196. )
  4197. return self.check_call(constructor, args, [nodes.ARG_POS] * len(args), e)[0]
  4198. def find_typeddict_context(
  4199. self, context: Type | None, dict_expr: DictExpr
  4200. ) -> list[TypedDictType]:
  4201. context = get_proper_type(context)
  4202. if isinstance(context, TypedDictType):
  4203. return [context]
  4204. elif isinstance(context, UnionType):
  4205. items = []
  4206. for item in context.items:
  4207. item_contexts = self.find_typeddict_context(item, dict_expr)
  4208. for item_context in item_contexts:
  4209. if self.match_typeddict_call_with_dict(
  4210. item_context, dict_expr.items, dict_expr
  4211. ):
  4212. items.append(item_context)
  4213. return items
  4214. # No TypedDict type in context.
  4215. return []
  4216. def visit_lambda_expr(self, e: LambdaExpr) -> Type:
  4217. """Type check lambda expression."""
  4218. self.chk.check_default_args(e, body_is_trivial=False)
  4219. inferred_type, type_override = self.infer_lambda_type_using_context(e)
  4220. if not inferred_type:
  4221. self.chk.return_types.append(AnyType(TypeOfAny.special_form))
  4222. # Type check everything in the body except for the final return
  4223. # statement (it can contain tuple unpacking before return).
  4224. with self.chk.scope.push_function(e):
  4225. # Lambdas can have more than one element in body,
  4226. # when we add "fictional" AssigmentStatement nodes, like in:
  4227. # `lambda (a, b): a`
  4228. for stmt in e.body.body[:-1]:
  4229. stmt.accept(self.chk)
  4230. # Only type check the return expression, not the return statement.
  4231. # This is important as otherwise the following statements would be
  4232. # considered unreachable. There's no useful type context.
  4233. ret_type = self.accept(e.expr(), allow_none_return=True)
  4234. fallback = self.named_type("builtins.function")
  4235. self.chk.return_types.pop()
  4236. return callable_type(e, fallback, ret_type)
  4237. else:
  4238. # Type context available.
  4239. self.chk.return_types.append(inferred_type.ret_type)
  4240. self.chk.check_func_item(e, type_override=type_override)
  4241. if not self.chk.has_type(e.expr()):
  4242. # TODO: return expression must be accepted before exiting function scope.
  4243. self.accept(e.expr(), allow_none_return=True)
  4244. ret_type = self.chk.lookup_type(e.expr())
  4245. self.chk.return_types.pop()
  4246. return replace_callable_return_type(inferred_type, ret_type)
  4247. def infer_lambda_type_using_context(
  4248. self, e: LambdaExpr
  4249. ) -> tuple[CallableType | None, CallableType | None]:
  4250. """Try to infer lambda expression type using context.
  4251. Return None if could not infer type.
  4252. The second item in the return type is the type_override parameter for check_func_item.
  4253. """
  4254. # TODO also accept 'Any' context
  4255. ctx = get_proper_type(self.type_context[-1])
  4256. if isinstance(ctx, UnionType):
  4257. callables = [
  4258. t for t in get_proper_types(ctx.relevant_items()) if isinstance(t, CallableType)
  4259. ]
  4260. if len(callables) == 1:
  4261. ctx = callables[0]
  4262. if not ctx or not isinstance(ctx, CallableType):
  4263. return None, None
  4264. # The context may have function type variables in it. We replace them
  4265. # since these are the type variables we are ultimately trying to infer;
  4266. # they must be considered as indeterminate. We use ErasedType since it
  4267. # does not affect type inference results (it is for purposes like this
  4268. # only).
  4269. callable_ctx = get_proper_type(replace_meta_vars(ctx, ErasedType()))
  4270. assert isinstance(callable_ctx, CallableType)
  4271. # The callable_ctx may have a fallback of builtins.type if the context
  4272. # is a constructor -- but this fallback doesn't make sense for lambdas.
  4273. callable_ctx = callable_ctx.copy_modified(fallback=self.named_type("builtins.function"))
  4274. if callable_ctx.type_guard is not None:
  4275. # Lambda's return type cannot be treated as a `TypeGuard`,
  4276. # because it is implicit. And `TypeGuard`s must be explicit.
  4277. # See https://github.com/python/mypy/issues/9927
  4278. return None, None
  4279. arg_kinds = [arg.kind for arg in e.arguments]
  4280. if callable_ctx.is_ellipsis_args or ctx.param_spec() is not None:
  4281. # Fill in Any arguments to match the arguments of the lambda.
  4282. callable_ctx = callable_ctx.copy_modified(
  4283. is_ellipsis_args=False,
  4284. arg_types=[AnyType(TypeOfAny.special_form)] * len(arg_kinds),
  4285. arg_kinds=arg_kinds,
  4286. arg_names=e.arg_names.copy(),
  4287. )
  4288. if ARG_STAR in arg_kinds or ARG_STAR2 in arg_kinds:
  4289. # TODO treat this case appropriately
  4290. return callable_ctx, None
  4291. if callable_ctx.arg_kinds != arg_kinds:
  4292. # Incompatible context; cannot use it to infer types.
  4293. self.chk.fail(message_registry.CANNOT_INFER_LAMBDA_TYPE, e)
  4294. return None, None
  4295. return callable_ctx, callable_ctx
  4296. def visit_super_expr(self, e: SuperExpr) -> Type:
  4297. """Type check a super expression (non-lvalue)."""
  4298. # We have an expression like super(T, var).member
  4299. # First compute the types of T and var
  4300. types = self._super_arg_types(e)
  4301. if isinstance(types, tuple):
  4302. type_type, instance_type = types
  4303. else:
  4304. return types
  4305. # Now get the MRO
  4306. type_info = type_info_from_type(type_type)
  4307. if type_info is None:
  4308. self.chk.fail(message_registry.UNSUPPORTED_ARG_1_FOR_SUPER, e)
  4309. return AnyType(TypeOfAny.from_error)
  4310. instance_info = type_info_from_type(instance_type)
  4311. if instance_info is None:
  4312. self.chk.fail(message_registry.UNSUPPORTED_ARG_2_FOR_SUPER, e)
  4313. return AnyType(TypeOfAny.from_error)
  4314. mro = instance_info.mro
  4315. # The base is the first MRO entry *after* type_info that has a member
  4316. # with the right name
  4317. index = None
  4318. if type_info in mro:
  4319. index = mro.index(type_info)
  4320. else:
  4321. method = self.chk.scope.top_function()
  4322. # Mypy explicitly allows supertype upper bounds (and no upper bound at all)
  4323. # for annotating self-types. However, if such an annotation is used for
  4324. # checking super() we will still get an error. So to be consistent, we also
  4325. # allow such imprecise annotations for use with super(), where we fall back
  4326. # to the current class MRO instead. This works only from inside a method.
  4327. if method is not None and is_self_type_like(
  4328. instance_type, is_classmethod=method.is_class
  4329. ):
  4330. if e.info and type_info in e.info.mro:
  4331. mro = e.info.mro
  4332. index = mro.index(type_info)
  4333. if index is None:
  4334. if (
  4335. instance_info.is_protocol
  4336. and instance_info != type_info
  4337. and not type_info.is_protocol
  4338. ):
  4339. # A special case for mixins, in this case super() should point
  4340. # directly to the host protocol, this is not safe, since the real MRO
  4341. # is not known yet for mixin, but this feature is more like an escape hatch.
  4342. index = -1
  4343. else:
  4344. self.chk.fail(message_registry.SUPER_ARG_2_NOT_INSTANCE_OF_ARG_1, e)
  4345. return AnyType(TypeOfAny.from_error)
  4346. if len(mro) == index + 1:
  4347. self.chk.fail(message_registry.TARGET_CLASS_HAS_NO_BASE_CLASS, e)
  4348. return AnyType(TypeOfAny.from_error)
  4349. for base in mro[index + 1 :]:
  4350. if e.name in base.names or base == mro[-1]:
  4351. if e.info and e.info.fallback_to_any and base == mro[-1]:
  4352. # There's an undefined base class, and we're at the end of the
  4353. # chain. That's not an error.
  4354. return AnyType(TypeOfAny.special_form)
  4355. return analyze_member_access(
  4356. name=e.name,
  4357. typ=instance_type,
  4358. is_lvalue=False,
  4359. is_super=True,
  4360. is_operator=False,
  4361. original_type=instance_type,
  4362. override_info=base,
  4363. context=e,
  4364. msg=self.msg,
  4365. chk=self.chk,
  4366. in_literal_context=self.is_literal_context(),
  4367. )
  4368. assert False, "unreachable"
  4369. def _super_arg_types(self, e: SuperExpr) -> Type | tuple[Type, Type]:
  4370. """
  4371. Computes the types of the type and instance expressions in super(T, instance), or the
  4372. implicit ones for zero-argument super() expressions. Returns a single type for the whole
  4373. super expression when possible (for errors, anys), otherwise the pair of computed types.
  4374. """
  4375. if not self.chk.in_checked_function():
  4376. return AnyType(TypeOfAny.unannotated)
  4377. elif len(e.call.args) == 0:
  4378. if not e.info:
  4379. # This has already been reported by the semantic analyzer.
  4380. return AnyType(TypeOfAny.from_error)
  4381. elif self.chk.scope.active_class():
  4382. self.chk.fail(message_registry.SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED, e)
  4383. return AnyType(TypeOfAny.from_error)
  4384. # Zero-argument super() is like super(<current class>, <self>)
  4385. current_type = fill_typevars(e.info)
  4386. type_type: ProperType = TypeType(current_type)
  4387. # Use the type of the self argument, in case it was annotated
  4388. method = self.chk.scope.top_function()
  4389. assert method is not None
  4390. if method.arguments:
  4391. instance_type: Type = method.arguments[0].variable.type or current_type
  4392. else:
  4393. self.chk.fail(message_registry.SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED, e)
  4394. return AnyType(TypeOfAny.from_error)
  4395. elif ARG_STAR in e.call.arg_kinds:
  4396. self.chk.fail(message_registry.SUPER_VARARGS_NOT_SUPPORTED, e)
  4397. return AnyType(TypeOfAny.from_error)
  4398. elif set(e.call.arg_kinds) != {ARG_POS}:
  4399. self.chk.fail(message_registry.SUPER_POSITIONAL_ARGS_REQUIRED, e)
  4400. return AnyType(TypeOfAny.from_error)
  4401. elif len(e.call.args) == 1:
  4402. self.chk.fail(message_registry.SUPER_WITH_SINGLE_ARG_NOT_SUPPORTED, e)
  4403. return AnyType(TypeOfAny.from_error)
  4404. elif len(e.call.args) == 2:
  4405. type_type = get_proper_type(self.accept(e.call.args[0]))
  4406. instance_type = self.accept(e.call.args[1])
  4407. else:
  4408. self.chk.fail(message_registry.TOO_MANY_ARGS_FOR_SUPER, e)
  4409. return AnyType(TypeOfAny.from_error)
  4410. # Imprecisely assume that the type is the current class
  4411. if isinstance(type_type, AnyType):
  4412. if e.info:
  4413. type_type = TypeType(fill_typevars(e.info))
  4414. else:
  4415. return AnyType(TypeOfAny.from_another_any, source_any=type_type)
  4416. elif isinstance(type_type, TypeType):
  4417. type_item = type_type.item
  4418. if isinstance(type_item, AnyType):
  4419. if e.info:
  4420. type_type = TypeType(fill_typevars(e.info))
  4421. else:
  4422. return AnyType(TypeOfAny.from_another_any, source_any=type_item)
  4423. if not isinstance(type_type, TypeType) and not (
  4424. isinstance(type_type, FunctionLike) and type_type.is_type_obj()
  4425. ):
  4426. self.msg.first_argument_for_super_must_be_type(type_type, e)
  4427. return AnyType(TypeOfAny.from_error)
  4428. # Imprecisely assume that the instance is of the current class
  4429. instance_type = get_proper_type(instance_type)
  4430. if isinstance(instance_type, AnyType):
  4431. if e.info:
  4432. instance_type = fill_typevars(e.info)
  4433. else:
  4434. return AnyType(TypeOfAny.from_another_any, source_any=instance_type)
  4435. elif isinstance(instance_type, TypeType):
  4436. instance_item = instance_type.item
  4437. if isinstance(instance_item, AnyType):
  4438. if e.info:
  4439. instance_type = TypeType(fill_typevars(e.info))
  4440. else:
  4441. return AnyType(TypeOfAny.from_another_any, source_any=instance_item)
  4442. return type_type, instance_type
  4443. def visit_slice_expr(self, e: SliceExpr) -> Type:
  4444. try:
  4445. supports_index = self.chk.named_type("typing_extensions.SupportsIndex")
  4446. except KeyError:
  4447. supports_index = self.chk.named_type("builtins.int") # thanks, fixture life
  4448. expected = make_optional_type(supports_index)
  4449. for index in [e.begin_index, e.end_index, e.stride]:
  4450. if index:
  4451. t = self.accept(index)
  4452. self.chk.check_subtype(t, expected, index, message_registry.INVALID_SLICE_INDEX)
  4453. return self.named_type("builtins.slice")
  4454. def visit_list_comprehension(self, e: ListComprehension) -> Type:
  4455. return self.check_generator_or_comprehension(
  4456. e.generator, "builtins.list", "<list-comprehension>"
  4457. )
  4458. def visit_set_comprehension(self, e: SetComprehension) -> Type:
  4459. return self.check_generator_or_comprehension(
  4460. e.generator, "builtins.set", "<set-comprehension>"
  4461. )
  4462. def visit_generator_expr(self, e: GeneratorExpr) -> Type:
  4463. # If any of the comprehensions use async for, the expression will return an async generator
  4464. # object, or if the left-side expression uses await.
  4465. if any(e.is_async) or has_await_expression(e.left_expr):
  4466. typ = "typing.AsyncGenerator"
  4467. # received type is always None in async generator expressions
  4468. additional_args: list[Type] = [NoneType()]
  4469. else:
  4470. typ = "typing.Generator"
  4471. # received type and returned type are None
  4472. additional_args = [NoneType(), NoneType()]
  4473. return self.check_generator_or_comprehension(
  4474. e, typ, "<generator>", additional_args=additional_args
  4475. )
  4476. def check_generator_or_comprehension(
  4477. self,
  4478. gen: GeneratorExpr,
  4479. type_name: str,
  4480. id_for_messages: str,
  4481. additional_args: list[Type] | None = None,
  4482. ) -> Type:
  4483. """Type check a generator expression or a list comprehension."""
  4484. additional_args = additional_args or []
  4485. with self.chk.binder.frame_context(can_skip=True, fall_through=0):
  4486. self.check_for_comp(gen)
  4487. # Infer the type of the list comprehension by using a synthetic generic
  4488. # callable type.
  4489. tv = TypeVarType(
  4490. "T",
  4491. "T",
  4492. id=-1,
  4493. values=[],
  4494. upper_bound=self.object_type(),
  4495. default=AnyType(TypeOfAny.from_omitted_generics),
  4496. )
  4497. tv_list: list[Type] = [tv]
  4498. constructor = CallableType(
  4499. tv_list,
  4500. [nodes.ARG_POS],
  4501. [None],
  4502. self.chk.named_generic_type(type_name, tv_list + additional_args),
  4503. self.chk.named_type("builtins.function"),
  4504. name=id_for_messages,
  4505. variables=[tv],
  4506. )
  4507. return self.check_call(constructor, [gen.left_expr], [nodes.ARG_POS], gen)[0]
  4508. def visit_dictionary_comprehension(self, e: DictionaryComprehension) -> Type:
  4509. """Type check a dictionary comprehension."""
  4510. with self.chk.binder.frame_context(can_skip=True, fall_through=0):
  4511. self.check_for_comp(e)
  4512. # Infer the type of the list comprehension by using a synthetic generic
  4513. # callable type.
  4514. ktdef = TypeVarType(
  4515. "KT",
  4516. "KT",
  4517. id=-1,
  4518. values=[],
  4519. upper_bound=self.object_type(),
  4520. default=AnyType(TypeOfAny.from_omitted_generics),
  4521. )
  4522. vtdef = TypeVarType(
  4523. "VT",
  4524. "VT",
  4525. id=-2,
  4526. values=[],
  4527. upper_bound=self.object_type(),
  4528. default=AnyType(TypeOfAny.from_omitted_generics),
  4529. )
  4530. constructor = CallableType(
  4531. [ktdef, vtdef],
  4532. [nodes.ARG_POS, nodes.ARG_POS],
  4533. [None, None],
  4534. self.chk.named_generic_type("builtins.dict", [ktdef, vtdef]),
  4535. self.chk.named_type("builtins.function"),
  4536. name="<dictionary-comprehension>",
  4537. variables=[ktdef, vtdef],
  4538. )
  4539. return self.check_call(
  4540. constructor, [e.key, e.value], [nodes.ARG_POS, nodes.ARG_POS], e
  4541. )[0]
  4542. def check_for_comp(self, e: GeneratorExpr | DictionaryComprehension) -> None:
  4543. """Check the for_comp part of comprehensions. That is the part from 'for':
  4544. ... for x in y if z
  4545. Note: This adds the type information derived from the condlists to the current binder.
  4546. """
  4547. for index, sequence, conditions, is_async in zip(
  4548. e.indices, e.sequences, e.condlists, e.is_async
  4549. ):
  4550. if is_async:
  4551. _, sequence_type = self.chk.analyze_async_iterable_item_type(sequence)
  4552. else:
  4553. _, sequence_type = self.chk.analyze_iterable_item_type(sequence)
  4554. self.chk.analyze_index_variables(index, sequence_type, True, e)
  4555. for condition in conditions:
  4556. self.accept(condition)
  4557. # values are only part of the comprehension when all conditions are true
  4558. true_map, false_map = self.chk.find_isinstance_check(condition)
  4559. if true_map:
  4560. self.chk.push_type_map(true_map)
  4561. if codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes:
  4562. if true_map is None:
  4563. self.msg.redundant_condition_in_comprehension(False, condition)
  4564. elif false_map is None:
  4565. self.msg.redundant_condition_in_comprehension(True, condition)
  4566. def visit_conditional_expr(self, e: ConditionalExpr, allow_none_return: bool = False) -> Type:
  4567. self.accept(e.cond)
  4568. ctx = self.type_context[-1]
  4569. # Gain type information from isinstance if it is there
  4570. # but only for the current expression
  4571. if_map, else_map = self.chk.find_isinstance_check(e.cond)
  4572. if codes.REDUNDANT_EXPR in self.chk.options.enabled_error_codes:
  4573. if if_map is None:
  4574. self.msg.redundant_condition_in_if(False, e.cond)
  4575. elif else_map is None:
  4576. self.msg.redundant_condition_in_if(True, e.cond)
  4577. if_type = self.analyze_cond_branch(
  4578. if_map, e.if_expr, context=ctx, allow_none_return=allow_none_return
  4579. )
  4580. # we want to keep the narrowest value of if_type for union'ing the branches
  4581. # however, it would be silly to pass a literal as a type context. Pass the
  4582. # underlying fallback type instead.
  4583. if_type_fallback = simple_literal_type(get_proper_type(if_type)) or if_type
  4584. # Analyze the right branch using full type context and store the type
  4585. full_context_else_type = self.analyze_cond_branch(
  4586. else_map, e.else_expr, context=ctx, allow_none_return=allow_none_return
  4587. )
  4588. if not mypy.checker.is_valid_inferred_type(if_type):
  4589. # Analyze the right branch disregarding the left branch.
  4590. else_type = full_context_else_type
  4591. # we want to keep the narrowest value of else_type for union'ing the branches
  4592. # however, it would be silly to pass a literal as a type context. Pass the
  4593. # underlying fallback type instead.
  4594. else_type_fallback = simple_literal_type(get_proper_type(else_type)) or else_type
  4595. # If it would make a difference, re-analyze the left
  4596. # branch using the right branch's type as context.
  4597. if ctx is None or not is_equivalent(else_type_fallback, ctx):
  4598. # TODO: If it's possible that the previous analysis of
  4599. # the left branch produced errors that are avoided
  4600. # using this context, suppress those errors.
  4601. if_type = self.analyze_cond_branch(
  4602. if_map,
  4603. e.if_expr,
  4604. context=else_type_fallback,
  4605. allow_none_return=allow_none_return,
  4606. )
  4607. elif if_type_fallback == ctx:
  4608. # There is no point re-running the analysis if if_type is equal to ctx.
  4609. # That would be an exact duplicate of the work we just did.
  4610. # This optimization is particularly important to avoid exponential blowup with nested
  4611. # if/else expressions: https://github.com/python/mypy/issues/9591
  4612. # TODO: would checking for is_proper_subtype also work and cover more cases?
  4613. else_type = full_context_else_type
  4614. else:
  4615. # Analyze the right branch in the context of the left
  4616. # branch's type.
  4617. else_type = self.analyze_cond_branch(
  4618. else_map,
  4619. e.else_expr,
  4620. context=if_type_fallback,
  4621. allow_none_return=allow_none_return,
  4622. )
  4623. # Only create a union type if the type context is a union, to be mostly
  4624. # compatible with older mypy versions where we always did a join.
  4625. #
  4626. # TODO: Always create a union or at least in more cases?
  4627. if isinstance(get_proper_type(self.type_context[-1]), UnionType):
  4628. res: Type = make_simplified_union([if_type, full_context_else_type])
  4629. else:
  4630. res = join.join_types(if_type, else_type)
  4631. return res
  4632. def analyze_cond_branch(
  4633. self,
  4634. map: dict[Expression, Type] | None,
  4635. node: Expression,
  4636. context: Type | None,
  4637. allow_none_return: bool = False,
  4638. ) -> Type:
  4639. with self.chk.binder.frame_context(can_skip=True, fall_through=0):
  4640. if map is None:
  4641. # We still need to type check node, in case we want to
  4642. # process it for isinstance checks later
  4643. self.accept(node, type_context=context, allow_none_return=allow_none_return)
  4644. return UninhabitedType()
  4645. self.chk.push_type_map(map)
  4646. return self.accept(node, type_context=context, allow_none_return=allow_none_return)
  4647. #
  4648. # Helpers
  4649. #
  4650. def accept(
  4651. self,
  4652. node: Expression,
  4653. type_context: Type | None = None,
  4654. allow_none_return: bool = False,
  4655. always_allow_any: bool = False,
  4656. is_callee: bool = False,
  4657. ) -> Type:
  4658. """Type check a node in the given type context. If allow_none_return
  4659. is True and this expression is a call, allow it to return None. This
  4660. applies only to this expression and not any subexpressions.
  4661. """
  4662. if node in self.type_overrides:
  4663. # This branch is very fast, there is no point timing it.
  4664. return self.type_overrides[node]
  4665. # We don't use context manager here to get most precise data (and avoid overhead).
  4666. record_time = False
  4667. if self.collect_line_checking_stats and not self.in_expression:
  4668. t0 = time.perf_counter_ns()
  4669. self.in_expression = True
  4670. record_time = True
  4671. self.type_context.append(type_context)
  4672. old_is_callee = self.is_callee
  4673. self.is_callee = is_callee
  4674. try:
  4675. if allow_none_return and isinstance(node, CallExpr):
  4676. typ = self.visit_call_expr(node, allow_none_return=True)
  4677. elif allow_none_return and isinstance(node, YieldFromExpr):
  4678. typ = self.visit_yield_from_expr(node, allow_none_return=True)
  4679. elif allow_none_return and isinstance(node, ConditionalExpr):
  4680. typ = self.visit_conditional_expr(node, allow_none_return=True)
  4681. elif allow_none_return and isinstance(node, AwaitExpr):
  4682. typ = self.visit_await_expr(node, allow_none_return=True)
  4683. else:
  4684. typ = node.accept(self)
  4685. except Exception as err:
  4686. report_internal_error(
  4687. err, self.chk.errors.file, node.line, self.chk.errors, self.chk.options
  4688. )
  4689. self.is_callee = old_is_callee
  4690. self.type_context.pop()
  4691. assert typ is not None
  4692. self.chk.store_type(node, typ)
  4693. if (
  4694. self.chk.options.disallow_any_expr
  4695. and not always_allow_any
  4696. and not self.chk.is_stub
  4697. and self.chk.in_checked_function()
  4698. and has_any_type(typ)
  4699. and not self.chk.current_node_deferred
  4700. ):
  4701. self.msg.disallowed_any_type(typ, node)
  4702. if not self.chk.in_checked_function() or self.chk.current_node_deferred:
  4703. result: Type = AnyType(TypeOfAny.unannotated)
  4704. else:
  4705. result = typ
  4706. if record_time:
  4707. self.per_line_checking_time_ns[node.line] += time.perf_counter_ns() - t0
  4708. self.in_expression = False
  4709. return result
  4710. def named_type(self, name: str) -> Instance:
  4711. """Return an instance type with type given by the name and no type
  4712. arguments. Alias for TypeChecker.named_type.
  4713. """
  4714. return self.chk.named_type(name)
  4715. def is_valid_var_arg(self, typ: Type) -> bool:
  4716. """Is a type valid as a *args argument?"""
  4717. typ = get_proper_type(typ)
  4718. return isinstance(typ, (TupleType, AnyType, ParamSpecType, UnpackType)) or is_subtype(
  4719. typ, self.chk.named_generic_type("typing.Iterable", [AnyType(TypeOfAny.special_form)])
  4720. )
  4721. def is_valid_keyword_var_arg(self, typ: Type) -> bool:
  4722. """Is a type valid as a **kwargs argument?"""
  4723. return (
  4724. is_subtype(
  4725. typ,
  4726. self.chk.named_generic_type(
  4727. "_typeshed.SupportsKeysAndGetItem",
  4728. [self.named_type("builtins.str"), AnyType(TypeOfAny.special_form)],
  4729. ),
  4730. )
  4731. or is_subtype(
  4732. typ,
  4733. self.chk.named_generic_type(
  4734. "_typeshed.SupportsKeysAndGetItem", [UninhabitedType(), UninhabitedType()]
  4735. ),
  4736. )
  4737. or isinstance(typ, ParamSpecType)
  4738. )
  4739. def has_member(self, typ: Type, member: str) -> bool:
  4740. """Does type have member with the given name?"""
  4741. # TODO: refactor this to use checkmember.analyze_member_access, otherwise
  4742. # these two should be carefully kept in sync.
  4743. # This is much faster than analyze_member_access, though, and so using
  4744. # it first as a filter is important for performance.
  4745. typ = get_proper_type(typ)
  4746. if isinstance(typ, TypeVarType):
  4747. typ = get_proper_type(typ.upper_bound)
  4748. if isinstance(typ, TupleType):
  4749. typ = tuple_fallback(typ)
  4750. if isinstance(typ, LiteralType):
  4751. typ = typ.fallback
  4752. if isinstance(typ, Instance):
  4753. return typ.type.has_readable_member(member)
  4754. if isinstance(typ, FunctionLike) and typ.is_type_obj():
  4755. return typ.fallback.type.has_readable_member(member)
  4756. elif isinstance(typ, AnyType):
  4757. return True
  4758. elif isinstance(typ, UnionType):
  4759. result = all(self.has_member(x, member) for x in typ.relevant_items())
  4760. return result
  4761. elif isinstance(typ, TypeType):
  4762. # Type[Union[X, ...]] is always normalized to Union[Type[X], ...],
  4763. # so we don't need to care about unions here.
  4764. item = typ.item
  4765. if isinstance(item, TypeVarType):
  4766. item = get_proper_type(item.upper_bound)
  4767. if isinstance(item, TupleType):
  4768. item = tuple_fallback(item)
  4769. if isinstance(item, Instance) and item.type.metaclass_type is not None:
  4770. return self.has_member(item.type.metaclass_type, member)
  4771. if isinstance(item, AnyType):
  4772. return True
  4773. return False
  4774. else:
  4775. return False
  4776. def not_ready_callback(self, name: str, context: Context) -> None:
  4777. """Called when we can't infer the type of a variable because it's not ready yet.
  4778. Either defer type checking of the enclosing function to the next
  4779. pass or report an error.
  4780. """
  4781. self.chk.handle_cannot_determine_type(name, context)
  4782. def visit_yield_expr(self, e: YieldExpr) -> Type:
  4783. return_type = self.chk.return_types[-1]
  4784. expected_item_type = self.chk.get_generator_yield_type(return_type, False)
  4785. if e.expr is None:
  4786. if (
  4787. not isinstance(get_proper_type(expected_item_type), (NoneType, AnyType))
  4788. and self.chk.in_checked_function()
  4789. ):
  4790. self.chk.fail(message_registry.YIELD_VALUE_EXPECTED, e)
  4791. else:
  4792. actual_item_type = self.accept(e.expr, expected_item_type)
  4793. self.chk.check_subtype(
  4794. actual_item_type,
  4795. expected_item_type,
  4796. e,
  4797. message_registry.INCOMPATIBLE_TYPES_IN_YIELD,
  4798. "actual type",
  4799. "expected type",
  4800. )
  4801. return self.chk.get_generator_receive_type(return_type, False)
  4802. def visit_await_expr(self, e: AwaitExpr, allow_none_return: bool = False) -> Type:
  4803. expected_type = self.type_context[-1]
  4804. if expected_type is not None:
  4805. expected_type = self.chk.named_generic_type("typing.Awaitable", [expected_type])
  4806. actual_type = get_proper_type(self.accept(e.expr, expected_type))
  4807. if isinstance(actual_type, AnyType):
  4808. return AnyType(TypeOfAny.from_another_any, source_any=actual_type)
  4809. ret = self.check_awaitable_expr(
  4810. actual_type, e, message_registry.INCOMPATIBLE_TYPES_IN_AWAIT
  4811. )
  4812. if not allow_none_return and isinstance(get_proper_type(ret), NoneType):
  4813. self.chk.msg.does_not_return_value(None, e)
  4814. return ret
  4815. def check_awaitable_expr(
  4816. self, t: Type, ctx: Context, msg: str | ErrorMessage, ignore_binder: bool = False
  4817. ) -> Type:
  4818. """Check the argument to `await` and extract the type of value.
  4819. Also used by `async for` and `async with`.
  4820. """
  4821. if not self.chk.check_subtype(
  4822. t, self.named_type("typing.Awaitable"), ctx, msg, "actual type", "expected type"
  4823. ):
  4824. return AnyType(TypeOfAny.special_form)
  4825. else:
  4826. generator = self.check_method_call_by_name("__await__", t, [], [], ctx)[0]
  4827. ret_type = self.chk.get_generator_return_type(generator, False)
  4828. ret_type = get_proper_type(ret_type)
  4829. if (
  4830. not ignore_binder
  4831. and isinstance(ret_type, UninhabitedType)
  4832. and not ret_type.ambiguous
  4833. ):
  4834. self.chk.binder.unreachable()
  4835. return ret_type
  4836. def visit_yield_from_expr(self, e: YieldFromExpr, allow_none_return: bool = False) -> Type:
  4837. # NOTE: Whether `yield from` accepts an `async def` decorated
  4838. # with `@types.coroutine` (or `@asyncio.coroutine`) depends on
  4839. # whether the generator containing the `yield from` is itself
  4840. # thus decorated. But it accepts a generator regardless of
  4841. # how it's decorated.
  4842. return_type = self.chk.return_types[-1]
  4843. # TODO: What should the context for the sub-expression be?
  4844. # If the containing function has type Generator[X, Y, ...],
  4845. # the context should be Generator[X, Y, T], where T is the
  4846. # context of the 'yield from' itself (but it isn't known).
  4847. subexpr_type = get_proper_type(self.accept(e.expr))
  4848. # Check that the expr is an instance of Iterable and get the type of the iterator produced
  4849. # by __iter__.
  4850. if isinstance(subexpr_type, AnyType):
  4851. iter_type: Type = AnyType(TypeOfAny.from_another_any, source_any=subexpr_type)
  4852. elif self.chk.type_is_iterable(subexpr_type):
  4853. if is_async_def(subexpr_type) and not has_coroutine_decorator(return_type):
  4854. self.chk.msg.yield_from_invalid_operand_type(subexpr_type, e)
  4855. any_type = AnyType(TypeOfAny.special_form)
  4856. generic_generator_type = self.chk.named_generic_type(
  4857. "typing.Generator", [any_type, any_type, any_type]
  4858. )
  4859. iter_type, _ = self.check_method_call_by_name(
  4860. "__iter__", subexpr_type, [], [], context=generic_generator_type
  4861. )
  4862. else:
  4863. if not (is_async_def(subexpr_type) and has_coroutine_decorator(return_type)):
  4864. self.chk.msg.yield_from_invalid_operand_type(subexpr_type, e)
  4865. iter_type = AnyType(TypeOfAny.from_error)
  4866. else:
  4867. iter_type = self.check_awaitable_expr(
  4868. subexpr_type, e, message_registry.INCOMPATIBLE_TYPES_IN_YIELD_FROM
  4869. )
  4870. # Check that the iterator's item type matches the type yielded by the Generator function
  4871. # containing this `yield from` expression.
  4872. expected_item_type = self.chk.get_generator_yield_type(return_type, False)
  4873. actual_item_type = self.chk.get_generator_yield_type(iter_type, False)
  4874. self.chk.check_subtype(
  4875. actual_item_type,
  4876. expected_item_type,
  4877. e,
  4878. message_registry.INCOMPATIBLE_TYPES_IN_YIELD_FROM,
  4879. "actual type",
  4880. "expected type",
  4881. )
  4882. # Determine the type of the entire yield from expression.
  4883. iter_type = get_proper_type(iter_type)
  4884. if isinstance(iter_type, Instance) and iter_type.type.fullname == "typing.Generator":
  4885. expr_type = self.chk.get_generator_return_type(iter_type, False)
  4886. else:
  4887. # Non-Generators don't return anything from `yield from` expressions.
  4888. # However special-case Any (which might be produced by an error).
  4889. actual_item_type = get_proper_type(actual_item_type)
  4890. if isinstance(actual_item_type, AnyType):
  4891. expr_type = AnyType(TypeOfAny.from_another_any, source_any=actual_item_type)
  4892. else:
  4893. # Treat `Iterator[X]` as a shorthand for `Generator[X, None, Any]`.
  4894. expr_type = NoneType()
  4895. if not allow_none_return and isinstance(get_proper_type(expr_type), NoneType):
  4896. self.chk.msg.does_not_return_value(None, e)
  4897. return expr_type
  4898. def visit_temp_node(self, e: TempNode) -> Type:
  4899. return e.type
  4900. def visit_type_var_expr(self, e: TypeVarExpr) -> Type:
  4901. p_default = get_proper_type(e.default)
  4902. if not (
  4903. isinstance(p_default, AnyType)
  4904. and p_default.type_of_any == TypeOfAny.from_omitted_generics
  4905. ):
  4906. if not is_subtype(p_default, e.upper_bound):
  4907. self.chk.fail("TypeVar default must be a subtype of the bound type", e)
  4908. if e.values and not any(p_default == value for value in e.values):
  4909. self.chk.fail("TypeVar default must be one of the constraint types", e)
  4910. return AnyType(TypeOfAny.special_form)
  4911. def visit_paramspec_expr(self, e: ParamSpecExpr) -> Type:
  4912. return AnyType(TypeOfAny.special_form)
  4913. def visit_type_var_tuple_expr(self, e: TypeVarTupleExpr) -> Type:
  4914. return AnyType(TypeOfAny.special_form)
  4915. def visit_newtype_expr(self, e: NewTypeExpr) -> Type:
  4916. return AnyType(TypeOfAny.special_form)
  4917. def visit_namedtuple_expr(self, e: NamedTupleExpr) -> Type:
  4918. tuple_type = e.info.tuple_type
  4919. if tuple_type:
  4920. if self.chk.options.disallow_any_unimported and has_any_from_unimported_type(
  4921. tuple_type
  4922. ):
  4923. self.msg.unimported_type_becomes_any("NamedTuple type", tuple_type, e)
  4924. check_for_explicit_any(
  4925. tuple_type, self.chk.options, self.chk.is_typeshed_stub, self.msg, context=e
  4926. )
  4927. return AnyType(TypeOfAny.special_form)
  4928. def visit_enum_call_expr(self, e: EnumCallExpr) -> Type:
  4929. for name, value in zip(e.items, e.values):
  4930. if value is not None:
  4931. typ = self.accept(value)
  4932. if not isinstance(get_proper_type(typ), AnyType):
  4933. var = e.info.names[name].node
  4934. if isinstance(var, Var):
  4935. # Inline TypeChecker.set_inferred_type(),
  4936. # without the lvalue. (This doesn't really do
  4937. # much, since the value attribute is defined
  4938. # to have type Any in the typeshed stub.)
  4939. var.type = typ
  4940. var.is_inferred = True
  4941. return AnyType(TypeOfAny.special_form)
  4942. def visit_typeddict_expr(self, e: TypedDictExpr) -> Type:
  4943. return AnyType(TypeOfAny.special_form)
  4944. def visit__promote_expr(self, e: PromoteExpr) -> Type:
  4945. return e.type
  4946. def visit_star_expr(self, e: StarExpr) -> Type:
  4947. # TODO: should this ever be called (see e.g. mypyc visitor)?
  4948. return self.accept(e.expr)
  4949. def object_type(self) -> Instance:
  4950. """Return instance type 'object'."""
  4951. return self.named_type("builtins.object")
  4952. def bool_type(self) -> Instance:
  4953. """Return instance type 'bool'."""
  4954. return self.named_type("builtins.bool")
  4955. @overload
  4956. def narrow_type_from_binder(self, expr: Expression, known_type: Type) -> Type:
  4957. ...
  4958. @overload
  4959. def narrow_type_from_binder(
  4960. self, expr: Expression, known_type: Type, skip_non_overlapping: bool
  4961. ) -> Type | None:
  4962. ...
  4963. def narrow_type_from_binder(
  4964. self, expr: Expression, known_type: Type, skip_non_overlapping: bool = False
  4965. ) -> Type | None:
  4966. """Narrow down a known type of expression using information in conditional type binder.
  4967. If 'skip_non_overlapping' is True, return None if the type and restriction are
  4968. non-overlapping.
  4969. """
  4970. if literal(expr) >= LITERAL_TYPE:
  4971. restriction = self.chk.binder.get(expr)
  4972. # If the current node is deferred, some variables may get Any types that they
  4973. # otherwise wouldn't have. We don't want to narrow down these since it may
  4974. # produce invalid inferred Optional[Any] types, at least.
  4975. if restriction and not (
  4976. isinstance(get_proper_type(known_type), AnyType) and self.chk.current_node_deferred
  4977. ):
  4978. # Note: this call should match the one in narrow_declared_type().
  4979. if skip_non_overlapping and not is_overlapping_types(
  4980. known_type, restriction, prohibit_none_typevar_overlap=True
  4981. ):
  4982. return None
  4983. return narrow_declared_type(known_type, restriction)
  4984. return known_type
  4985. def has_abstract_type_part(self, caller_type: ProperType, callee_type: ProperType) -> bool:
  4986. # TODO: support other possible types here
  4987. if isinstance(caller_type, TupleType) and isinstance(callee_type, TupleType):
  4988. return any(
  4989. self.has_abstract_type(get_proper_type(caller), get_proper_type(callee))
  4990. for caller, callee in zip(caller_type.items, callee_type.items)
  4991. )
  4992. return self.has_abstract_type(caller_type, callee_type)
  4993. def has_abstract_type(self, caller_type: ProperType, callee_type: ProperType) -> bool:
  4994. return (
  4995. isinstance(caller_type, CallableType)
  4996. and isinstance(callee_type, TypeType)
  4997. and caller_type.is_type_obj()
  4998. and (caller_type.type_object().is_abstract or caller_type.type_object().is_protocol)
  4999. and isinstance(callee_type.item, Instance)
  5000. and (callee_type.item.type.is_abstract or callee_type.item.type.is_protocol)
  5001. and not self.chk.allow_abstract_call
  5002. )
  5003. def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
  5004. """Whether t contains an Any type"""
  5005. return t.accept(HasAnyType(ignore_in_type_obj))
  5006. class HasAnyType(types.BoolTypeQuery):
  5007. def __init__(self, ignore_in_type_obj: bool) -> None:
  5008. super().__init__(types.ANY_STRATEGY)
  5009. self.ignore_in_type_obj = ignore_in_type_obj
  5010. def visit_any(self, t: AnyType) -> bool:
  5011. return t.type_of_any != TypeOfAny.special_form # special forms are not real Any types
  5012. def visit_callable_type(self, t: CallableType) -> bool:
  5013. if self.ignore_in_type_obj and t.is_type_obj():
  5014. return False
  5015. return super().visit_callable_type(t)
  5016. def visit_type_var(self, t: TypeVarType) -> bool:
  5017. default = [t.default] if t.has_default() else []
  5018. return self.query_types([t.upper_bound, *default] + t.values)
  5019. def visit_param_spec(self, t: ParamSpecType) -> bool:
  5020. default = [t.default] if t.has_default() else []
  5021. return self.query_types([t.upper_bound, *default])
  5022. def visit_type_var_tuple(self, t: TypeVarTupleType) -> bool:
  5023. default = [t.default] if t.has_default() else []
  5024. return self.query_types([t.upper_bound, *default])
  5025. def has_coroutine_decorator(t: Type) -> bool:
  5026. """Whether t came from a function decorated with `@coroutine`."""
  5027. t = get_proper_type(t)
  5028. return isinstance(t, Instance) and t.type.fullname == "typing.AwaitableGenerator"
  5029. def is_async_def(t: Type) -> bool:
  5030. """Whether t came from a function defined using `async def`."""
  5031. # In check_func_def(), when we see a function decorated with
  5032. # `@typing.coroutine` or `@async.coroutine`, we change the
  5033. # return type to typing.AwaitableGenerator[...], so that its
  5034. # type is compatible with either Generator or Awaitable.
  5035. # But for the check here we need to know whether the original
  5036. # function (before decoration) was an `async def`. The
  5037. # AwaitableGenerator type conveniently preserves the original
  5038. # type as its 4th parameter (3rd when using 0-origin indexing
  5039. # :-), so that we can recover that information here.
  5040. # (We really need to see whether the original, undecorated
  5041. # function was an `async def`, which is orthogonal to its
  5042. # decorations.)
  5043. t = get_proper_type(t)
  5044. if (
  5045. isinstance(t, Instance)
  5046. and t.type.fullname == "typing.AwaitableGenerator"
  5047. and len(t.args) >= 4
  5048. ):
  5049. t = get_proper_type(t.args[3])
  5050. return isinstance(t, Instance) and t.type.fullname == "typing.Coroutine"
  5051. def is_non_empty_tuple(t: Type) -> bool:
  5052. t = get_proper_type(t)
  5053. return isinstance(t, TupleType) and bool(t.items)
  5054. def is_duplicate_mapping(
  5055. mapping: list[int], actual_types: list[Type], actual_kinds: list[ArgKind]
  5056. ) -> bool:
  5057. return (
  5058. len(mapping) > 1
  5059. # Multiple actuals can map to the same formal if they both come from
  5060. # varargs (*args and **kwargs); in this case at runtime it is possible
  5061. # that here are no duplicates. We need to allow this, as the convention
  5062. # f(..., *args, **kwargs) is common enough.
  5063. and not (
  5064. len(mapping) == 2
  5065. and actual_kinds[mapping[0]] == nodes.ARG_STAR
  5066. and actual_kinds[mapping[1]] == nodes.ARG_STAR2
  5067. )
  5068. # Multiple actuals can map to the same formal if there are multiple
  5069. # **kwargs which cannot be mapped with certainty (non-TypedDict
  5070. # **kwargs).
  5071. and not all(
  5072. actual_kinds[m] == nodes.ARG_STAR2
  5073. and not isinstance(get_proper_type(actual_types[m]), TypedDictType)
  5074. for m in mapping
  5075. )
  5076. )
  5077. def replace_callable_return_type(c: CallableType, new_ret_type: Type) -> CallableType:
  5078. """Return a copy of a callable type with a different return type."""
  5079. return c.copy_modified(ret_type=new_ret_type)
  5080. def apply_poly(tp: CallableType, poly_tvars: Sequence[TypeVarLikeType]) -> CallableType | None:
  5081. """Make free type variables generic in the type if possible.
  5082. This will translate the type `tp` while trying to create valid bindings for
  5083. type variables `poly_tvars` while traversing the type. This follows the same rules
  5084. as we do during semantic analysis phase, examples:
  5085. * Callable[Callable[[T], T], T] -> def [T] (def (T) -> T) -> T
  5086. * Callable[[], Callable[[T], T]] -> def () -> def [T] (T -> T)
  5087. * List[T] -> None (not possible)
  5088. """
  5089. try:
  5090. return tp.copy_modified(
  5091. arg_types=[t.accept(PolyTranslator(poly_tvars)) for t in tp.arg_types],
  5092. ret_type=tp.ret_type.accept(PolyTranslator(poly_tvars)),
  5093. variables=[],
  5094. )
  5095. except PolyTranslationError:
  5096. return None
  5097. class PolyTranslationError(Exception):
  5098. pass
  5099. class PolyTranslator(TypeTranslator):
  5100. """Make free type variables generic in the type if possible.
  5101. See docstring for apply_poly() for details.
  5102. """
  5103. def __init__(self, poly_tvars: Sequence[TypeVarLikeType]) -> None:
  5104. self.poly_tvars = set(poly_tvars)
  5105. # This is a simplified version of TypeVarScope used during semantic analysis.
  5106. self.bound_tvars: set[TypeVarLikeType] = set()
  5107. self.seen_aliases: set[TypeInfo] = set()
  5108. def visit_callable_type(self, t: CallableType) -> Type:
  5109. found_vars = set()
  5110. for arg in t.arg_types:
  5111. found_vars |= set(get_type_vars(arg)) & self.poly_tvars
  5112. found_vars -= self.bound_tvars
  5113. self.bound_tvars |= found_vars
  5114. result = super().visit_callable_type(t)
  5115. self.bound_tvars -= found_vars
  5116. assert isinstance(result, ProperType) and isinstance(result, CallableType)
  5117. result.variables = list(result.variables) + list(found_vars)
  5118. return result
  5119. def visit_type_var(self, t: TypeVarType) -> Type:
  5120. if t in self.poly_tvars and t not in self.bound_tvars:
  5121. raise PolyTranslationError()
  5122. return super().visit_type_var(t)
  5123. def visit_param_spec(self, t: ParamSpecType) -> Type:
  5124. # TODO: Support polymorphic apply for ParamSpec.
  5125. raise PolyTranslationError()
  5126. def visit_type_var_tuple(self, t: TypeVarTupleType) -> Type:
  5127. # TODO: Support polymorphic apply for TypeVarTuple.
  5128. raise PolyTranslationError()
  5129. def visit_type_alias_type(self, t: TypeAliasType) -> Type:
  5130. if not t.args:
  5131. return t.copy_modified()
  5132. if not t.is_recursive:
  5133. return get_proper_type(t).accept(self)
  5134. # We can't handle polymorphic application for recursive generic aliases
  5135. # without risking an infinite recursion, just give up for now.
  5136. raise PolyTranslationError()
  5137. def visit_instance(self, t: Instance) -> Type:
  5138. # There is the same problem with callback protocols as with aliases
  5139. # (callback protocols are essentially more flexible aliases to callables).
  5140. # Note: consider supporting bindings in instances, e.g. LRUCache[[x: T], T].
  5141. if t.args and t.type.is_protocol and t.type.protocol_members == ["__call__"]:
  5142. if t.type in self.seen_aliases:
  5143. raise PolyTranslationError()
  5144. self.seen_aliases.add(t.type)
  5145. call = find_member("__call__", t, t, is_operator=True)
  5146. assert call is not None
  5147. return call.accept(self)
  5148. return super().visit_instance(t)
  5149. class ArgInferSecondPassQuery(types.BoolTypeQuery):
  5150. """Query whether an argument type should be inferred in the second pass.
  5151. The result is True if the type has a type variable in a callable return
  5152. type anywhere. For example, the result for Callable[[], T] is True if t is
  5153. a type variable.
  5154. """
  5155. def __init__(self) -> None:
  5156. super().__init__(types.ANY_STRATEGY)
  5157. def visit_callable_type(self, t: CallableType) -> bool:
  5158. return self.query_types(t.arg_types) or t.accept(HasTypeVarQuery())
  5159. class HasTypeVarQuery(types.BoolTypeQuery):
  5160. """Visitor for querying whether a type has a type variable component."""
  5161. def __init__(self) -> None:
  5162. super().__init__(types.ANY_STRATEGY)
  5163. def visit_type_var(self, t: TypeVarType) -> bool:
  5164. return True
  5165. def has_erased_component(t: Type | None) -> bool:
  5166. return t is not None and t.accept(HasErasedComponentsQuery())
  5167. class HasErasedComponentsQuery(types.BoolTypeQuery):
  5168. """Visitor for querying whether a type has an erased component."""
  5169. def __init__(self) -> None:
  5170. super().__init__(types.ANY_STRATEGY)
  5171. def visit_erased_type(self, t: ErasedType) -> bool:
  5172. return True
  5173. def has_uninhabited_component(t: Type | None) -> bool:
  5174. return t is not None and t.accept(HasUninhabitedComponentsQuery())
  5175. class HasUninhabitedComponentsQuery(types.BoolTypeQuery):
  5176. """Visitor for querying whether a type has an UninhabitedType component."""
  5177. def __init__(self) -> None:
  5178. super().__init__(types.ANY_STRATEGY)
  5179. def visit_uninhabited_type(self, t: UninhabitedType) -> bool:
  5180. return True
  5181. def arg_approximate_similarity(actual: Type, formal: Type) -> bool:
  5182. """Return if caller argument (actual) is roughly compatible with signature arg (formal).
  5183. This function is deliberately loose and will report two types are similar
  5184. as long as their "shapes" are plausibly the same.
  5185. This is useful when we're doing error reporting: for example, if we're trying
  5186. to select an overload alternative and there's no exact match, we can use
  5187. this function to help us identify which alternative the user might have
  5188. *meant* to match.
  5189. """
  5190. actual = get_proper_type(actual)
  5191. formal = get_proper_type(formal)
  5192. # Erase typevars: we'll consider them all to have the same "shape".
  5193. if isinstance(actual, TypeVarType):
  5194. actual = erase_to_union_or_bound(actual)
  5195. if isinstance(formal, TypeVarType):
  5196. formal = erase_to_union_or_bound(formal)
  5197. # Callable or Type[...]-ish types
  5198. def is_typetype_like(typ: ProperType) -> bool:
  5199. return (
  5200. isinstance(typ, TypeType)
  5201. or (isinstance(typ, FunctionLike) and typ.is_type_obj())
  5202. or (isinstance(typ, Instance) and typ.type.fullname == "builtins.type")
  5203. )
  5204. if isinstance(formal, CallableType):
  5205. if isinstance(actual, (CallableType, Overloaded, TypeType)):
  5206. return True
  5207. if is_typetype_like(actual) and is_typetype_like(formal):
  5208. return True
  5209. # Unions
  5210. if isinstance(actual, UnionType):
  5211. return any(arg_approximate_similarity(item, formal) for item in actual.relevant_items())
  5212. if isinstance(formal, UnionType):
  5213. return any(arg_approximate_similarity(actual, item) for item in formal.relevant_items())
  5214. # TypedDicts
  5215. if isinstance(actual, TypedDictType):
  5216. if isinstance(formal, TypedDictType):
  5217. return True
  5218. return arg_approximate_similarity(actual.fallback, formal)
  5219. # Instances
  5220. # For instances, we mostly defer to the existing is_subtype check.
  5221. if isinstance(formal, Instance):
  5222. if isinstance(actual, CallableType):
  5223. actual = actual.fallback
  5224. if isinstance(actual, Overloaded):
  5225. actual = actual.items[0].fallback
  5226. if isinstance(actual, TupleType):
  5227. actual = tuple_fallback(actual)
  5228. if isinstance(actual, Instance) and formal.type in actual.type.mro:
  5229. # Try performing a quick check as an optimization
  5230. return True
  5231. # Fall back to a standard subtype check for the remaining kinds of type.
  5232. return is_subtype(erasetype.erase_type(actual), erasetype.erase_type(formal))
  5233. def any_causes_overload_ambiguity(
  5234. items: list[CallableType],
  5235. return_types: list[Type],
  5236. arg_types: list[Type],
  5237. arg_kinds: list[ArgKind],
  5238. arg_names: Sequence[str | None] | None,
  5239. ) -> bool:
  5240. """May an argument containing 'Any' cause ambiguous result type on call to overloaded function?
  5241. Note that this sometimes returns True even if there is no ambiguity, since a correct
  5242. implementation would be complex (and the call would be imprecisely typed due to Any
  5243. types anyway).
  5244. Args:
  5245. items: Overload items matching the actual arguments
  5246. arg_types: Actual argument types
  5247. arg_kinds: Actual argument kinds
  5248. arg_names: Actual argument names
  5249. """
  5250. if all_same_types(return_types):
  5251. return False
  5252. actual_to_formal = [
  5253. map_formals_to_actuals(
  5254. arg_kinds, arg_names, item.arg_kinds, item.arg_names, lambda i: arg_types[i]
  5255. )
  5256. for item in items
  5257. ]
  5258. for arg_idx, arg_type in enumerate(arg_types):
  5259. # We ignore Anys in type object callables as ambiguity
  5260. # creators, since that can lead to falsely claiming ambiguity
  5261. # for overloads between Type and Callable.
  5262. if has_any_type(arg_type, ignore_in_type_obj=True):
  5263. matching_formals_unfiltered = [
  5264. (item_idx, lookup[arg_idx])
  5265. for item_idx, lookup in enumerate(actual_to_formal)
  5266. if lookup[arg_idx]
  5267. ]
  5268. matching_returns = []
  5269. matching_formals = []
  5270. for item_idx, formals in matching_formals_unfiltered:
  5271. matched_callable = items[item_idx]
  5272. matching_returns.append(matched_callable.ret_type)
  5273. # Note: if an actual maps to multiple formals of differing types within
  5274. # a single callable, then we know at least one of those formals must be
  5275. # a different type then the formal(s) in some other callable.
  5276. # So it's safe to just append everything to the same list.
  5277. for formal in formals:
  5278. matching_formals.append(matched_callable.arg_types[formal])
  5279. if not all_same_types(matching_formals) and not all_same_types(matching_returns):
  5280. # Any maps to multiple different types, and the return types of these items differ.
  5281. return True
  5282. return False
  5283. def all_same_types(types: list[Type]) -> bool:
  5284. if not types:
  5285. return True
  5286. return all(is_same_type(t, types[0]) for t in types[1:])
  5287. def merge_typevars_in_callables_by_name(
  5288. callables: Sequence[CallableType],
  5289. ) -> tuple[list[CallableType], list[TypeVarType]]:
  5290. """Takes all the typevars present in the callables and 'combines' the ones with the same name.
  5291. For example, suppose we have two callables with signatures "f(x: T, y: S) -> T" and
  5292. "f(x: List[Tuple[T, S]]) -> Tuple[T, S]". Both callables use typevars named "T" and
  5293. "S", but we treat them as distinct, unrelated typevars. (E.g. they could both have
  5294. distinct ids.)
  5295. If we pass in both callables into this function, it returns a list containing two
  5296. new callables that are identical in signature, but use the same underlying TypeVarType
  5297. for T and S.
  5298. This is useful if we want to take the output lists and "merge" them into one callable
  5299. in some way -- for example, when unioning together overloads.
  5300. Returns both the new list of callables and a list of all distinct TypeVarType objects used.
  5301. """
  5302. output: list[CallableType] = []
  5303. unique_typevars: dict[str, TypeVarType] = {}
  5304. variables: list[TypeVarType] = []
  5305. for target in callables:
  5306. if target.is_generic():
  5307. target = freshen_function_type_vars(target)
  5308. rename = {} # Dict[TypeVarId, TypeVar]
  5309. for tv in target.variables:
  5310. name = tv.fullname
  5311. if name not in unique_typevars:
  5312. # TODO(PEP612): fix for ParamSpecType
  5313. if isinstance(tv, ParamSpecType):
  5314. continue
  5315. assert isinstance(tv, TypeVarType)
  5316. unique_typevars[name] = tv
  5317. variables.append(tv)
  5318. rename[tv.id] = unique_typevars[name]
  5319. target = expand_type(target, rename)
  5320. output.append(target)
  5321. return output, variables
  5322. def try_getting_literal(typ: Type) -> ProperType:
  5323. """If possible, get a more precise literal type for a given type."""
  5324. typ = get_proper_type(typ)
  5325. if isinstance(typ, Instance) and typ.last_known_value is not None:
  5326. return typ.last_known_value
  5327. return typ
  5328. def is_expr_literal_type(node: Expression) -> bool:
  5329. """Returns 'true' if the given node is a Literal"""
  5330. if isinstance(node, IndexExpr):
  5331. base = node.base
  5332. return isinstance(base, RefExpr) and base.fullname in LITERAL_TYPE_NAMES
  5333. if isinstance(node, NameExpr):
  5334. underlying = node.node
  5335. return isinstance(underlying, TypeAlias) and isinstance(
  5336. get_proper_type(underlying.target), LiteralType
  5337. )
  5338. return False
  5339. def has_bytes_component(typ: Type) -> bool:
  5340. """Is this one of builtin byte types, or a union that contains it?"""
  5341. typ = get_proper_type(typ)
  5342. byte_types = {"builtins.bytes", "builtins.bytearray"}
  5343. if isinstance(typ, UnionType):
  5344. return any(has_bytes_component(t) for t in typ.items)
  5345. if isinstance(typ, Instance) and typ.type.fullname in byte_types:
  5346. return True
  5347. return False
  5348. def type_info_from_type(typ: Type) -> TypeInfo | None:
  5349. """Gets the TypeInfo for a type, indirecting through things like type variables and tuples."""
  5350. typ = get_proper_type(typ)
  5351. if isinstance(typ, FunctionLike) and typ.is_type_obj():
  5352. return typ.type_object()
  5353. if isinstance(typ, TypeType):
  5354. typ = typ.item
  5355. if isinstance(typ, TypeVarType):
  5356. typ = get_proper_type(typ.upper_bound)
  5357. if isinstance(typ, TupleType):
  5358. typ = tuple_fallback(typ)
  5359. if isinstance(typ, Instance):
  5360. return typ.type
  5361. # A complicated type. Too tricky, give up.
  5362. # TODO: Do something more clever here.
  5363. return None
  5364. def is_operator_method(fullname: str | None) -> bool:
  5365. if not fullname:
  5366. return False
  5367. short_name = fullname.split(".")[-1]
  5368. return (
  5369. short_name in operators.op_methods.values()
  5370. or short_name in operators.reverse_op_methods.values()
  5371. or short_name in operators.unary_op_methods.values()
  5372. )
  5373. def get_partial_instance_type(t: Type | None) -> PartialType | None:
  5374. if t is None or not isinstance(t, PartialType) or t.type is None:
  5375. return None
  5376. return t