typing_extensions.py 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570
  1. import abc
  2. import builtins
  3. import collections
  4. import collections.abc
  5. import contextlib
  6. import enum
  7. import functools
  8. import inspect
  9. import keyword
  10. import operator
  11. import sys
  12. import types as _types
  13. import typing
  14. import warnings
  15. __all__ = [
  16. # Super-special typing primitives.
  17. 'Any',
  18. 'ClassVar',
  19. 'Concatenate',
  20. 'Final',
  21. 'LiteralString',
  22. 'ParamSpec',
  23. 'ParamSpecArgs',
  24. 'ParamSpecKwargs',
  25. 'Self',
  26. 'Type',
  27. 'TypeVar',
  28. 'TypeVarTuple',
  29. 'Unpack',
  30. # ABCs (from collections.abc).
  31. 'Awaitable',
  32. 'AsyncIterator',
  33. 'AsyncIterable',
  34. 'Coroutine',
  35. 'AsyncGenerator',
  36. 'AsyncContextManager',
  37. 'Buffer',
  38. 'ChainMap',
  39. # Concrete collection types.
  40. 'ContextManager',
  41. 'Counter',
  42. 'Deque',
  43. 'DefaultDict',
  44. 'NamedTuple',
  45. 'OrderedDict',
  46. 'TypedDict',
  47. # Structural checks, a.k.a. protocols.
  48. 'SupportsAbs',
  49. 'SupportsBytes',
  50. 'SupportsComplex',
  51. 'SupportsFloat',
  52. 'SupportsIndex',
  53. 'SupportsInt',
  54. 'SupportsRound',
  55. # One-off things.
  56. 'Annotated',
  57. 'assert_never',
  58. 'assert_type',
  59. 'clear_overloads',
  60. 'dataclass_transform',
  61. 'deprecated',
  62. 'Doc',
  63. 'evaluate_forward_ref',
  64. 'get_overloads',
  65. 'final',
  66. 'Format',
  67. 'get_annotations',
  68. 'get_args',
  69. 'get_origin',
  70. 'get_original_bases',
  71. 'get_protocol_members',
  72. 'get_type_hints',
  73. 'IntVar',
  74. 'is_protocol',
  75. 'is_typeddict',
  76. 'Literal',
  77. 'NewType',
  78. 'overload',
  79. 'override',
  80. 'Protocol',
  81. 'reveal_type',
  82. 'runtime',
  83. 'runtime_checkable',
  84. 'Text',
  85. 'TypeAlias',
  86. 'TypeAliasType',
  87. 'TypeForm',
  88. 'TypeGuard',
  89. 'TypeIs',
  90. 'TYPE_CHECKING',
  91. 'Never',
  92. 'NoReturn',
  93. 'ReadOnly',
  94. 'Required',
  95. 'NotRequired',
  96. 'NoDefault',
  97. 'NoExtraItems',
  98. # Pure aliases, have always been in typing
  99. 'AbstractSet',
  100. 'AnyStr',
  101. 'BinaryIO',
  102. 'Callable',
  103. 'Collection',
  104. 'Container',
  105. 'Dict',
  106. 'ForwardRef',
  107. 'FrozenSet',
  108. 'Generator',
  109. 'Generic',
  110. 'Hashable',
  111. 'IO',
  112. 'ItemsView',
  113. 'Iterable',
  114. 'Iterator',
  115. 'KeysView',
  116. 'List',
  117. 'Mapping',
  118. 'MappingView',
  119. 'Match',
  120. 'MutableMapping',
  121. 'MutableSequence',
  122. 'MutableSet',
  123. 'Optional',
  124. 'Pattern',
  125. 'Reversible',
  126. 'Sequence',
  127. 'Set',
  128. 'Sized',
  129. 'TextIO',
  130. 'Tuple',
  131. 'Union',
  132. 'ValuesView',
  133. 'cast',
  134. 'no_type_check',
  135. 'no_type_check_decorator',
  136. ]
  137. # for backward compatibility
  138. PEP_560 = True
  139. GenericMeta = type
  140. _PEP_696_IMPLEMENTED = sys.version_info >= (3, 13, 0, "beta")
  141. # Added with bpo-45166 to 3.10.1+ and some 3.9 versions
  142. _FORWARD_REF_HAS_CLASS = "__forward_is_class__" in typing.ForwardRef.__slots__
  143. # The functions below are modified copies of typing internal helpers.
  144. # They are needed by _ProtocolMeta and they provide support for PEP 646.
  145. class _Sentinel:
  146. def __repr__(self):
  147. return "<sentinel>"
  148. _marker = _Sentinel()
  149. if sys.version_info >= (3, 10):
  150. def _should_collect_from_parameters(t):
  151. return isinstance(
  152. t, (typing._GenericAlias, _types.GenericAlias, _types.UnionType)
  153. )
  154. elif sys.version_info >= (3, 9):
  155. def _should_collect_from_parameters(t):
  156. return isinstance(t, (typing._GenericAlias, _types.GenericAlias))
  157. else:
  158. def _should_collect_from_parameters(t):
  159. return isinstance(t, typing._GenericAlias) and not t._special
  160. NoReturn = typing.NoReturn
  161. # Some unconstrained type variables. These are used by the container types.
  162. # (These are not for export.)
  163. T = typing.TypeVar('T') # Any type.
  164. KT = typing.TypeVar('KT') # Key type.
  165. VT = typing.TypeVar('VT') # Value type.
  166. T_co = typing.TypeVar('T_co', covariant=True) # Any type covariant containers.
  167. T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.
  168. if sys.version_info >= (3, 11):
  169. from typing import Any
  170. else:
  171. class _AnyMeta(type):
  172. def __instancecheck__(self, obj):
  173. if self is Any:
  174. raise TypeError("typing_extensions.Any cannot be used with isinstance()")
  175. return super().__instancecheck__(obj)
  176. def __repr__(self):
  177. if self is Any:
  178. return "typing_extensions.Any"
  179. return super().__repr__()
  180. class Any(metaclass=_AnyMeta):
  181. """Special type indicating an unconstrained type.
  182. - Any is compatible with every type.
  183. - Any assumed to have all methods.
  184. - All values assumed to be instances of Any.
  185. Note that all the above statements are true from the point of view of
  186. static type checkers. At runtime, Any should not be used with instance
  187. checks.
  188. """
  189. def __new__(cls, *args, **kwargs):
  190. if cls is Any:
  191. raise TypeError("Any cannot be instantiated")
  192. return super().__new__(cls, *args, **kwargs)
  193. ClassVar = typing.ClassVar
  194. class _ExtensionsSpecialForm(typing._SpecialForm, _root=True):
  195. def __repr__(self):
  196. return 'typing_extensions.' + self._name
  197. Final = typing.Final
  198. if sys.version_info >= (3, 11):
  199. final = typing.final
  200. else:
  201. # @final exists in 3.8+, but we backport it for all versions
  202. # before 3.11 to keep support for the __final__ attribute.
  203. # See https://bugs.python.org/issue46342
  204. def final(f):
  205. """This decorator can be used to indicate to type checkers that
  206. the decorated method cannot be overridden, and decorated class
  207. cannot be subclassed. For example:
  208. class Base:
  209. @final
  210. def done(self) -> None:
  211. ...
  212. class Sub(Base):
  213. def done(self) -> None: # Error reported by type checker
  214. ...
  215. @final
  216. class Leaf:
  217. ...
  218. class Other(Leaf): # Error reported by type checker
  219. ...
  220. There is no runtime checking of these properties. The decorator
  221. sets the ``__final__`` attribute to ``True`` on the decorated object
  222. to allow runtime introspection.
  223. """
  224. try:
  225. f.__final__ = True
  226. except (AttributeError, TypeError):
  227. # Skip the attribute silently if it is not writable.
  228. # AttributeError happens if the object has __slots__ or a
  229. # read-only property, TypeError if it's a builtin class.
  230. pass
  231. return f
  232. def IntVar(name):
  233. return typing.TypeVar(name)
  234. # A Literal bug was fixed in 3.11.0, 3.10.1 and 3.9.8
  235. if sys.version_info >= (3, 10, 1):
  236. Literal = typing.Literal
  237. else:
  238. def _flatten_literal_params(parameters):
  239. """An internal helper for Literal creation: flatten Literals among parameters"""
  240. params = []
  241. for p in parameters:
  242. if isinstance(p, _LiteralGenericAlias):
  243. params.extend(p.__args__)
  244. else:
  245. params.append(p)
  246. return tuple(params)
  247. def _value_and_type_iter(params):
  248. for p in params:
  249. yield p, type(p)
  250. class _LiteralGenericAlias(typing._GenericAlias, _root=True):
  251. def __eq__(self, other):
  252. if not isinstance(other, _LiteralGenericAlias):
  253. return NotImplemented
  254. these_args_deduped = set(_value_and_type_iter(self.__args__))
  255. other_args_deduped = set(_value_and_type_iter(other.__args__))
  256. return these_args_deduped == other_args_deduped
  257. def __hash__(self):
  258. return hash(frozenset(_value_and_type_iter(self.__args__)))
  259. class _LiteralForm(_ExtensionsSpecialForm, _root=True):
  260. def __init__(self, doc: str):
  261. self._name = 'Literal'
  262. self._doc = self.__doc__ = doc
  263. def __getitem__(self, parameters):
  264. if not isinstance(parameters, tuple):
  265. parameters = (parameters,)
  266. parameters = _flatten_literal_params(parameters)
  267. val_type_pairs = list(_value_and_type_iter(parameters))
  268. try:
  269. deduped_pairs = set(val_type_pairs)
  270. except TypeError:
  271. # unhashable parameters
  272. pass
  273. else:
  274. # similar logic to typing._deduplicate on Python 3.9+
  275. if len(deduped_pairs) < len(val_type_pairs):
  276. new_parameters = []
  277. for pair in val_type_pairs:
  278. if pair in deduped_pairs:
  279. new_parameters.append(pair[0])
  280. deduped_pairs.remove(pair)
  281. assert not deduped_pairs, deduped_pairs
  282. parameters = tuple(new_parameters)
  283. return _LiteralGenericAlias(self, parameters)
  284. Literal = _LiteralForm(doc="""\
  285. A type that can be used to indicate to type checkers
  286. that the corresponding value has a value literally equivalent
  287. to the provided parameter. For example:
  288. var: Literal[4] = 4
  289. The type checker understands that 'var' is literally equal to
  290. the value 4 and no other value.
  291. Literal[...] cannot be subclassed. There is no runtime
  292. checking verifying that the parameter is actually a value
  293. instead of a type.""")
  294. _overload_dummy = typing._overload_dummy
  295. if hasattr(typing, "get_overloads"): # 3.11+
  296. overload = typing.overload
  297. get_overloads = typing.get_overloads
  298. clear_overloads = typing.clear_overloads
  299. else:
  300. # {module: {qualname: {firstlineno: func}}}
  301. _overload_registry = collections.defaultdict(
  302. functools.partial(collections.defaultdict, dict)
  303. )
  304. def overload(func):
  305. """Decorator for overloaded functions/methods.
  306. In a stub file, place two or more stub definitions for the same
  307. function in a row, each decorated with @overload. For example:
  308. @overload
  309. def utf8(value: None) -> None: ...
  310. @overload
  311. def utf8(value: bytes) -> bytes: ...
  312. @overload
  313. def utf8(value: str) -> bytes: ...
  314. In a non-stub file (i.e. a regular .py file), do the same but
  315. follow it with an implementation. The implementation should *not*
  316. be decorated with @overload. For example:
  317. @overload
  318. def utf8(value: None) -> None: ...
  319. @overload
  320. def utf8(value: bytes) -> bytes: ...
  321. @overload
  322. def utf8(value: str) -> bytes: ...
  323. def utf8(value):
  324. # implementation goes here
  325. The overloads for a function can be retrieved at runtime using the
  326. get_overloads() function.
  327. """
  328. # classmethod and staticmethod
  329. f = getattr(func, "__func__", func)
  330. try:
  331. _overload_registry[f.__module__][f.__qualname__][
  332. f.__code__.co_firstlineno
  333. ] = func
  334. except AttributeError:
  335. # Not a normal function; ignore.
  336. pass
  337. return _overload_dummy
  338. def get_overloads(func):
  339. """Return all defined overloads for *func* as a sequence."""
  340. # classmethod and staticmethod
  341. f = getattr(func, "__func__", func)
  342. if f.__module__ not in _overload_registry:
  343. return []
  344. mod_dict = _overload_registry[f.__module__]
  345. if f.__qualname__ not in mod_dict:
  346. return []
  347. return list(mod_dict[f.__qualname__].values())
  348. def clear_overloads():
  349. """Clear all overloads in the registry."""
  350. _overload_registry.clear()
  351. # This is not a real generic class. Don't use outside annotations.
  352. Type = typing.Type
  353. # Various ABCs mimicking those in collections.abc.
  354. # A few are simply re-exported for completeness.
  355. Awaitable = typing.Awaitable
  356. Coroutine = typing.Coroutine
  357. AsyncIterable = typing.AsyncIterable
  358. AsyncIterator = typing.AsyncIterator
  359. Deque = typing.Deque
  360. DefaultDict = typing.DefaultDict
  361. OrderedDict = typing.OrderedDict
  362. Counter = typing.Counter
  363. ChainMap = typing.ChainMap
  364. Text = typing.Text
  365. TYPE_CHECKING = typing.TYPE_CHECKING
  366. if sys.version_info >= (3, 13, 0, "beta"):
  367. from typing import AsyncContextManager, AsyncGenerator, ContextManager, Generator
  368. else:
  369. def _is_dunder(attr):
  370. return attr.startswith('__') and attr.endswith('__')
  371. # Python <3.9 doesn't have typing._SpecialGenericAlias
  372. _special_generic_alias_base = getattr(
  373. typing, "_SpecialGenericAlias", typing._GenericAlias
  374. )
  375. class _SpecialGenericAlias(_special_generic_alias_base, _root=True):
  376. def __init__(self, origin, nparams, *, inst=True, name=None, defaults=()):
  377. if _special_generic_alias_base is typing._GenericAlias:
  378. # Python <3.9
  379. self.__origin__ = origin
  380. self._nparams = nparams
  381. super().__init__(origin, nparams, special=True, inst=inst, name=name)
  382. else:
  383. # Python >= 3.9
  384. super().__init__(origin, nparams, inst=inst, name=name)
  385. self._defaults = defaults
  386. def __setattr__(self, attr, val):
  387. allowed_attrs = {'_name', '_inst', '_nparams', '_defaults'}
  388. if _special_generic_alias_base is typing._GenericAlias:
  389. # Python <3.9
  390. allowed_attrs.add("__origin__")
  391. if _is_dunder(attr) or attr in allowed_attrs:
  392. object.__setattr__(self, attr, val)
  393. else:
  394. setattr(self.__origin__, attr, val)
  395. @typing._tp_cache
  396. def __getitem__(self, params):
  397. if not isinstance(params, tuple):
  398. params = (params,)
  399. msg = "Parameters to generic types must be types."
  400. params = tuple(typing._type_check(p, msg) for p in params)
  401. if (
  402. self._defaults
  403. and len(params) < self._nparams
  404. and len(params) + len(self._defaults) >= self._nparams
  405. ):
  406. params = (*params, *self._defaults[len(params) - self._nparams:])
  407. actual_len = len(params)
  408. if actual_len != self._nparams:
  409. if self._defaults:
  410. expected = f"at least {self._nparams - len(self._defaults)}"
  411. else:
  412. expected = str(self._nparams)
  413. if not self._nparams:
  414. raise TypeError(f"{self} is not a generic class")
  415. raise TypeError(
  416. f"Too {'many' if actual_len > self._nparams else 'few'}"
  417. f" arguments for {self};"
  418. f" actual {actual_len}, expected {expected}"
  419. )
  420. return self.copy_with(params)
  421. _NoneType = type(None)
  422. Generator = _SpecialGenericAlias(
  423. collections.abc.Generator, 3, defaults=(_NoneType, _NoneType)
  424. )
  425. AsyncGenerator = _SpecialGenericAlias(
  426. collections.abc.AsyncGenerator, 2, defaults=(_NoneType,)
  427. )
  428. ContextManager = _SpecialGenericAlias(
  429. contextlib.AbstractContextManager,
  430. 2,
  431. name="ContextManager",
  432. defaults=(typing.Optional[bool],)
  433. )
  434. AsyncContextManager = _SpecialGenericAlias(
  435. contextlib.AbstractAsyncContextManager,
  436. 2,
  437. name="AsyncContextManager",
  438. defaults=(typing.Optional[bool],)
  439. )
  440. _PROTO_ALLOWLIST = {
  441. 'collections.abc': [
  442. 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
  443. 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer',
  444. ],
  445. 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
  446. 'typing_extensions': ['Buffer'],
  447. }
  448. _EXCLUDED_ATTRS = frozenset(typing.EXCLUDED_ATTRIBUTES) | {
  449. "__match_args__", "__protocol_attrs__", "__non_callable_proto_members__",
  450. "__final__",
  451. }
  452. def _get_protocol_attrs(cls):
  453. attrs = set()
  454. for base in cls.__mro__[:-1]: # without object
  455. if base.__name__ in {'Protocol', 'Generic'}:
  456. continue
  457. annotations = getattr(base, '__annotations__', {})
  458. for attr in (*base.__dict__, *annotations):
  459. if (not attr.startswith('_abc_') and attr not in _EXCLUDED_ATTRS):
  460. attrs.add(attr)
  461. return attrs
  462. def _caller(depth=2):
  463. try:
  464. return sys._getframe(depth).f_globals.get('__name__', '__main__')
  465. except (AttributeError, ValueError): # For platforms without _getframe()
  466. return None
  467. # `__match_args__` attribute was removed from protocol members in 3.13,
  468. # we want to backport this change to older Python versions.
  469. if sys.version_info >= (3, 13):
  470. Protocol = typing.Protocol
  471. else:
  472. def _allow_reckless_class_checks(depth=3):
  473. """Allow instance and class checks for special stdlib modules.
  474. The abc and functools modules indiscriminately call isinstance() and
  475. issubclass() on the whole MRO of a user class, which may contain protocols.
  476. """
  477. return _caller(depth) in {'abc', 'functools', None}
  478. def _no_init(self, *args, **kwargs):
  479. if type(self)._is_protocol:
  480. raise TypeError('Protocols cannot be instantiated')
  481. def _type_check_issubclass_arg_1(arg):
  482. """Raise TypeError if `arg` is not an instance of `type`
  483. in `issubclass(arg, <protocol>)`.
  484. In most cases, this is verified by type.__subclasscheck__.
  485. Checking it again unnecessarily would slow down issubclass() checks,
  486. so, we don't perform this check unless we absolutely have to.
  487. For various error paths, however,
  488. we want to ensure that *this* error message is shown to the user
  489. where relevant, rather than a typing.py-specific error message.
  490. """
  491. if not isinstance(arg, type):
  492. # Same error message as for issubclass(1, int).
  493. raise TypeError('issubclass() arg 1 must be a class')
  494. # Inheriting from typing._ProtocolMeta isn't actually desirable,
  495. # but is necessary to allow typing.Protocol and typing_extensions.Protocol
  496. # to mix without getting TypeErrors about "metaclass conflict"
  497. class _ProtocolMeta(type(typing.Protocol)):
  498. # This metaclass is somewhat unfortunate,
  499. # but is necessary for several reasons...
  500. #
  501. # NOTE: DO NOT call super() in any methods in this class
  502. # That would call the methods on typing._ProtocolMeta on Python 3.8-3.11
  503. # and those are slow
  504. def __new__(mcls, name, bases, namespace, **kwargs):
  505. if name == "Protocol" and len(bases) < 2:
  506. pass
  507. elif {Protocol, typing.Protocol} & set(bases):
  508. for base in bases:
  509. if not (
  510. base in {object, typing.Generic, Protocol, typing.Protocol}
  511. or base.__name__ in _PROTO_ALLOWLIST.get(base.__module__, [])
  512. or is_protocol(base)
  513. ):
  514. raise TypeError(
  515. f"Protocols can only inherit from other protocols, "
  516. f"got {base!r}"
  517. )
  518. return abc.ABCMeta.__new__(mcls, name, bases, namespace, **kwargs)
  519. def __init__(cls, *args, **kwargs):
  520. abc.ABCMeta.__init__(cls, *args, **kwargs)
  521. if getattr(cls, "_is_protocol", False):
  522. cls.__protocol_attrs__ = _get_protocol_attrs(cls)
  523. def __subclasscheck__(cls, other):
  524. if cls is Protocol:
  525. return type.__subclasscheck__(cls, other)
  526. if (
  527. getattr(cls, '_is_protocol', False)
  528. and not _allow_reckless_class_checks()
  529. ):
  530. if not getattr(cls, '_is_runtime_protocol', False):
  531. _type_check_issubclass_arg_1(other)
  532. raise TypeError(
  533. "Instance and class checks can only be used with "
  534. "@runtime_checkable protocols"
  535. )
  536. if (
  537. # this attribute is set by @runtime_checkable:
  538. cls.__non_callable_proto_members__
  539. and cls.__dict__.get("__subclasshook__") is _proto_hook
  540. ):
  541. _type_check_issubclass_arg_1(other)
  542. non_method_attrs = sorted(cls.__non_callable_proto_members__)
  543. raise TypeError(
  544. "Protocols with non-method members don't support issubclass()."
  545. f" Non-method members: {str(non_method_attrs)[1:-1]}."
  546. )
  547. return abc.ABCMeta.__subclasscheck__(cls, other)
  548. def __instancecheck__(cls, instance):
  549. # We need this method for situations where attributes are
  550. # assigned in __init__.
  551. if cls is Protocol:
  552. return type.__instancecheck__(cls, instance)
  553. if not getattr(cls, "_is_protocol", False):
  554. # i.e., it's a concrete subclass of a protocol
  555. return abc.ABCMeta.__instancecheck__(cls, instance)
  556. if (
  557. not getattr(cls, '_is_runtime_protocol', False) and
  558. not _allow_reckless_class_checks()
  559. ):
  560. raise TypeError("Instance and class checks can only be used with"
  561. " @runtime_checkable protocols")
  562. if abc.ABCMeta.__instancecheck__(cls, instance):
  563. return True
  564. for attr in cls.__protocol_attrs__:
  565. try:
  566. val = inspect.getattr_static(instance, attr)
  567. except AttributeError:
  568. break
  569. # this attribute is set by @runtime_checkable:
  570. if val is None and attr not in cls.__non_callable_proto_members__:
  571. break
  572. else:
  573. return True
  574. return False
  575. def __eq__(cls, other):
  576. # Hack so that typing.Generic.__class_getitem__
  577. # treats typing_extensions.Protocol
  578. # as equivalent to typing.Protocol
  579. if abc.ABCMeta.__eq__(cls, other) is True:
  580. return True
  581. return cls is Protocol and other is typing.Protocol
  582. # This has to be defined, or the abc-module cache
  583. # complains about classes with this metaclass being unhashable,
  584. # if we define only __eq__!
  585. def __hash__(cls) -> int:
  586. return type.__hash__(cls)
  587. @classmethod
  588. def _proto_hook(cls, other):
  589. if not cls.__dict__.get('_is_protocol', False):
  590. return NotImplemented
  591. for attr in cls.__protocol_attrs__:
  592. for base in other.__mro__:
  593. # Check if the members appears in the class dictionary...
  594. if attr in base.__dict__:
  595. if base.__dict__[attr] is None:
  596. return NotImplemented
  597. break
  598. # ...or in annotations, if it is a sub-protocol.
  599. annotations = getattr(base, '__annotations__', {})
  600. if (
  601. isinstance(annotations, collections.abc.Mapping)
  602. and attr in annotations
  603. and is_protocol(other)
  604. ):
  605. break
  606. else:
  607. return NotImplemented
  608. return True
  609. class Protocol(typing.Generic, metaclass=_ProtocolMeta):
  610. __doc__ = typing.Protocol.__doc__
  611. __slots__ = ()
  612. _is_protocol = True
  613. _is_runtime_protocol = False
  614. def __init_subclass__(cls, *args, **kwargs):
  615. super().__init_subclass__(*args, **kwargs)
  616. # Determine if this is a protocol or a concrete subclass.
  617. if not cls.__dict__.get('_is_protocol', False):
  618. cls._is_protocol = any(b is Protocol for b in cls.__bases__)
  619. # Set (or override) the protocol subclass hook.
  620. if '__subclasshook__' not in cls.__dict__:
  621. cls.__subclasshook__ = _proto_hook
  622. # Prohibit instantiation for protocol classes
  623. if cls._is_protocol and cls.__init__ is Protocol.__init__:
  624. cls.__init__ = _no_init
  625. if sys.version_info >= (3, 13):
  626. runtime_checkable = typing.runtime_checkable
  627. else:
  628. def runtime_checkable(cls):
  629. """Mark a protocol class as a runtime protocol.
  630. Such protocol can be used with isinstance() and issubclass().
  631. Raise TypeError if applied to a non-protocol class.
  632. This allows a simple-minded structural check very similar to
  633. one trick ponies in collections.abc such as Iterable.
  634. For example::
  635. @runtime_checkable
  636. class Closable(Protocol):
  637. def close(self): ...
  638. assert isinstance(open('/some/file'), Closable)
  639. Warning: this will check only the presence of the required methods,
  640. not their type signatures!
  641. """
  642. if not issubclass(cls, typing.Generic) or not getattr(cls, '_is_protocol', False):
  643. raise TypeError(f'@runtime_checkable can be only applied to protocol classes,'
  644. f' got {cls!r}')
  645. cls._is_runtime_protocol = True
  646. # typing.Protocol classes on <=3.11 break if we execute this block,
  647. # because typing.Protocol classes on <=3.11 don't have a
  648. # `__protocol_attrs__` attribute, and this block relies on the
  649. # `__protocol_attrs__` attribute. Meanwhile, typing.Protocol classes on 3.12.2+
  650. # break if we *don't* execute this block, because *they* assume that all
  651. # protocol classes have a `__non_callable_proto_members__` attribute
  652. # (which this block sets)
  653. if isinstance(cls, _ProtocolMeta) or sys.version_info >= (3, 12, 2):
  654. # PEP 544 prohibits using issubclass()
  655. # with protocols that have non-method members.
  656. # See gh-113320 for why we compute this attribute here,
  657. # rather than in `_ProtocolMeta.__init__`
  658. cls.__non_callable_proto_members__ = set()
  659. for attr in cls.__protocol_attrs__:
  660. try:
  661. is_callable = callable(getattr(cls, attr, None))
  662. except Exception as e:
  663. raise TypeError(
  664. f"Failed to determine whether protocol member {attr!r} "
  665. "is a method member"
  666. ) from e
  667. else:
  668. if not is_callable:
  669. cls.__non_callable_proto_members__.add(attr)
  670. return cls
  671. # The "runtime" alias exists for backwards compatibility.
  672. runtime = runtime_checkable
  673. # Our version of runtime-checkable protocols is faster on Python 3.8-3.11
  674. if sys.version_info >= (3, 12):
  675. SupportsInt = typing.SupportsInt
  676. SupportsFloat = typing.SupportsFloat
  677. SupportsComplex = typing.SupportsComplex
  678. SupportsBytes = typing.SupportsBytes
  679. SupportsIndex = typing.SupportsIndex
  680. SupportsAbs = typing.SupportsAbs
  681. SupportsRound = typing.SupportsRound
  682. else:
  683. @runtime_checkable
  684. class SupportsInt(Protocol):
  685. """An ABC with one abstract method __int__."""
  686. __slots__ = ()
  687. @abc.abstractmethod
  688. def __int__(self) -> int:
  689. pass
  690. @runtime_checkable
  691. class SupportsFloat(Protocol):
  692. """An ABC with one abstract method __float__."""
  693. __slots__ = ()
  694. @abc.abstractmethod
  695. def __float__(self) -> float:
  696. pass
  697. @runtime_checkable
  698. class SupportsComplex(Protocol):
  699. """An ABC with one abstract method __complex__."""
  700. __slots__ = ()
  701. @abc.abstractmethod
  702. def __complex__(self) -> complex:
  703. pass
  704. @runtime_checkable
  705. class SupportsBytes(Protocol):
  706. """An ABC with one abstract method __bytes__."""
  707. __slots__ = ()
  708. @abc.abstractmethod
  709. def __bytes__(self) -> bytes:
  710. pass
  711. @runtime_checkable
  712. class SupportsIndex(Protocol):
  713. __slots__ = ()
  714. @abc.abstractmethod
  715. def __index__(self) -> int:
  716. pass
  717. @runtime_checkable
  718. class SupportsAbs(Protocol[T_co]):
  719. """
  720. An ABC with one abstract method __abs__ that is covariant in its return type.
  721. """
  722. __slots__ = ()
  723. @abc.abstractmethod
  724. def __abs__(self) -> T_co:
  725. pass
  726. @runtime_checkable
  727. class SupportsRound(Protocol[T_co]):
  728. """
  729. An ABC with one abstract method __round__ that is covariant in its return type.
  730. """
  731. __slots__ = ()
  732. @abc.abstractmethod
  733. def __round__(self, ndigits: int = 0) -> T_co:
  734. pass
  735. def _ensure_subclassable(mro_entries):
  736. def inner(func):
  737. if sys.implementation.name == "pypy" and sys.version_info < (3, 9):
  738. cls_dict = {
  739. "__call__": staticmethod(func),
  740. "__mro_entries__": staticmethod(mro_entries)
  741. }
  742. t = type(func.__name__, (), cls_dict)
  743. return functools.update_wrapper(t(), func)
  744. else:
  745. func.__mro_entries__ = mro_entries
  746. return func
  747. return inner
  748. _NEEDS_SINGLETONMETA = (
  749. not hasattr(typing, "NoDefault") or not hasattr(typing, "NoExtraItems")
  750. )
  751. if _NEEDS_SINGLETONMETA:
  752. class SingletonMeta(type):
  753. def __setattr__(cls, attr, value):
  754. # TypeError is consistent with the behavior of NoneType
  755. raise TypeError(
  756. f"cannot set {attr!r} attribute of immutable type {cls.__name__!r}"
  757. )
  758. if hasattr(typing, "NoDefault"):
  759. NoDefault = typing.NoDefault
  760. else:
  761. class NoDefaultType(metaclass=SingletonMeta):
  762. """The type of the NoDefault singleton."""
  763. __slots__ = ()
  764. def __new__(cls):
  765. return globals().get("NoDefault") or object.__new__(cls)
  766. def __repr__(self):
  767. return "typing_extensions.NoDefault"
  768. def __reduce__(self):
  769. return "NoDefault"
  770. NoDefault = NoDefaultType()
  771. del NoDefaultType
  772. if hasattr(typing, "NoExtraItems"):
  773. NoExtraItems = typing.NoExtraItems
  774. else:
  775. class NoExtraItemsType(metaclass=SingletonMeta):
  776. """The type of the NoExtraItems singleton."""
  777. __slots__ = ()
  778. def __new__(cls):
  779. return globals().get("NoExtraItems") or object.__new__(cls)
  780. def __repr__(self):
  781. return "typing_extensions.NoExtraItems"
  782. def __reduce__(self):
  783. return "NoExtraItems"
  784. NoExtraItems = NoExtraItemsType()
  785. del NoExtraItemsType
  786. if _NEEDS_SINGLETONMETA:
  787. del SingletonMeta
  788. # Update this to something like >=3.13.0b1 if and when
  789. # PEP 728 is implemented in CPython
  790. _PEP_728_IMPLEMENTED = False
  791. if _PEP_728_IMPLEMENTED:
  792. # The standard library TypedDict in Python 3.8 does not store runtime information
  793. # about which (if any) keys are optional. See https://bugs.python.org/issue38834
  794. # The standard library TypedDict in Python 3.9.0/1 does not honour the "total"
  795. # keyword with old-style TypedDict(). See https://bugs.python.org/issue42059
  796. # The standard library TypedDict below Python 3.11 does not store runtime
  797. # information about optional and required keys when using Required or NotRequired.
  798. # Generic TypedDicts are also impossible using typing.TypedDict on Python <3.11.
  799. # Aaaand on 3.12 we add __orig_bases__ to TypedDict
  800. # to enable better runtime introspection.
  801. # On 3.13 we deprecate some odd ways of creating TypedDicts.
  802. # Also on 3.13, PEP 705 adds the ReadOnly[] qualifier.
  803. # PEP 728 (still pending) makes more changes.
  804. TypedDict = typing.TypedDict
  805. _TypedDictMeta = typing._TypedDictMeta
  806. is_typeddict = typing.is_typeddict
  807. else:
  808. # 3.10.0 and later
  809. _TAKES_MODULE = "module" in inspect.signature(typing._type_check).parameters
  810. def _get_typeddict_qualifiers(annotation_type):
  811. while True:
  812. annotation_origin = get_origin(annotation_type)
  813. if annotation_origin is Annotated:
  814. annotation_args = get_args(annotation_type)
  815. if annotation_args:
  816. annotation_type = annotation_args[0]
  817. else:
  818. break
  819. elif annotation_origin is Required:
  820. yield Required
  821. annotation_type, = get_args(annotation_type)
  822. elif annotation_origin is NotRequired:
  823. yield NotRequired
  824. annotation_type, = get_args(annotation_type)
  825. elif annotation_origin is ReadOnly:
  826. yield ReadOnly
  827. annotation_type, = get_args(annotation_type)
  828. else:
  829. break
  830. class _TypedDictMeta(type):
  831. def __new__(cls, name, bases, ns, *, total=True, closed=None,
  832. extra_items=NoExtraItems):
  833. """Create new typed dict class object.
  834. This method is called when TypedDict is subclassed,
  835. or when TypedDict is instantiated. This way
  836. TypedDict supports all three syntax forms described in its docstring.
  837. Subclasses and instances of TypedDict return actual dictionaries.
  838. """
  839. for base in bases:
  840. if type(base) is not _TypedDictMeta and base is not typing.Generic:
  841. raise TypeError('cannot inherit from both a TypedDict type '
  842. 'and a non-TypedDict base class')
  843. if closed is not None and extra_items is not NoExtraItems:
  844. raise TypeError(f"Cannot combine closed={closed!r} and extra_items")
  845. if any(issubclass(b, typing.Generic) for b in bases):
  846. generic_base = (typing.Generic,)
  847. else:
  848. generic_base = ()
  849. # typing.py generally doesn't let you inherit from plain Generic, unless
  850. # the name of the class happens to be "Protocol"
  851. tp_dict = type.__new__(_TypedDictMeta, "Protocol", (*generic_base, dict), ns)
  852. tp_dict.__name__ = name
  853. if tp_dict.__qualname__ == "Protocol":
  854. tp_dict.__qualname__ = name
  855. if not hasattr(tp_dict, '__orig_bases__'):
  856. tp_dict.__orig_bases__ = bases
  857. annotations = {}
  858. if "__annotations__" in ns:
  859. own_annotations = ns["__annotations__"]
  860. elif "__annotate__" in ns:
  861. # TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
  862. own_annotations = ns["__annotate__"](1)
  863. else:
  864. own_annotations = {}
  865. msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
  866. if _TAKES_MODULE:
  867. own_annotations = {
  868. n: typing._type_check(tp, msg, module=tp_dict.__module__)
  869. for n, tp in own_annotations.items()
  870. }
  871. else:
  872. own_annotations = {
  873. n: typing._type_check(tp, msg)
  874. for n, tp in own_annotations.items()
  875. }
  876. required_keys = set()
  877. optional_keys = set()
  878. readonly_keys = set()
  879. mutable_keys = set()
  880. extra_items_type = extra_items
  881. for base in bases:
  882. base_dict = base.__dict__
  883. annotations.update(base_dict.get('__annotations__', {}))
  884. required_keys.update(base_dict.get('__required_keys__', ()))
  885. optional_keys.update(base_dict.get('__optional_keys__', ()))
  886. readonly_keys.update(base_dict.get('__readonly_keys__', ()))
  887. mutable_keys.update(base_dict.get('__mutable_keys__', ()))
  888. # This was specified in an earlier version of PEP 728. Support
  889. # is retained for backwards compatibility, but only for Python
  890. # 3.13 and lower.
  891. if (closed and sys.version_info < (3, 14)
  892. and "__extra_items__" in own_annotations):
  893. annotation_type = own_annotations.pop("__extra_items__")
  894. qualifiers = set(_get_typeddict_qualifiers(annotation_type))
  895. if Required in qualifiers:
  896. raise TypeError(
  897. "Special key __extra_items__ does not support "
  898. "Required"
  899. )
  900. if NotRequired in qualifiers:
  901. raise TypeError(
  902. "Special key __extra_items__ does not support "
  903. "NotRequired"
  904. )
  905. extra_items_type = annotation_type
  906. annotations.update(own_annotations)
  907. for annotation_key, annotation_type in own_annotations.items():
  908. qualifiers = set(_get_typeddict_qualifiers(annotation_type))
  909. if Required in qualifiers:
  910. required_keys.add(annotation_key)
  911. elif NotRequired in qualifiers:
  912. optional_keys.add(annotation_key)
  913. elif total:
  914. required_keys.add(annotation_key)
  915. else:
  916. optional_keys.add(annotation_key)
  917. if ReadOnly in qualifiers:
  918. mutable_keys.discard(annotation_key)
  919. readonly_keys.add(annotation_key)
  920. else:
  921. mutable_keys.add(annotation_key)
  922. readonly_keys.discard(annotation_key)
  923. tp_dict.__annotations__ = annotations
  924. tp_dict.__required_keys__ = frozenset(required_keys)
  925. tp_dict.__optional_keys__ = frozenset(optional_keys)
  926. tp_dict.__readonly_keys__ = frozenset(readonly_keys)
  927. tp_dict.__mutable_keys__ = frozenset(mutable_keys)
  928. tp_dict.__total__ = total
  929. tp_dict.__closed__ = closed
  930. tp_dict.__extra_items__ = extra_items_type
  931. return tp_dict
  932. __call__ = dict # static method
  933. def __subclasscheck__(cls, other):
  934. # Typed dicts are only for static structural subtyping.
  935. raise TypeError('TypedDict does not support instance and class checks')
  936. __instancecheck__ = __subclasscheck__
  937. _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
  938. @_ensure_subclassable(lambda bases: (_TypedDict,))
  939. def TypedDict(
  940. typename,
  941. fields=_marker,
  942. /,
  943. *,
  944. total=True,
  945. closed=None,
  946. extra_items=NoExtraItems,
  947. **kwargs
  948. ):
  949. """A simple typed namespace. At runtime it is equivalent to a plain dict.
  950. TypedDict creates a dictionary type such that a type checker will expect all
  951. instances to have a certain set of keys, where each key is
  952. associated with a value of a consistent type. This expectation
  953. is not checked at runtime.
  954. Usage::
  955. class Point2D(TypedDict):
  956. x: int
  957. y: int
  958. label: str
  959. a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
  960. b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
  961. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
  962. The type info can be accessed via the Point2D.__annotations__ dict, and
  963. the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
  964. TypedDict supports an additional equivalent form::
  965. Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
  966. By default, all keys must be present in a TypedDict. It is possible
  967. to override this by specifying totality::
  968. class Point2D(TypedDict, total=False):
  969. x: int
  970. y: int
  971. This means that a Point2D TypedDict can have any of the keys omitted. A type
  972. checker is only expected to support a literal False or True as the value of
  973. the total argument. True is the default, and makes all items defined in the
  974. class body be required.
  975. The Required and NotRequired special forms can also be used to mark
  976. individual keys as being required or not required::
  977. class Point2D(TypedDict):
  978. x: int # the "x" key must always be present (Required is the default)
  979. y: NotRequired[int] # the "y" key can be omitted
  980. See PEP 655 for more details on Required and NotRequired.
  981. """
  982. if fields is _marker or fields is None:
  983. if fields is _marker:
  984. deprecated_thing = "Failing to pass a value for the 'fields' parameter"
  985. else:
  986. deprecated_thing = "Passing `None` as the 'fields' parameter"
  987. example = f"`{typename} = TypedDict({typename!r}, {{}})`"
  988. deprecation_msg = (
  989. f"{deprecated_thing} is deprecated and will be disallowed in "
  990. "Python 3.15. To create a TypedDict class with 0 fields "
  991. "using the functional syntax, pass an empty dictionary, e.g. "
  992. ) + example + "."
  993. warnings.warn(deprecation_msg, DeprecationWarning, stacklevel=2)
  994. # Support a field called "closed"
  995. if closed is not False and closed is not True and closed is not None:
  996. kwargs["closed"] = closed
  997. closed = None
  998. # Or "extra_items"
  999. if extra_items is not NoExtraItems:
  1000. kwargs["extra_items"] = extra_items
  1001. extra_items = NoExtraItems
  1002. fields = kwargs
  1003. elif kwargs:
  1004. raise TypeError("TypedDict takes either a dict or keyword arguments,"
  1005. " but not both")
  1006. if kwargs:
  1007. if sys.version_info >= (3, 13):
  1008. raise TypeError("TypedDict takes no keyword arguments")
  1009. warnings.warn(
  1010. "The kwargs-based syntax for TypedDict definitions is deprecated "
  1011. "in Python 3.11, will be removed in Python 3.13, and may not be "
  1012. "understood by third-party type checkers.",
  1013. DeprecationWarning,
  1014. stacklevel=2,
  1015. )
  1016. ns = {'__annotations__': dict(fields)}
  1017. module = _caller()
  1018. if module is not None:
  1019. # Setting correct module is necessary to make typed dict classes pickleable.
  1020. ns['__module__'] = module
  1021. td = _TypedDictMeta(typename, (), ns, total=total, closed=closed,
  1022. extra_items=extra_items)
  1023. td.__orig_bases__ = (TypedDict,)
  1024. return td
  1025. if hasattr(typing, "_TypedDictMeta"):
  1026. _TYPEDDICT_TYPES = (typing._TypedDictMeta, _TypedDictMeta)
  1027. else:
  1028. _TYPEDDICT_TYPES = (_TypedDictMeta,)
  1029. def is_typeddict(tp):
  1030. """Check if an annotation is a TypedDict class
  1031. For example::
  1032. class Film(TypedDict):
  1033. title: str
  1034. year: int
  1035. is_typeddict(Film) # => True
  1036. is_typeddict(Union[list, str]) # => False
  1037. """
  1038. # On 3.8, this would otherwise return True
  1039. if hasattr(typing, "TypedDict") and tp is typing.TypedDict:
  1040. return False
  1041. return isinstance(tp, _TYPEDDICT_TYPES)
  1042. if hasattr(typing, "assert_type"):
  1043. assert_type = typing.assert_type
  1044. else:
  1045. def assert_type(val, typ, /):
  1046. """Assert (to the type checker) that the value is of the given type.
  1047. When the type checker encounters a call to assert_type(), it
  1048. emits an error if the value is not of the specified type::
  1049. def greet(name: str) -> None:
  1050. assert_type(name, str) # ok
  1051. assert_type(name, int) # type checker error
  1052. At runtime this returns the first argument unchanged and otherwise
  1053. does nothing.
  1054. """
  1055. return val
  1056. if hasattr(typing, "ReadOnly"): # 3.13+
  1057. get_type_hints = typing.get_type_hints
  1058. else: # <=3.13
  1059. # replaces _strip_annotations()
  1060. def _strip_extras(t):
  1061. """Strips Annotated, Required and NotRequired from a given type."""
  1062. if isinstance(t, _AnnotatedAlias):
  1063. return _strip_extras(t.__origin__)
  1064. if hasattr(t, "__origin__") and t.__origin__ in (Required, NotRequired, ReadOnly):
  1065. return _strip_extras(t.__args__[0])
  1066. if isinstance(t, typing._GenericAlias):
  1067. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  1068. if stripped_args == t.__args__:
  1069. return t
  1070. return t.copy_with(stripped_args)
  1071. if hasattr(_types, "GenericAlias") and isinstance(t, _types.GenericAlias):
  1072. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  1073. if stripped_args == t.__args__:
  1074. return t
  1075. return _types.GenericAlias(t.__origin__, stripped_args)
  1076. if hasattr(_types, "UnionType") and isinstance(t, _types.UnionType):
  1077. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  1078. if stripped_args == t.__args__:
  1079. return t
  1080. return functools.reduce(operator.or_, stripped_args)
  1081. return t
  1082. def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
  1083. """Return type hints for an object.
  1084. This is often the same as obj.__annotations__, but it handles
  1085. forward references encoded as string literals, adds Optional[t] if a
  1086. default value equal to None is set and recursively replaces all
  1087. 'Annotated[T, ...]', 'Required[T]' or 'NotRequired[T]' with 'T'
  1088. (unless 'include_extras=True').
  1089. The argument may be a module, class, method, or function. The annotations
  1090. are returned as a dictionary. For classes, annotations include also
  1091. inherited members.
  1092. TypeError is raised if the argument is not of a type that can contain
  1093. annotations, and an empty dictionary is returned if no annotations are
  1094. present.
  1095. BEWARE -- the behavior of globalns and localns is counterintuitive
  1096. (unless you are familiar with how eval() and exec() work). The
  1097. search order is locals first, then globals.
  1098. - If no dict arguments are passed, an attempt is made to use the
  1099. globals from obj (or the respective module's globals for classes),
  1100. and these are also used as the locals. If the object does not appear
  1101. to have globals, an empty dictionary is used.
  1102. - If one dict argument is passed, it is used for both globals and
  1103. locals.
  1104. - If two dict arguments are passed, they specify globals and
  1105. locals, respectively.
  1106. """
  1107. if hasattr(typing, "Annotated"): # 3.9+
  1108. hint = typing.get_type_hints(
  1109. obj, globalns=globalns, localns=localns, include_extras=True
  1110. )
  1111. else: # 3.8
  1112. hint = typing.get_type_hints(obj, globalns=globalns, localns=localns)
  1113. if sys.version_info < (3, 11):
  1114. _clean_optional(obj, hint, globalns, localns)
  1115. if sys.version_info < (3, 9):
  1116. # In 3.8 eval_type does not flatten Optional[ForwardRef] correctly
  1117. # This will recreate and and cache Unions.
  1118. hint = {
  1119. k: (t
  1120. if get_origin(t) != Union
  1121. else Union[t.__args__])
  1122. for k, t in hint.items()
  1123. }
  1124. if include_extras:
  1125. return hint
  1126. return {k: _strip_extras(t) for k, t in hint.items()}
  1127. _NoneType = type(None)
  1128. def _could_be_inserted_optional(t):
  1129. """detects Union[..., None] pattern"""
  1130. # 3.8+ compatible checking before _UnionGenericAlias
  1131. if get_origin(t) is not Union:
  1132. return False
  1133. # Assume if last argument is not None they are user defined
  1134. if t.__args__[-1] is not _NoneType:
  1135. return False
  1136. return True
  1137. # < 3.11
  1138. def _clean_optional(obj, hints, globalns=None, localns=None):
  1139. # reverts injected Union[..., None] cases from typing.get_type_hints
  1140. # when a None default value is used.
  1141. # see https://github.com/python/typing_extensions/issues/310
  1142. if not hints or isinstance(obj, type):
  1143. return
  1144. defaults = typing._get_defaults(obj) # avoid accessing __annotations___
  1145. if not defaults:
  1146. return
  1147. original_hints = obj.__annotations__
  1148. for name, value in hints.items():
  1149. # Not a Union[..., None] or replacement conditions not fullfilled
  1150. if (not _could_be_inserted_optional(value)
  1151. or name not in defaults
  1152. or defaults[name] is not None
  1153. ):
  1154. continue
  1155. original_value = original_hints[name]
  1156. # value=NoneType should have caused a skip above but check for safety
  1157. if original_value is None:
  1158. original_value = _NoneType
  1159. # Forward reference
  1160. if isinstance(original_value, str):
  1161. if globalns is None:
  1162. if isinstance(obj, _types.ModuleType):
  1163. globalns = obj.__dict__
  1164. else:
  1165. nsobj = obj
  1166. # Find globalns for the unwrapped object.
  1167. while hasattr(nsobj, '__wrapped__'):
  1168. nsobj = nsobj.__wrapped__
  1169. globalns = getattr(nsobj, '__globals__', {})
  1170. if localns is None:
  1171. localns = globalns
  1172. elif localns is None:
  1173. localns = globalns
  1174. if sys.version_info < (3, 9):
  1175. original_value = ForwardRef(original_value)
  1176. else:
  1177. original_value = ForwardRef(
  1178. original_value,
  1179. is_argument=not isinstance(obj, _types.ModuleType)
  1180. )
  1181. original_evaluated = typing._eval_type(original_value, globalns, localns)
  1182. if sys.version_info < (3, 9) and get_origin(original_evaluated) is Union:
  1183. # Union[str, None, "str"] is not reduced to Union[str, None]
  1184. original_evaluated = Union[original_evaluated.__args__]
  1185. # Compare if values differ. Note that even if equal
  1186. # value might be cached by typing._tp_cache contrary to original_evaluated
  1187. if original_evaluated != value or (
  1188. # 3.10: ForwardRefs of UnionType might be turned into _UnionGenericAlias
  1189. hasattr(_types, "UnionType")
  1190. and isinstance(original_evaluated, _types.UnionType)
  1191. and not isinstance(value, _types.UnionType)
  1192. ):
  1193. hints[name] = original_evaluated
  1194. # Python 3.9+ has PEP 593 (Annotated)
  1195. if hasattr(typing, 'Annotated'):
  1196. Annotated = typing.Annotated
  1197. # Not exported and not a public API, but needed for get_origin() and get_args()
  1198. # to work.
  1199. _AnnotatedAlias = typing._AnnotatedAlias
  1200. # 3.8
  1201. else:
  1202. class _AnnotatedAlias(typing._GenericAlias, _root=True):
  1203. """Runtime representation of an annotated type.
  1204. At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
  1205. with extra annotations. The alias behaves like a normal typing alias,
  1206. instantiating is the same as instantiating the underlying type, binding
  1207. it to types is also the same.
  1208. """
  1209. def __init__(self, origin, metadata):
  1210. if isinstance(origin, _AnnotatedAlias):
  1211. metadata = origin.__metadata__ + metadata
  1212. origin = origin.__origin__
  1213. super().__init__(origin, origin)
  1214. self.__metadata__ = metadata
  1215. def copy_with(self, params):
  1216. assert len(params) == 1
  1217. new_type = params[0]
  1218. return _AnnotatedAlias(new_type, self.__metadata__)
  1219. def __repr__(self):
  1220. return (f"typing_extensions.Annotated[{typing._type_repr(self.__origin__)}, "
  1221. f"{', '.join(repr(a) for a in self.__metadata__)}]")
  1222. def __reduce__(self):
  1223. return operator.getitem, (
  1224. Annotated, (self.__origin__, *self.__metadata__)
  1225. )
  1226. def __eq__(self, other):
  1227. if not isinstance(other, _AnnotatedAlias):
  1228. return NotImplemented
  1229. if self.__origin__ != other.__origin__:
  1230. return False
  1231. return self.__metadata__ == other.__metadata__
  1232. def __hash__(self):
  1233. return hash((self.__origin__, self.__metadata__))
  1234. class Annotated:
  1235. """Add context specific metadata to a type.
  1236. Example: Annotated[int, runtime_check.Unsigned] indicates to the
  1237. hypothetical runtime_check module that this type is an unsigned int.
  1238. Every other consumer of this type can ignore this metadata and treat
  1239. this type as int.
  1240. The first argument to Annotated must be a valid type (and will be in
  1241. the __origin__ field), the remaining arguments are kept as a tuple in
  1242. the __extra__ field.
  1243. Details:
  1244. - It's an error to call `Annotated` with less than two arguments.
  1245. - Nested Annotated are flattened::
  1246. Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
  1247. - Instantiating an annotated type is equivalent to instantiating the
  1248. underlying type::
  1249. Annotated[C, Ann1](5) == C(5)
  1250. - Annotated can be used as a generic type alias::
  1251. Optimized = Annotated[T, runtime.Optimize()]
  1252. Optimized[int] == Annotated[int, runtime.Optimize()]
  1253. OptimizedList = Annotated[List[T], runtime.Optimize()]
  1254. OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
  1255. """
  1256. __slots__ = ()
  1257. def __new__(cls, *args, **kwargs):
  1258. raise TypeError("Type Annotated cannot be instantiated.")
  1259. @typing._tp_cache
  1260. def __class_getitem__(cls, params):
  1261. if not isinstance(params, tuple) or len(params) < 2:
  1262. raise TypeError("Annotated[...] should be used "
  1263. "with at least two arguments (a type and an "
  1264. "annotation).")
  1265. allowed_special_forms = (ClassVar, Final)
  1266. if get_origin(params[0]) in allowed_special_forms:
  1267. origin = params[0]
  1268. else:
  1269. msg = "Annotated[t, ...]: t must be a type."
  1270. origin = typing._type_check(params[0], msg)
  1271. metadata = tuple(params[1:])
  1272. return _AnnotatedAlias(origin, metadata)
  1273. def __init_subclass__(cls, *args, **kwargs):
  1274. raise TypeError(
  1275. f"Cannot subclass {cls.__module__}.Annotated"
  1276. )
  1277. # Python 3.8 has get_origin() and get_args() but those implementations aren't
  1278. # Annotated-aware, so we can't use those. Python 3.9's versions don't support
  1279. # ParamSpecArgs and ParamSpecKwargs, so only Python 3.10's versions will do.
  1280. if sys.version_info[:2] >= (3, 10):
  1281. get_origin = typing.get_origin
  1282. get_args = typing.get_args
  1283. # 3.8-3.9
  1284. else:
  1285. try:
  1286. # 3.9+
  1287. from typing import _BaseGenericAlias
  1288. except ImportError:
  1289. _BaseGenericAlias = typing._GenericAlias
  1290. try:
  1291. # 3.9+
  1292. from typing import GenericAlias as _typing_GenericAlias
  1293. except ImportError:
  1294. _typing_GenericAlias = typing._GenericAlias
  1295. def get_origin(tp):
  1296. """Get the unsubscripted version of a type.
  1297. This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
  1298. and Annotated. Return None for unsupported types. Examples::
  1299. get_origin(Literal[42]) is Literal
  1300. get_origin(int) is None
  1301. get_origin(ClassVar[int]) is ClassVar
  1302. get_origin(Generic) is Generic
  1303. get_origin(Generic[T]) is Generic
  1304. get_origin(Union[T, int]) is Union
  1305. get_origin(List[Tuple[T, T]][int]) == list
  1306. get_origin(P.args) is P
  1307. """
  1308. if isinstance(tp, _AnnotatedAlias):
  1309. return Annotated
  1310. if isinstance(tp, (typing._GenericAlias, _typing_GenericAlias, _BaseGenericAlias,
  1311. ParamSpecArgs, ParamSpecKwargs)):
  1312. return tp.__origin__
  1313. if tp is typing.Generic:
  1314. return typing.Generic
  1315. return None
  1316. def get_args(tp):
  1317. """Get type arguments with all substitutions performed.
  1318. For unions, basic simplifications used by Union constructor are performed.
  1319. Examples::
  1320. get_args(Dict[str, int]) == (str, int)
  1321. get_args(int) == ()
  1322. get_args(Union[int, Union[T, int], str][int]) == (int, str)
  1323. get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
  1324. get_args(Callable[[], T][int]) == ([], int)
  1325. """
  1326. if isinstance(tp, _AnnotatedAlias):
  1327. return (tp.__origin__, *tp.__metadata__)
  1328. if isinstance(tp, (typing._GenericAlias, _typing_GenericAlias)):
  1329. if getattr(tp, "_special", False):
  1330. return ()
  1331. res = tp.__args__
  1332. if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
  1333. res = (list(res[:-1]), res[-1])
  1334. return res
  1335. return ()
  1336. # 3.10+
  1337. if hasattr(typing, 'TypeAlias'):
  1338. TypeAlias = typing.TypeAlias
  1339. # 3.9
  1340. elif sys.version_info[:2] >= (3, 9):
  1341. @_ExtensionsSpecialForm
  1342. def TypeAlias(self, parameters):
  1343. """Special marker indicating that an assignment should
  1344. be recognized as a proper type alias definition by type
  1345. checkers.
  1346. For example::
  1347. Predicate: TypeAlias = Callable[..., bool]
  1348. It's invalid when used anywhere except as in the example above.
  1349. """
  1350. raise TypeError(f"{self} is not subscriptable")
  1351. # 3.8
  1352. else:
  1353. TypeAlias = _ExtensionsSpecialForm(
  1354. 'TypeAlias',
  1355. doc="""Special marker indicating that an assignment should
  1356. be recognized as a proper type alias definition by type
  1357. checkers.
  1358. For example::
  1359. Predicate: TypeAlias = Callable[..., bool]
  1360. It's invalid when used anywhere except as in the example
  1361. above."""
  1362. )
  1363. def _set_default(type_param, default):
  1364. type_param.has_default = lambda: default is not NoDefault
  1365. type_param.__default__ = default
  1366. def _set_module(typevarlike):
  1367. # for pickling:
  1368. def_mod = _caller(depth=3)
  1369. if def_mod != 'typing_extensions':
  1370. typevarlike.__module__ = def_mod
  1371. class _DefaultMixin:
  1372. """Mixin for TypeVarLike defaults."""
  1373. __slots__ = ()
  1374. __init__ = _set_default
  1375. # Classes using this metaclass must provide a _backported_typevarlike ClassVar
  1376. class _TypeVarLikeMeta(type):
  1377. def __instancecheck__(cls, __instance: Any) -> bool:
  1378. return isinstance(__instance, cls._backported_typevarlike)
  1379. if _PEP_696_IMPLEMENTED:
  1380. from typing import TypeVar
  1381. else:
  1382. # Add default and infer_variance parameters from PEP 696 and 695
  1383. class TypeVar(metaclass=_TypeVarLikeMeta):
  1384. """Type variable."""
  1385. _backported_typevarlike = typing.TypeVar
  1386. def __new__(cls, name, *constraints, bound=None,
  1387. covariant=False, contravariant=False,
  1388. default=NoDefault, infer_variance=False):
  1389. if hasattr(typing, "TypeAliasType"):
  1390. # PEP 695 implemented (3.12+), can pass infer_variance to typing.TypeVar
  1391. typevar = typing.TypeVar(name, *constraints, bound=bound,
  1392. covariant=covariant, contravariant=contravariant,
  1393. infer_variance=infer_variance)
  1394. else:
  1395. typevar = typing.TypeVar(name, *constraints, bound=bound,
  1396. covariant=covariant, contravariant=contravariant)
  1397. if infer_variance and (covariant or contravariant):
  1398. raise ValueError("Variance cannot be specified with infer_variance.")
  1399. typevar.__infer_variance__ = infer_variance
  1400. _set_default(typevar, default)
  1401. _set_module(typevar)
  1402. def _tvar_prepare_subst(alias, args):
  1403. if (
  1404. typevar.has_default()
  1405. and alias.__parameters__.index(typevar) == len(args)
  1406. ):
  1407. args += (typevar.__default__,)
  1408. return args
  1409. typevar.__typing_prepare_subst__ = _tvar_prepare_subst
  1410. return typevar
  1411. def __init_subclass__(cls) -> None:
  1412. raise TypeError(f"type '{__name__}.TypeVar' is not an acceptable base type")
  1413. # Python 3.10+ has PEP 612
  1414. if hasattr(typing, 'ParamSpecArgs'):
  1415. ParamSpecArgs = typing.ParamSpecArgs
  1416. ParamSpecKwargs = typing.ParamSpecKwargs
  1417. # 3.8-3.9
  1418. else:
  1419. class _Immutable:
  1420. """Mixin to indicate that object should not be copied."""
  1421. __slots__ = ()
  1422. def __copy__(self):
  1423. return self
  1424. def __deepcopy__(self, memo):
  1425. return self
  1426. class ParamSpecArgs(_Immutable):
  1427. """The args for a ParamSpec object.
  1428. Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
  1429. ParamSpecArgs objects have a reference back to their ParamSpec:
  1430. P.args.__origin__ is P
  1431. This type is meant for runtime introspection and has no special meaning to
  1432. static type checkers.
  1433. """
  1434. def __init__(self, origin):
  1435. self.__origin__ = origin
  1436. def __repr__(self):
  1437. return f"{self.__origin__.__name__}.args"
  1438. def __eq__(self, other):
  1439. if not isinstance(other, ParamSpecArgs):
  1440. return NotImplemented
  1441. return self.__origin__ == other.__origin__
  1442. class ParamSpecKwargs(_Immutable):
  1443. """The kwargs for a ParamSpec object.
  1444. Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
  1445. ParamSpecKwargs objects have a reference back to their ParamSpec:
  1446. P.kwargs.__origin__ is P
  1447. This type is meant for runtime introspection and has no special meaning to
  1448. static type checkers.
  1449. """
  1450. def __init__(self, origin):
  1451. self.__origin__ = origin
  1452. def __repr__(self):
  1453. return f"{self.__origin__.__name__}.kwargs"
  1454. def __eq__(self, other):
  1455. if not isinstance(other, ParamSpecKwargs):
  1456. return NotImplemented
  1457. return self.__origin__ == other.__origin__
  1458. if _PEP_696_IMPLEMENTED:
  1459. from typing import ParamSpec
  1460. # 3.10+
  1461. elif hasattr(typing, 'ParamSpec'):
  1462. # Add default parameter - PEP 696
  1463. class ParamSpec(metaclass=_TypeVarLikeMeta):
  1464. """Parameter specification."""
  1465. _backported_typevarlike = typing.ParamSpec
  1466. def __new__(cls, name, *, bound=None,
  1467. covariant=False, contravariant=False,
  1468. infer_variance=False, default=NoDefault):
  1469. if hasattr(typing, "TypeAliasType"):
  1470. # PEP 695 implemented, can pass infer_variance to typing.TypeVar
  1471. paramspec = typing.ParamSpec(name, bound=bound,
  1472. covariant=covariant,
  1473. contravariant=contravariant,
  1474. infer_variance=infer_variance)
  1475. else:
  1476. paramspec = typing.ParamSpec(name, bound=bound,
  1477. covariant=covariant,
  1478. contravariant=contravariant)
  1479. paramspec.__infer_variance__ = infer_variance
  1480. _set_default(paramspec, default)
  1481. _set_module(paramspec)
  1482. def _paramspec_prepare_subst(alias, args):
  1483. params = alias.__parameters__
  1484. i = params.index(paramspec)
  1485. if i == len(args) and paramspec.has_default():
  1486. args = [*args, paramspec.__default__]
  1487. if i >= len(args):
  1488. raise TypeError(f"Too few arguments for {alias}")
  1489. # Special case where Z[[int, str, bool]] == Z[int, str, bool] in PEP 612.
  1490. if len(params) == 1 and not typing._is_param_expr(args[0]):
  1491. assert i == 0
  1492. args = (args,)
  1493. # Convert lists to tuples to help other libraries cache the results.
  1494. elif isinstance(args[i], list):
  1495. args = (*args[:i], tuple(args[i]), *args[i + 1:])
  1496. return args
  1497. paramspec.__typing_prepare_subst__ = _paramspec_prepare_subst
  1498. return paramspec
  1499. def __init_subclass__(cls) -> None:
  1500. raise TypeError(f"type '{__name__}.ParamSpec' is not an acceptable base type")
  1501. # 3.8-3.9
  1502. else:
  1503. # Inherits from list as a workaround for Callable checks in Python < 3.9.2.
  1504. class ParamSpec(list, _DefaultMixin):
  1505. """Parameter specification variable.
  1506. Usage::
  1507. P = ParamSpec('P')
  1508. Parameter specification variables exist primarily for the benefit of static
  1509. type checkers. They are used to forward the parameter types of one
  1510. callable to another callable, a pattern commonly found in higher order
  1511. functions and decorators. They are only valid when used in ``Concatenate``,
  1512. or s the first argument to ``Callable``. In Python 3.10 and higher,
  1513. they are also supported in user-defined Generics at runtime.
  1514. See class Generic for more information on generic types. An
  1515. example for annotating a decorator::
  1516. T = TypeVar('T')
  1517. P = ParamSpec('P')
  1518. def add_logging(f: Callable[P, T]) -> Callable[P, T]:
  1519. '''A type-safe decorator to add logging to a function.'''
  1520. def inner(*args: P.args, **kwargs: P.kwargs) -> T:
  1521. logging.info(f'{f.__name__} was called')
  1522. return f(*args, **kwargs)
  1523. return inner
  1524. @add_logging
  1525. def add_two(x: float, y: float) -> float:
  1526. '''Add two numbers together.'''
  1527. return x + y
  1528. Parameter specification variables defined with covariant=True or
  1529. contravariant=True can be used to declare covariant or contravariant
  1530. generic types. These keyword arguments are valid, but their actual semantics
  1531. are yet to be decided. See PEP 612 for details.
  1532. Parameter specification variables can be introspected. e.g.:
  1533. P.__name__ == 'T'
  1534. P.__bound__ == None
  1535. P.__covariant__ == False
  1536. P.__contravariant__ == False
  1537. Note that only parameter specification variables defined in global scope can
  1538. be pickled.
  1539. """
  1540. # Trick Generic __parameters__.
  1541. __class__ = typing.TypeVar
  1542. @property
  1543. def args(self):
  1544. return ParamSpecArgs(self)
  1545. @property
  1546. def kwargs(self):
  1547. return ParamSpecKwargs(self)
  1548. def __init__(self, name, *, bound=None, covariant=False, contravariant=False,
  1549. infer_variance=False, default=NoDefault):
  1550. list.__init__(self, [self])
  1551. self.__name__ = name
  1552. self.__covariant__ = bool(covariant)
  1553. self.__contravariant__ = bool(contravariant)
  1554. self.__infer_variance__ = bool(infer_variance)
  1555. if bound:
  1556. self.__bound__ = typing._type_check(bound, 'Bound must be a type.')
  1557. else:
  1558. self.__bound__ = None
  1559. _DefaultMixin.__init__(self, default)
  1560. # for pickling:
  1561. def_mod = _caller()
  1562. if def_mod != 'typing_extensions':
  1563. self.__module__ = def_mod
  1564. def __repr__(self):
  1565. if self.__infer_variance__:
  1566. prefix = ''
  1567. elif self.__covariant__:
  1568. prefix = '+'
  1569. elif self.__contravariant__:
  1570. prefix = '-'
  1571. else:
  1572. prefix = '~'
  1573. return prefix + self.__name__
  1574. def __hash__(self):
  1575. return object.__hash__(self)
  1576. def __eq__(self, other):
  1577. return self is other
  1578. def __reduce__(self):
  1579. return self.__name__
  1580. # Hack to get typing._type_check to pass.
  1581. def __call__(self, *args, **kwargs):
  1582. pass
  1583. # 3.8-3.9
  1584. if not hasattr(typing, 'Concatenate'):
  1585. # Inherits from list as a workaround for Callable checks in Python < 3.9.2.
  1586. # 3.9.0-1
  1587. if not hasattr(typing, '_type_convert'):
  1588. def _type_convert(arg, module=None, *, allow_special_forms=False):
  1589. """For converting None to type(None), and strings to ForwardRef."""
  1590. if arg is None:
  1591. return type(None)
  1592. if isinstance(arg, str):
  1593. if sys.version_info <= (3, 9, 6):
  1594. return ForwardRef(arg)
  1595. if sys.version_info <= (3, 9, 7):
  1596. return ForwardRef(arg, module=module)
  1597. return ForwardRef(arg, module=module, is_class=allow_special_forms)
  1598. return arg
  1599. else:
  1600. _type_convert = typing._type_convert
  1601. class _ConcatenateGenericAlias(list):
  1602. # Trick Generic into looking into this for __parameters__.
  1603. __class__ = typing._GenericAlias
  1604. # Flag in 3.8.
  1605. _special = False
  1606. def __init__(self, origin, args):
  1607. super().__init__(args)
  1608. self.__origin__ = origin
  1609. self.__args__ = args
  1610. def __repr__(self):
  1611. _type_repr = typing._type_repr
  1612. return (f'{_type_repr(self.__origin__)}'
  1613. f'[{", ".join(_type_repr(arg) for arg in self.__args__)}]')
  1614. def __hash__(self):
  1615. return hash((self.__origin__, self.__args__))
  1616. # Hack to get typing._type_check to pass in Generic.
  1617. def __call__(self, *args, **kwargs):
  1618. pass
  1619. @property
  1620. def __parameters__(self):
  1621. return tuple(
  1622. tp for tp in self.__args__ if isinstance(tp, (typing.TypeVar, ParamSpec))
  1623. )
  1624. # 3.8; needed for typing._subst_tvars
  1625. # 3.9 used by __getitem__ below
  1626. def copy_with(self, params):
  1627. if isinstance(params[-1], _ConcatenateGenericAlias):
  1628. params = (*params[:-1], *params[-1].__args__)
  1629. elif isinstance(params[-1], (list, tuple)):
  1630. return (*params[:-1], *params[-1])
  1631. elif (not (params[-1] is ... or isinstance(params[-1], ParamSpec))):
  1632. raise TypeError("The last parameter to Concatenate should be a "
  1633. "ParamSpec variable or ellipsis.")
  1634. return self.__class__(self.__origin__, params)
  1635. # 3.9; accessed during GenericAlias.__getitem__ when substituting
  1636. def __getitem__(self, args):
  1637. if self.__origin__ in (Generic, Protocol):
  1638. # Can't subscript Generic[...] or Protocol[...].
  1639. raise TypeError(f"Cannot subscript already-subscripted {self}")
  1640. if not self.__parameters__:
  1641. raise TypeError(f"{self} is not a generic class")
  1642. if not isinstance(args, tuple):
  1643. args = (args,)
  1644. args = _unpack_args(*(_type_convert(p) for p in args))
  1645. params = self.__parameters__
  1646. for param in params:
  1647. prepare = getattr(param, "__typing_prepare_subst__", None)
  1648. if prepare is not None:
  1649. args = prepare(self, args)
  1650. # 3.8 - 3.9 & typing.ParamSpec
  1651. elif isinstance(param, ParamSpec):
  1652. i = params.index(param)
  1653. if (
  1654. i == len(args)
  1655. and getattr(param, '__default__', NoDefault) is not NoDefault
  1656. ):
  1657. args = [*args, param.__default__]
  1658. if i >= len(args):
  1659. raise TypeError(f"Too few arguments for {self}")
  1660. # Special case for Z[[int, str, bool]] == Z[int, str, bool]
  1661. if len(params) == 1 and not _is_param_expr(args[0]):
  1662. assert i == 0
  1663. args = (args,)
  1664. elif (
  1665. isinstance(args[i], list)
  1666. # 3.8 - 3.9
  1667. # This class inherits from list do not convert
  1668. and not isinstance(args[i], _ConcatenateGenericAlias)
  1669. ):
  1670. args = (*args[:i], tuple(args[i]), *args[i + 1:])
  1671. alen = len(args)
  1672. plen = len(params)
  1673. if alen != plen:
  1674. raise TypeError(
  1675. f"Too {'many' if alen > plen else 'few'} arguments for {self};"
  1676. f" actual {alen}, expected {plen}"
  1677. )
  1678. subst = dict(zip(self.__parameters__, args))
  1679. # determine new args
  1680. new_args = []
  1681. for arg in self.__args__:
  1682. if isinstance(arg, type):
  1683. new_args.append(arg)
  1684. continue
  1685. if isinstance(arg, TypeVar):
  1686. arg = subst[arg]
  1687. if (
  1688. (isinstance(arg, typing._GenericAlias) and _is_unpack(arg))
  1689. or (
  1690. hasattr(_types, "GenericAlias")
  1691. and isinstance(arg, _types.GenericAlias)
  1692. and getattr(arg, "__unpacked__", False)
  1693. )
  1694. ):
  1695. raise TypeError(f"{arg} is not valid as type argument")
  1696. elif isinstance(arg,
  1697. typing._GenericAlias
  1698. if not hasattr(_types, "GenericAlias") else
  1699. (typing._GenericAlias, _types.GenericAlias)
  1700. ):
  1701. subparams = arg.__parameters__
  1702. if subparams:
  1703. subargs = tuple(subst[x] for x in subparams)
  1704. arg = arg[subargs]
  1705. new_args.append(arg)
  1706. return self.copy_with(tuple(new_args))
  1707. # 3.10+
  1708. else:
  1709. _ConcatenateGenericAlias = typing._ConcatenateGenericAlias
  1710. # 3.10
  1711. if sys.version_info < (3, 11):
  1712. class _ConcatenateGenericAlias(typing._ConcatenateGenericAlias, _root=True):
  1713. # needed for checks in collections.abc.Callable to accept this class
  1714. __module__ = "typing"
  1715. def copy_with(self, params):
  1716. if isinstance(params[-1], (list, tuple)):
  1717. return (*params[:-1], *params[-1])
  1718. if isinstance(params[-1], typing._ConcatenateGenericAlias):
  1719. params = (*params[:-1], *params[-1].__args__)
  1720. elif not (params[-1] is ... or isinstance(params[-1], ParamSpec)):
  1721. raise TypeError("The last parameter to Concatenate should be a "
  1722. "ParamSpec variable or ellipsis.")
  1723. return super(typing._ConcatenateGenericAlias, self).copy_with(params)
  1724. def __getitem__(self, args):
  1725. value = super().__getitem__(args)
  1726. if isinstance(value, tuple) and any(_is_unpack(t) for t in value):
  1727. return tuple(_unpack_args(*(n for n in value)))
  1728. return value
  1729. # 3.8-3.9.2
  1730. class _EllipsisDummy: ...
  1731. # 3.8-3.10
  1732. def _create_concatenate_alias(origin, parameters):
  1733. if parameters[-1] is ... and sys.version_info < (3, 9, 2):
  1734. # Hack: Arguments must be types, replace it with one.
  1735. parameters = (*parameters[:-1], _EllipsisDummy)
  1736. if sys.version_info >= (3, 10, 3):
  1737. concatenate = _ConcatenateGenericAlias(origin, parameters,
  1738. _typevar_types=(TypeVar, ParamSpec),
  1739. _paramspec_tvars=True)
  1740. else:
  1741. concatenate = _ConcatenateGenericAlias(origin, parameters)
  1742. if parameters[-1] is not _EllipsisDummy:
  1743. return concatenate
  1744. # Remove dummy again
  1745. concatenate.__args__ = tuple(p if p is not _EllipsisDummy else ...
  1746. for p in concatenate.__args__)
  1747. if sys.version_info < (3, 10):
  1748. # backport needs __args__ adjustment only
  1749. return concatenate
  1750. concatenate.__parameters__ = tuple(p for p in concatenate.__parameters__
  1751. if p is not _EllipsisDummy)
  1752. return concatenate
  1753. # 3.8-3.10
  1754. @typing._tp_cache
  1755. def _concatenate_getitem(self, parameters):
  1756. if parameters == ():
  1757. raise TypeError("Cannot take a Concatenate of no types.")
  1758. if not isinstance(parameters, tuple):
  1759. parameters = (parameters,)
  1760. if not (parameters[-1] is ... or isinstance(parameters[-1], ParamSpec)):
  1761. raise TypeError("The last parameter to Concatenate should be a "
  1762. "ParamSpec variable or ellipsis.")
  1763. msg = "Concatenate[arg, ...]: each arg must be a type."
  1764. parameters = (*(typing._type_check(p, msg) for p in parameters[:-1]),
  1765. parameters[-1])
  1766. return _create_concatenate_alias(self, parameters)
  1767. # 3.11+; Concatenate does not accept ellipsis in 3.10
  1768. if sys.version_info >= (3, 11):
  1769. Concatenate = typing.Concatenate
  1770. # 3.9-3.10
  1771. elif sys.version_info[:2] >= (3, 9):
  1772. @_ExtensionsSpecialForm
  1773. def Concatenate(self, parameters):
  1774. """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
  1775. higher order function which adds, removes or transforms parameters of a
  1776. callable.
  1777. For example::
  1778. Callable[Concatenate[int, P], int]
  1779. See PEP 612 for detailed information.
  1780. """
  1781. return _concatenate_getitem(self, parameters)
  1782. # 3.8
  1783. else:
  1784. class _ConcatenateForm(_ExtensionsSpecialForm, _root=True):
  1785. def __getitem__(self, parameters):
  1786. return _concatenate_getitem(self, parameters)
  1787. Concatenate = _ConcatenateForm(
  1788. 'Concatenate',
  1789. doc="""Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
  1790. higher order function which adds, removes or transforms parameters of a
  1791. callable.
  1792. For example::
  1793. Callable[Concatenate[int, P], int]
  1794. See PEP 612 for detailed information.
  1795. """)
  1796. # 3.10+
  1797. if hasattr(typing, 'TypeGuard'):
  1798. TypeGuard = typing.TypeGuard
  1799. # 3.9
  1800. elif sys.version_info[:2] >= (3, 9):
  1801. @_ExtensionsSpecialForm
  1802. def TypeGuard(self, parameters):
  1803. """Special typing form used to annotate the return type of a user-defined
  1804. type guard function. ``TypeGuard`` only accepts a single type argument.
  1805. At runtime, functions marked this way should return a boolean.
  1806. ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
  1807. type checkers to determine a more precise type of an expression within a
  1808. program's code flow. Usually type narrowing is done by analyzing
  1809. conditional code flow and applying the narrowing to a block of code. The
  1810. conditional expression here is sometimes referred to as a "type guard".
  1811. Sometimes it would be convenient to use a user-defined boolean function
  1812. as a type guard. Such a function should use ``TypeGuard[...]`` as its
  1813. return type to alert static type checkers to this intention.
  1814. Using ``-> TypeGuard`` tells the static type checker that for a given
  1815. function:
  1816. 1. The return value is a boolean.
  1817. 2. If the return value is ``True``, the type of its argument
  1818. is the type inside ``TypeGuard``.
  1819. For example::
  1820. def is_str(val: Union[str, float]):
  1821. # "isinstance" type guard
  1822. if isinstance(val, str):
  1823. # Type of ``val`` is narrowed to ``str``
  1824. ...
  1825. else:
  1826. # Else, type of ``val`` is narrowed to ``float``.
  1827. ...
  1828. Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
  1829. form of ``TypeA`` (it can even be a wider form) and this may lead to
  1830. type-unsafe results. The main reason is to allow for things like
  1831. narrowing ``List[object]`` to ``List[str]`` even though the latter is not
  1832. a subtype of the former, since ``List`` is invariant. The responsibility of
  1833. writing type-safe type guards is left to the user.
  1834. ``TypeGuard`` also works with type variables. For more information, see
  1835. PEP 647 (User-Defined Type Guards).
  1836. """
  1837. item = typing._type_check(parameters, f'{self} accepts only a single type.')
  1838. return typing._GenericAlias(self, (item,))
  1839. # 3.8
  1840. else:
  1841. class _TypeGuardForm(_ExtensionsSpecialForm, _root=True):
  1842. def __getitem__(self, parameters):
  1843. item = typing._type_check(parameters,
  1844. f'{self._name} accepts only a single type')
  1845. return typing._GenericAlias(self, (item,))
  1846. TypeGuard = _TypeGuardForm(
  1847. 'TypeGuard',
  1848. doc="""Special typing form used to annotate the return type of a user-defined
  1849. type guard function. ``TypeGuard`` only accepts a single type argument.
  1850. At runtime, functions marked this way should return a boolean.
  1851. ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
  1852. type checkers to determine a more precise type of an expression within a
  1853. program's code flow. Usually type narrowing is done by analyzing
  1854. conditional code flow and applying the narrowing to a block of code. The
  1855. conditional expression here is sometimes referred to as a "type guard".
  1856. Sometimes it would be convenient to use a user-defined boolean function
  1857. as a type guard. Such a function should use ``TypeGuard[...]`` as its
  1858. return type to alert static type checkers to this intention.
  1859. Using ``-> TypeGuard`` tells the static type checker that for a given
  1860. function:
  1861. 1. The return value is a boolean.
  1862. 2. If the return value is ``True``, the type of its argument
  1863. is the type inside ``TypeGuard``.
  1864. For example::
  1865. def is_str(val: Union[str, float]):
  1866. # "isinstance" type guard
  1867. if isinstance(val, str):
  1868. # Type of ``val`` is narrowed to ``str``
  1869. ...
  1870. else:
  1871. # Else, type of ``val`` is narrowed to ``float``.
  1872. ...
  1873. Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
  1874. form of ``TypeA`` (it can even be a wider form) and this may lead to
  1875. type-unsafe results. The main reason is to allow for things like
  1876. narrowing ``List[object]`` to ``List[str]`` even though the latter is not
  1877. a subtype of the former, since ``List`` is invariant. The responsibility of
  1878. writing type-safe type guards is left to the user.
  1879. ``TypeGuard`` also works with type variables. For more information, see
  1880. PEP 647 (User-Defined Type Guards).
  1881. """)
  1882. # 3.13+
  1883. if hasattr(typing, 'TypeIs'):
  1884. TypeIs = typing.TypeIs
  1885. # 3.9
  1886. elif sys.version_info[:2] >= (3, 9):
  1887. @_ExtensionsSpecialForm
  1888. def TypeIs(self, parameters):
  1889. """Special typing form used to annotate the return type of a user-defined
  1890. type narrower function. ``TypeIs`` only accepts a single type argument.
  1891. At runtime, functions marked this way should return a boolean.
  1892. ``TypeIs`` aims to benefit *type narrowing* -- a technique used by static
  1893. type checkers to determine a more precise type of an expression within a
  1894. program's code flow. Usually type narrowing is done by analyzing
  1895. conditional code flow and applying the narrowing to a block of code. The
  1896. conditional expression here is sometimes referred to as a "type guard".
  1897. Sometimes it would be convenient to use a user-defined boolean function
  1898. as a type guard. Such a function should use ``TypeIs[...]`` as its
  1899. return type to alert static type checkers to this intention.
  1900. Using ``-> TypeIs`` tells the static type checker that for a given
  1901. function:
  1902. 1. The return value is a boolean.
  1903. 2. If the return value is ``True``, the type of its argument
  1904. is the intersection of the type inside ``TypeIs`` and the argument's
  1905. previously known type.
  1906. For example::
  1907. def is_awaitable(val: object) -> TypeIs[Awaitable[Any]]:
  1908. return hasattr(val, '__await__')
  1909. def f(val: Union[int, Awaitable[int]]) -> int:
  1910. if is_awaitable(val):
  1911. assert_type(val, Awaitable[int])
  1912. else:
  1913. assert_type(val, int)
  1914. ``TypeIs`` also works with type variables. For more information, see
  1915. PEP 742 (Narrowing types with TypeIs).
  1916. """
  1917. item = typing._type_check(parameters, f'{self} accepts only a single type.')
  1918. return typing._GenericAlias(self, (item,))
  1919. # 3.8
  1920. else:
  1921. class _TypeIsForm(_ExtensionsSpecialForm, _root=True):
  1922. def __getitem__(self, parameters):
  1923. item = typing._type_check(parameters,
  1924. f'{self._name} accepts only a single type')
  1925. return typing._GenericAlias(self, (item,))
  1926. TypeIs = _TypeIsForm(
  1927. 'TypeIs',
  1928. doc="""Special typing form used to annotate the return type of a user-defined
  1929. type narrower function. ``TypeIs`` only accepts a single type argument.
  1930. At runtime, functions marked this way should return a boolean.
  1931. ``TypeIs`` aims to benefit *type narrowing* -- a technique used by static
  1932. type checkers to determine a more precise type of an expression within a
  1933. program's code flow. Usually type narrowing is done by analyzing
  1934. conditional code flow and applying the narrowing to a block of code. The
  1935. conditional expression here is sometimes referred to as a "type guard".
  1936. Sometimes it would be convenient to use a user-defined boolean function
  1937. as a type guard. Such a function should use ``TypeIs[...]`` as its
  1938. return type to alert static type checkers to this intention.
  1939. Using ``-> TypeIs`` tells the static type checker that for a given
  1940. function:
  1941. 1. The return value is a boolean.
  1942. 2. If the return value is ``True``, the type of its argument
  1943. is the intersection of the type inside ``TypeIs`` and the argument's
  1944. previously known type.
  1945. For example::
  1946. def is_awaitable(val: object) -> TypeIs[Awaitable[Any]]:
  1947. return hasattr(val, '__await__')
  1948. def f(val: Union[int, Awaitable[int]]) -> int:
  1949. if is_awaitable(val):
  1950. assert_type(val, Awaitable[int])
  1951. else:
  1952. assert_type(val, int)
  1953. ``TypeIs`` also works with type variables. For more information, see
  1954. PEP 742 (Narrowing types with TypeIs).
  1955. """)
  1956. # 3.14+?
  1957. if hasattr(typing, 'TypeForm'):
  1958. TypeForm = typing.TypeForm
  1959. # 3.9
  1960. elif sys.version_info[:2] >= (3, 9):
  1961. class _TypeFormForm(_ExtensionsSpecialForm, _root=True):
  1962. # TypeForm(X) is equivalent to X but indicates to the type checker
  1963. # that the object is a TypeForm.
  1964. def __call__(self, obj, /):
  1965. return obj
  1966. @_TypeFormForm
  1967. def TypeForm(self, parameters):
  1968. """A special form representing the value that results from the evaluation
  1969. of a type expression. This value encodes the information supplied in the
  1970. type expression, and it represents the type described by that type expression.
  1971. When used in a type expression, TypeForm describes a set of type form objects.
  1972. It accepts a single type argument, which must be a valid type expression.
  1973. ``TypeForm[T]`` describes the set of all type form objects that represent
  1974. the type T or types that are assignable to T.
  1975. Usage:
  1976. def cast[T](typ: TypeForm[T], value: Any) -> T: ...
  1977. reveal_type(cast(int, "x")) # int
  1978. See PEP 747 for more information.
  1979. """
  1980. item = typing._type_check(parameters, f'{self} accepts only a single type.')
  1981. return typing._GenericAlias(self, (item,))
  1982. # 3.8
  1983. else:
  1984. class _TypeFormForm(_ExtensionsSpecialForm, _root=True):
  1985. def __getitem__(self, parameters):
  1986. item = typing._type_check(parameters,
  1987. f'{self._name} accepts only a single type')
  1988. return typing._GenericAlias(self, (item,))
  1989. def __call__(self, obj, /):
  1990. return obj
  1991. TypeForm = _TypeFormForm(
  1992. 'TypeForm',
  1993. doc="""A special form representing the value that results from the evaluation
  1994. of a type expression. This value encodes the information supplied in the
  1995. type expression, and it represents the type described by that type expression.
  1996. When used in a type expression, TypeForm describes a set of type form objects.
  1997. It accepts a single type argument, which must be a valid type expression.
  1998. ``TypeForm[T]`` describes the set of all type form objects that represent
  1999. the type T or types that are assignable to T.
  2000. Usage:
  2001. def cast[T](typ: TypeForm[T], value: Any) -> T: ...
  2002. reveal_type(cast(int, "x")) # int
  2003. See PEP 747 for more information.
  2004. """)
  2005. # Vendored from cpython typing._SpecialFrom
  2006. class _SpecialForm(typing._Final, _root=True):
  2007. __slots__ = ('_name', '__doc__', '_getitem')
  2008. def __init__(self, getitem):
  2009. self._getitem = getitem
  2010. self._name = getitem.__name__
  2011. self.__doc__ = getitem.__doc__
  2012. def __getattr__(self, item):
  2013. if item in {'__name__', '__qualname__'}:
  2014. return self._name
  2015. raise AttributeError(item)
  2016. def __mro_entries__(self, bases):
  2017. raise TypeError(f"Cannot subclass {self!r}")
  2018. def __repr__(self):
  2019. return f'typing_extensions.{self._name}'
  2020. def __reduce__(self):
  2021. return self._name
  2022. def __call__(self, *args, **kwds):
  2023. raise TypeError(f"Cannot instantiate {self!r}")
  2024. def __or__(self, other):
  2025. return typing.Union[self, other]
  2026. def __ror__(self, other):
  2027. return typing.Union[other, self]
  2028. def __instancecheck__(self, obj):
  2029. raise TypeError(f"{self} cannot be used with isinstance()")
  2030. def __subclasscheck__(self, cls):
  2031. raise TypeError(f"{self} cannot be used with issubclass()")
  2032. @typing._tp_cache
  2033. def __getitem__(self, parameters):
  2034. return self._getitem(self, parameters)
  2035. if hasattr(typing, "LiteralString"): # 3.11+
  2036. LiteralString = typing.LiteralString
  2037. else:
  2038. @_SpecialForm
  2039. def LiteralString(self, params):
  2040. """Represents an arbitrary literal string.
  2041. Example::
  2042. from typing_extensions import LiteralString
  2043. def query(sql: LiteralString) -> ...:
  2044. ...
  2045. query("SELECT * FROM table") # ok
  2046. query(f"SELECT * FROM {input()}") # not ok
  2047. See PEP 675 for details.
  2048. """
  2049. raise TypeError(f"{self} is not subscriptable")
  2050. if hasattr(typing, "Self"): # 3.11+
  2051. Self = typing.Self
  2052. else:
  2053. @_SpecialForm
  2054. def Self(self, params):
  2055. """Used to spell the type of "self" in classes.
  2056. Example::
  2057. from typing import Self
  2058. class ReturnsSelf:
  2059. def parse(self, data: bytes) -> Self:
  2060. ...
  2061. return self
  2062. """
  2063. raise TypeError(f"{self} is not subscriptable")
  2064. if hasattr(typing, "Never"): # 3.11+
  2065. Never = typing.Never
  2066. else:
  2067. @_SpecialForm
  2068. def Never(self, params):
  2069. """The bottom type, a type that has no members.
  2070. This can be used to define a function that should never be
  2071. called, or a function that never returns::
  2072. from typing_extensions import Never
  2073. def never_call_me(arg: Never) -> None:
  2074. pass
  2075. def int_or_str(arg: int | str) -> None:
  2076. never_call_me(arg) # type checker error
  2077. match arg:
  2078. case int():
  2079. print("It's an int")
  2080. case str():
  2081. print("It's a str")
  2082. case _:
  2083. never_call_me(arg) # ok, arg is of type Never
  2084. """
  2085. raise TypeError(f"{self} is not subscriptable")
  2086. if hasattr(typing, 'Required'): # 3.11+
  2087. Required = typing.Required
  2088. NotRequired = typing.NotRequired
  2089. elif sys.version_info[:2] >= (3, 9): # 3.9-3.10
  2090. @_ExtensionsSpecialForm
  2091. def Required(self, parameters):
  2092. """A special typing construct to mark a key of a total=False TypedDict
  2093. as required. For example:
  2094. class Movie(TypedDict, total=False):
  2095. title: Required[str]
  2096. year: int
  2097. m = Movie(
  2098. title='The Matrix', # typechecker error if key is omitted
  2099. year=1999,
  2100. )
  2101. There is no runtime checking that a required key is actually provided
  2102. when instantiating a related TypedDict.
  2103. """
  2104. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  2105. return typing._GenericAlias(self, (item,))
  2106. @_ExtensionsSpecialForm
  2107. def NotRequired(self, parameters):
  2108. """A special typing construct to mark a key of a TypedDict as
  2109. potentially missing. For example:
  2110. class Movie(TypedDict):
  2111. title: str
  2112. year: NotRequired[int]
  2113. m = Movie(
  2114. title='The Matrix', # typechecker error if key is omitted
  2115. year=1999,
  2116. )
  2117. """
  2118. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  2119. return typing._GenericAlias(self, (item,))
  2120. else: # 3.8
  2121. class _RequiredForm(_ExtensionsSpecialForm, _root=True):
  2122. def __getitem__(self, parameters):
  2123. item = typing._type_check(parameters,
  2124. f'{self._name} accepts only a single type.')
  2125. return typing._GenericAlias(self, (item,))
  2126. Required = _RequiredForm(
  2127. 'Required',
  2128. doc="""A special typing construct to mark a key of a total=False TypedDict
  2129. as required. For example:
  2130. class Movie(TypedDict, total=False):
  2131. title: Required[str]
  2132. year: int
  2133. m = Movie(
  2134. title='The Matrix', # typechecker error if key is omitted
  2135. year=1999,
  2136. )
  2137. There is no runtime checking that a required key is actually provided
  2138. when instantiating a related TypedDict.
  2139. """)
  2140. NotRequired = _RequiredForm(
  2141. 'NotRequired',
  2142. doc="""A special typing construct to mark a key of a TypedDict as
  2143. potentially missing. For example:
  2144. class Movie(TypedDict):
  2145. title: str
  2146. year: NotRequired[int]
  2147. m = Movie(
  2148. title='The Matrix', # typechecker error if key is omitted
  2149. year=1999,
  2150. )
  2151. """)
  2152. if hasattr(typing, 'ReadOnly'):
  2153. ReadOnly = typing.ReadOnly
  2154. elif sys.version_info[:2] >= (3, 9): # 3.9-3.12
  2155. @_ExtensionsSpecialForm
  2156. def ReadOnly(self, parameters):
  2157. """A special typing construct to mark an item of a TypedDict as read-only.
  2158. For example:
  2159. class Movie(TypedDict):
  2160. title: ReadOnly[str]
  2161. year: int
  2162. def mutate_movie(m: Movie) -> None:
  2163. m["year"] = 1992 # allowed
  2164. m["title"] = "The Matrix" # typechecker error
  2165. There is no runtime checking for this property.
  2166. """
  2167. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  2168. return typing._GenericAlias(self, (item,))
  2169. else: # 3.8
  2170. class _ReadOnlyForm(_ExtensionsSpecialForm, _root=True):
  2171. def __getitem__(self, parameters):
  2172. item = typing._type_check(parameters,
  2173. f'{self._name} accepts only a single type.')
  2174. return typing._GenericAlias(self, (item,))
  2175. ReadOnly = _ReadOnlyForm(
  2176. 'ReadOnly',
  2177. doc="""A special typing construct to mark a key of a TypedDict as read-only.
  2178. For example:
  2179. class Movie(TypedDict):
  2180. title: ReadOnly[str]
  2181. year: int
  2182. def mutate_movie(m: Movie) -> None:
  2183. m["year"] = 1992 # allowed
  2184. m["title"] = "The Matrix" # typechecker error
  2185. There is no runtime checking for this propery.
  2186. """)
  2187. _UNPACK_DOC = """\
  2188. Type unpack operator.
  2189. The type unpack operator takes the child types from some container type,
  2190. such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
  2191. example:
  2192. # For some generic class `Foo`:
  2193. Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
  2194. Ts = TypeVarTuple('Ts')
  2195. # Specifies that `Bar` is generic in an arbitrary number of types.
  2196. # (Think of `Ts` as a tuple of an arbitrary number of individual
  2197. # `TypeVar`s, which the `Unpack` is 'pulling out' directly into the
  2198. # `Generic[]`.)
  2199. class Bar(Generic[Unpack[Ts]]): ...
  2200. Bar[int] # Valid
  2201. Bar[int, str] # Also valid
  2202. From Python 3.11, this can also be done using the `*` operator:
  2203. Foo[*tuple[int, str]]
  2204. class Bar(Generic[*Ts]): ...
  2205. The operator can also be used along with a `TypedDict` to annotate
  2206. `**kwargs` in a function signature. For instance:
  2207. class Movie(TypedDict):
  2208. name: str
  2209. year: int
  2210. # This function expects two keyword arguments - *name* of type `str` and
  2211. # *year* of type `int`.
  2212. def foo(**kwargs: Unpack[Movie]): ...
  2213. Note that there is only some runtime checking of this operator. Not
  2214. everything the runtime allows may be accepted by static type checkers.
  2215. For more information, see PEP 646 and PEP 692.
  2216. """
  2217. if sys.version_info >= (3, 12): # PEP 692 changed the repr of Unpack[]
  2218. Unpack = typing.Unpack
  2219. def _is_unpack(obj):
  2220. return get_origin(obj) is Unpack
  2221. elif sys.version_info[:2] >= (3, 9): # 3.9+
  2222. class _UnpackSpecialForm(_ExtensionsSpecialForm, _root=True):
  2223. def __init__(self, getitem):
  2224. super().__init__(getitem)
  2225. self.__doc__ = _UNPACK_DOC
  2226. class _UnpackAlias(typing._GenericAlias, _root=True):
  2227. if sys.version_info < (3, 11):
  2228. # needed for compatibility with Generic[Unpack[Ts]]
  2229. __class__ = typing.TypeVar
  2230. @property
  2231. def __typing_unpacked_tuple_args__(self):
  2232. assert self.__origin__ is Unpack
  2233. assert len(self.__args__) == 1
  2234. arg, = self.__args__
  2235. if isinstance(arg, (typing._GenericAlias, _types.GenericAlias)):
  2236. if arg.__origin__ is not tuple:
  2237. raise TypeError("Unpack[...] must be used with a tuple type")
  2238. return arg.__args__
  2239. return None
  2240. @property
  2241. def __typing_is_unpacked_typevartuple__(self):
  2242. assert self.__origin__ is Unpack
  2243. assert len(self.__args__) == 1
  2244. return isinstance(self.__args__[0], TypeVarTuple)
  2245. def __getitem__(self, args):
  2246. if self.__typing_is_unpacked_typevartuple__:
  2247. return args
  2248. return super().__getitem__(args)
  2249. @_UnpackSpecialForm
  2250. def Unpack(self, parameters):
  2251. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  2252. return _UnpackAlias(self, (item,))
  2253. def _is_unpack(obj):
  2254. return isinstance(obj, _UnpackAlias)
  2255. else: # 3.8
  2256. class _UnpackAlias(typing._GenericAlias, _root=True):
  2257. __class__ = typing.TypeVar
  2258. @property
  2259. def __typing_unpacked_tuple_args__(self):
  2260. assert self.__origin__ is Unpack
  2261. assert len(self.__args__) == 1
  2262. arg, = self.__args__
  2263. if isinstance(arg, typing._GenericAlias):
  2264. if arg.__origin__ is not tuple:
  2265. raise TypeError("Unpack[...] must be used with a tuple type")
  2266. return arg.__args__
  2267. return None
  2268. @property
  2269. def __typing_is_unpacked_typevartuple__(self):
  2270. assert self.__origin__ is Unpack
  2271. assert len(self.__args__) == 1
  2272. return isinstance(self.__args__[0], TypeVarTuple)
  2273. def __getitem__(self, args):
  2274. if self.__typing_is_unpacked_typevartuple__:
  2275. return args
  2276. return super().__getitem__(args)
  2277. class _UnpackForm(_ExtensionsSpecialForm, _root=True):
  2278. def __getitem__(self, parameters):
  2279. item = typing._type_check(parameters,
  2280. f'{self._name} accepts only a single type.')
  2281. return _UnpackAlias(self, (item,))
  2282. Unpack = _UnpackForm('Unpack', doc=_UNPACK_DOC)
  2283. def _is_unpack(obj):
  2284. return isinstance(obj, _UnpackAlias)
  2285. def _unpack_args(*args):
  2286. newargs = []
  2287. for arg in args:
  2288. subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
  2289. if subargs is not None and (not (subargs and subargs[-1] is ...)):
  2290. newargs.extend(subargs)
  2291. else:
  2292. newargs.append(arg)
  2293. return newargs
  2294. if _PEP_696_IMPLEMENTED:
  2295. from typing import TypeVarTuple
  2296. elif hasattr(typing, "TypeVarTuple"): # 3.11+
  2297. # Add default parameter - PEP 696
  2298. class TypeVarTuple(metaclass=_TypeVarLikeMeta):
  2299. """Type variable tuple."""
  2300. _backported_typevarlike = typing.TypeVarTuple
  2301. def __new__(cls, name, *, default=NoDefault):
  2302. tvt = typing.TypeVarTuple(name)
  2303. _set_default(tvt, default)
  2304. _set_module(tvt)
  2305. def _typevartuple_prepare_subst(alias, args):
  2306. params = alias.__parameters__
  2307. typevartuple_index = params.index(tvt)
  2308. for param in params[typevartuple_index + 1:]:
  2309. if isinstance(param, TypeVarTuple):
  2310. raise TypeError(
  2311. f"More than one TypeVarTuple parameter in {alias}"
  2312. )
  2313. alen = len(args)
  2314. plen = len(params)
  2315. left = typevartuple_index
  2316. right = plen - typevartuple_index - 1
  2317. var_tuple_index = None
  2318. fillarg = None
  2319. for k, arg in enumerate(args):
  2320. if not isinstance(arg, type):
  2321. subargs = getattr(arg, '__typing_unpacked_tuple_args__', None)
  2322. if subargs and len(subargs) == 2 and subargs[-1] is ...:
  2323. if var_tuple_index is not None:
  2324. raise TypeError(
  2325. "More than one unpacked "
  2326. "arbitrary-length tuple argument"
  2327. )
  2328. var_tuple_index = k
  2329. fillarg = subargs[0]
  2330. if var_tuple_index is not None:
  2331. left = min(left, var_tuple_index)
  2332. right = min(right, alen - var_tuple_index - 1)
  2333. elif left + right > alen:
  2334. raise TypeError(f"Too few arguments for {alias};"
  2335. f" actual {alen}, expected at least {plen - 1}")
  2336. if left == alen - right and tvt.has_default():
  2337. replacement = _unpack_args(tvt.__default__)
  2338. else:
  2339. replacement = args[left: alen - right]
  2340. return (
  2341. *args[:left],
  2342. *([fillarg] * (typevartuple_index - left)),
  2343. replacement,
  2344. *([fillarg] * (plen - right - left - typevartuple_index - 1)),
  2345. *args[alen - right:],
  2346. )
  2347. tvt.__typing_prepare_subst__ = _typevartuple_prepare_subst
  2348. return tvt
  2349. def __init_subclass__(self, *args, **kwds):
  2350. raise TypeError("Cannot subclass special typing classes")
  2351. else: # <=3.10
  2352. class TypeVarTuple(_DefaultMixin):
  2353. """Type variable tuple.
  2354. Usage::
  2355. Ts = TypeVarTuple('Ts')
  2356. In the same way that a normal type variable is a stand-in for a single
  2357. type such as ``int``, a type variable *tuple* is a stand-in for a *tuple*
  2358. type such as ``Tuple[int, str]``.
  2359. Type variable tuples can be used in ``Generic`` declarations.
  2360. Consider the following example::
  2361. class Array(Generic[*Ts]): ...
  2362. The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
  2363. where ``T1`` and ``T2`` are type variables. To use these type variables
  2364. as type parameters of ``Array``, we must *unpack* the type variable tuple using
  2365. the star operator: ``*Ts``. The signature of ``Array`` then behaves
  2366. as if we had simply written ``class Array(Generic[T1, T2]): ...``.
  2367. In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
  2368. us to parameterise the class with an *arbitrary* number of type parameters.
  2369. Type variable tuples can be used anywhere a normal ``TypeVar`` can.
  2370. This includes class definitions, as shown above, as well as function
  2371. signatures and variable annotations::
  2372. class Array(Generic[*Ts]):
  2373. def __init__(self, shape: Tuple[*Ts]):
  2374. self._shape: Tuple[*Ts] = shape
  2375. def get_shape(self) -> Tuple[*Ts]:
  2376. return self._shape
  2377. shape = (Height(480), Width(640))
  2378. x: Array[Height, Width] = Array(shape)
  2379. y = abs(x) # Inferred type is Array[Height, Width]
  2380. z = x + x # ... is Array[Height, Width]
  2381. x.get_shape() # ... is tuple[Height, Width]
  2382. """
  2383. # Trick Generic __parameters__.
  2384. __class__ = typing.TypeVar
  2385. def __iter__(self):
  2386. yield self.__unpacked__
  2387. def __init__(self, name, *, default=NoDefault):
  2388. self.__name__ = name
  2389. _DefaultMixin.__init__(self, default)
  2390. # for pickling:
  2391. def_mod = _caller()
  2392. if def_mod != 'typing_extensions':
  2393. self.__module__ = def_mod
  2394. self.__unpacked__ = Unpack[self]
  2395. def __repr__(self):
  2396. return self.__name__
  2397. def __hash__(self):
  2398. return object.__hash__(self)
  2399. def __eq__(self, other):
  2400. return self is other
  2401. def __reduce__(self):
  2402. return self.__name__
  2403. def __init_subclass__(self, *args, **kwds):
  2404. if '_root' not in kwds:
  2405. raise TypeError("Cannot subclass special typing classes")
  2406. if hasattr(typing, "reveal_type"): # 3.11+
  2407. reveal_type = typing.reveal_type
  2408. else: # <=3.10
  2409. def reveal_type(obj: T, /) -> T:
  2410. """Reveal the inferred type of a variable.
  2411. When a static type checker encounters a call to ``reveal_type()``,
  2412. it will emit the inferred type of the argument::
  2413. x: int = 1
  2414. reveal_type(x)
  2415. Running a static type checker (e.g., ``mypy``) on this example
  2416. will produce output similar to 'Revealed type is "builtins.int"'.
  2417. At runtime, the function prints the runtime type of the
  2418. argument and returns it unchanged.
  2419. """
  2420. print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
  2421. return obj
  2422. if hasattr(typing, "_ASSERT_NEVER_REPR_MAX_LENGTH"): # 3.11+
  2423. _ASSERT_NEVER_REPR_MAX_LENGTH = typing._ASSERT_NEVER_REPR_MAX_LENGTH
  2424. else: # <=3.10
  2425. _ASSERT_NEVER_REPR_MAX_LENGTH = 100
  2426. if hasattr(typing, "assert_never"): # 3.11+
  2427. assert_never = typing.assert_never
  2428. else: # <=3.10
  2429. def assert_never(arg: Never, /) -> Never:
  2430. """Assert to the type checker that a line of code is unreachable.
  2431. Example::
  2432. def int_or_str(arg: int | str) -> None:
  2433. match arg:
  2434. case int():
  2435. print("It's an int")
  2436. case str():
  2437. print("It's a str")
  2438. case _:
  2439. assert_never(arg)
  2440. If a type checker finds that a call to assert_never() is
  2441. reachable, it will emit an error.
  2442. At runtime, this throws an exception when called.
  2443. """
  2444. value = repr(arg)
  2445. if len(value) > _ASSERT_NEVER_REPR_MAX_LENGTH:
  2446. value = value[:_ASSERT_NEVER_REPR_MAX_LENGTH] + '...'
  2447. raise AssertionError(f"Expected code to be unreachable, but got: {value}")
  2448. if sys.version_info >= (3, 12): # 3.12+
  2449. # dataclass_transform exists in 3.11 but lacks the frozen_default parameter
  2450. dataclass_transform = typing.dataclass_transform
  2451. else: # <=3.11
  2452. def dataclass_transform(
  2453. *,
  2454. eq_default: bool = True,
  2455. order_default: bool = False,
  2456. kw_only_default: bool = False,
  2457. frozen_default: bool = False,
  2458. field_specifiers: typing.Tuple[
  2459. typing.Union[typing.Type[typing.Any], typing.Callable[..., typing.Any]],
  2460. ...
  2461. ] = (),
  2462. **kwargs: typing.Any,
  2463. ) -> typing.Callable[[T], T]:
  2464. """Decorator that marks a function, class, or metaclass as providing
  2465. dataclass-like behavior.
  2466. Example:
  2467. from typing_extensions import dataclass_transform
  2468. _T = TypeVar("_T")
  2469. # Used on a decorator function
  2470. @dataclass_transform()
  2471. def create_model(cls: type[_T]) -> type[_T]:
  2472. ...
  2473. return cls
  2474. @create_model
  2475. class CustomerModel:
  2476. id: int
  2477. name: str
  2478. # Used on a base class
  2479. @dataclass_transform()
  2480. class ModelBase: ...
  2481. class CustomerModel(ModelBase):
  2482. id: int
  2483. name: str
  2484. # Used on a metaclass
  2485. @dataclass_transform()
  2486. class ModelMeta(type): ...
  2487. class ModelBase(metaclass=ModelMeta): ...
  2488. class CustomerModel(ModelBase):
  2489. id: int
  2490. name: str
  2491. Each of the ``CustomerModel`` classes defined in this example will now
  2492. behave similarly to a dataclass created with the ``@dataclasses.dataclass``
  2493. decorator. For example, the type checker will synthesize an ``__init__``
  2494. method.
  2495. The arguments to this decorator can be used to customize this behavior:
  2496. - ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
  2497. True or False if it is omitted by the caller.
  2498. - ``order_default`` indicates whether the ``order`` parameter is
  2499. assumed to be True or False if it is omitted by the caller.
  2500. - ``kw_only_default`` indicates whether the ``kw_only`` parameter is
  2501. assumed to be True or False if it is omitted by the caller.
  2502. - ``frozen_default`` indicates whether the ``frozen`` parameter is
  2503. assumed to be True or False if it is omitted by the caller.
  2504. - ``field_specifiers`` specifies a static list of supported classes
  2505. or functions that describe fields, similar to ``dataclasses.field()``.
  2506. At runtime, this decorator records its arguments in the
  2507. ``__dataclass_transform__`` attribute on the decorated object.
  2508. See PEP 681 for details.
  2509. """
  2510. def decorator(cls_or_fn):
  2511. cls_or_fn.__dataclass_transform__ = {
  2512. "eq_default": eq_default,
  2513. "order_default": order_default,
  2514. "kw_only_default": kw_only_default,
  2515. "frozen_default": frozen_default,
  2516. "field_specifiers": field_specifiers,
  2517. "kwargs": kwargs,
  2518. }
  2519. return cls_or_fn
  2520. return decorator
  2521. if hasattr(typing, "override"): # 3.12+
  2522. override = typing.override
  2523. else: # <=3.11
  2524. _F = typing.TypeVar("_F", bound=typing.Callable[..., typing.Any])
  2525. def override(arg: _F, /) -> _F:
  2526. """Indicate that a method is intended to override a method in a base class.
  2527. Usage:
  2528. class Base:
  2529. def method(self) -> None:
  2530. pass
  2531. class Child(Base):
  2532. @override
  2533. def method(self) -> None:
  2534. super().method()
  2535. When this decorator is applied to a method, the type checker will
  2536. validate that it overrides a method with the same name on a base class.
  2537. This helps prevent bugs that may occur when a base class is changed
  2538. without an equivalent change to a child class.
  2539. There is no runtime checking of these properties. The decorator
  2540. sets the ``__override__`` attribute to ``True`` on the decorated object
  2541. to allow runtime introspection.
  2542. See PEP 698 for details.
  2543. """
  2544. try:
  2545. arg.__override__ = True
  2546. except (AttributeError, TypeError):
  2547. # Skip the attribute silently if it is not writable.
  2548. # AttributeError happens if the object has __slots__ or a
  2549. # read-only property, TypeError if it's a builtin class.
  2550. pass
  2551. return arg
  2552. if hasattr(warnings, "deprecated"):
  2553. deprecated = warnings.deprecated
  2554. else:
  2555. _T = typing.TypeVar("_T")
  2556. class deprecated:
  2557. """Indicate that a class, function or overload is deprecated.
  2558. When this decorator is applied to an object, the type checker
  2559. will generate a diagnostic on usage of the deprecated object.
  2560. Usage:
  2561. @deprecated("Use B instead")
  2562. class A:
  2563. pass
  2564. @deprecated("Use g instead")
  2565. def f():
  2566. pass
  2567. @overload
  2568. @deprecated("int support is deprecated")
  2569. def g(x: int) -> int: ...
  2570. @overload
  2571. def g(x: str) -> int: ...
  2572. The warning specified by *category* will be emitted at runtime
  2573. on use of deprecated objects. For functions, that happens on calls;
  2574. for classes, on instantiation and on creation of subclasses.
  2575. If the *category* is ``None``, no warning is emitted at runtime.
  2576. The *stacklevel* determines where the
  2577. warning is emitted. If it is ``1`` (the default), the warning
  2578. is emitted at the direct caller of the deprecated object; if it
  2579. is higher, it is emitted further up the stack.
  2580. Static type checker behavior is not affected by the *category*
  2581. and *stacklevel* arguments.
  2582. The deprecation message passed to the decorator is saved in the
  2583. ``__deprecated__`` attribute on the decorated object.
  2584. If applied to an overload, the decorator
  2585. must be after the ``@overload`` decorator for the attribute to
  2586. exist on the overload as returned by ``get_overloads()``.
  2587. See PEP 702 for details.
  2588. """
  2589. def __init__(
  2590. self,
  2591. message: str,
  2592. /,
  2593. *,
  2594. category: typing.Optional[typing.Type[Warning]] = DeprecationWarning,
  2595. stacklevel: int = 1,
  2596. ) -> None:
  2597. if not isinstance(message, str):
  2598. raise TypeError(
  2599. "Expected an object of type str for 'message', not "
  2600. f"{type(message).__name__!r}"
  2601. )
  2602. self.message = message
  2603. self.category = category
  2604. self.stacklevel = stacklevel
  2605. def __call__(self, arg: _T, /) -> _T:
  2606. # Make sure the inner functions created below don't
  2607. # retain a reference to self.
  2608. msg = self.message
  2609. category = self.category
  2610. stacklevel = self.stacklevel
  2611. if category is None:
  2612. arg.__deprecated__ = msg
  2613. return arg
  2614. elif isinstance(arg, type):
  2615. import functools
  2616. from types import MethodType
  2617. original_new = arg.__new__
  2618. @functools.wraps(original_new)
  2619. def __new__(cls, *args, **kwargs):
  2620. if cls is arg:
  2621. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2622. if original_new is not object.__new__:
  2623. return original_new(cls, *args, **kwargs)
  2624. # Mirrors a similar check in object.__new__.
  2625. elif cls.__init__ is object.__init__ and (args or kwargs):
  2626. raise TypeError(f"{cls.__name__}() takes no arguments")
  2627. else:
  2628. return original_new(cls)
  2629. arg.__new__ = staticmethod(__new__)
  2630. original_init_subclass = arg.__init_subclass__
  2631. # We need slightly different behavior if __init_subclass__
  2632. # is a bound method (likely if it was implemented in Python)
  2633. if isinstance(original_init_subclass, MethodType):
  2634. original_init_subclass = original_init_subclass.__func__
  2635. @functools.wraps(original_init_subclass)
  2636. def __init_subclass__(*args, **kwargs):
  2637. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2638. return original_init_subclass(*args, **kwargs)
  2639. arg.__init_subclass__ = classmethod(__init_subclass__)
  2640. # Or otherwise, which likely means it's a builtin such as
  2641. # object's implementation of __init_subclass__.
  2642. else:
  2643. @functools.wraps(original_init_subclass)
  2644. def __init_subclass__(*args, **kwargs):
  2645. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2646. return original_init_subclass(*args, **kwargs)
  2647. arg.__init_subclass__ = __init_subclass__
  2648. arg.__deprecated__ = __new__.__deprecated__ = msg
  2649. __init_subclass__.__deprecated__ = msg
  2650. return arg
  2651. elif callable(arg):
  2652. import asyncio.coroutines
  2653. import functools
  2654. import inspect
  2655. @functools.wraps(arg)
  2656. def wrapper(*args, **kwargs):
  2657. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2658. return arg(*args, **kwargs)
  2659. if asyncio.coroutines.iscoroutinefunction(arg):
  2660. if sys.version_info >= (3, 12):
  2661. wrapper = inspect.markcoroutinefunction(wrapper)
  2662. else:
  2663. wrapper._is_coroutine = asyncio.coroutines._is_coroutine
  2664. arg.__deprecated__ = wrapper.__deprecated__ = msg
  2665. return wrapper
  2666. else:
  2667. raise TypeError(
  2668. "@deprecated decorator with non-None category must be applied to "
  2669. f"a class or callable, not {arg!r}"
  2670. )
  2671. if sys.version_info < (3, 10):
  2672. def _is_param_expr(arg):
  2673. return arg is ... or isinstance(
  2674. arg, (tuple, list, ParamSpec, _ConcatenateGenericAlias)
  2675. )
  2676. else:
  2677. def _is_param_expr(arg):
  2678. return arg is ... or isinstance(
  2679. arg,
  2680. (
  2681. tuple,
  2682. list,
  2683. ParamSpec,
  2684. _ConcatenateGenericAlias,
  2685. typing._ConcatenateGenericAlias,
  2686. ),
  2687. )
  2688. # We have to do some monkey patching to deal with the dual nature of
  2689. # Unpack/TypeVarTuple:
  2690. # - We want Unpack to be a kind of TypeVar so it gets accepted in
  2691. # Generic[Unpack[Ts]]
  2692. # - We want it to *not* be treated as a TypeVar for the purposes of
  2693. # counting generic parameters, so that when we subscript a generic,
  2694. # the runtime doesn't try to substitute the Unpack with the subscripted type.
  2695. if not hasattr(typing, "TypeVarTuple"):
  2696. def _check_generic(cls, parameters, elen=_marker):
  2697. """Check correct count for parameters of a generic cls (internal helper).
  2698. This gives a nice error message in case of count mismatch.
  2699. """
  2700. # If substituting a single ParamSpec with multiple arguments
  2701. # we do not check the count
  2702. if (inspect.isclass(cls) and issubclass(cls, typing.Generic)
  2703. and len(cls.__parameters__) == 1
  2704. and isinstance(cls.__parameters__[0], ParamSpec)
  2705. and parameters
  2706. and not _is_param_expr(parameters[0])
  2707. ):
  2708. # Generic modifies parameters variable, but here we cannot do this
  2709. return
  2710. if not elen:
  2711. raise TypeError(f"{cls} is not a generic class")
  2712. if elen is _marker:
  2713. if not hasattr(cls, "__parameters__") or not cls.__parameters__:
  2714. raise TypeError(f"{cls} is not a generic class")
  2715. elen = len(cls.__parameters__)
  2716. alen = len(parameters)
  2717. if alen != elen:
  2718. expect_val = elen
  2719. if hasattr(cls, "__parameters__"):
  2720. parameters = [p for p in cls.__parameters__ if not _is_unpack(p)]
  2721. num_tv_tuples = sum(isinstance(p, TypeVarTuple) for p in parameters)
  2722. if (num_tv_tuples > 0) and (alen >= elen - num_tv_tuples):
  2723. return
  2724. # deal with TypeVarLike defaults
  2725. # required TypeVarLikes cannot appear after a defaulted one.
  2726. if alen < elen:
  2727. # since we validate TypeVarLike default in _collect_type_vars
  2728. # or _collect_parameters we can safely check parameters[alen]
  2729. if (
  2730. getattr(parameters[alen], '__default__', NoDefault)
  2731. is not NoDefault
  2732. ):
  2733. return
  2734. num_default_tv = sum(getattr(p, '__default__', NoDefault)
  2735. is not NoDefault for p in parameters)
  2736. elen -= num_default_tv
  2737. expect_val = f"at least {elen}"
  2738. things = "arguments" if sys.version_info >= (3, 10) else "parameters"
  2739. raise TypeError(f"Too {'many' if alen > elen else 'few'} {things}"
  2740. f" for {cls}; actual {alen}, expected {expect_val}")
  2741. else:
  2742. # Python 3.11+
  2743. def _check_generic(cls, parameters, elen):
  2744. """Check correct count for parameters of a generic cls (internal helper).
  2745. This gives a nice error message in case of count mismatch.
  2746. """
  2747. if not elen:
  2748. raise TypeError(f"{cls} is not a generic class")
  2749. alen = len(parameters)
  2750. if alen != elen:
  2751. expect_val = elen
  2752. if hasattr(cls, "__parameters__"):
  2753. parameters = [p for p in cls.__parameters__ if not _is_unpack(p)]
  2754. # deal with TypeVarLike defaults
  2755. # required TypeVarLikes cannot appear after a defaulted one.
  2756. if alen < elen:
  2757. # since we validate TypeVarLike default in _collect_type_vars
  2758. # or _collect_parameters we can safely check parameters[alen]
  2759. if (
  2760. getattr(parameters[alen], '__default__', NoDefault)
  2761. is not NoDefault
  2762. ):
  2763. return
  2764. num_default_tv = sum(getattr(p, '__default__', NoDefault)
  2765. is not NoDefault for p in parameters)
  2766. elen -= num_default_tv
  2767. expect_val = f"at least {elen}"
  2768. raise TypeError(f"Too {'many' if alen > elen else 'few'} arguments"
  2769. f" for {cls}; actual {alen}, expected {expect_val}")
  2770. if not _PEP_696_IMPLEMENTED:
  2771. typing._check_generic = _check_generic
  2772. def _has_generic_or_protocol_as_origin() -> bool:
  2773. try:
  2774. frame = sys._getframe(2)
  2775. # - Catch AttributeError: not all Python implementations have sys._getframe()
  2776. # - Catch ValueError: maybe we're called from an unexpected module
  2777. # and the call stack isn't deep enough
  2778. except (AttributeError, ValueError):
  2779. return False # err on the side of leniency
  2780. else:
  2781. # If we somehow get invoked from outside typing.py,
  2782. # also err on the side of leniency
  2783. if frame.f_globals.get("__name__") != "typing":
  2784. return False
  2785. origin = frame.f_locals.get("origin")
  2786. # Cannot use "in" because origin may be an object with a buggy __eq__ that
  2787. # throws an error.
  2788. return origin is typing.Generic or origin is Protocol or origin is typing.Protocol
  2789. _TYPEVARTUPLE_TYPES = {TypeVarTuple, getattr(typing, "TypeVarTuple", None)}
  2790. def _is_unpacked_typevartuple(x) -> bool:
  2791. if get_origin(x) is not Unpack:
  2792. return False
  2793. args = get_args(x)
  2794. return (
  2795. bool(args)
  2796. and len(args) == 1
  2797. and type(args[0]) in _TYPEVARTUPLE_TYPES
  2798. )
  2799. # Python 3.11+ _collect_type_vars was renamed to _collect_parameters
  2800. if hasattr(typing, '_collect_type_vars'):
  2801. def _collect_type_vars(types, typevar_types=None):
  2802. """Collect all type variable contained in types in order of
  2803. first appearance (lexicographic order). For example::
  2804. _collect_type_vars((T, List[S, T])) == (T, S)
  2805. """
  2806. if typevar_types is None:
  2807. typevar_types = typing.TypeVar
  2808. tvars = []
  2809. # A required TypeVarLike cannot appear after a TypeVarLike with a default
  2810. # if it was a direct call to `Generic[]` or `Protocol[]`
  2811. enforce_default_ordering = _has_generic_or_protocol_as_origin()
  2812. default_encountered = False
  2813. # Also, a TypeVarLike with a default cannot appear after a TypeVarTuple
  2814. type_var_tuple_encountered = False
  2815. for t in types:
  2816. if _is_unpacked_typevartuple(t):
  2817. type_var_tuple_encountered = True
  2818. elif (
  2819. isinstance(t, typevar_types) and not isinstance(t, _UnpackAlias)
  2820. and t not in tvars
  2821. ):
  2822. if enforce_default_ordering:
  2823. has_default = getattr(t, '__default__', NoDefault) is not NoDefault
  2824. if has_default:
  2825. if type_var_tuple_encountered:
  2826. raise TypeError('Type parameter with a default'
  2827. ' follows TypeVarTuple')
  2828. default_encountered = True
  2829. elif default_encountered:
  2830. raise TypeError(f'Type parameter {t!r} without a default'
  2831. ' follows type parameter with a default')
  2832. tvars.append(t)
  2833. if _should_collect_from_parameters(t):
  2834. tvars.extend([t for t in t.__parameters__ if t not in tvars])
  2835. elif isinstance(t, tuple):
  2836. # Collect nested type_vars
  2837. # tuple wrapped by _prepare_paramspec_params(cls, params)
  2838. for x in t:
  2839. for collected in _collect_type_vars([x]):
  2840. if collected not in tvars:
  2841. tvars.append(collected)
  2842. return tuple(tvars)
  2843. typing._collect_type_vars = _collect_type_vars
  2844. else:
  2845. def _collect_parameters(args):
  2846. """Collect all type variables and parameter specifications in args
  2847. in order of first appearance (lexicographic order).
  2848. For example::
  2849. assert _collect_parameters((T, Callable[P, T])) == (T, P)
  2850. """
  2851. parameters = []
  2852. # A required TypeVarLike cannot appear after a TypeVarLike with default
  2853. # if it was a direct call to `Generic[]` or `Protocol[]`
  2854. enforce_default_ordering = _has_generic_or_protocol_as_origin()
  2855. default_encountered = False
  2856. # Also, a TypeVarLike with a default cannot appear after a TypeVarTuple
  2857. type_var_tuple_encountered = False
  2858. for t in args:
  2859. if isinstance(t, type):
  2860. # We don't want __parameters__ descriptor of a bare Python class.
  2861. pass
  2862. elif isinstance(t, tuple):
  2863. # `t` might be a tuple, when `ParamSpec` is substituted with
  2864. # `[T, int]`, or `[int, *Ts]`, etc.
  2865. for x in t:
  2866. for collected in _collect_parameters([x]):
  2867. if collected not in parameters:
  2868. parameters.append(collected)
  2869. elif hasattr(t, '__typing_subst__'):
  2870. if t not in parameters:
  2871. if enforce_default_ordering:
  2872. has_default = (
  2873. getattr(t, '__default__', NoDefault) is not NoDefault
  2874. )
  2875. if type_var_tuple_encountered and has_default:
  2876. raise TypeError('Type parameter with a default'
  2877. ' follows TypeVarTuple')
  2878. if has_default:
  2879. default_encountered = True
  2880. elif default_encountered:
  2881. raise TypeError(f'Type parameter {t!r} without a default'
  2882. ' follows type parameter with a default')
  2883. parameters.append(t)
  2884. else:
  2885. if _is_unpacked_typevartuple(t):
  2886. type_var_tuple_encountered = True
  2887. for x in getattr(t, '__parameters__', ()):
  2888. if x not in parameters:
  2889. parameters.append(x)
  2890. return tuple(parameters)
  2891. if not _PEP_696_IMPLEMENTED:
  2892. typing._collect_parameters = _collect_parameters
  2893. # Backport typing.NamedTuple as it exists in Python 3.13.
  2894. # In 3.11, the ability to define generic `NamedTuple`s was supported.
  2895. # This was explicitly disallowed in 3.9-3.10, and only half-worked in <=3.8.
  2896. # On 3.12, we added __orig_bases__ to call-based NamedTuples
  2897. # On 3.13, we deprecated kwargs-based NamedTuples
  2898. if sys.version_info >= (3, 13):
  2899. NamedTuple = typing.NamedTuple
  2900. else:
  2901. def _make_nmtuple(name, types, module, defaults=()):
  2902. fields = [n for n, t in types]
  2903. annotations = {n: typing._type_check(t, f"field {n} annotation must be a type")
  2904. for n, t in types}
  2905. nm_tpl = collections.namedtuple(name, fields,
  2906. defaults=defaults, module=module)
  2907. nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = annotations
  2908. # The `_field_types` attribute was removed in 3.9;
  2909. # in earlier versions, it is the same as the `__annotations__` attribute
  2910. if sys.version_info < (3, 9):
  2911. nm_tpl._field_types = annotations
  2912. return nm_tpl
  2913. _prohibited_namedtuple_fields = typing._prohibited
  2914. _special_namedtuple_fields = frozenset({'__module__', '__name__', '__annotations__'})
  2915. class _NamedTupleMeta(type):
  2916. def __new__(cls, typename, bases, ns):
  2917. assert _NamedTuple in bases
  2918. for base in bases:
  2919. if base is not _NamedTuple and base is not typing.Generic:
  2920. raise TypeError(
  2921. 'can only inherit from a NamedTuple type and Generic')
  2922. bases = tuple(tuple if base is _NamedTuple else base for base in bases)
  2923. if "__annotations__" in ns:
  2924. types = ns["__annotations__"]
  2925. elif "__annotate__" in ns:
  2926. # TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
  2927. types = ns["__annotate__"](1)
  2928. else:
  2929. types = {}
  2930. default_names = []
  2931. for field_name in types:
  2932. if field_name in ns:
  2933. default_names.append(field_name)
  2934. elif default_names:
  2935. raise TypeError(f"Non-default namedtuple field {field_name} "
  2936. f"cannot follow default field"
  2937. f"{'s' if len(default_names) > 1 else ''} "
  2938. f"{', '.join(default_names)}")
  2939. nm_tpl = _make_nmtuple(
  2940. typename, types.items(),
  2941. defaults=[ns[n] for n in default_names],
  2942. module=ns['__module__']
  2943. )
  2944. nm_tpl.__bases__ = bases
  2945. if typing.Generic in bases:
  2946. if hasattr(typing, '_generic_class_getitem'): # 3.12+
  2947. nm_tpl.__class_getitem__ = classmethod(typing._generic_class_getitem)
  2948. else:
  2949. class_getitem = typing.Generic.__class_getitem__.__func__
  2950. nm_tpl.__class_getitem__ = classmethod(class_getitem)
  2951. # update from user namespace without overriding special namedtuple attributes
  2952. for key, val in ns.items():
  2953. if key in _prohibited_namedtuple_fields:
  2954. raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
  2955. elif key not in _special_namedtuple_fields:
  2956. if key not in nm_tpl._fields:
  2957. setattr(nm_tpl, key, ns[key])
  2958. try:
  2959. set_name = type(val).__set_name__
  2960. except AttributeError:
  2961. pass
  2962. else:
  2963. try:
  2964. set_name(val, nm_tpl, key)
  2965. except BaseException as e:
  2966. msg = (
  2967. f"Error calling __set_name__ on {type(val).__name__!r} "
  2968. f"instance {key!r} in {typename!r}"
  2969. )
  2970. # BaseException.add_note() existed on py311,
  2971. # but the __set_name__ machinery didn't start
  2972. # using add_note() until py312.
  2973. # Making sure exceptions are raised in the same way
  2974. # as in "normal" classes seems most important here.
  2975. if sys.version_info >= (3, 12):
  2976. e.add_note(msg)
  2977. raise
  2978. else:
  2979. raise RuntimeError(msg) from e
  2980. if typing.Generic in bases:
  2981. nm_tpl.__init_subclass__()
  2982. return nm_tpl
  2983. _NamedTuple = type.__new__(_NamedTupleMeta, 'NamedTuple', (), {})
  2984. def _namedtuple_mro_entries(bases):
  2985. assert NamedTuple in bases
  2986. return (_NamedTuple,)
  2987. @_ensure_subclassable(_namedtuple_mro_entries)
  2988. def NamedTuple(typename, fields=_marker, /, **kwargs):
  2989. """Typed version of namedtuple.
  2990. Usage::
  2991. class Employee(NamedTuple):
  2992. name: str
  2993. id: int
  2994. This is equivalent to::
  2995. Employee = collections.namedtuple('Employee', ['name', 'id'])
  2996. The resulting class has an extra __annotations__ attribute, giving a
  2997. dict that maps field names to types. (The field names are also in
  2998. the _fields attribute, which is part of the namedtuple API.)
  2999. An alternative equivalent functional syntax is also accepted::
  3000. Employee = NamedTuple('Employee', [('name', str), ('id', int)])
  3001. """
  3002. if fields is _marker:
  3003. if kwargs:
  3004. deprecated_thing = "Creating NamedTuple classes using keyword arguments"
  3005. deprecation_msg = (
  3006. "{name} is deprecated and will be disallowed in Python {remove}. "
  3007. "Use the class-based or functional syntax instead."
  3008. )
  3009. else:
  3010. deprecated_thing = "Failing to pass a value for the 'fields' parameter"
  3011. example = f"`{typename} = NamedTuple({typename!r}, [])`"
  3012. deprecation_msg = (
  3013. "{name} is deprecated and will be disallowed in Python {remove}. "
  3014. "To create a NamedTuple class with 0 fields "
  3015. "using the functional syntax, "
  3016. "pass an empty list, e.g. "
  3017. ) + example + "."
  3018. elif fields is None:
  3019. if kwargs:
  3020. raise TypeError(
  3021. "Cannot pass `None` as the 'fields' parameter "
  3022. "and also specify fields using keyword arguments"
  3023. )
  3024. else:
  3025. deprecated_thing = "Passing `None` as the 'fields' parameter"
  3026. example = f"`{typename} = NamedTuple({typename!r}, [])`"
  3027. deprecation_msg = (
  3028. "{name} is deprecated and will be disallowed in Python {remove}. "
  3029. "To create a NamedTuple class with 0 fields "
  3030. "using the functional syntax, "
  3031. "pass an empty list, e.g. "
  3032. ) + example + "."
  3033. elif kwargs:
  3034. raise TypeError("Either list of fields or keywords"
  3035. " can be provided to NamedTuple, not both")
  3036. if fields is _marker or fields is None:
  3037. warnings.warn(
  3038. deprecation_msg.format(name=deprecated_thing, remove="3.15"),
  3039. DeprecationWarning,
  3040. stacklevel=2,
  3041. )
  3042. fields = kwargs.items()
  3043. nt = _make_nmtuple(typename, fields, module=_caller())
  3044. nt.__orig_bases__ = (NamedTuple,)
  3045. return nt
  3046. if hasattr(collections.abc, "Buffer"):
  3047. Buffer = collections.abc.Buffer
  3048. else:
  3049. class Buffer(abc.ABC): # noqa: B024
  3050. """Base class for classes that implement the buffer protocol.
  3051. The buffer protocol allows Python objects to expose a low-level
  3052. memory buffer interface. Before Python 3.12, it is not possible
  3053. to implement the buffer protocol in pure Python code, or even
  3054. to check whether a class implements the buffer protocol. In
  3055. Python 3.12 and higher, the ``__buffer__`` method allows access
  3056. to the buffer protocol from Python code, and the
  3057. ``collections.abc.Buffer`` ABC allows checking whether a class
  3058. implements the buffer protocol.
  3059. To indicate support for the buffer protocol in earlier versions,
  3060. inherit from this ABC, either in a stub file or at runtime,
  3061. or use ABC registration. This ABC provides no methods, because
  3062. there is no Python-accessible methods shared by pre-3.12 buffer
  3063. classes. It is useful primarily for static checks.
  3064. """
  3065. # As a courtesy, register the most common stdlib buffer classes.
  3066. Buffer.register(memoryview)
  3067. Buffer.register(bytearray)
  3068. Buffer.register(bytes)
  3069. # Backport of types.get_original_bases, available on 3.12+ in CPython
  3070. if hasattr(_types, "get_original_bases"):
  3071. get_original_bases = _types.get_original_bases
  3072. else:
  3073. def get_original_bases(cls, /):
  3074. """Return the class's "original" bases prior to modification by `__mro_entries__`.
  3075. Examples::
  3076. from typing import TypeVar, Generic
  3077. from typing_extensions import NamedTuple, TypedDict
  3078. T = TypeVar("T")
  3079. class Foo(Generic[T]): ...
  3080. class Bar(Foo[int], float): ...
  3081. class Baz(list[str]): ...
  3082. Eggs = NamedTuple("Eggs", [("a", int), ("b", str)])
  3083. Spam = TypedDict("Spam", {"a": int, "b": str})
  3084. assert get_original_bases(Bar) == (Foo[int], float)
  3085. assert get_original_bases(Baz) == (list[str],)
  3086. assert get_original_bases(Eggs) == (NamedTuple,)
  3087. assert get_original_bases(Spam) == (TypedDict,)
  3088. assert get_original_bases(int) == (object,)
  3089. """
  3090. try:
  3091. return cls.__dict__.get("__orig_bases__", cls.__bases__)
  3092. except AttributeError:
  3093. raise TypeError(
  3094. f'Expected an instance of type, not {type(cls).__name__!r}'
  3095. ) from None
  3096. # NewType is a class on Python 3.10+, making it pickleable
  3097. # The error message for subclassing instances of NewType was improved on 3.11+
  3098. if sys.version_info >= (3, 11):
  3099. NewType = typing.NewType
  3100. else:
  3101. class NewType:
  3102. """NewType creates simple unique types with almost zero
  3103. runtime overhead. NewType(name, tp) is considered a subtype of tp
  3104. by static type checkers. At runtime, NewType(name, tp) returns
  3105. a dummy callable that simply returns its argument. Usage::
  3106. UserId = NewType('UserId', int)
  3107. def name_by_id(user_id: UserId) -> str:
  3108. ...
  3109. UserId('user') # Fails type check
  3110. name_by_id(42) # Fails type check
  3111. name_by_id(UserId(42)) # OK
  3112. num = UserId(5) + 1 # type: int
  3113. """
  3114. def __call__(self, obj, /):
  3115. return obj
  3116. def __init__(self, name, tp):
  3117. self.__qualname__ = name
  3118. if '.' in name:
  3119. name = name.rpartition('.')[-1]
  3120. self.__name__ = name
  3121. self.__supertype__ = tp
  3122. def_mod = _caller()
  3123. if def_mod != 'typing_extensions':
  3124. self.__module__ = def_mod
  3125. def __mro_entries__(self, bases):
  3126. # We defined __mro_entries__ to get a better error message
  3127. # if a user attempts to subclass a NewType instance. bpo-46170
  3128. supercls_name = self.__name__
  3129. class Dummy:
  3130. def __init_subclass__(cls):
  3131. subcls_name = cls.__name__
  3132. raise TypeError(
  3133. f"Cannot subclass an instance of NewType. "
  3134. f"Perhaps you were looking for: "
  3135. f"`{subcls_name} = NewType({subcls_name!r}, {supercls_name})`"
  3136. )
  3137. return (Dummy,)
  3138. def __repr__(self):
  3139. return f'{self.__module__}.{self.__qualname__}'
  3140. def __reduce__(self):
  3141. return self.__qualname__
  3142. if sys.version_info >= (3, 10):
  3143. # PEP 604 methods
  3144. # It doesn't make sense to have these methods on Python <3.10
  3145. def __or__(self, other):
  3146. return typing.Union[self, other]
  3147. def __ror__(self, other):
  3148. return typing.Union[other, self]
  3149. if sys.version_info >= (3, 14):
  3150. TypeAliasType = typing.TypeAliasType
  3151. # 3.8-3.13
  3152. else:
  3153. def _is_unionable(obj):
  3154. """Corresponds to is_unionable() in unionobject.c in CPython."""
  3155. return obj is None or isinstance(obj, (
  3156. type,
  3157. _types.GenericAlias,
  3158. _types.UnionType,
  3159. TypeAliasType,
  3160. ))
  3161. if sys.version_info < (3, 10):
  3162. # Copied and pasted from https://github.com/python/cpython/blob/986a4e1b6fcae7fe7a1d0a26aea446107dd58dd2/Objects/genericaliasobject.c#L568-L582,
  3163. # so that we emulate the behaviour of `types.GenericAlias`
  3164. # on the latest versions of CPython
  3165. _ATTRIBUTE_DELEGATION_EXCLUSIONS = frozenset({
  3166. "__class__",
  3167. "__bases__",
  3168. "__origin__",
  3169. "__args__",
  3170. "__unpacked__",
  3171. "__parameters__",
  3172. "__typing_unpacked_tuple_args__",
  3173. "__mro_entries__",
  3174. "__reduce_ex__",
  3175. "__reduce__",
  3176. "__copy__",
  3177. "__deepcopy__",
  3178. })
  3179. class _TypeAliasGenericAlias(typing._GenericAlias, _root=True):
  3180. def __getattr__(self, attr):
  3181. if attr in _ATTRIBUTE_DELEGATION_EXCLUSIONS:
  3182. return object.__getattr__(self, attr)
  3183. return getattr(self.__origin__, attr)
  3184. if sys.version_info < (3, 9):
  3185. def __getitem__(self, item):
  3186. result = super().__getitem__(item)
  3187. result.__class__ = type(self)
  3188. return result
  3189. class TypeAliasType:
  3190. """Create named, parameterized type aliases.
  3191. This provides a backport of the new `type` statement in Python 3.12:
  3192. type ListOrSet[T] = list[T] | set[T]
  3193. is equivalent to:
  3194. T = TypeVar("T")
  3195. ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,))
  3196. The name ListOrSet can then be used as an alias for the type it refers to.
  3197. The type_params argument should contain all the type parameters used
  3198. in the value of the type alias. If the alias is not generic, this
  3199. argument is omitted.
  3200. Static type checkers should only support type aliases declared using
  3201. TypeAliasType that follow these rules:
  3202. - The first argument (the name) must be a string literal.
  3203. - The TypeAliasType instance must be immediately assigned to a variable
  3204. of the same name. (For example, 'X = TypeAliasType("Y", int)' is invalid,
  3205. as is 'X, Y = TypeAliasType("X", int), TypeAliasType("Y", int)').
  3206. """
  3207. def __init__(self, name: str, value, *, type_params=()):
  3208. if not isinstance(name, str):
  3209. raise TypeError("TypeAliasType name must be a string")
  3210. if not isinstance(type_params, tuple):
  3211. raise TypeError("type_params must be a tuple")
  3212. self.__value__ = value
  3213. self.__type_params__ = type_params
  3214. default_value_encountered = False
  3215. parameters = []
  3216. for type_param in type_params:
  3217. if (
  3218. not isinstance(type_param, (TypeVar, TypeVarTuple, ParamSpec))
  3219. # 3.8-3.11
  3220. # Unpack Backport passes isinstance(type_param, TypeVar)
  3221. or _is_unpack(type_param)
  3222. ):
  3223. raise TypeError(f"Expected a type param, got {type_param!r}")
  3224. has_default = (
  3225. getattr(type_param, '__default__', NoDefault) is not NoDefault
  3226. )
  3227. if default_value_encountered and not has_default:
  3228. raise TypeError(f"non-default type parameter '{type_param!r}'"
  3229. " follows default type parameter")
  3230. if has_default:
  3231. default_value_encountered = True
  3232. if isinstance(type_param, TypeVarTuple):
  3233. parameters.extend(type_param)
  3234. else:
  3235. parameters.append(type_param)
  3236. self.__parameters__ = tuple(parameters)
  3237. def_mod = _caller()
  3238. if def_mod != 'typing_extensions':
  3239. self.__module__ = def_mod
  3240. # Setting this attribute closes the TypeAliasType from further modification
  3241. self.__name__ = name
  3242. def __setattr__(self, name: str, value: object, /) -> None:
  3243. if hasattr(self, "__name__"):
  3244. self._raise_attribute_error(name)
  3245. super().__setattr__(name, value)
  3246. def __delattr__(self, name: str, /) -> Never:
  3247. self._raise_attribute_error(name)
  3248. def _raise_attribute_error(self, name: str) -> Never:
  3249. # Match the Python 3.12 error messages exactly
  3250. if name == "__name__":
  3251. raise AttributeError("readonly attribute")
  3252. elif name in {"__value__", "__type_params__", "__parameters__", "__module__"}:
  3253. raise AttributeError(
  3254. f"attribute '{name}' of 'typing.TypeAliasType' objects "
  3255. "is not writable"
  3256. )
  3257. else:
  3258. raise AttributeError(
  3259. f"'typing.TypeAliasType' object has no attribute '{name}'"
  3260. )
  3261. def __repr__(self) -> str:
  3262. return self.__name__
  3263. if sys.version_info < (3, 11):
  3264. def _check_single_param(self, param, recursion=0):
  3265. # Allow [], [int], [int, str], [int, ...], [int, T]
  3266. if param is ...:
  3267. return ...
  3268. if param is None:
  3269. return None
  3270. # Note in <= 3.9 _ConcatenateGenericAlias inherits from list
  3271. if isinstance(param, list) and recursion == 0:
  3272. return [self._check_single_param(arg, recursion+1)
  3273. for arg in param]
  3274. return typing._type_check(
  3275. param, f'Subscripting {self.__name__} requires a type.'
  3276. )
  3277. def _check_parameters(self, parameters):
  3278. if sys.version_info < (3, 11):
  3279. return tuple(
  3280. self._check_single_param(item)
  3281. for item in parameters
  3282. )
  3283. return tuple(typing._type_check(
  3284. item, f'Subscripting {self.__name__} requires a type.'
  3285. )
  3286. for item in parameters
  3287. )
  3288. def __getitem__(self, parameters):
  3289. if not self.__type_params__:
  3290. raise TypeError("Only generic type aliases are subscriptable")
  3291. if not isinstance(parameters, tuple):
  3292. parameters = (parameters,)
  3293. # Using 3.9 here will create problems with Concatenate
  3294. if sys.version_info >= (3, 10):
  3295. return _types.GenericAlias(self, parameters)
  3296. type_vars = _collect_type_vars(parameters)
  3297. parameters = self._check_parameters(parameters)
  3298. alias = _TypeAliasGenericAlias(self, parameters)
  3299. # alias.__parameters__ is not complete if Concatenate is present
  3300. # as it is converted to a list from which no parameters are extracted.
  3301. if alias.__parameters__ != type_vars:
  3302. alias.__parameters__ = type_vars
  3303. return alias
  3304. def __reduce__(self):
  3305. return self.__name__
  3306. def __init_subclass__(cls, *args, **kwargs):
  3307. raise TypeError(
  3308. "type 'typing_extensions.TypeAliasType' is not an acceptable base type"
  3309. )
  3310. # The presence of this method convinces typing._type_check
  3311. # that TypeAliasTypes are types.
  3312. def __call__(self):
  3313. raise TypeError("Type alias is not callable")
  3314. if sys.version_info >= (3, 10):
  3315. def __or__(self, right):
  3316. # For forward compatibility with 3.12, reject Unions
  3317. # that are not accepted by the built-in Union.
  3318. if not _is_unionable(right):
  3319. return NotImplemented
  3320. return typing.Union[self, right]
  3321. def __ror__(self, left):
  3322. if not _is_unionable(left):
  3323. return NotImplemented
  3324. return typing.Union[left, self]
  3325. if hasattr(typing, "is_protocol"):
  3326. is_protocol = typing.is_protocol
  3327. get_protocol_members = typing.get_protocol_members
  3328. else:
  3329. def is_protocol(tp: type, /) -> bool:
  3330. """Return True if the given type is a Protocol.
  3331. Example::
  3332. >>> from typing_extensions import Protocol, is_protocol
  3333. >>> class P(Protocol):
  3334. ... def a(self) -> str: ...
  3335. ... b: int
  3336. >>> is_protocol(P)
  3337. True
  3338. >>> is_protocol(int)
  3339. False
  3340. """
  3341. return (
  3342. isinstance(tp, type)
  3343. and getattr(tp, '_is_protocol', False)
  3344. and tp is not Protocol
  3345. and tp is not typing.Protocol
  3346. )
  3347. def get_protocol_members(tp: type, /) -> typing.FrozenSet[str]:
  3348. """Return the set of members defined in a Protocol.
  3349. Example::
  3350. >>> from typing_extensions import Protocol, get_protocol_members
  3351. >>> class P(Protocol):
  3352. ... def a(self) -> str: ...
  3353. ... b: int
  3354. >>> get_protocol_members(P)
  3355. frozenset({'a', 'b'})
  3356. Raise a TypeError for arguments that are not Protocols.
  3357. """
  3358. if not is_protocol(tp):
  3359. raise TypeError(f'{tp!r} is not a Protocol')
  3360. if hasattr(tp, '__protocol_attrs__'):
  3361. return frozenset(tp.__protocol_attrs__)
  3362. return frozenset(_get_protocol_attrs(tp))
  3363. if hasattr(typing, "Doc"):
  3364. Doc = typing.Doc
  3365. else:
  3366. class Doc:
  3367. """Define the documentation of a type annotation using ``Annotated``, to be
  3368. used in class attributes, function and method parameters, return values,
  3369. and variables.
  3370. The value should be a positional-only string literal to allow static tools
  3371. like editors and documentation generators to use it.
  3372. This complements docstrings.
  3373. The string value passed is available in the attribute ``documentation``.
  3374. Example::
  3375. >>> from typing_extensions import Annotated, Doc
  3376. >>> def hi(to: Annotated[str, Doc("Who to say hi to")]) -> None: ...
  3377. """
  3378. def __init__(self, documentation: str, /) -> None:
  3379. self.documentation = documentation
  3380. def __repr__(self) -> str:
  3381. return f"Doc({self.documentation!r})"
  3382. def __hash__(self) -> int:
  3383. return hash(self.documentation)
  3384. def __eq__(self, other: object) -> bool:
  3385. if not isinstance(other, Doc):
  3386. return NotImplemented
  3387. return self.documentation == other.documentation
  3388. _CapsuleType = getattr(_types, "CapsuleType", None)
  3389. if _CapsuleType is None:
  3390. try:
  3391. import _socket
  3392. except ImportError:
  3393. pass
  3394. else:
  3395. _CAPI = getattr(_socket, "CAPI", None)
  3396. if _CAPI is not None:
  3397. _CapsuleType = type(_CAPI)
  3398. if _CapsuleType is not None:
  3399. CapsuleType = _CapsuleType
  3400. __all__.append("CapsuleType")
  3401. # Using this convoluted approach so that this keeps working
  3402. # whether we end up using PEP 649 as written, PEP 749, or
  3403. # some other variation: in any case, inspect.get_annotations
  3404. # will continue to exist and will gain a `format` parameter.
  3405. _PEP_649_OR_749_IMPLEMENTED = (
  3406. hasattr(inspect, 'get_annotations')
  3407. and inspect.get_annotations.__kwdefaults__ is not None
  3408. and "format" in inspect.get_annotations.__kwdefaults__
  3409. )
  3410. class Format(enum.IntEnum):
  3411. VALUE = 1
  3412. FORWARDREF = 2
  3413. STRING = 3
  3414. if _PEP_649_OR_749_IMPLEMENTED:
  3415. get_annotations = inspect.get_annotations
  3416. else:
  3417. def get_annotations(obj, *, globals=None, locals=None, eval_str=False,
  3418. format=Format.VALUE):
  3419. """Compute the annotations dict for an object.
  3420. obj may be a callable, class, or module.
  3421. Passing in an object of any other type raises TypeError.
  3422. Returns a dict. get_annotations() returns a new dict every time
  3423. it's called; calling it twice on the same object will return two
  3424. different but equivalent dicts.
  3425. This is a backport of `inspect.get_annotations`, which has been
  3426. in the standard library since Python 3.10. See the standard library
  3427. documentation for more:
  3428. https://docs.python.org/3/library/inspect.html#inspect.get_annotations
  3429. This backport adds the *format* argument introduced by PEP 649. The
  3430. three formats supported are:
  3431. * VALUE: the annotations are returned as-is. This is the default and
  3432. it is compatible with the behavior on previous Python versions.
  3433. * FORWARDREF: return annotations as-is if possible, but replace any
  3434. undefined names with ForwardRef objects. The implementation proposed by
  3435. PEP 649 relies on language changes that cannot be backported; the
  3436. typing-extensions implementation simply returns the same result as VALUE.
  3437. * STRING: return annotations as strings, in a format close to the original
  3438. source. Again, this behavior cannot be replicated directly in a backport.
  3439. As an approximation, typing-extensions retrieves the annotations under
  3440. VALUE semantics and then stringifies them.
  3441. The purpose of this backport is to allow users who would like to use
  3442. FORWARDREF or STRING semantics once PEP 649 is implemented, but who also
  3443. want to support earlier Python versions, to simply write:
  3444. typing_extensions.get_annotations(obj, format=Format.FORWARDREF)
  3445. """
  3446. format = Format(format)
  3447. if eval_str and format is not Format.VALUE:
  3448. raise ValueError("eval_str=True is only supported with format=Format.VALUE")
  3449. if isinstance(obj, type):
  3450. # class
  3451. obj_dict = getattr(obj, '__dict__', None)
  3452. if obj_dict and hasattr(obj_dict, 'get'):
  3453. ann = obj_dict.get('__annotations__', None)
  3454. if isinstance(ann, _types.GetSetDescriptorType):
  3455. ann = None
  3456. else:
  3457. ann = None
  3458. obj_globals = None
  3459. module_name = getattr(obj, '__module__', None)
  3460. if module_name:
  3461. module = sys.modules.get(module_name, None)
  3462. if module:
  3463. obj_globals = getattr(module, '__dict__', None)
  3464. obj_locals = dict(vars(obj))
  3465. unwrap = obj
  3466. elif isinstance(obj, _types.ModuleType):
  3467. # module
  3468. ann = getattr(obj, '__annotations__', None)
  3469. obj_globals = obj.__dict__
  3470. obj_locals = None
  3471. unwrap = None
  3472. elif callable(obj):
  3473. # this includes types.Function, types.BuiltinFunctionType,
  3474. # types.BuiltinMethodType, functools.partial, functools.singledispatch,
  3475. # "class funclike" from Lib/test/test_inspect... on and on it goes.
  3476. ann = getattr(obj, '__annotations__', None)
  3477. obj_globals = getattr(obj, '__globals__', None)
  3478. obj_locals = None
  3479. unwrap = obj
  3480. elif hasattr(obj, '__annotations__'):
  3481. ann = obj.__annotations__
  3482. obj_globals = obj_locals = unwrap = None
  3483. else:
  3484. raise TypeError(f"{obj!r} is not a module, class, or callable.")
  3485. if ann is None:
  3486. return {}
  3487. if not isinstance(ann, dict):
  3488. raise ValueError(f"{obj!r}.__annotations__ is neither a dict nor None")
  3489. if not ann:
  3490. return {}
  3491. if not eval_str:
  3492. if format is Format.STRING:
  3493. return {
  3494. key: value if isinstance(value, str) else typing._type_repr(value)
  3495. for key, value in ann.items()
  3496. }
  3497. return dict(ann)
  3498. if unwrap is not None:
  3499. while True:
  3500. if hasattr(unwrap, '__wrapped__'):
  3501. unwrap = unwrap.__wrapped__
  3502. continue
  3503. if isinstance(unwrap, functools.partial):
  3504. unwrap = unwrap.func
  3505. continue
  3506. break
  3507. if hasattr(unwrap, "__globals__"):
  3508. obj_globals = unwrap.__globals__
  3509. if globals is None:
  3510. globals = obj_globals
  3511. if locals is None:
  3512. locals = obj_locals or {}
  3513. # "Inject" type parameters into the local namespace
  3514. # (unless they are shadowed by assignments *in* the local namespace),
  3515. # as a way of emulating annotation scopes when calling `eval()`
  3516. if type_params := getattr(obj, "__type_params__", ()):
  3517. locals = {param.__name__: param for param in type_params} | locals
  3518. return_value = {key:
  3519. value if not isinstance(value, str) else eval(value, globals, locals)
  3520. for key, value in ann.items() }
  3521. return return_value
  3522. if hasattr(typing, "evaluate_forward_ref"):
  3523. evaluate_forward_ref = typing.evaluate_forward_ref
  3524. else:
  3525. # Implements annotationlib.ForwardRef.evaluate
  3526. def _eval_with_owner(
  3527. forward_ref, *, owner=None, globals=None, locals=None, type_params=None
  3528. ):
  3529. if forward_ref.__forward_evaluated__:
  3530. return forward_ref.__forward_value__
  3531. if getattr(forward_ref, "__cell__", None) is not None:
  3532. try:
  3533. value = forward_ref.__cell__.cell_contents
  3534. except ValueError:
  3535. pass
  3536. else:
  3537. forward_ref.__forward_evaluated__ = True
  3538. forward_ref.__forward_value__ = value
  3539. return value
  3540. if owner is None:
  3541. owner = getattr(forward_ref, "__owner__", None)
  3542. if (
  3543. globals is None
  3544. and getattr(forward_ref, "__forward_module__", None) is not None
  3545. ):
  3546. globals = getattr(
  3547. sys.modules.get(forward_ref.__forward_module__, None), "__dict__", None
  3548. )
  3549. if globals is None:
  3550. globals = getattr(forward_ref, "__globals__", None)
  3551. if globals is None:
  3552. if isinstance(owner, type):
  3553. module_name = getattr(owner, "__module__", None)
  3554. if module_name:
  3555. module = sys.modules.get(module_name, None)
  3556. if module:
  3557. globals = getattr(module, "__dict__", None)
  3558. elif isinstance(owner, _types.ModuleType):
  3559. globals = getattr(owner, "__dict__", None)
  3560. elif callable(owner):
  3561. globals = getattr(owner, "__globals__", None)
  3562. # If we pass None to eval() below, the globals of this module are used.
  3563. if globals is None:
  3564. globals = {}
  3565. if locals is None:
  3566. locals = {}
  3567. if isinstance(owner, type):
  3568. locals.update(vars(owner))
  3569. if type_params is None and owner is not None:
  3570. # "Inject" type parameters into the local namespace
  3571. # (unless they are shadowed by assignments *in* the local namespace),
  3572. # as a way of emulating annotation scopes when calling `eval()`
  3573. type_params = getattr(owner, "__type_params__", None)
  3574. # type parameters require some special handling,
  3575. # as they exist in their own scope
  3576. # but `eval()` does not have a dedicated parameter for that scope.
  3577. # For classes, names in type parameter scopes should override
  3578. # names in the global scope (which here are called `localns`!),
  3579. # but should in turn be overridden by names in the class scope
  3580. # (which here are called `globalns`!)
  3581. if type_params is not None:
  3582. globals = dict(globals)
  3583. locals = dict(locals)
  3584. for param in type_params:
  3585. param_name = param.__name__
  3586. if (
  3587. _FORWARD_REF_HAS_CLASS and not forward_ref.__forward_is_class__
  3588. ) or param_name not in globals:
  3589. globals[param_name] = param
  3590. locals.pop(param_name, None)
  3591. arg = forward_ref.__forward_arg__
  3592. if arg.isidentifier() and not keyword.iskeyword(arg):
  3593. if arg in locals:
  3594. value = locals[arg]
  3595. elif arg in globals:
  3596. value = globals[arg]
  3597. elif hasattr(builtins, arg):
  3598. return getattr(builtins, arg)
  3599. else:
  3600. raise NameError(arg)
  3601. else:
  3602. code = forward_ref.__forward_code__
  3603. value = eval(code, globals, locals)
  3604. forward_ref.__forward_evaluated__ = True
  3605. forward_ref.__forward_value__ = value
  3606. return value
  3607. def _lax_type_check(
  3608. value, msg, is_argument=True, *, module=None, allow_special_forms=False
  3609. ):
  3610. """
  3611. A lax Python 3.11+ like version of typing._type_check
  3612. """
  3613. if hasattr(typing, "_type_convert"):
  3614. if (
  3615. sys.version_info >= (3, 10, 3)
  3616. or (3, 9, 10) < sys.version_info[:3] < (3, 10)
  3617. ):
  3618. # allow_special_forms introduced later cpython/#30926 (bpo-46539)
  3619. type_ = typing._type_convert(
  3620. value,
  3621. module=module,
  3622. allow_special_forms=allow_special_forms,
  3623. )
  3624. # module was added with bpo-41249 before is_class (bpo-46539)
  3625. elif "__forward_module__" in typing.ForwardRef.__slots__:
  3626. type_ = typing._type_convert(value, module=module)
  3627. else:
  3628. type_ = typing._type_convert(value)
  3629. else:
  3630. if value is None:
  3631. return type(None)
  3632. if isinstance(value, str):
  3633. return ForwardRef(value)
  3634. type_ = value
  3635. invalid_generic_forms = (Generic, Protocol)
  3636. if not allow_special_forms:
  3637. invalid_generic_forms += (ClassVar,)
  3638. if is_argument:
  3639. invalid_generic_forms += (Final,)
  3640. if (
  3641. isinstance(type_, typing._GenericAlias)
  3642. and get_origin(type_) in invalid_generic_forms
  3643. ):
  3644. raise TypeError(f"{type_} is not valid as type argument") from None
  3645. if type_ in (Any, LiteralString, NoReturn, Never, Self, TypeAlias):
  3646. return type_
  3647. if allow_special_forms and type_ in (ClassVar, Final):
  3648. return type_
  3649. if (
  3650. isinstance(type_, (_SpecialForm, typing._SpecialForm))
  3651. or type_ in (Generic, Protocol)
  3652. ):
  3653. raise TypeError(f"Plain {type_} is not valid as type argument") from None
  3654. if type(type_) is tuple: # lax version with tuple instead of callable
  3655. raise TypeError(f"{msg} Got {type_!r:.100}.")
  3656. return type_
  3657. def evaluate_forward_ref(
  3658. forward_ref,
  3659. *,
  3660. owner=None,
  3661. globals=None,
  3662. locals=None,
  3663. type_params=None,
  3664. format=Format.VALUE,
  3665. _recursive_guard=frozenset(),
  3666. ):
  3667. """Evaluate a forward reference as a type hint.
  3668. This is similar to calling the ForwardRef.evaluate() method,
  3669. but unlike that method, evaluate_forward_ref() also:
  3670. * Recursively evaluates forward references nested within the type hint.
  3671. * Rejects certain objects that are not valid type hints.
  3672. * Replaces type hints that evaluate to None with types.NoneType.
  3673. * Supports the *FORWARDREF* and *STRING* formats.
  3674. *forward_ref* must be an instance of ForwardRef. *owner*, if given,
  3675. should be the object that holds the annotations that the forward reference
  3676. derived from, such as a module, class object, or function. It is used to
  3677. infer the namespaces to use for looking up names. *globals* and *locals*
  3678. can also be explicitly given to provide the global and local namespaces.
  3679. *type_params* is a tuple of type parameters that are in scope when
  3680. evaluating the forward reference. This parameter must be provided (though
  3681. it may be an empty tuple) if *owner* is not given and the forward reference
  3682. does not already have an owner set. *format* specifies the format of the
  3683. annotation and is a member of the annotationlib.Format enum.
  3684. """
  3685. if format == Format.STRING:
  3686. return forward_ref.__forward_arg__
  3687. if forward_ref.__forward_arg__ in _recursive_guard:
  3688. return forward_ref
  3689. # Evaluate the forward reference
  3690. try:
  3691. value = _eval_with_owner(
  3692. forward_ref,
  3693. owner=owner,
  3694. globals=globals,
  3695. locals=locals,
  3696. type_params=type_params,
  3697. )
  3698. except NameError:
  3699. if format == Format.FORWARDREF:
  3700. return forward_ref
  3701. else:
  3702. raise
  3703. msg = "Forward references must evaluate to types."
  3704. if not _FORWARD_REF_HAS_CLASS:
  3705. allow_special_forms = not forward_ref.__forward_is_argument__
  3706. else:
  3707. allow_special_forms = forward_ref.__forward_is_class__
  3708. type_ = _lax_type_check(
  3709. value,
  3710. msg,
  3711. is_argument=forward_ref.__forward_is_argument__,
  3712. allow_special_forms=allow_special_forms,
  3713. )
  3714. # Recursively evaluate the type
  3715. if isinstance(type_, ForwardRef):
  3716. if getattr(type_, "__forward_module__", True) is not None:
  3717. globals = None
  3718. return evaluate_forward_ref(
  3719. type_,
  3720. globals=globals,
  3721. locals=locals,
  3722. type_params=type_params, owner=owner,
  3723. _recursive_guard=_recursive_guard, format=format
  3724. )
  3725. if sys.version_info < (3, 12, 5) and type_params:
  3726. # Make use of type_params
  3727. locals = dict(locals) if locals else {}
  3728. for tvar in type_params:
  3729. if tvar.__name__ not in locals: # lets not overwrite something present
  3730. locals[tvar.__name__] = tvar
  3731. if sys.version_info < (3, 9):
  3732. return typing._eval_type(
  3733. type_,
  3734. globals,
  3735. locals,
  3736. )
  3737. if sys.version_info < (3, 12, 5):
  3738. return typing._eval_type(
  3739. type_,
  3740. globals,
  3741. locals,
  3742. recursive_guard=_recursive_guard | {forward_ref.__forward_arg__},
  3743. )
  3744. if sys.version_info < (3, 14):
  3745. return typing._eval_type(
  3746. type_,
  3747. globals,
  3748. locals,
  3749. type_params,
  3750. recursive_guard=_recursive_guard | {forward_ref.__forward_arg__},
  3751. )
  3752. return typing._eval_type(
  3753. type_,
  3754. globals,
  3755. locals,
  3756. type_params,
  3757. recursive_guard=_recursive_guard | {forward_ref.__forward_arg__},
  3758. format=format,
  3759. owner=owner,
  3760. )
  3761. # Aliases for items that have always been in typing.
  3762. # Explicitly assign these (rather than using `from typing import *` at the top),
  3763. # so that we get a CI error if one of these is deleted from typing.py
  3764. # in a future version of Python
  3765. AbstractSet = typing.AbstractSet
  3766. AnyStr = typing.AnyStr
  3767. BinaryIO = typing.BinaryIO
  3768. Callable = typing.Callable
  3769. Collection = typing.Collection
  3770. Container = typing.Container
  3771. Dict = typing.Dict
  3772. ForwardRef = typing.ForwardRef
  3773. FrozenSet = typing.FrozenSet
  3774. Generic = typing.Generic
  3775. Hashable = typing.Hashable
  3776. IO = typing.IO
  3777. ItemsView = typing.ItemsView
  3778. Iterable = typing.Iterable
  3779. Iterator = typing.Iterator
  3780. KeysView = typing.KeysView
  3781. List = typing.List
  3782. Mapping = typing.Mapping
  3783. MappingView = typing.MappingView
  3784. Match = typing.Match
  3785. MutableMapping = typing.MutableMapping
  3786. MutableSequence = typing.MutableSequence
  3787. MutableSet = typing.MutableSet
  3788. Optional = typing.Optional
  3789. Pattern = typing.Pattern
  3790. Reversible = typing.Reversible
  3791. Sequence = typing.Sequence
  3792. Set = typing.Set
  3793. Sized = typing.Sized
  3794. TextIO = typing.TextIO
  3795. Tuple = typing.Tuple
  3796. Union = typing.Union
  3797. ValuesView = typing.ValuesView
  3798. cast = typing.cast
  3799. no_type_check = typing.no_type_check
  3800. no_type_check_decorator = typing.no_type_check_decorator