typing_extensions.py 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  1. import abc
  2. import collections
  3. import collections.abc
  4. import functools
  5. import inspect
  6. import operator
  7. import sys
  8. import types as _types
  9. import typing
  10. import warnings
  11. __all__ = [
  12. # Super-special typing primitives.
  13. 'Any',
  14. 'ClassVar',
  15. 'Concatenate',
  16. 'Final',
  17. 'LiteralString',
  18. 'ParamSpec',
  19. 'ParamSpecArgs',
  20. 'ParamSpecKwargs',
  21. 'Self',
  22. 'Type',
  23. 'TypeVar',
  24. 'TypeVarTuple',
  25. 'Unpack',
  26. # ABCs (from collections.abc).
  27. 'Awaitable',
  28. 'AsyncIterator',
  29. 'AsyncIterable',
  30. 'Coroutine',
  31. 'AsyncGenerator',
  32. 'AsyncContextManager',
  33. 'Buffer',
  34. 'ChainMap',
  35. # Concrete collection types.
  36. 'ContextManager',
  37. 'Counter',
  38. 'Deque',
  39. 'DefaultDict',
  40. 'NamedTuple',
  41. 'OrderedDict',
  42. 'TypedDict',
  43. # Structural checks, a.k.a. protocols.
  44. 'SupportsAbs',
  45. 'SupportsBytes',
  46. 'SupportsComplex',
  47. 'SupportsFloat',
  48. 'SupportsIndex',
  49. 'SupportsInt',
  50. 'SupportsRound',
  51. # One-off things.
  52. 'Annotated',
  53. 'assert_never',
  54. 'assert_type',
  55. 'clear_overloads',
  56. 'dataclass_transform',
  57. 'deprecated',
  58. 'Doc',
  59. 'get_overloads',
  60. 'final',
  61. 'get_args',
  62. 'get_origin',
  63. 'get_original_bases',
  64. 'get_protocol_members',
  65. 'get_type_hints',
  66. 'IntVar',
  67. 'is_protocol',
  68. 'is_typeddict',
  69. 'Literal',
  70. 'NewType',
  71. 'overload',
  72. 'override',
  73. 'Protocol',
  74. 'reveal_type',
  75. 'runtime',
  76. 'runtime_checkable',
  77. 'Text',
  78. 'TypeAlias',
  79. 'TypeAliasType',
  80. 'TypeGuard',
  81. 'TYPE_CHECKING',
  82. 'Never',
  83. 'NoReturn',
  84. 'ReadOnly',
  85. 'Required',
  86. 'NotRequired',
  87. # Pure aliases, have always been in typing
  88. 'AbstractSet',
  89. 'AnyStr',
  90. 'BinaryIO',
  91. 'Callable',
  92. 'Collection',
  93. 'Container',
  94. 'Dict',
  95. 'ForwardRef',
  96. 'FrozenSet',
  97. 'Generator',
  98. 'Generic',
  99. 'Hashable',
  100. 'IO',
  101. 'ItemsView',
  102. 'Iterable',
  103. 'Iterator',
  104. 'KeysView',
  105. 'List',
  106. 'Mapping',
  107. 'MappingView',
  108. 'Match',
  109. 'MutableMapping',
  110. 'MutableSequence',
  111. 'MutableSet',
  112. 'Optional',
  113. 'Pattern',
  114. 'Reversible',
  115. 'Sequence',
  116. 'Set',
  117. 'Sized',
  118. 'TextIO',
  119. 'Tuple',
  120. 'Union',
  121. 'ValuesView',
  122. 'cast',
  123. 'no_type_check',
  124. 'no_type_check_decorator',
  125. ]
  126. # for backward compatibility
  127. PEP_560 = True
  128. GenericMeta = type
  129. # The functions below are modified copies of typing internal helpers.
  130. # They are needed by _ProtocolMeta and they provide support for PEP 646.
  131. class _Sentinel:
  132. def __repr__(self):
  133. return "<sentinel>"
  134. _marker = _Sentinel()
  135. def _check_generic(cls, parameters, elen=_marker):
  136. """Check correct count for parameters of a generic cls (internal helper).
  137. This gives a nice error message in case of count mismatch.
  138. """
  139. if not elen:
  140. raise TypeError(f"{cls} is not a generic class")
  141. if elen is _marker:
  142. if not hasattr(cls, "__parameters__") or not cls.__parameters__:
  143. raise TypeError(f"{cls} is not a generic class")
  144. elen = len(cls.__parameters__)
  145. alen = len(parameters)
  146. if alen != elen:
  147. if hasattr(cls, "__parameters__"):
  148. parameters = [p for p in cls.__parameters__ if not _is_unpack(p)]
  149. num_tv_tuples = sum(isinstance(p, TypeVarTuple) for p in parameters)
  150. if (num_tv_tuples > 0) and (alen >= elen - num_tv_tuples):
  151. return
  152. raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
  153. f" actual {alen}, expected {elen}")
  154. if sys.version_info >= (3, 10):
  155. def _should_collect_from_parameters(t):
  156. return isinstance(
  157. t, (typing._GenericAlias, _types.GenericAlias, _types.UnionType)
  158. )
  159. elif sys.version_info >= (3, 9):
  160. def _should_collect_from_parameters(t):
  161. return isinstance(t, (typing._GenericAlias, _types.GenericAlias))
  162. else:
  163. def _should_collect_from_parameters(t):
  164. return isinstance(t, typing._GenericAlias) and not t._special
  165. def _collect_type_vars(types, typevar_types=None):
  166. """Collect all type variable contained in types in order of
  167. first appearance (lexicographic order). For example::
  168. _collect_type_vars((T, List[S, T])) == (T, S)
  169. """
  170. if typevar_types is None:
  171. typevar_types = typing.TypeVar
  172. tvars = []
  173. for t in types:
  174. if (
  175. isinstance(t, typevar_types) and
  176. t not in tvars and
  177. not _is_unpack(t)
  178. ):
  179. tvars.append(t)
  180. if _should_collect_from_parameters(t):
  181. tvars.extend([t for t in t.__parameters__ if t not in tvars])
  182. return tuple(tvars)
  183. NoReturn = typing.NoReturn
  184. # Some unconstrained type variables. These are used by the container types.
  185. # (These are not for export.)
  186. T = typing.TypeVar('T') # Any type.
  187. KT = typing.TypeVar('KT') # Key type.
  188. VT = typing.TypeVar('VT') # Value type.
  189. T_co = typing.TypeVar('T_co', covariant=True) # Any type covariant containers.
  190. T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.
  191. if sys.version_info >= (3, 11):
  192. from typing import Any
  193. else:
  194. class _AnyMeta(type):
  195. def __instancecheck__(self, obj):
  196. if self is Any:
  197. raise TypeError("typing_extensions.Any cannot be used with isinstance()")
  198. return super().__instancecheck__(obj)
  199. def __repr__(self):
  200. if self is Any:
  201. return "typing_extensions.Any"
  202. return super().__repr__()
  203. class Any(metaclass=_AnyMeta):
  204. """Special type indicating an unconstrained type.
  205. - Any is compatible with every type.
  206. - Any assumed to have all methods.
  207. - All values assumed to be instances of Any.
  208. Note that all the above statements are true from the point of view of
  209. static type checkers. At runtime, Any should not be used with instance
  210. checks.
  211. """
  212. def __new__(cls, *args, **kwargs):
  213. if cls is Any:
  214. raise TypeError("Any cannot be instantiated")
  215. return super().__new__(cls, *args, **kwargs)
  216. ClassVar = typing.ClassVar
  217. class _ExtensionsSpecialForm(typing._SpecialForm, _root=True):
  218. def __repr__(self):
  219. return 'typing_extensions.' + self._name
  220. Final = typing.Final
  221. if sys.version_info >= (3, 11):
  222. final = typing.final
  223. else:
  224. # @final exists in 3.8+, but we backport it for all versions
  225. # before 3.11 to keep support for the __final__ attribute.
  226. # See https://bugs.python.org/issue46342
  227. def final(f):
  228. """This decorator can be used to indicate to type checkers that
  229. the decorated method cannot be overridden, and decorated class
  230. cannot be subclassed. For example:
  231. class Base:
  232. @final
  233. def done(self) -> None:
  234. ...
  235. class Sub(Base):
  236. def done(self) -> None: # Error reported by type checker
  237. ...
  238. @final
  239. class Leaf:
  240. ...
  241. class Other(Leaf): # Error reported by type checker
  242. ...
  243. There is no runtime checking of these properties. The decorator
  244. sets the ``__final__`` attribute to ``True`` on the decorated object
  245. to allow runtime introspection.
  246. """
  247. try:
  248. f.__final__ = True
  249. except (AttributeError, TypeError):
  250. # Skip the attribute silently if it is not writable.
  251. # AttributeError happens if the object has __slots__ or a
  252. # read-only property, TypeError if it's a builtin class.
  253. pass
  254. return f
  255. def IntVar(name):
  256. return typing.TypeVar(name)
  257. # A Literal bug was fixed in 3.11.0, 3.10.1 and 3.9.8
  258. if sys.version_info >= (3, 10, 1):
  259. Literal = typing.Literal
  260. else:
  261. def _flatten_literal_params(parameters):
  262. """An internal helper for Literal creation: flatten Literals among parameters"""
  263. params = []
  264. for p in parameters:
  265. if isinstance(p, _LiteralGenericAlias):
  266. params.extend(p.__args__)
  267. else:
  268. params.append(p)
  269. return tuple(params)
  270. def _value_and_type_iter(params):
  271. for p in params:
  272. yield p, type(p)
  273. class _LiteralGenericAlias(typing._GenericAlias, _root=True):
  274. def __eq__(self, other):
  275. if not isinstance(other, _LiteralGenericAlias):
  276. return NotImplemented
  277. these_args_deduped = set(_value_and_type_iter(self.__args__))
  278. other_args_deduped = set(_value_and_type_iter(other.__args__))
  279. return these_args_deduped == other_args_deduped
  280. def __hash__(self):
  281. return hash(frozenset(_value_and_type_iter(self.__args__)))
  282. class _LiteralForm(_ExtensionsSpecialForm, _root=True):
  283. def __init__(self, doc: str):
  284. self._name = 'Literal'
  285. self._doc = self.__doc__ = doc
  286. def __getitem__(self, parameters):
  287. if not isinstance(parameters, tuple):
  288. parameters = (parameters,)
  289. parameters = _flatten_literal_params(parameters)
  290. val_type_pairs = list(_value_and_type_iter(parameters))
  291. try:
  292. deduped_pairs = set(val_type_pairs)
  293. except TypeError:
  294. # unhashable parameters
  295. pass
  296. else:
  297. # similar logic to typing._deduplicate on Python 3.9+
  298. if len(deduped_pairs) < len(val_type_pairs):
  299. new_parameters = []
  300. for pair in val_type_pairs:
  301. if pair in deduped_pairs:
  302. new_parameters.append(pair[0])
  303. deduped_pairs.remove(pair)
  304. assert not deduped_pairs, deduped_pairs
  305. parameters = tuple(new_parameters)
  306. return _LiteralGenericAlias(self, parameters)
  307. Literal = _LiteralForm(doc="""\
  308. A type that can be used to indicate to type checkers
  309. that the corresponding value has a value literally equivalent
  310. to the provided parameter. For example:
  311. var: Literal[4] = 4
  312. The type checker understands that 'var' is literally equal to
  313. the value 4 and no other value.
  314. Literal[...] cannot be subclassed. There is no runtime
  315. checking verifying that the parameter is actually a value
  316. instead of a type.""")
  317. _overload_dummy = typing._overload_dummy
  318. if hasattr(typing, "get_overloads"): # 3.11+
  319. overload = typing.overload
  320. get_overloads = typing.get_overloads
  321. clear_overloads = typing.clear_overloads
  322. else:
  323. # {module: {qualname: {firstlineno: func}}}
  324. _overload_registry = collections.defaultdict(
  325. functools.partial(collections.defaultdict, dict)
  326. )
  327. def overload(func):
  328. """Decorator for overloaded functions/methods.
  329. In a stub file, place two or more stub definitions for the same
  330. function in a row, each decorated with @overload. For example:
  331. @overload
  332. def utf8(value: None) -> None: ...
  333. @overload
  334. def utf8(value: bytes) -> bytes: ...
  335. @overload
  336. def utf8(value: str) -> bytes: ...
  337. In a non-stub file (i.e. a regular .py file), do the same but
  338. follow it with an implementation. The implementation should *not*
  339. be decorated with @overload. For example:
  340. @overload
  341. def utf8(value: None) -> None: ...
  342. @overload
  343. def utf8(value: bytes) -> bytes: ...
  344. @overload
  345. def utf8(value: str) -> bytes: ...
  346. def utf8(value):
  347. # implementation goes here
  348. The overloads for a function can be retrieved at runtime using the
  349. get_overloads() function.
  350. """
  351. # classmethod and staticmethod
  352. f = getattr(func, "__func__", func)
  353. try:
  354. _overload_registry[f.__module__][f.__qualname__][
  355. f.__code__.co_firstlineno
  356. ] = func
  357. except AttributeError:
  358. # Not a normal function; ignore.
  359. pass
  360. return _overload_dummy
  361. def get_overloads(func):
  362. """Return all defined overloads for *func* as a sequence."""
  363. # classmethod and staticmethod
  364. f = getattr(func, "__func__", func)
  365. if f.__module__ not in _overload_registry:
  366. return []
  367. mod_dict = _overload_registry[f.__module__]
  368. if f.__qualname__ not in mod_dict:
  369. return []
  370. return list(mod_dict[f.__qualname__].values())
  371. def clear_overloads():
  372. """Clear all overloads in the registry."""
  373. _overload_registry.clear()
  374. # This is not a real generic class. Don't use outside annotations.
  375. Type = typing.Type
  376. # Various ABCs mimicking those in collections.abc.
  377. # A few are simply re-exported for completeness.
  378. Awaitable = typing.Awaitable
  379. Coroutine = typing.Coroutine
  380. AsyncIterable = typing.AsyncIterable
  381. AsyncIterator = typing.AsyncIterator
  382. Deque = typing.Deque
  383. ContextManager = typing.ContextManager
  384. AsyncContextManager = typing.AsyncContextManager
  385. DefaultDict = typing.DefaultDict
  386. OrderedDict = typing.OrderedDict
  387. Counter = typing.Counter
  388. ChainMap = typing.ChainMap
  389. AsyncGenerator = typing.AsyncGenerator
  390. Text = typing.Text
  391. TYPE_CHECKING = typing.TYPE_CHECKING
  392. _PROTO_ALLOWLIST = {
  393. 'collections.abc': [
  394. 'Callable', 'Awaitable', 'Iterable', 'Iterator', 'AsyncIterable',
  395. 'Hashable', 'Sized', 'Container', 'Collection', 'Reversible', 'Buffer',
  396. ],
  397. 'contextlib': ['AbstractContextManager', 'AbstractAsyncContextManager'],
  398. 'typing_extensions': ['Buffer'],
  399. }
  400. _EXCLUDED_ATTRS = {
  401. "__abstractmethods__", "__annotations__", "__weakref__", "_is_protocol",
  402. "_is_runtime_protocol", "__dict__", "__slots__", "__parameters__",
  403. "__orig_bases__", "__module__", "_MutableMapping__marker", "__doc__",
  404. "__subclasshook__", "__orig_class__", "__init__", "__new__",
  405. "__protocol_attrs__", "__callable_proto_members_only__",
  406. "__match_args__",
  407. }
  408. if sys.version_info >= (3, 9):
  409. _EXCLUDED_ATTRS.add("__class_getitem__")
  410. if sys.version_info >= (3, 12):
  411. _EXCLUDED_ATTRS.add("__type_params__")
  412. _EXCLUDED_ATTRS = frozenset(_EXCLUDED_ATTRS)
  413. def _get_protocol_attrs(cls):
  414. attrs = set()
  415. for base in cls.__mro__[:-1]: # without object
  416. if base.__name__ in {'Protocol', 'Generic'}:
  417. continue
  418. annotations = getattr(base, '__annotations__', {})
  419. for attr in (*base.__dict__, *annotations):
  420. if (not attr.startswith('_abc_') and attr not in _EXCLUDED_ATTRS):
  421. attrs.add(attr)
  422. return attrs
  423. def _caller(depth=2):
  424. try:
  425. return sys._getframe(depth).f_globals.get('__name__', '__main__')
  426. except (AttributeError, ValueError): # For platforms without _getframe()
  427. return None
  428. # `__match_args__` attribute was removed from protocol members in 3.13,
  429. # we want to backport this change to older Python versions.
  430. if sys.version_info >= (3, 13):
  431. Protocol = typing.Protocol
  432. else:
  433. def _allow_reckless_class_checks(depth=3):
  434. """Allow instance and class checks for special stdlib modules.
  435. The abc and functools modules indiscriminately call isinstance() and
  436. issubclass() on the whole MRO of a user class, which may contain protocols.
  437. """
  438. return _caller(depth) in {'abc', 'functools', None}
  439. def _no_init(self, *args, **kwargs):
  440. if type(self)._is_protocol:
  441. raise TypeError('Protocols cannot be instantiated')
  442. # Inheriting from typing._ProtocolMeta isn't actually desirable,
  443. # but is necessary to allow typing.Protocol and typing_extensions.Protocol
  444. # to mix without getting TypeErrors about "metaclass conflict"
  445. class _ProtocolMeta(type(typing.Protocol)):
  446. # This metaclass is somewhat unfortunate,
  447. # but is necessary for several reasons...
  448. #
  449. # NOTE: DO NOT call super() in any methods in this class
  450. # That would call the methods on typing._ProtocolMeta on Python 3.8-3.11
  451. # and those are slow
  452. def __new__(mcls, name, bases, namespace, **kwargs):
  453. if name == "Protocol" and len(bases) < 2:
  454. pass
  455. elif {Protocol, typing.Protocol} & set(bases):
  456. for base in bases:
  457. if not (
  458. base in {object, typing.Generic, Protocol, typing.Protocol}
  459. or base.__name__ in _PROTO_ALLOWLIST.get(base.__module__, [])
  460. or is_protocol(base)
  461. ):
  462. raise TypeError(
  463. f"Protocols can only inherit from other protocols, "
  464. f"got {base!r}"
  465. )
  466. return abc.ABCMeta.__new__(mcls, name, bases, namespace, **kwargs)
  467. def __init__(cls, *args, **kwargs):
  468. abc.ABCMeta.__init__(cls, *args, **kwargs)
  469. if getattr(cls, "_is_protocol", False):
  470. cls.__protocol_attrs__ = _get_protocol_attrs(cls)
  471. # PEP 544 prohibits using issubclass()
  472. # with protocols that have non-method members.
  473. cls.__callable_proto_members_only__ = all(
  474. callable(getattr(cls, attr, None)) for attr in cls.__protocol_attrs__
  475. )
  476. def __subclasscheck__(cls, other):
  477. if cls is Protocol:
  478. return type.__subclasscheck__(cls, other)
  479. if (
  480. getattr(cls, '_is_protocol', False)
  481. and not _allow_reckless_class_checks()
  482. ):
  483. if not isinstance(other, type):
  484. # Same error message as for issubclass(1, int).
  485. raise TypeError('issubclass() arg 1 must be a class')
  486. if (
  487. not cls.__callable_proto_members_only__
  488. and cls.__dict__.get("__subclasshook__") is _proto_hook
  489. ):
  490. non_method_attrs = sorted(
  491. attr for attr in cls.__protocol_attrs__
  492. if not callable(getattr(cls, attr, None))
  493. )
  494. raise TypeError(
  495. "Protocols with non-method members don't support issubclass()."
  496. f" Non-method members: {str(non_method_attrs)[1:-1]}."
  497. )
  498. if not getattr(cls, '_is_runtime_protocol', False):
  499. raise TypeError(
  500. "Instance and class checks can only be used with "
  501. "@runtime_checkable protocols"
  502. )
  503. return abc.ABCMeta.__subclasscheck__(cls, other)
  504. def __instancecheck__(cls, instance):
  505. # We need this method for situations where attributes are
  506. # assigned in __init__.
  507. if cls is Protocol:
  508. return type.__instancecheck__(cls, instance)
  509. if not getattr(cls, "_is_protocol", False):
  510. # i.e., it's a concrete subclass of a protocol
  511. return abc.ABCMeta.__instancecheck__(cls, instance)
  512. if (
  513. not getattr(cls, '_is_runtime_protocol', False) and
  514. not _allow_reckless_class_checks()
  515. ):
  516. raise TypeError("Instance and class checks can only be used with"
  517. " @runtime_checkable protocols")
  518. if abc.ABCMeta.__instancecheck__(cls, instance):
  519. return True
  520. for attr in cls.__protocol_attrs__:
  521. try:
  522. val = inspect.getattr_static(instance, attr)
  523. except AttributeError:
  524. break
  525. if val is None and callable(getattr(cls, attr, None)):
  526. break
  527. else:
  528. return True
  529. return False
  530. def __eq__(cls, other):
  531. # Hack so that typing.Generic.__class_getitem__
  532. # treats typing_extensions.Protocol
  533. # as equivalent to typing.Protocol
  534. if abc.ABCMeta.__eq__(cls, other) is True:
  535. return True
  536. return cls is Protocol and other is typing.Protocol
  537. # This has to be defined, or the abc-module cache
  538. # complains about classes with this metaclass being unhashable,
  539. # if we define only __eq__!
  540. def __hash__(cls) -> int:
  541. return type.__hash__(cls)
  542. @classmethod
  543. def _proto_hook(cls, other):
  544. if not cls.__dict__.get('_is_protocol', False):
  545. return NotImplemented
  546. for attr in cls.__protocol_attrs__:
  547. for base in other.__mro__:
  548. # Check if the members appears in the class dictionary...
  549. if attr in base.__dict__:
  550. if base.__dict__[attr] is None:
  551. return NotImplemented
  552. break
  553. # ...or in annotations, if it is a sub-protocol.
  554. annotations = getattr(base, '__annotations__', {})
  555. if (
  556. isinstance(annotations, collections.abc.Mapping)
  557. and attr in annotations
  558. and is_protocol(other)
  559. ):
  560. break
  561. else:
  562. return NotImplemented
  563. return True
  564. class Protocol(typing.Generic, metaclass=_ProtocolMeta):
  565. __doc__ = typing.Protocol.__doc__
  566. __slots__ = ()
  567. _is_protocol = True
  568. _is_runtime_protocol = False
  569. def __init_subclass__(cls, *args, **kwargs):
  570. super().__init_subclass__(*args, **kwargs)
  571. # Determine if this is a protocol or a concrete subclass.
  572. if not cls.__dict__.get('_is_protocol', False):
  573. cls._is_protocol = any(b is Protocol for b in cls.__bases__)
  574. # Set (or override) the protocol subclass hook.
  575. if '__subclasshook__' not in cls.__dict__:
  576. cls.__subclasshook__ = _proto_hook
  577. # Prohibit instantiation for protocol classes
  578. if cls._is_protocol and cls.__init__ is Protocol.__init__:
  579. cls.__init__ = _no_init
  580. # The "runtime" alias exists for backwards compatibility.
  581. runtime = runtime_checkable = typing.runtime_checkable
  582. # Our version of runtime-checkable protocols is faster on Python 3.8-3.11
  583. if sys.version_info >= (3, 12):
  584. SupportsInt = typing.SupportsInt
  585. SupportsFloat = typing.SupportsFloat
  586. SupportsComplex = typing.SupportsComplex
  587. SupportsBytes = typing.SupportsBytes
  588. SupportsIndex = typing.SupportsIndex
  589. SupportsAbs = typing.SupportsAbs
  590. SupportsRound = typing.SupportsRound
  591. else:
  592. @runtime_checkable
  593. class SupportsInt(Protocol):
  594. """An ABC with one abstract method __int__."""
  595. __slots__ = ()
  596. @abc.abstractmethod
  597. def __int__(self) -> int:
  598. pass
  599. @runtime_checkable
  600. class SupportsFloat(Protocol):
  601. """An ABC with one abstract method __float__."""
  602. __slots__ = ()
  603. @abc.abstractmethod
  604. def __float__(self) -> float:
  605. pass
  606. @runtime_checkable
  607. class SupportsComplex(Protocol):
  608. """An ABC with one abstract method __complex__."""
  609. __slots__ = ()
  610. @abc.abstractmethod
  611. def __complex__(self) -> complex:
  612. pass
  613. @runtime_checkable
  614. class SupportsBytes(Protocol):
  615. """An ABC with one abstract method __bytes__."""
  616. __slots__ = ()
  617. @abc.abstractmethod
  618. def __bytes__(self) -> bytes:
  619. pass
  620. @runtime_checkable
  621. class SupportsIndex(Protocol):
  622. __slots__ = ()
  623. @abc.abstractmethod
  624. def __index__(self) -> int:
  625. pass
  626. @runtime_checkable
  627. class SupportsAbs(Protocol[T_co]):
  628. """
  629. An ABC with one abstract method __abs__ that is covariant in its return type.
  630. """
  631. __slots__ = ()
  632. @abc.abstractmethod
  633. def __abs__(self) -> T_co:
  634. pass
  635. @runtime_checkable
  636. class SupportsRound(Protocol[T_co]):
  637. """
  638. An ABC with one abstract method __round__ that is covariant in its return type.
  639. """
  640. __slots__ = ()
  641. @abc.abstractmethod
  642. def __round__(self, ndigits: int = 0) -> T_co:
  643. pass
  644. def _ensure_subclassable(mro_entries):
  645. def inner(func):
  646. if sys.implementation.name == "pypy" and sys.version_info < (3, 9):
  647. cls_dict = {
  648. "__call__": staticmethod(func),
  649. "__mro_entries__": staticmethod(mro_entries)
  650. }
  651. t = type(func.__name__, (), cls_dict)
  652. return functools.update_wrapper(t(), func)
  653. else:
  654. func.__mro_entries__ = mro_entries
  655. return func
  656. return inner
  657. if hasattr(typing, "ReadOnly"):
  658. # The standard library TypedDict in Python 3.8 does not store runtime information
  659. # about which (if any) keys are optional. See https://bugs.python.org/issue38834
  660. # The standard library TypedDict in Python 3.9.0/1 does not honour the "total"
  661. # keyword with old-style TypedDict(). See https://bugs.python.org/issue42059
  662. # The standard library TypedDict below Python 3.11 does not store runtime
  663. # information about optional and required keys when using Required or NotRequired.
  664. # Generic TypedDicts are also impossible using typing.TypedDict on Python <3.11.
  665. # Aaaand on 3.12 we add __orig_bases__ to TypedDict
  666. # to enable better runtime introspection.
  667. # On 3.13 we deprecate some odd ways of creating TypedDicts.
  668. # PEP 705 proposes adding the ReadOnly[] qualifier.
  669. TypedDict = typing.TypedDict
  670. _TypedDictMeta = typing._TypedDictMeta
  671. is_typeddict = typing.is_typeddict
  672. else:
  673. # 3.10.0 and later
  674. _TAKES_MODULE = "module" in inspect.signature(typing._type_check).parameters
  675. def _get_typeddict_qualifiers(annotation_type):
  676. while True:
  677. annotation_origin = get_origin(annotation_type)
  678. if annotation_origin is Annotated:
  679. annotation_args = get_args(annotation_type)
  680. if annotation_args:
  681. annotation_type = annotation_args[0]
  682. else:
  683. break
  684. elif annotation_origin is Required:
  685. yield Required
  686. annotation_type, = get_args(annotation_type)
  687. elif annotation_origin is NotRequired:
  688. yield NotRequired
  689. annotation_type, = get_args(annotation_type)
  690. elif annotation_origin is ReadOnly:
  691. yield ReadOnly
  692. annotation_type, = get_args(annotation_type)
  693. else:
  694. break
  695. class _TypedDictMeta(type):
  696. def __new__(cls, name, bases, ns, *, total=True):
  697. """Create new typed dict class object.
  698. This method is called when TypedDict is subclassed,
  699. or when TypedDict is instantiated. This way
  700. TypedDict supports all three syntax forms described in its docstring.
  701. Subclasses and instances of TypedDict return actual dictionaries.
  702. """
  703. for base in bases:
  704. if type(base) is not _TypedDictMeta and base is not typing.Generic:
  705. raise TypeError('cannot inherit from both a TypedDict type '
  706. 'and a non-TypedDict base class')
  707. if any(issubclass(b, typing.Generic) for b in bases):
  708. generic_base = (typing.Generic,)
  709. else:
  710. generic_base = ()
  711. # typing.py generally doesn't let you inherit from plain Generic, unless
  712. # the name of the class happens to be "Protocol"
  713. tp_dict = type.__new__(_TypedDictMeta, "Protocol", (*generic_base, dict), ns)
  714. tp_dict.__name__ = name
  715. if tp_dict.__qualname__ == "Protocol":
  716. tp_dict.__qualname__ = name
  717. if not hasattr(tp_dict, '__orig_bases__'):
  718. tp_dict.__orig_bases__ = bases
  719. annotations = {}
  720. own_annotations = ns.get('__annotations__', {})
  721. msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
  722. if _TAKES_MODULE:
  723. own_annotations = {
  724. n: typing._type_check(tp, msg, module=tp_dict.__module__)
  725. for n, tp in own_annotations.items()
  726. }
  727. else:
  728. own_annotations = {
  729. n: typing._type_check(tp, msg)
  730. for n, tp in own_annotations.items()
  731. }
  732. required_keys = set()
  733. optional_keys = set()
  734. readonly_keys = set()
  735. mutable_keys = set()
  736. for base in bases:
  737. base_dict = base.__dict__
  738. annotations.update(base_dict.get('__annotations__', {}))
  739. required_keys.update(base_dict.get('__required_keys__', ()))
  740. optional_keys.update(base_dict.get('__optional_keys__', ()))
  741. readonly_keys.update(base_dict.get('__readonly_keys__', ()))
  742. mutable_keys.update(base_dict.get('__mutable_keys__', ()))
  743. annotations.update(own_annotations)
  744. for annotation_key, annotation_type in own_annotations.items():
  745. qualifiers = set(_get_typeddict_qualifiers(annotation_type))
  746. if Required in qualifiers:
  747. required_keys.add(annotation_key)
  748. elif NotRequired in qualifiers:
  749. optional_keys.add(annotation_key)
  750. elif total:
  751. required_keys.add(annotation_key)
  752. else:
  753. optional_keys.add(annotation_key)
  754. if ReadOnly in qualifiers:
  755. if annotation_key in mutable_keys:
  756. raise TypeError(
  757. f"Cannot override mutable key {annotation_key!r}"
  758. " with read-only key"
  759. )
  760. readonly_keys.add(annotation_key)
  761. else:
  762. mutable_keys.add(annotation_key)
  763. readonly_keys.discard(annotation_key)
  764. tp_dict.__annotations__ = annotations
  765. tp_dict.__required_keys__ = frozenset(required_keys)
  766. tp_dict.__optional_keys__ = frozenset(optional_keys)
  767. tp_dict.__readonly_keys__ = frozenset(readonly_keys)
  768. tp_dict.__mutable_keys__ = frozenset(mutable_keys)
  769. if not hasattr(tp_dict, '__total__'):
  770. tp_dict.__total__ = total
  771. return tp_dict
  772. __call__ = dict # static method
  773. def __subclasscheck__(cls, other):
  774. # Typed dicts are only for static structural subtyping.
  775. raise TypeError('TypedDict does not support instance and class checks')
  776. __instancecheck__ = __subclasscheck__
  777. _TypedDict = type.__new__(_TypedDictMeta, 'TypedDict', (), {})
  778. @_ensure_subclassable(lambda bases: (_TypedDict,))
  779. def TypedDict(typename, fields=_marker, /, *, total=True, **kwargs):
  780. """A simple typed namespace. At runtime it is equivalent to a plain dict.
  781. TypedDict creates a dictionary type such that a type checker will expect all
  782. instances to have a certain set of keys, where each key is
  783. associated with a value of a consistent type. This expectation
  784. is not checked at runtime.
  785. Usage::
  786. class Point2D(TypedDict):
  787. x: int
  788. y: int
  789. label: str
  790. a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK
  791. b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check
  792. assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first')
  793. The type info can be accessed via the Point2D.__annotations__ dict, and
  794. the Point2D.__required_keys__ and Point2D.__optional_keys__ frozensets.
  795. TypedDict supports an additional equivalent form::
  796. Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str})
  797. By default, all keys must be present in a TypedDict. It is possible
  798. to override this by specifying totality::
  799. class Point2D(TypedDict, total=False):
  800. x: int
  801. y: int
  802. This means that a Point2D TypedDict can have any of the keys omitted. A type
  803. checker is only expected to support a literal False or True as the value of
  804. the total argument. True is the default, and makes all items defined in the
  805. class body be required.
  806. The Required and NotRequired special forms can also be used to mark
  807. individual keys as being required or not required::
  808. class Point2D(TypedDict):
  809. x: int # the "x" key must always be present (Required is the default)
  810. y: NotRequired[int] # the "y" key can be omitted
  811. See PEP 655 for more details on Required and NotRequired.
  812. """
  813. if fields is _marker or fields is None:
  814. if fields is _marker:
  815. deprecated_thing = "Failing to pass a value for the 'fields' parameter"
  816. else:
  817. deprecated_thing = "Passing `None` as the 'fields' parameter"
  818. example = f"`{typename} = TypedDict({typename!r}, {{}})`"
  819. deprecation_msg = (
  820. f"{deprecated_thing} is deprecated and will be disallowed in "
  821. "Python 3.15. To create a TypedDict class with 0 fields "
  822. "using the functional syntax, pass an empty dictionary, e.g. "
  823. ) + example + "."
  824. warnings.warn(deprecation_msg, DeprecationWarning, stacklevel=2)
  825. fields = kwargs
  826. elif kwargs:
  827. raise TypeError("TypedDict takes either a dict or keyword arguments,"
  828. " but not both")
  829. if kwargs:
  830. if sys.version_info >= (3, 13):
  831. raise TypeError("TypedDict takes no keyword arguments")
  832. warnings.warn(
  833. "The kwargs-based syntax for TypedDict definitions is deprecated "
  834. "in Python 3.11, will be removed in Python 3.13, and may not be "
  835. "understood by third-party type checkers.",
  836. DeprecationWarning,
  837. stacklevel=2,
  838. )
  839. ns = {'__annotations__': dict(fields)}
  840. module = _caller()
  841. if module is not None:
  842. # Setting correct module is necessary to make typed dict classes pickleable.
  843. ns['__module__'] = module
  844. td = _TypedDictMeta(typename, (), ns, total=total)
  845. td.__orig_bases__ = (TypedDict,)
  846. return td
  847. if hasattr(typing, "_TypedDictMeta"):
  848. _TYPEDDICT_TYPES = (typing._TypedDictMeta, _TypedDictMeta)
  849. else:
  850. _TYPEDDICT_TYPES = (_TypedDictMeta,)
  851. def is_typeddict(tp):
  852. """Check if an annotation is a TypedDict class
  853. For example::
  854. class Film(TypedDict):
  855. title: str
  856. year: int
  857. is_typeddict(Film) # => True
  858. is_typeddict(Union[list, str]) # => False
  859. """
  860. # On 3.8, this would otherwise return True
  861. if hasattr(typing, "TypedDict") and tp is typing.TypedDict:
  862. return False
  863. return isinstance(tp, _TYPEDDICT_TYPES)
  864. if hasattr(typing, "assert_type"):
  865. assert_type = typing.assert_type
  866. else:
  867. def assert_type(val, typ, /):
  868. """Assert (to the type checker) that the value is of the given type.
  869. When the type checker encounters a call to assert_type(), it
  870. emits an error if the value is not of the specified type::
  871. def greet(name: str) -> None:
  872. assert_type(name, str) # ok
  873. assert_type(name, int) # type checker error
  874. At runtime this returns the first argument unchanged and otherwise
  875. does nothing.
  876. """
  877. return val
  878. if hasattr(typing, "Required"): # 3.11+
  879. get_type_hints = typing.get_type_hints
  880. else: # <=3.10
  881. # replaces _strip_annotations()
  882. def _strip_extras(t):
  883. """Strips Annotated, Required and NotRequired from a given type."""
  884. if isinstance(t, _AnnotatedAlias):
  885. return _strip_extras(t.__origin__)
  886. if hasattr(t, "__origin__") and t.__origin__ in (Required, NotRequired):
  887. return _strip_extras(t.__args__[0])
  888. if isinstance(t, typing._GenericAlias):
  889. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  890. if stripped_args == t.__args__:
  891. return t
  892. return t.copy_with(stripped_args)
  893. if hasattr(_types, "GenericAlias") and isinstance(t, _types.GenericAlias):
  894. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  895. if stripped_args == t.__args__:
  896. return t
  897. return _types.GenericAlias(t.__origin__, stripped_args)
  898. if hasattr(_types, "UnionType") and isinstance(t, _types.UnionType):
  899. stripped_args = tuple(_strip_extras(a) for a in t.__args__)
  900. if stripped_args == t.__args__:
  901. return t
  902. return functools.reduce(operator.or_, stripped_args)
  903. return t
  904. def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
  905. """Return type hints for an object.
  906. This is often the same as obj.__annotations__, but it handles
  907. forward references encoded as string literals, adds Optional[t] if a
  908. default value equal to None is set and recursively replaces all
  909. 'Annotated[T, ...]', 'Required[T]' or 'NotRequired[T]' with 'T'
  910. (unless 'include_extras=True').
  911. The argument may be a module, class, method, or function. The annotations
  912. are returned as a dictionary. For classes, annotations include also
  913. inherited members.
  914. TypeError is raised if the argument is not of a type that can contain
  915. annotations, and an empty dictionary is returned if no annotations are
  916. present.
  917. BEWARE -- the behavior of globalns and localns is counterintuitive
  918. (unless you are familiar with how eval() and exec() work). The
  919. search order is locals first, then globals.
  920. - If no dict arguments are passed, an attempt is made to use the
  921. globals from obj (or the respective module's globals for classes),
  922. and these are also used as the locals. If the object does not appear
  923. to have globals, an empty dictionary is used.
  924. - If one dict argument is passed, it is used for both globals and
  925. locals.
  926. - If two dict arguments are passed, they specify globals and
  927. locals, respectively.
  928. """
  929. if hasattr(typing, "Annotated"): # 3.9+
  930. hint = typing.get_type_hints(
  931. obj, globalns=globalns, localns=localns, include_extras=True
  932. )
  933. else: # 3.8
  934. hint = typing.get_type_hints(obj, globalns=globalns, localns=localns)
  935. if include_extras:
  936. return hint
  937. return {k: _strip_extras(t) for k, t in hint.items()}
  938. # Python 3.9+ has PEP 593 (Annotated)
  939. if hasattr(typing, 'Annotated'):
  940. Annotated = typing.Annotated
  941. # Not exported and not a public API, but needed for get_origin() and get_args()
  942. # to work.
  943. _AnnotatedAlias = typing._AnnotatedAlias
  944. # 3.8
  945. else:
  946. class _AnnotatedAlias(typing._GenericAlias, _root=True):
  947. """Runtime representation of an annotated type.
  948. At its core 'Annotated[t, dec1, dec2, ...]' is an alias for the type 't'
  949. with extra annotations. The alias behaves like a normal typing alias,
  950. instantiating is the same as instantiating the underlying type, binding
  951. it to types is also the same.
  952. """
  953. def __init__(self, origin, metadata):
  954. if isinstance(origin, _AnnotatedAlias):
  955. metadata = origin.__metadata__ + metadata
  956. origin = origin.__origin__
  957. super().__init__(origin, origin)
  958. self.__metadata__ = metadata
  959. def copy_with(self, params):
  960. assert len(params) == 1
  961. new_type = params[0]
  962. return _AnnotatedAlias(new_type, self.__metadata__)
  963. def __repr__(self):
  964. return (f"typing_extensions.Annotated[{typing._type_repr(self.__origin__)}, "
  965. f"{', '.join(repr(a) for a in self.__metadata__)}]")
  966. def __reduce__(self):
  967. return operator.getitem, (
  968. Annotated, (self.__origin__,) + self.__metadata__
  969. )
  970. def __eq__(self, other):
  971. if not isinstance(other, _AnnotatedAlias):
  972. return NotImplemented
  973. if self.__origin__ != other.__origin__:
  974. return False
  975. return self.__metadata__ == other.__metadata__
  976. def __hash__(self):
  977. return hash((self.__origin__, self.__metadata__))
  978. class Annotated:
  979. """Add context specific metadata to a type.
  980. Example: Annotated[int, runtime_check.Unsigned] indicates to the
  981. hypothetical runtime_check module that this type is an unsigned int.
  982. Every other consumer of this type can ignore this metadata and treat
  983. this type as int.
  984. The first argument to Annotated must be a valid type (and will be in
  985. the __origin__ field), the remaining arguments are kept as a tuple in
  986. the __extra__ field.
  987. Details:
  988. - It's an error to call `Annotated` with less than two arguments.
  989. - Nested Annotated are flattened::
  990. Annotated[Annotated[T, Ann1, Ann2], Ann3] == Annotated[T, Ann1, Ann2, Ann3]
  991. - Instantiating an annotated type is equivalent to instantiating the
  992. underlying type::
  993. Annotated[C, Ann1](5) == C(5)
  994. - Annotated can be used as a generic type alias::
  995. Optimized = Annotated[T, runtime.Optimize()]
  996. Optimized[int] == Annotated[int, runtime.Optimize()]
  997. OptimizedList = Annotated[List[T], runtime.Optimize()]
  998. OptimizedList[int] == Annotated[List[int], runtime.Optimize()]
  999. """
  1000. __slots__ = ()
  1001. def __new__(cls, *args, **kwargs):
  1002. raise TypeError("Type Annotated cannot be instantiated.")
  1003. @typing._tp_cache
  1004. def __class_getitem__(cls, params):
  1005. if not isinstance(params, tuple) or len(params) < 2:
  1006. raise TypeError("Annotated[...] should be used "
  1007. "with at least two arguments (a type and an "
  1008. "annotation).")
  1009. allowed_special_forms = (ClassVar, Final)
  1010. if get_origin(params[0]) in allowed_special_forms:
  1011. origin = params[0]
  1012. else:
  1013. msg = "Annotated[t, ...]: t must be a type."
  1014. origin = typing._type_check(params[0], msg)
  1015. metadata = tuple(params[1:])
  1016. return _AnnotatedAlias(origin, metadata)
  1017. def __init_subclass__(cls, *args, **kwargs):
  1018. raise TypeError(
  1019. f"Cannot subclass {cls.__module__}.Annotated"
  1020. )
  1021. # Python 3.8 has get_origin() and get_args() but those implementations aren't
  1022. # Annotated-aware, so we can't use those. Python 3.9's versions don't support
  1023. # ParamSpecArgs and ParamSpecKwargs, so only Python 3.10's versions will do.
  1024. if sys.version_info[:2] >= (3, 10):
  1025. get_origin = typing.get_origin
  1026. get_args = typing.get_args
  1027. # 3.8-3.9
  1028. else:
  1029. try:
  1030. # 3.9+
  1031. from typing import _BaseGenericAlias
  1032. except ImportError:
  1033. _BaseGenericAlias = typing._GenericAlias
  1034. try:
  1035. # 3.9+
  1036. from typing import GenericAlias as _typing_GenericAlias
  1037. except ImportError:
  1038. _typing_GenericAlias = typing._GenericAlias
  1039. def get_origin(tp):
  1040. """Get the unsubscripted version of a type.
  1041. This supports generic types, Callable, Tuple, Union, Literal, Final, ClassVar
  1042. and Annotated. Return None for unsupported types. Examples::
  1043. get_origin(Literal[42]) is Literal
  1044. get_origin(int) is None
  1045. get_origin(ClassVar[int]) is ClassVar
  1046. get_origin(Generic) is Generic
  1047. get_origin(Generic[T]) is Generic
  1048. get_origin(Union[T, int]) is Union
  1049. get_origin(List[Tuple[T, T]][int]) == list
  1050. get_origin(P.args) is P
  1051. """
  1052. if isinstance(tp, _AnnotatedAlias):
  1053. return Annotated
  1054. if isinstance(tp, (typing._GenericAlias, _typing_GenericAlias, _BaseGenericAlias,
  1055. ParamSpecArgs, ParamSpecKwargs)):
  1056. return tp.__origin__
  1057. if tp is typing.Generic:
  1058. return typing.Generic
  1059. return None
  1060. def get_args(tp):
  1061. """Get type arguments with all substitutions performed.
  1062. For unions, basic simplifications used by Union constructor are performed.
  1063. Examples::
  1064. get_args(Dict[str, int]) == (str, int)
  1065. get_args(int) == ()
  1066. get_args(Union[int, Union[T, int], str][int]) == (int, str)
  1067. get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
  1068. get_args(Callable[[], T][int]) == ([], int)
  1069. """
  1070. if isinstance(tp, _AnnotatedAlias):
  1071. return (tp.__origin__,) + tp.__metadata__
  1072. if isinstance(tp, (typing._GenericAlias, _typing_GenericAlias)):
  1073. if getattr(tp, "_special", False):
  1074. return ()
  1075. res = tp.__args__
  1076. if get_origin(tp) is collections.abc.Callable and res[0] is not Ellipsis:
  1077. res = (list(res[:-1]), res[-1])
  1078. return res
  1079. return ()
  1080. # 3.10+
  1081. if hasattr(typing, 'TypeAlias'):
  1082. TypeAlias = typing.TypeAlias
  1083. # 3.9
  1084. elif sys.version_info[:2] >= (3, 9):
  1085. @_ExtensionsSpecialForm
  1086. def TypeAlias(self, parameters):
  1087. """Special marker indicating that an assignment should
  1088. be recognized as a proper type alias definition by type
  1089. checkers.
  1090. For example::
  1091. Predicate: TypeAlias = Callable[..., bool]
  1092. It's invalid when used anywhere except as in the example above.
  1093. """
  1094. raise TypeError(f"{self} is not subscriptable")
  1095. # 3.8
  1096. else:
  1097. TypeAlias = _ExtensionsSpecialForm(
  1098. 'TypeAlias',
  1099. doc="""Special marker indicating that an assignment should
  1100. be recognized as a proper type alias definition by type
  1101. checkers.
  1102. For example::
  1103. Predicate: TypeAlias = Callable[..., bool]
  1104. It's invalid when used anywhere except as in the example
  1105. above."""
  1106. )
  1107. def _set_default(type_param, default):
  1108. if isinstance(default, (tuple, list)):
  1109. type_param.__default__ = tuple((typing._type_check(d, "Default must be a type")
  1110. for d in default))
  1111. elif default != _marker:
  1112. if isinstance(type_param, ParamSpec) and default is ...: # ... not valid <3.11
  1113. type_param.__default__ = default
  1114. else:
  1115. type_param.__default__ = typing._type_check(default, "Default must be a type")
  1116. else:
  1117. type_param.__default__ = None
  1118. def _set_module(typevarlike):
  1119. # for pickling:
  1120. def_mod = _caller(depth=3)
  1121. if def_mod != 'typing_extensions':
  1122. typevarlike.__module__ = def_mod
  1123. class _DefaultMixin:
  1124. """Mixin for TypeVarLike defaults."""
  1125. __slots__ = ()
  1126. __init__ = _set_default
  1127. # Classes using this metaclass must provide a _backported_typevarlike ClassVar
  1128. class _TypeVarLikeMeta(type):
  1129. def __instancecheck__(cls, __instance: Any) -> bool:
  1130. return isinstance(__instance, cls._backported_typevarlike)
  1131. # Add default and infer_variance parameters from PEP 696 and 695
  1132. class TypeVar(metaclass=_TypeVarLikeMeta):
  1133. """Type variable."""
  1134. _backported_typevarlike = typing.TypeVar
  1135. def __new__(cls, name, *constraints, bound=None,
  1136. covariant=False, contravariant=False,
  1137. default=_marker, infer_variance=False):
  1138. if hasattr(typing, "TypeAliasType"):
  1139. # PEP 695 implemented (3.12+), can pass infer_variance to typing.TypeVar
  1140. typevar = typing.TypeVar(name, *constraints, bound=bound,
  1141. covariant=covariant, contravariant=contravariant,
  1142. infer_variance=infer_variance)
  1143. else:
  1144. typevar = typing.TypeVar(name, *constraints, bound=bound,
  1145. covariant=covariant, contravariant=contravariant)
  1146. if infer_variance and (covariant or contravariant):
  1147. raise ValueError("Variance cannot be specified with infer_variance.")
  1148. typevar.__infer_variance__ = infer_variance
  1149. _set_default(typevar, default)
  1150. _set_module(typevar)
  1151. return typevar
  1152. def __init_subclass__(cls) -> None:
  1153. raise TypeError(f"type '{__name__}.TypeVar' is not an acceptable base type")
  1154. # Python 3.10+ has PEP 612
  1155. if hasattr(typing, 'ParamSpecArgs'):
  1156. ParamSpecArgs = typing.ParamSpecArgs
  1157. ParamSpecKwargs = typing.ParamSpecKwargs
  1158. # 3.8-3.9
  1159. else:
  1160. class _Immutable:
  1161. """Mixin to indicate that object should not be copied."""
  1162. __slots__ = ()
  1163. def __copy__(self):
  1164. return self
  1165. def __deepcopy__(self, memo):
  1166. return self
  1167. class ParamSpecArgs(_Immutable):
  1168. """The args for a ParamSpec object.
  1169. Given a ParamSpec object P, P.args is an instance of ParamSpecArgs.
  1170. ParamSpecArgs objects have a reference back to their ParamSpec:
  1171. P.args.__origin__ is P
  1172. This type is meant for runtime introspection and has no special meaning to
  1173. static type checkers.
  1174. """
  1175. def __init__(self, origin):
  1176. self.__origin__ = origin
  1177. def __repr__(self):
  1178. return f"{self.__origin__.__name__}.args"
  1179. def __eq__(self, other):
  1180. if not isinstance(other, ParamSpecArgs):
  1181. return NotImplemented
  1182. return self.__origin__ == other.__origin__
  1183. class ParamSpecKwargs(_Immutable):
  1184. """The kwargs for a ParamSpec object.
  1185. Given a ParamSpec object P, P.kwargs is an instance of ParamSpecKwargs.
  1186. ParamSpecKwargs objects have a reference back to their ParamSpec:
  1187. P.kwargs.__origin__ is P
  1188. This type is meant for runtime introspection and has no special meaning to
  1189. static type checkers.
  1190. """
  1191. def __init__(self, origin):
  1192. self.__origin__ = origin
  1193. def __repr__(self):
  1194. return f"{self.__origin__.__name__}.kwargs"
  1195. def __eq__(self, other):
  1196. if not isinstance(other, ParamSpecKwargs):
  1197. return NotImplemented
  1198. return self.__origin__ == other.__origin__
  1199. # 3.10+
  1200. if hasattr(typing, 'ParamSpec'):
  1201. # Add default parameter - PEP 696
  1202. class ParamSpec(metaclass=_TypeVarLikeMeta):
  1203. """Parameter specification."""
  1204. _backported_typevarlike = typing.ParamSpec
  1205. def __new__(cls, name, *, bound=None,
  1206. covariant=False, contravariant=False,
  1207. infer_variance=False, default=_marker):
  1208. if hasattr(typing, "TypeAliasType"):
  1209. # PEP 695 implemented, can pass infer_variance to typing.TypeVar
  1210. paramspec = typing.ParamSpec(name, bound=bound,
  1211. covariant=covariant,
  1212. contravariant=contravariant,
  1213. infer_variance=infer_variance)
  1214. else:
  1215. paramspec = typing.ParamSpec(name, bound=bound,
  1216. covariant=covariant,
  1217. contravariant=contravariant)
  1218. paramspec.__infer_variance__ = infer_variance
  1219. _set_default(paramspec, default)
  1220. _set_module(paramspec)
  1221. return paramspec
  1222. def __init_subclass__(cls) -> None:
  1223. raise TypeError(f"type '{__name__}.ParamSpec' is not an acceptable base type")
  1224. # 3.8-3.9
  1225. else:
  1226. # Inherits from list as a workaround for Callable checks in Python < 3.9.2.
  1227. class ParamSpec(list, _DefaultMixin):
  1228. """Parameter specification variable.
  1229. Usage::
  1230. P = ParamSpec('P')
  1231. Parameter specification variables exist primarily for the benefit of static
  1232. type checkers. They are used to forward the parameter types of one
  1233. callable to another callable, a pattern commonly found in higher order
  1234. functions and decorators. They are only valid when used in ``Concatenate``,
  1235. or s the first argument to ``Callable``. In Python 3.10 and higher,
  1236. they are also supported in user-defined Generics at runtime.
  1237. See class Generic for more information on generic types. An
  1238. example for annotating a decorator::
  1239. T = TypeVar('T')
  1240. P = ParamSpec('P')
  1241. def add_logging(f: Callable[P, T]) -> Callable[P, T]:
  1242. '''A type-safe decorator to add logging to a function.'''
  1243. def inner(*args: P.args, **kwargs: P.kwargs) -> T:
  1244. logging.info(f'{f.__name__} was called')
  1245. return f(*args, **kwargs)
  1246. return inner
  1247. @add_logging
  1248. def add_two(x: float, y: float) -> float:
  1249. '''Add two numbers together.'''
  1250. return x + y
  1251. Parameter specification variables defined with covariant=True or
  1252. contravariant=True can be used to declare covariant or contravariant
  1253. generic types. These keyword arguments are valid, but their actual semantics
  1254. are yet to be decided. See PEP 612 for details.
  1255. Parameter specification variables can be introspected. e.g.:
  1256. P.__name__ == 'T'
  1257. P.__bound__ == None
  1258. P.__covariant__ == False
  1259. P.__contravariant__ == False
  1260. Note that only parameter specification variables defined in global scope can
  1261. be pickled.
  1262. """
  1263. # Trick Generic __parameters__.
  1264. __class__ = typing.TypeVar
  1265. @property
  1266. def args(self):
  1267. return ParamSpecArgs(self)
  1268. @property
  1269. def kwargs(self):
  1270. return ParamSpecKwargs(self)
  1271. def __init__(self, name, *, bound=None, covariant=False, contravariant=False,
  1272. infer_variance=False, default=_marker):
  1273. super().__init__([self])
  1274. self.__name__ = name
  1275. self.__covariant__ = bool(covariant)
  1276. self.__contravariant__ = bool(contravariant)
  1277. self.__infer_variance__ = bool(infer_variance)
  1278. if bound:
  1279. self.__bound__ = typing._type_check(bound, 'Bound must be a type.')
  1280. else:
  1281. self.__bound__ = None
  1282. _DefaultMixin.__init__(self, default)
  1283. # for pickling:
  1284. def_mod = _caller()
  1285. if def_mod != 'typing_extensions':
  1286. self.__module__ = def_mod
  1287. def __repr__(self):
  1288. if self.__infer_variance__:
  1289. prefix = ''
  1290. elif self.__covariant__:
  1291. prefix = '+'
  1292. elif self.__contravariant__:
  1293. prefix = '-'
  1294. else:
  1295. prefix = '~'
  1296. return prefix + self.__name__
  1297. def __hash__(self):
  1298. return object.__hash__(self)
  1299. def __eq__(self, other):
  1300. return self is other
  1301. def __reduce__(self):
  1302. return self.__name__
  1303. # Hack to get typing._type_check to pass.
  1304. def __call__(self, *args, **kwargs):
  1305. pass
  1306. # 3.8-3.9
  1307. if not hasattr(typing, 'Concatenate'):
  1308. # Inherits from list as a workaround for Callable checks in Python < 3.9.2.
  1309. class _ConcatenateGenericAlias(list):
  1310. # Trick Generic into looking into this for __parameters__.
  1311. __class__ = typing._GenericAlias
  1312. # Flag in 3.8.
  1313. _special = False
  1314. def __init__(self, origin, args):
  1315. super().__init__(args)
  1316. self.__origin__ = origin
  1317. self.__args__ = args
  1318. def __repr__(self):
  1319. _type_repr = typing._type_repr
  1320. return (f'{_type_repr(self.__origin__)}'
  1321. f'[{", ".join(_type_repr(arg) for arg in self.__args__)}]')
  1322. def __hash__(self):
  1323. return hash((self.__origin__, self.__args__))
  1324. # Hack to get typing._type_check to pass in Generic.
  1325. def __call__(self, *args, **kwargs):
  1326. pass
  1327. @property
  1328. def __parameters__(self):
  1329. return tuple(
  1330. tp for tp in self.__args__ if isinstance(tp, (typing.TypeVar, ParamSpec))
  1331. )
  1332. # 3.8-3.9
  1333. @typing._tp_cache
  1334. def _concatenate_getitem(self, parameters):
  1335. if parameters == ():
  1336. raise TypeError("Cannot take a Concatenate of no types.")
  1337. if not isinstance(parameters, tuple):
  1338. parameters = (parameters,)
  1339. if not isinstance(parameters[-1], ParamSpec):
  1340. raise TypeError("The last parameter to Concatenate should be a "
  1341. "ParamSpec variable.")
  1342. msg = "Concatenate[arg, ...]: each arg must be a type."
  1343. parameters = tuple(typing._type_check(p, msg) for p in parameters)
  1344. return _ConcatenateGenericAlias(self, parameters)
  1345. # 3.10+
  1346. if hasattr(typing, 'Concatenate'):
  1347. Concatenate = typing.Concatenate
  1348. _ConcatenateGenericAlias = typing._ConcatenateGenericAlias # noqa: F811
  1349. # 3.9
  1350. elif sys.version_info[:2] >= (3, 9):
  1351. @_ExtensionsSpecialForm
  1352. def Concatenate(self, parameters):
  1353. """Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
  1354. higher order function which adds, removes or transforms parameters of a
  1355. callable.
  1356. For example::
  1357. Callable[Concatenate[int, P], int]
  1358. See PEP 612 for detailed information.
  1359. """
  1360. return _concatenate_getitem(self, parameters)
  1361. # 3.8
  1362. else:
  1363. class _ConcatenateForm(_ExtensionsSpecialForm, _root=True):
  1364. def __getitem__(self, parameters):
  1365. return _concatenate_getitem(self, parameters)
  1366. Concatenate = _ConcatenateForm(
  1367. 'Concatenate',
  1368. doc="""Used in conjunction with ``ParamSpec`` and ``Callable`` to represent a
  1369. higher order function which adds, removes or transforms parameters of a
  1370. callable.
  1371. For example::
  1372. Callable[Concatenate[int, P], int]
  1373. See PEP 612 for detailed information.
  1374. """)
  1375. # 3.10+
  1376. if hasattr(typing, 'TypeGuard'):
  1377. TypeGuard = typing.TypeGuard
  1378. # 3.9
  1379. elif sys.version_info[:2] >= (3, 9):
  1380. @_ExtensionsSpecialForm
  1381. def TypeGuard(self, parameters):
  1382. """Special typing form used to annotate the return type of a user-defined
  1383. type guard function. ``TypeGuard`` only accepts a single type argument.
  1384. At runtime, functions marked this way should return a boolean.
  1385. ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
  1386. type checkers to determine a more precise type of an expression within a
  1387. program's code flow. Usually type narrowing is done by analyzing
  1388. conditional code flow and applying the narrowing to a block of code. The
  1389. conditional expression here is sometimes referred to as a "type guard".
  1390. Sometimes it would be convenient to use a user-defined boolean function
  1391. as a type guard. Such a function should use ``TypeGuard[...]`` as its
  1392. return type to alert static type checkers to this intention.
  1393. Using ``-> TypeGuard`` tells the static type checker that for a given
  1394. function:
  1395. 1. The return value is a boolean.
  1396. 2. If the return value is ``True``, the type of its argument
  1397. is the type inside ``TypeGuard``.
  1398. For example::
  1399. def is_str(val: Union[str, float]):
  1400. # "isinstance" type guard
  1401. if isinstance(val, str):
  1402. # Type of ``val`` is narrowed to ``str``
  1403. ...
  1404. else:
  1405. # Else, type of ``val`` is narrowed to ``float``.
  1406. ...
  1407. Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
  1408. form of ``TypeA`` (it can even be a wider form) and this may lead to
  1409. type-unsafe results. The main reason is to allow for things like
  1410. narrowing ``List[object]`` to ``List[str]`` even though the latter is not
  1411. a subtype of the former, since ``List`` is invariant. The responsibility of
  1412. writing type-safe type guards is left to the user.
  1413. ``TypeGuard`` also works with type variables. For more information, see
  1414. PEP 647 (User-Defined Type Guards).
  1415. """
  1416. item = typing._type_check(parameters, f'{self} accepts only a single type.')
  1417. return typing._GenericAlias(self, (item,))
  1418. # 3.8
  1419. else:
  1420. class _TypeGuardForm(_ExtensionsSpecialForm, _root=True):
  1421. def __getitem__(self, parameters):
  1422. item = typing._type_check(parameters,
  1423. f'{self._name} accepts only a single type')
  1424. return typing._GenericAlias(self, (item,))
  1425. TypeGuard = _TypeGuardForm(
  1426. 'TypeGuard',
  1427. doc="""Special typing form used to annotate the return type of a user-defined
  1428. type guard function. ``TypeGuard`` only accepts a single type argument.
  1429. At runtime, functions marked this way should return a boolean.
  1430. ``TypeGuard`` aims to benefit *type narrowing* -- a technique used by static
  1431. type checkers to determine a more precise type of an expression within a
  1432. program's code flow. Usually type narrowing is done by analyzing
  1433. conditional code flow and applying the narrowing to a block of code. The
  1434. conditional expression here is sometimes referred to as a "type guard".
  1435. Sometimes it would be convenient to use a user-defined boolean function
  1436. as a type guard. Such a function should use ``TypeGuard[...]`` as its
  1437. return type to alert static type checkers to this intention.
  1438. Using ``-> TypeGuard`` tells the static type checker that for a given
  1439. function:
  1440. 1. The return value is a boolean.
  1441. 2. If the return value is ``True``, the type of its argument
  1442. is the type inside ``TypeGuard``.
  1443. For example::
  1444. def is_str(val: Union[str, float]):
  1445. # "isinstance" type guard
  1446. if isinstance(val, str):
  1447. # Type of ``val`` is narrowed to ``str``
  1448. ...
  1449. else:
  1450. # Else, type of ``val`` is narrowed to ``float``.
  1451. ...
  1452. Strict type narrowing is not enforced -- ``TypeB`` need not be a narrower
  1453. form of ``TypeA`` (it can even be a wider form) and this may lead to
  1454. type-unsafe results. The main reason is to allow for things like
  1455. narrowing ``List[object]`` to ``List[str]`` even though the latter is not
  1456. a subtype of the former, since ``List`` is invariant. The responsibility of
  1457. writing type-safe type guards is left to the user.
  1458. ``TypeGuard`` also works with type variables. For more information, see
  1459. PEP 647 (User-Defined Type Guards).
  1460. """)
  1461. # Vendored from cpython typing._SpecialFrom
  1462. class _SpecialForm(typing._Final, _root=True):
  1463. __slots__ = ('_name', '__doc__', '_getitem')
  1464. def __init__(self, getitem):
  1465. self._getitem = getitem
  1466. self._name = getitem.__name__
  1467. self.__doc__ = getitem.__doc__
  1468. def __getattr__(self, item):
  1469. if item in {'__name__', '__qualname__'}:
  1470. return self._name
  1471. raise AttributeError(item)
  1472. def __mro_entries__(self, bases):
  1473. raise TypeError(f"Cannot subclass {self!r}")
  1474. def __repr__(self):
  1475. return f'typing_extensions.{self._name}'
  1476. def __reduce__(self):
  1477. return self._name
  1478. def __call__(self, *args, **kwds):
  1479. raise TypeError(f"Cannot instantiate {self!r}")
  1480. def __or__(self, other):
  1481. return typing.Union[self, other]
  1482. def __ror__(self, other):
  1483. return typing.Union[other, self]
  1484. def __instancecheck__(self, obj):
  1485. raise TypeError(f"{self} cannot be used with isinstance()")
  1486. def __subclasscheck__(self, cls):
  1487. raise TypeError(f"{self} cannot be used with issubclass()")
  1488. @typing._tp_cache
  1489. def __getitem__(self, parameters):
  1490. return self._getitem(self, parameters)
  1491. if hasattr(typing, "LiteralString"): # 3.11+
  1492. LiteralString = typing.LiteralString
  1493. else:
  1494. @_SpecialForm
  1495. def LiteralString(self, params):
  1496. """Represents an arbitrary literal string.
  1497. Example::
  1498. from typing_extensions import LiteralString
  1499. def query(sql: LiteralString) -> ...:
  1500. ...
  1501. query("SELECT * FROM table") # ok
  1502. query(f"SELECT * FROM {input()}") # not ok
  1503. See PEP 675 for details.
  1504. """
  1505. raise TypeError(f"{self} is not subscriptable")
  1506. if hasattr(typing, "Self"): # 3.11+
  1507. Self = typing.Self
  1508. else:
  1509. @_SpecialForm
  1510. def Self(self, params):
  1511. """Used to spell the type of "self" in classes.
  1512. Example::
  1513. from typing import Self
  1514. class ReturnsSelf:
  1515. def parse(self, data: bytes) -> Self:
  1516. ...
  1517. return self
  1518. """
  1519. raise TypeError(f"{self} is not subscriptable")
  1520. if hasattr(typing, "Never"): # 3.11+
  1521. Never = typing.Never
  1522. else:
  1523. @_SpecialForm
  1524. def Never(self, params):
  1525. """The bottom type, a type that has no members.
  1526. This can be used to define a function that should never be
  1527. called, or a function that never returns::
  1528. from typing_extensions import Never
  1529. def never_call_me(arg: Never) -> None:
  1530. pass
  1531. def int_or_str(arg: int | str) -> None:
  1532. never_call_me(arg) # type checker error
  1533. match arg:
  1534. case int():
  1535. print("It's an int")
  1536. case str():
  1537. print("It's a str")
  1538. case _:
  1539. never_call_me(arg) # ok, arg is of type Never
  1540. """
  1541. raise TypeError(f"{self} is not subscriptable")
  1542. if hasattr(typing, 'Required'): # 3.11+
  1543. Required = typing.Required
  1544. NotRequired = typing.NotRequired
  1545. elif sys.version_info[:2] >= (3, 9): # 3.9-3.10
  1546. @_ExtensionsSpecialForm
  1547. def Required(self, parameters):
  1548. """A special typing construct to mark a key of a total=False TypedDict
  1549. as required. For example:
  1550. class Movie(TypedDict, total=False):
  1551. title: Required[str]
  1552. year: int
  1553. m = Movie(
  1554. title='The Matrix', # typechecker error if key is omitted
  1555. year=1999,
  1556. )
  1557. There is no runtime checking that a required key is actually provided
  1558. when instantiating a related TypedDict.
  1559. """
  1560. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  1561. return typing._GenericAlias(self, (item,))
  1562. @_ExtensionsSpecialForm
  1563. def NotRequired(self, parameters):
  1564. """A special typing construct to mark a key of a TypedDict as
  1565. potentially missing. For example:
  1566. class Movie(TypedDict):
  1567. title: str
  1568. year: NotRequired[int]
  1569. m = Movie(
  1570. title='The Matrix', # typechecker error if key is omitted
  1571. year=1999,
  1572. )
  1573. """
  1574. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  1575. return typing._GenericAlias(self, (item,))
  1576. else: # 3.8
  1577. class _RequiredForm(_ExtensionsSpecialForm, _root=True):
  1578. def __getitem__(self, parameters):
  1579. item = typing._type_check(parameters,
  1580. f'{self._name} accepts only a single type.')
  1581. return typing._GenericAlias(self, (item,))
  1582. Required = _RequiredForm(
  1583. 'Required',
  1584. doc="""A special typing construct to mark a key of a total=False TypedDict
  1585. as required. For example:
  1586. class Movie(TypedDict, total=False):
  1587. title: Required[str]
  1588. year: int
  1589. m = Movie(
  1590. title='The Matrix', # typechecker error if key is omitted
  1591. year=1999,
  1592. )
  1593. There is no runtime checking that a required key is actually provided
  1594. when instantiating a related TypedDict.
  1595. """)
  1596. NotRequired = _RequiredForm(
  1597. 'NotRequired',
  1598. doc="""A special typing construct to mark a key of a TypedDict as
  1599. potentially missing. For example:
  1600. class Movie(TypedDict):
  1601. title: str
  1602. year: NotRequired[int]
  1603. m = Movie(
  1604. title='The Matrix', # typechecker error if key is omitted
  1605. year=1999,
  1606. )
  1607. """)
  1608. if hasattr(typing, 'ReadOnly'):
  1609. ReadOnly = typing.ReadOnly
  1610. elif sys.version_info[:2] >= (3, 9): # 3.9-3.12
  1611. @_ExtensionsSpecialForm
  1612. def ReadOnly(self, parameters):
  1613. """A special typing construct to mark an item of a TypedDict as read-only.
  1614. For example:
  1615. class Movie(TypedDict):
  1616. title: ReadOnly[str]
  1617. year: int
  1618. def mutate_movie(m: Movie) -> None:
  1619. m["year"] = 1992 # allowed
  1620. m["title"] = "The Matrix" # typechecker error
  1621. There is no runtime checking for this property.
  1622. """
  1623. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  1624. return typing._GenericAlias(self, (item,))
  1625. else: # 3.8
  1626. class _ReadOnlyForm(_ExtensionsSpecialForm, _root=True):
  1627. def __getitem__(self, parameters):
  1628. item = typing._type_check(parameters,
  1629. f'{self._name} accepts only a single type.')
  1630. return typing._GenericAlias(self, (item,))
  1631. ReadOnly = _ReadOnlyForm(
  1632. 'ReadOnly',
  1633. doc="""A special typing construct to mark a key of a TypedDict as read-only.
  1634. For example:
  1635. class Movie(TypedDict):
  1636. title: ReadOnly[str]
  1637. year: int
  1638. def mutate_movie(m: Movie) -> None:
  1639. m["year"] = 1992 # allowed
  1640. m["title"] = "The Matrix" # typechecker error
  1641. There is no runtime checking for this propery.
  1642. """)
  1643. _UNPACK_DOC = """\
  1644. Type unpack operator.
  1645. The type unpack operator takes the child types from some container type,
  1646. such as `tuple[int, str]` or a `TypeVarTuple`, and 'pulls them out'. For
  1647. example:
  1648. # For some generic class `Foo`:
  1649. Foo[Unpack[tuple[int, str]]] # Equivalent to Foo[int, str]
  1650. Ts = TypeVarTuple('Ts')
  1651. # Specifies that `Bar` is generic in an arbitrary number of types.
  1652. # (Think of `Ts` as a tuple of an arbitrary number of individual
  1653. # `TypeVar`s, which the `Unpack` is 'pulling out' directly into the
  1654. # `Generic[]`.)
  1655. class Bar(Generic[Unpack[Ts]]): ...
  1656. Bar[int] # Valid
  1657. Bar[int, str] # Also valid
  1658. From Python 3.11, this can also be done using the `*` operator:
  1659. Foo[*tuple[int, str]]
  1660. class Bar(Generic[*Ts]): ...
  1661. The operator can also be used along with a `TypedDict` to annotate
  1662. `**kwargs` in a function signature. For instance:
  1663. class Movie(TypedDict):
  1664. name: str
  1665. year: int
  1666. # This function expects two keyword arguments - *name* of type `str` and
  1667. # *year* of type `int`.
  1668. def foo(**kwargs: Unpack[Movie]): ...
  1669. Note that there is only some runtime checking of this operator. Not
  1670. everything the runtime allows may be accepted by static type checkers.
  1671. For more information, see PEP 646 and PEP 692.
  1672. """
  1673. if sys.version_info >= (3, 12): # PEP 692 changed the repr of Unpack[]
  1674. Unpack = typing.Unpack
  1675. def _is_unpack(obj):
  1676. return get_origin(obj) is Unpack
  1677. elif sys.version_info[:2] >= (3, 9): # 3.9+
  1678. class _UnpackSpecialForm(_ExtensionsSpecialForm, _root=True):
  1679. def __init__(self, getitem):
  1680. super().__init__(getitem)
  1681. self.__doc__ = _UNPACK_DOC
  1682. class _UnpackAlias(typing._GenericAlias, _root=True):
  1683. __class__ = typing.TypeVar
  1684. @_UnpackSpecialForm
  1685. def Unpack(self, parameters):
  1686. item = typing._type_check(parameters, f'{self._name} accepts only a single type.')
  1687. return _UnpackAlias(self, (item,))
  1688. def _is_unpack(obj):
  1689. return isinstance(obj, _UnpackAlias)
  1690. else: # 3.8
  1691. class _UnpackAlias(typing._GenericAlias, _root=True):
  1692. __class__ = typing.TypeVar
  1693. class _UnpackForm(_ExtensionsSpecialForm, _root=True):
  1694. def __getitem__(self, parameters):
  1695. item = typing._type_check(parameters,
  1696. f'{self._name} accepts only a single type.')
  1697. return _UnpackAlias(self, (item,))
  1698. Unpack = _UnpackForm('Unpack', doc=_UNPACK_DOC)
  1699. def _is_unpack(obj):
  1700. return isinstance(obj, _UnpackAlias)
  1701. if hasattr(typing, "TypeVarTuple"): # 3.11+
  1702. # Add default parameter - PEP 696
  1703. class TypeVarTuple(metaclass=_TypeVarLikeMeta):
  1704. """Type variable tuple."""
  1705. _backported_typevarlike = typing.TypeVarTuple
  1706. def __new__(cls, name, *, default=_marker):
  1707. tvt = typing.TypeVarTuple(name)
  1708. _set_default(tvt, default)
  1709. _set_module(tvt)
  1710. return tvt
  1711. def __init_subclass__(self, *args, **kwds):
  1712. raise TypeError("Cannot subclass special typing classes")
  1713. else: # <=3.10
  1714. class TypeVarTuple(_DefaultMixin):
  1715. """Type variable tuple.
  1716. Usage::
  1717. Ts = TypeVarTuple('Ts')
  1718. In the same way that a normal type variable is a stand-in for a single
  1719. type such as ``int``, a type variable *tuple* is a stand-in for a *tuple*
  1720. type such as ``Tuple[int, str]``.
  1721. Type variable tuples can be used in ``Generic`` declarations.
  1722. Consider the following example::
  1723. class Array(Generic[*Ts]): ...
  1724. The ``Ts`` type variable tuple here behaves like ``tuple[T1, T2]``,
  1725. where ``T1`` and ``T2`` are type variables. To use these type variables
  1726. as type parameters of ``Array``, we must *unpack* the type variable tuple using
  1727. the star operator: ``*Ts``. The signature of ``Array`` then behaves
  1728. as if we had simply written ``class Array(Generic[T1, T2]): ...``.
  1729. In contrast to ``Generic[T1, T2]``, however, ``Generic[*Shape]`` allows
  1730. us to parameterise the class with an *arbitrary* number of type parameters.
  1731. Type variable tuples can be used anywhere a normal ``TypeVar`` can.
  1732. This includes class definitions, as shown above, as well as function
  1733. signatures and variable annotations::
  1734. class Array(Generic[*Ts]):
  1735. def __init__(self, shape: Tuple[*Ts]):
  1736. self._shape: Tuple[*Ts] = shape
  1737. def get_shape(self) -> Tuple[*Ts]:
  1738. return self._shape
  1739. shape = (Height(480), Width(640))
  1740. x: Array[Height, Width] = Array(shape)
  1741. y = abs(x) # Inferred type is Array[Height, Width]
  1742. z = x + x # ... is Array[Height, Width]
  1743. x.get_shape() # ... is tuple[Height, Width]
  1744. """
  1745. # Trick Generic __parameters__.
  1746. __class__ = typing.TypeVar
  1747. def __iter__(self):
  1748. yield self.__unpacked__
  1749. def __init__(self, name, *, default=_marker):
  1750. self.__name__ = name
  1751. _DefaultMixin.__init__(self, default)
  1752. # for pickling:
  1753. def_mod = _caller()
  1754. if def_mod != 'typing_extensions':
  1755. self.__module__ = def_mod
  1756. self.__unpacked__ = Unpack[self]
  1757. def __repr__(self):
  1758. return self.__name__
  1759. def __hash__(self):
  1760. return object.__hash__(self)
  1761. def __eq__(self, other):
  1762. return self is other
  1763. def __reduce__(self):
  1764. return self.__name__
  1765. def __init_subclass__(self, *args, **kwds):
  1766. if '_root' not in kwds:
  1767. raise TypeError("Cannot subclass special typing classes")
  1768. if hasattr(typing, "reveal_type"): # 3.11+
  1769. reveal_type = typing.reveal_type
  1770. else: # <=3.10
  1771. def reveal_type(obj: T, /) -> T:
  1772. """Reveal the inferred type of a variable.
  1773. When a static type checker encounters a call to ``reveal_type()``,
  1774. it will emit the inferred type of the argument::
  1775. x: int = 1
  1776. reveal_type(x)
  1777. Running a static type checker (e.g., ``mypy``) on this example
  1778. will produce output similar to 'Revealed type is "builtins.int"'.
  1779. At runtime, the function prints the runtime type of the
  1780. argument and returns it unchanged.
  1781. """
  1782. print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr)
  1783. return obj
  1784. if hasattr(typing, "assert_never"): # 3.11+
  1785. assert_never = typing.assert_never
  1786. else: # <=3.10
  1787. def assert_never(arg: Never, /) -> Never:
  1788. """Assert to the type checker that a line of code is unreachable.
  1789. Example::
  1790. def int_or_str(arg: int | str) -> None:
  1791. match arg:
  1792. case int():
  1793. print("It's an int")
  1794. case str():
  1795. print("It's a str")
  1796. case _:
  1797. assert_never(arg)
  1798. If a type checker finds that a call to assert_never() is
  1799. reachable, it will emit an error.
  1800. At runtime, this throws an exception when called.
  1801. """
  1802. raise AssertionError("Expected code to be unreachable")
  1803. if sys.version_info >= (3, 12): # 3.12+
  1804. # dataclass_transform exists in 3.11 but lacks the frozen_default parameter
  1805. dataclass_transform = typing.dataclass_transform
  1806. else: # <=3.11
  1807. def dataclass_transform(
  1808. *,
  1809. eq_default: bool = True,
  1810. order_default: bool = False,
  1811. kw_only_default: bool = False,
  1812. frozen_default: bool = False,
  1813. field_specifiers: typing.Tuple[
  1814. typing.Union[typing.Type[typing.Any], typing.Callable[..., typing.Any]],
  1815. ...
  1816. ] = (),
  1817. **kwargs: typing.Any,
  1818. ) -> typing.Callable[[T], T]:
  1819. """Decorator that marks a function, class, or metaclass as providing
  1820. dataclass-like behavior.
  1821. Example:
  1822. from typing_extensions import dataclass_transform
  1823. _T = TypeVar("_T")
  1824. # Used on a decorator function
  1825. @dataclass_transform()
  1826. def create_model(cls: type[_T]) -> type[_T]:
  1827. ...
  1828. return cls
  1829. @create_model
  1830. class CustomerModel:
  1831. id: int
  1832. name: str
  1833. # Used on a base class
  1834. @dataclass_transform()
  1835. class ModelBase: ...
  1836. class CustomerModel(ModelBase):
  1837. id: int
  1838. name: str
  1839. # Used on a metaclass
  1840. @dataclass_transform()
  1841. class ModelMeta(type): ...
  1842. class ModelBase(metaclass=ModelMeta): ...
  1843. class CustomerModel(ModelBase):
  1844. id: int
  1845. name: str
  1846. Each of the ``CustomerModel`` classes defined in this example will now
  1847. behave similarly to a dataclass created with the ``@dataclasses.dataclass``
  1848. decorator. For example, the type checker will synthesize an ``__init__``
  1849. method.
  1850. The arguments to this decorator can be used to customize this behavior:
  1851. - ``eq_default`` indicates whether the ``eq`` parameter is assumed to be
  1852. True or False if it is omitted by the caller.
  1853. - ``order_default`` indicates whether the ``order`` parameter is
  1854. assumed to be True or False if it is omitted by the caller.
  1855. - ``kw_only_default`` indicates whether the ``kw_only`` parameter is
  1856. assumed to be True or False if it is omitted by the caller.
  1857. - ``frozen_default`` indicates whether the ``frozen`` parameter is
  1858. assumed to be True or False if it is omitted by the caller.
  1859. - ``field_specifiers`` specifies a static list of supported classes
  1860. or functions that describe fields, similar to ``dataclasses.field()``.
  1861. At runtime, this decorator records its arguments in the
  1862. ``__dataclass_transform__`` attribute on the decorated object.
  1863. See PEP 681 for details.
  1864. """
  1865. def decorator(cls_or_fn):
  1866. cls_or_fn.__dataclass_transform__ = {
  1867. "eq_default": eq_default,
  1868. "order_default": order_default,
  1869. "kw_only_default": kw_only_default,
  1870. "frozen_default": frozen_default,
  1871. "field_specifiers": field_specifiers,
  1872. "kwargs": kwargs,
  1873. }
  1874. return cls_or_fn
  1875. return decorator
  1876. if hasattr(typing, "override"): # 3.12+
  1877. override = typing.override
  1878. else: # <=3.11
  1879. _F = typing.TypeVar("_F", bound=typing.Callable[..., typing.Any])
  1880. def override(arg: _F, /) -> _F:
  1881. """Indicate that a method is intended to override a method in a base class.
  1882. Usage:
  1883. class Base:
  1884. def method(self) -> None:
  1885. pass
  1886. class Child(Base):
  1887. @override
  1888. def method(self) -> None:
  1889. super().method()
  1890. When this decorator is applied to a method, the type checker will
  1891. validate that it overrides a method with the same name on a base class.
  1892. This helps prevent bugs that may occur when a base class is changed
  1893. without an equivalent change to a child class.
  1894. There is no runtime checking of these properties. The decorator
  1895. sets the ``__override__`` attribute to ``True`` on the decorated object
  1896. to allow runtime introspection.
  1897. See PEP 698 for details.
  1898. """
  1899. try:
  1900. arg.__override__ = True
  1901. except (AttributeError, TypeError):
  1902. # Skip the attribute silently if it is not writable.
  1903. # AttributeError happens if the object has __slots__ or a
  1904. # read-only property, TypeError if it's a builtin class.
  1905. pass
  1906. return arg
  1907. if hasattr(warnings, "deprecated"):
  1908. deprecated = warnings.deprecated
  1909. else:
  1910. _T = typing.TypeVar("_T")
  1911. class deprecated:
  1912. """Indicate that a class, function or overload is deprecated.
  1913. When this decorator is applied to an object, the type checker
  1914. will generate a diagnostic on usage of the deprecated object.
  1915. Usage:
  1916. @deprecated("Use B instead")
  1917. class A:
  1918. pass
  1919. @deprecated("Use g instead")
  1920. def f():
  1921. pass
  1922. @overload
  1923. @deprecated("int support is deprecated")
  1924. def g(x: int) -> int: ...
  1925. @overload
  1926. def g(x: str) -> int: ...
  1927. The warning specified by *category* will be emitted at runtime
  1928. on use of deprecated objects. For functions, that happens on calls;
  1929. for classes, on instantiation and on creation of subclasses.
  1930. If the *category* is ``None``, no warning is emitted at runtime.
  1931. The *stacklevel* determines where the
  1932. warning is emitted. If it is ``1`` (the default), the warning
  1933. is emitted at the direct caller of the deprecated object; if it
  1934. is higher, it is emitted further up the stack.
  1935. Static type checker behavior is not affected by the *category*
  1936. and *stacklevel* arguments.
  1937. The deprecation message passed to the decorator is saved in the
  1938. ``__deprecated__`` attribute on the decorated object.
  1939. If applied to an overload, the decorator
  1940. must be after the ``@overload`` decorator for the attribute to
  1941. exist on the overload as returned by ``get_overloads()``.
  1942. See PEP 702 for details.
  1943. """
  1944. def __init__(
  1945. self,
  1946. message: str,
  1947. /,
  1948. *,
  1949. category: typing.Optional[typing.Type[Warning]] = DeprecationWarning,
  1950. stacklevel: int = 1,
  1951. ) -> None:
  1952. if not isinstance(message, str):
  1953. raise TypeError(
  1954. "Expected an object of type str for 'message', not "
  1955. f"{type(message).__name__!r}"
  1956. )
  1957. self.message = message
  1958. self.category = category
  1959. self.stacklevel = stacklevel
  1960. def __call__(self, arg: _T, /) -> _T:
  1961. # Make sure the inner functions created below don't
  1962. # retain a reference to self.
  1963. msg = self.message
  1964. category = self.category
  1965. stacklevel = self.stacklevel
  1966. if category is None:
  1967. arg.__deprecated__ = msg
  1968. return arg
  1969. elif isinstance(arg, type):
  1970. import functools
  1971. from types import MethodType
  1972. original_new = arg.__new__
  1973. @functools.wraps(original_new)
  1974. def __new__(cls, *args, **kwargs):
  1975. if cls is arg:
  1976. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  1977. if original_new is not object.__new__:
  1978. return original_new(cls, *args, **kwargs)
  1979. # Mirrors a similar check in object.__new__.
  1980. elif cls.__init__ is object.__init__ and (args or kwargs):
  1981. raise TypeError(f"{cls.__name__}() takes no arguments")
  1982. else:
  1983. return original_new(cls)
  1984. arg.__new__ = staticmethod(__new__)
  1985. original_init_subclass = arg.__init_subclass__
  1986. # We need slightly different behavior if __init_subclass__
  1987. # is a bound method (likely if it was implemented in Python)
  1988. if isinstance(original_init_subclass, MethodType):
  1989. original_init_subclass = original_init_subclass.__func__
  1990. @functools.wraps(original_init_subclass)
  1991. def __init_subclass__(*args, **kwargs):
  1992. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  1993. return original_init_subclass(*args, **kwargs)
  1994. arg.__init_subclass__ = classmethod(__init_subclass__)
  1995. # Or otherwise, which likely means it's a builtin such as
  1996. # object's implementation of __init_subclass__.
  1997. else:
  1998. @functools.wraps(original_init_subclass)
  1999. def __init_subclass__(*args, **kwargs):
  2000. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2001. return original_init_subclass(*args, **kwargs)
  2002. arg.__init_subclass__ = __init_subclass__
  2003. arg.__deprecated__ = __new__.__deprecated__ = msg
  2004. __init_subclass__.__deprecated__ = msg
  2005. return arg
  2006. elif callable(arg):
  2007. import functools
  2008. @functools.wraps(arg)
  2009. def wrapper(*args, **kwargs):
  2010. warnings.warn(msg, category=category, stacklevel=stacklevel + 1)
  2011. return arg(*args, **kwargs)
  2012. arg.__deprecated__ = wrapper.__deprecated__ = msg
  2013. return wrapper
  2014. else:
  2015. raise TypeError(
  2016. "@deprecated decorator with non-None category must be applied to "
  2017. f"a class or callable, not {arg!r}"
  2018. )
  2019. # We have to do some monkey patching to deal with the dual nature of
  2020. # Unpack/TypeVarTuple:
  2021. # - We want Unpack to be a kind of TypeVar so it gets accepted in
  2022. # Generic[Unpack[Ts]]
  2023. # - We want it to *not* be treated as a TypeVar for the purposes of
  2024. # counting generic parameters, so that when we subscript a generic,
  2025. # the runtime doesn't try to substitute the Unpack with the subscripted type.
  2026. if not hasattr(typing, "TypeVarTuple"):
  2027. typing._collect_type_vars = _collect_type_vars
  2028. typing._check_generic = _check_generic
  2029. # Backport typing.NamedTuple as it exists in Python 3.13.
  2030. # In 3.11, the ability to define generic `NamedTuple`s was supported.
  2031. # This was explicitly disallowed in 3.9-3.10, and only half-worked in <=3.8.
  2032. # On 3.12, we added __orig_bases__ to call-based NamedTuples
  2033. # On 3.13, we deprecated kwargs-based NamedTuples
  2034. if sys.version_info >= (3, 13):
  2035. NamedTuple = typing.NamedTuple
  2036. else:
  2037. def _make_nmtuple(name, types, module, defaults=()):
  2038. fields = [n for n, t in types]
  2039. annotations = {n: typing._type_check(t, f"field {n} annotation must be a type")
  2040. for n, t in types}
  2041. nm_tpl = collections.namedtuple(name, fields,
  2042. defaults=defaults, module=module)
  2043. nm_tpl.__annotations__ = nm_tpl.__new__.__annotations__ = annotations
  2044. # The `_field_types` attribute was removed in 3.9;
  2045. # in earlier versions, it is the same as the `__annotations__` attribute
  2046. if sys.version_info < (3, 9):
  2047. nm_tpl._field_types = annotations
  2048. return nm_tpl
  2049. _prohibited_namedtuple_fields = typing._prohibited
  2050. _special_namedtuple_fields = frozenset({'__module__', '__name__', '__annotations__'})
  2051. class _NamedTupleMeta(type):
  2052. def __new__(cls, typename, bases, ns):
  2053. assert _NamedTuple in bases
  2054. for base in bases:
  2055. if base is not _NamedTuple and base is not typing.Generic:
  2056. raise TypeError(
  2057. 'can only inherit from a NamedTuple type and Generic')
  2058. bases = tuple(tuple if base is _NamedTuple else base for base in bases)
  2059. types = ns.get('__annotations__', {})
  2060. default_names = []
  2061. for field_name in types:
  2062. if field_name in ns:
  2063. default_names.append(field_name)
  2064. elif default_names:
  2065. raise TypeError(f"Non-default namedtuple field {field_name} "
  2066. f"cannot follow default field"
  2067. f"{'s' if len(default_names) > 1 else ''} "
  2068. f"{', '.join(default_names)}")
  2069. nm_tpl = _make_nmtuple(
  2070. typename, types.items(),
  2071. defaults=[ns[n] for n in default_names],
  2072. module=ns['__module__']
  2073. )
  2074. nm_tpl.__bases__ = bases
  2075. if typing.Generic in bases:
  2076. if hasattr(typing, '_generic_class_getitem'): # 3.12+
  2077. nm_tpl.__class_getitem__ = classmethod(typing._generic_class_getitem)
  2078. else:
  2079. class_getitem = typing.Generic.__class_getitem__.__func__
  2080. nm_tpl.__class_getitem__ = classmethod(class_getitem)
  2081. # update from user namespace without overriding special namedtuple attributes
  2082. for key, val in ns.items():
  2083. if key in _prohibited_namedtuple_fields:
  2084. raise AttributeError("Cannot overwrite NamedTuple attribute " + key)
  2085. elif key not in _special_namedtuple_fields:
  2086. if key not in nm_tpl._fields:
  2087. setattr(nm_tpl, key, ns[key])
  2088. try:
  2089. set_name = type(val).__set_name__
  2090. except AttributeError:
  2091. pass
  2092. else:
  2093. try:
  2094. set_name(val, nm_tpl, key)
  2095. except BaseException as e:
  2096. msg = (
  2097. f"Error calling __set_name__ on {type(val).__name__!r} "
  2098. f"instance {key!r} in {typename!r}"
  2099. )
  2100. # BaseException.add_note() existed on py311,
  2101. # but the __set_name__ machinery didn't start
  2102. # using add_note() until py312.
  2103. # Making sure exceptions are raised in the same way
  2104. # as in "normal" classes seems most important here.
  2105. if sys.version_info >= (3, 12):
  2106. e.add_note(msg)
  2107. raise
  2108. else:
  2109. raise RuntimeError(msg) from e
  2110. if typing.Generic in bases:
  2111. nm_tpl.__init_subclass__()
  2112. return nm_tpl
  2113. _NamedTuple = type.__new__(_NamedTupleMeta, 'NamedTuple', (), {})
  2114. def _namedtuple_mro_entries(bases):
  2115. assert NamedTuple in bases
  2116. return (_NamedTuple,)
  2117. @_ensure_subclassable(_namedtuple_mro_entries)
  2118. def NamedTuple(typename, fields=_marker, /, **kwargs):
  2119. """Typed version of namedtuple.
  2120. Usage::
  2121. class Employee(NamedTuple):
  2122. name: str
  2123. id: int
  2124. This is equivalent to::
  2125. Employee = collections.namedtuple('Employee', ['name', 'id'])
  2126. The resulting class has an extra __annotations__ attribute, giving a
  2127. dict that maps field names to types. (The field names are also in
  2128. the _fields attribute, which is part of the namedtuple API.)
  2129. An alternative equivalent functional syntax is also accepted::
  2130. Employee = NamedTuple('Employee', [('name', str), ('id', int)])
  2131. """
  2132. if fields is _marker:
  2133. if kwargs:
  2134. deprecated_thing = "Creating NamedTuple classes using keyword arguments"
  2135. deprecation_msg = (
  2136. "{name} is deprecated and will be disallowed in Python {remove}. "
  2137. "Use the class-based or functional syntax instead."
  2138. )
  2139. else:
  2140. deprecated_thing = "Failing to pass a value for the 'fields' parameter"
  2141. example = f"`{typename} = NamedTuple({typename!r}, [])`"
  2142. deprecation_msg = (
  2143. "{name} is deprecated and will be disallowed in Python {remove}. "
  2144. "To create a NamedTuple class with 0 fields "
  2145. "using the functional syntax, "
  2146. "pass an empty list, e.g. "
  2147. ) + example + "."
  2148. elif fields is None:
  2149. if kwargs:
  2150. raise TypeError(
  2151. "Cannot pass `None` as the 'fields' parameter "
  2152. "and also specify fields using keyword arguments"
  2153. )
  2154. else:
  2155. deprecated_thing = "Passing `None` as the 'fields' parameter"
  2156. example = f"`{typename} = NamedTuple({typename!r}, [])`"
  2157. deprecation_msg = (
  2158. "{name} is deprecated and will be disallowed in Python {remove}. "
  2159. "To create a NamedTuple class with 0 fields "
  2160. "using the functional syntax, "
  2161. "pass an empty list, e.g. "
  2162. ) + example + "."
  2163. elif kwargs:
  2164. raise TypeError("Either list of fields or keywords"
  2165. " can be provided to NamedTuple, not both")
  2166. if fields is _marker or fields is None:
  2167. warnings.warn(
  2168. deprecation_msg.format(name=deprecated_thing, remove="3.15"),
  2169. DeprecationWarning,
  2170. stacklevel=2,
  2171. )
  2172. fields = kwargs.items()
  2173. nt = _make_nmtuple(typename, fields, module=_caller())
  2174. nt.__orig_bases__ = (NamedTuple,)
  2175. return nt
  2176. if hasattr(collections.abc, "Buffer"):
  2177. Buffer = collections.abc.Buffer
  2178. else:
  2179. class Buffer(abc.ABC):
  2180. """Base class for classes that implement the buffer protocol.
  2181. The buffer protocol allows Python objects to expose a low-level
  2182. memory buffer interface. Before Python 3.12, it is not possible
  2183. to implement the buffer protocol in pure Python code, or even
  2184. to check whether a class implements the buffer protocol. In
  2185. Python 3.12 and higher, the ``__buffer__`` method allows access
  2186. to the buffer protocol from Python code, and the
  2187. ``collections.abc.Buffer`` ABC allows checking whether a class
  2188. implements the buffer protocol.
  2189. To indicate support for the buffer protocol in earlier versions,
  2190. inherit from this ABC, either in a stub file or at runtime,
  2191. or use ABC registration. This ABC provides no methods, because
  2192. there is no Python-accessible methods shared by pre-3.12 buffer
  2193. classes. It is useful primarily for static checks.
  2194. """
  2195. # As a courtesy, register the most common stdlib buffer classes.
  2196. Buffer.register(memoryview)
  2197. Buffer.register(bytearray)
  2198. Buffer.register(bytes)
  2199. # Backport of types.get_original_bases, available on 3.12+ in CPython
  2200. if hasattr(_types, "get_original_bases"):
  2201. get_original_bases = _types.get_original_bases
  2202. else:
  2203. def get_original_bases(cls, /):
  2204. """Return the class's "original" bases prior to modification by `__mro_entries__`.
  2205. Examples::
  2206. from typing import TypeVar, Generic
  2207. from typing_extensions import NamedTuple, TypedDict
  2208. T = TypeVar("T")
  2209. class Foo(Generic[T]): ...
  2210. class Bar(Foo[int], float): ...
  2211. class Baz(list[str]): ...
  2212. Eggs = NamedTuple("Eggs", [("a", int), ("b", str)])
  2213. Spam = TypedDict("Spam", {"a": int, "b": str})
  2214. assert get_original_bases(Bar) == (Foo[int], float)
  2215. assert get_original_bases(Baz) == (list[str],)
  2216. assert get_original_bases(Eggs) == (NamedTuple,)
  2217. assert get_original_bases(Spam) == (TypedDict,)
  2218. assert get_original_bases(int) == (object,)
  2219. """
  2220. try:
  2221. return cls.__dict__.get("__orig_bases__", cls.__bases__)
  2222. except AttributeError:
  2223. raise TypeError(
  2224. f'Expected an instance of type, not {type(cls).__name__!r}'
  2225. ) from None
  2226. # NewType is a class on Python 3.10+, making it pickleable
  2227. # The error message for subclassing instances of NewType was improved on 3.11+
  2228. if sys.version_info >= (3, 11):
  2229. NewType = typing.NewType
  2230. else:
  2231. class NewType:
  2232. """NewType creates simple unique types with almost zero
  2233. runtime overhead. NewType(name, tp) is considered a subtype of tp
  2234. by static type checkers. At runtime, NewType(name, tp) returns
  2235. a dummy callable that simply returns its argument. Usage::
  2236. UserId = NewType('UserId', int)
  2237. def name_by_id(user_id: UserId) -> str:
  2238. ...
  2239. UserId('user') # Fails type check
  2240. name_by_id(42) # Fails type check
  2241. name_by_id(UserId(42)) # OK
  2242. num = UserId(5) + 1 # type: int
  2243. """
  2244. def __call__(self, obj, /):
  2245. return obj
  2246. def __init__(self, name, tp):
  2247. self.__qualname__ = name
  2248. if '.' in name:
  2249. name = name.rpartition('.')[-1]
  2250. self.__name__ = name
  2251. self.__supertype__ = tp
  2252. def_mod = _caller()
  2253. if def_mod != 'typing_extensions':
  2254. self.__module__ = def_mod
  2255. def __mro_entries__(self, bases):
  2256. # We defined __mro_entries__ to get a better error message
  2257. # if a user attempts to subclass a NewType instance. bpo-46170
  2258. supercls_name = self.__name__
  2259. class Dummy:
  2260. def __init_subclass__(cls):
  2261. subcls_name = cls.__name__
  2262. raise TypeError(
  2263. f"Cannot subclass an instance of NewType. "
  2264. f"Perhaps you were looking for: "
  2265. f"`{subcls_name} = NewType({subcls_name!r}, {supercls_name})`"
  2266. )
  2267. return (Dummy,)
  2268. def __repr__(self):
  2269. return f'{self.__module__}.{self.__qualname__}'
  2270. def __reduce__(self):
  2271. return self.__qualname__
  2272. if sys.version_info >= (3, 10):
  2273. # PEP 604 methods
  2274. # It doesn't make sense to have these methods on Python <3.10
  2275. def __or__(self, other):
  2276. return typing.Union[self, other]
  2277. def __ror__(self, other):
  2278. return typing.Union[other, self]
  2279. if hasattr(typing, "TypeAliasType"):
  2280. TypeAliasType = typing.TypeAliasType
  2281. else:
  2282. def _is_unionable(obj):
  2283. """Corresponds to is_unionable() in unionobject.c in CPython."""
  2284. return obj is None or isinstance(obj, (
  2285. type,
  2286. _types.GenericAlias,
  2287. _types.UnionType,
  2288. TypeAliasType,
  2289. ))
  2290. class TypeAliasType:
  2291. """Create named, parameterized type aliases.
  2292. This provides a backport of the new `type` statement in Python 3.12:
  2293. type ListOrSet[T] = list[T] | set[T]
  2294. is equivalent to:
  2295. T = TypeVar("T")
  2296. ListOrSet = TypeAliasType("ListOrSet", list[T] | set[T], type_params=(T,))
  2297. The name ListOrSet can then be used as an alias for the type it refers to.
  2298. The type_params argument should contain all the type parameters used
  2299. in the value of the type alias. If the alias is not generic, this
  2300. argument is omitted.
  2301. Static type checkers should only support type aliases declared using
  2302. TypeAliasType that follow these rules:
  2303. - The first argument (the name) must be a string literal.
  2304. - The TypeAliasType instance must be immediately assigned to a variable
  2305. of the same name. (For example, 'X = TypeAliasType("Y", int)' is invalid,
  2306. as is 'X, Y = TypeAliasType("X", int), TypeAliasType("Y", int)').
  2307. """
  2308. def __init__(self, name: str, value, *, type_params=()):
  2309. if not isinstance(name, str):
  2310. raise TypeError("TypeAliasType name must be a string")
  2311. self.__value__ = value
  2312. self.__type_params__ = type_params
  2313. parameters = []
  2314. for type_param in type_params:
  2315. if isinstance(type_param, TypeVarTuple):
  2316. parameters.extend(type_param)
  2317. else:
  2318. parameters.append(type_param)
  2319. self.__parameters__ = tuple(parameters)
  2320. def_mod = _caller()
  2321. if def_mod != 'typing_extensions':
  2322. self.__module__ = def_mod
  2323. # Setting this attribute closes the TypeAliasType from further modification
  2324. self.__name__ = name
  2325. def __setattr__(self, name: str, value: object, /) -> None:
  2326. if hasattr(self, "__name__"):
  2327. self._raise_attribute_error(name)
  2328. super().__setattr__(name, value)
  2329. def __delattr__(self, name: str, /) -> Never:
  2330. self._raise_attribute_error(name)
  2331. def _raise_attribute_error(self, name: str) -> Never:
  2332. # Match the Python 3.12 error messages exactly
  2333. if name == "__name__":
  2334. raise AttributeError("readonly attribute")
  2335. elif name in {"__value__", "__type_params__", "__parameters__", "__module__"}:
  2336. raise AttributeError(
  2337. f"attribute '{name}' of 'typing.TypeAliasType' objects "
  2338. "is not writable"
  2339. )
  2340. else:
  2341. raise AttributeError(
  2342. f"'typing.TypeAliasType' object has no attribute '{name}'"
  2343. )
  2344. def __repr__(self) -> str:
  2345. return self.__name__
  2346. def __getitem__(self, parameters):
  2347. if not isinstance(parameters, tuple):
  2348. parameters = (parameters,)
  2349. parameters = [
  2350. typing._type_check(
  2351. item, f'Subscripting {self.__name__} requires a type.'
  2352. )
  2353. for item in parameters
  2354. ]
  2355. return typing._GenericAlias(self, tuple(parameters))
  2356. def __reduce__(self):
  2357. return self.__name__
  2358. def __init_subclass__(cls, *args, **kwargs):
  2359. raise TypeError(
  2360. "type 'typing_extensions.TypeAliasType' is not an acceptable base type"
  2361. )
  2362. # The presence of this method convinces typing._type_check
  2363. # that TypeAliasTypes are types.
  2364. def __call__(self):
  2365. raise TypeError("Type alias is not callable")
  2366. if sys.version_info >= (3, 10):
  2367. def __or__(self, right):
  2368. # For forward compatibility with 3.12, reject Unions
  2369. # that are not accepted by the built-in Union.
  2370. if not _is_unionable(right):
  2371. return NotImplemented
  2372. return typing.Union[self, right]
  2373. def __ror__(self, left):
  2374. if not _is_unionable(left):
  2375. return NotImplemented
  2376. return typing.Union[left, self]
  2377. if hasattr(typing, "is_protocol"):
  2378. is_protocol = typing.is_protocol
  2379. get_protocol_members = typing.get_protocol_members
  2380. else:
  2381. def is_protocol(tp: type, /) -> bool:
  2382. """Return True if the given type is a Protocol.
  2383. Example::
  2384. >>> from typing_extensions import Protocol, is_protocol
  2385. >>> class P(Protocol):
  2386. ... def a(self) -> str: ...
  2387. ... b: int
  2388. >>> is_protocol(P)
  2389. True
  2390. >>> is_protocol(int)
  2391. False
  2392. """
  2393. return (
  2394. isinstance(tp, type)
  2395. and getattr(tp, '_is_protocol', False)
  2396. and tp is not Protocol
  2397. and tp is not typing.Protocol
  2398. )
  2399. def get_protocol_members(tp: type, /) -> typing.FrozenSet[str]:
  2400. """Return the set of members defined in a Protocol.
  2401. Example::
  2402. >>> from typing_extensions import Protocol, get_protocol_members
  2403. >>> class P(Protocol):
  2404. ... def a(self) -> str: ...
  2405. ... b: int
  2406. >>> get_protocol_members(P)
  2407. frozenset({'a', 'b'})
  2408. Raise a TypeError for arguments that are not Protocols.
  2409. """
  2410. if not is_protocol(tp):
  2411. raise TypeError(f'{tp!r} is not a Protocol')
  2412. if hasattr(tp, '__protocol_attrs__'):
  2413. return frozenset(tp.__protocol_attrs__)
  2414. return frozenset(_get_protocol_attrs(tp))
  2415. if hasattr(typing, "Doc"):
  2416. Doc = typing.Doc
  2417. else:
  2418. class Doc:
  2419. """Define the documentation of a type annotation using ``Annotated``, to be
  2420. used in class attributes, function and method parameters, return values,
  2421. and variables.
  2422. The value should be a positional-only string literal to allow static tools
  2423. like editors and documentation generators to use it.
  2424. This complements docstrings.
  2425. The string value passed is available in the attribute ``documentation``.
  2426. Example::
  2427. >>> from typing_extensions import Annotated, Doc
  2428. >>> def hi(to: Annotated[str, Doc("Who to say hi to")]) -> None: ...
  2429. """
  2430. def __init__(self, documentation: str, /) -> None:
  2431. self.documentation = documentation
  2432. def __repr__(self) -> str:
  2433. return f"Doc({self.documentation!r})"
  2434. def __hash__(self) -> int:
  2435. return hash(self.documentation)
  2436. def __eq__(self, other: object) -> bool:
  2437. if not isinstance(other, Doc):
  2438. return NotImplemented
  2439. return self.documentation == other.documentation
  2440. # Aliases for items that have always been in typing.
  2441. # Explicitly assign these (rather than using `from typing import *` at the top),
  2442. # so that we get a CI error if one of these is deleted from typing.py
  2443. # in a future version of Python
  2444. AbstractSet = typing.AbstractSet
  2445. AnyStr = typing.AnyStr
  2446. BinaryIO = typing.BinaryIO
  2447. Callable = typing.Callable
  2448. Collection = typing.Collection
  2449. Container = typing.Container
  2450. Dict = typing.Dict
  2451. ForwardRef = typing.ForwardRef
  2452. FrozenSet = typing.FrozenSet
  2453. Generator = typing.Generator
  2454. Generic = typing.Generic
  2455. Hashable = typing.Hashable
  2456. IO = typing.IO
  2457. ItemsView = typing.ItemsView
  2458. Iterable = typing.Iterable
  2459. Iterator = typing.Iterator
  2460. KeysView = typing.KeysView
  2461. List = typing.List
  2462. Mapping = typing.Mapping
  2463. MappingView = typing.MappingView
  2464. Match = typing.Match
  2465. MutableMapping = typing.MutableMapping
  2466. MutableSequence = typing.MutableSequence
  2467. MutableSet = typing.MutableSet
  2468. Optional = typing.Optional
  2469. Pattern = typing.Pattern
  2470. Reversible = typing.Reversible
  2471. Sequence = typing.Sequence
  2472. Set = typing.Set
  2473. Sized = typing.Sized
  2474. TextIO = typing.TextIO
  2475. Tuple = typing.Tuple
  2476. Union = typing.Union
  2477. ValuesView = typing.ValuesView
  2478. cast = typing.cast
  2479. no_type_check = typing.no_type_check
  2480. no_type_check_decorator = typing.no_type_check_decorator