typing_objects.pyi 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. # Stub file generated using:
  2. # `stubgen --inspect-mode --include-docstrings -m typing_inspection.typing_objects`
  3. # (manual edits need to be applied).
  4. """Low-level introspection utilities for [`typing`][] members.
  5. The provided functions in this module check against both the [`typing`][] and [`typing_extensions`][]
  6. variants, if they exists and are different.
  7. """
  8. import sys
  9. from typing import Any, Final, ForwardRef, NewType, TypeVar
  10. from typing_extensions import ParamSpec, ParamSpecArgs, ParamSpecKwargs, TypeAliasType, TypeIs, TypeVarTuple, deprecated
  11. __all__ = [
  12. 'DEPRECATED_ALIASES',
  13. 'NoneType',
  14. 'is_annotated',
  15. 'is_any',
  16. 'is_classvar',
  17. 'is_concatenate',
  18. 'is_deprecated',
  19. 'is_final',
  20. 'is_generic',
  21. 'is_literal',
  22. 'is_literalstring',
  23. 'is_namedtuple',
  24. 'is_never',
  25. 'is_newtype',
  26. 'is_nodefault',
  27. 'is_noextraitems',
  28. 'is_noreturn',
  29. 'is_notrequired',
  30. 'is_paramspec',
  31. 'is_paramspecargs',
  32. 'is_paramspeckwargs',
  33. 'is_readonly',
  34. 'is_required',
  35. 'is_self',
  36. 'is_typealias',
  37. 'is_typealiastype',
  38. 'is_typeguard',
  39. 'is_typeis',
  40. 'is_typevar',
  41. 'is_typevartuple',
  42. 'is_union',
  43. 'is_unpack',
  44. ]
  45. if sys.version_info >= (3, 10):
  46. from types import NoneType
  47. else:
  48. NoneType = type(None)
  49. def is_annotated(obj: Any, /) -> bool:
  50. """
  51. Return whether the argument is the [`Annotated`][typing.Annotated] [special form][].
  52. ```pycon
  53. >>> is_annotated(Annotated)
  54. True
  55. >>> is_annotated(Annotated[int, ...])
  56. False
  57. ```
  58. """
  59. def is_any(obj: Any, /) -> bool:
  60. """
  61. Return whether the argument is the [`Any`][typing.Any] [special form][].
  62. ```pycon
  63. >>> is_any(Any)
  64. True
  65. ```
  66. """
  67. def is_classvar(obj: Any, /) -> bool:
  68. """
  69. Return whether the argument is the [`ClassVar`][typing.ClassVar] [type qualifier][].
  70. ```pycon
  71. >>> is_classvar(ClassVar)
  72. True
  73. >>> is_classvar(ClassVar[int])
  74. >>> False
  75. ```
  76. """
  77. def is_concatenate(obj: Any, /) -> bool:
  78. """
  79. Return whether the argument is the [`Concatenate`][typing.Concatenate] [special form][].
  80. ```pycon
  81. >>> is_concatenate(Concatenate)
  82. True
  83. >>> is_concatenate(Concatenate[int, P])
  84. False
  85. ```
  86. """
  87. def is_final(obj: Any, /) -> bool:
  88. """
  89. Return whether the argument is the [`Final`][typing.Final] [type qualifier][].
  90. ```pycon
  91. >>> is_final(Final)
  92. True
  93. >>> is_final(Final[int])
  94. False
  95. ```
  96. """
  97. def is_forwardref(obj: Any, /) -> TypeIs[ForwardRef]:
  98. """
  99. Return whether the argument is an instance of [`ForwardRef`][typing.ForwardRef].
  100. ```pycon
  101. >>> is_forwardref(ForwardRef('T'))
  102. True
  103. ```
  104. """
  105. def is_generic(obj: Any, /) -> bool:
  106. """
  107. Return whether the argument is the [`Generic`][typing.Generic] [special form][].
  108. ```pycon
  109. >>> is_generic(Generic)
  110. True
  111. >>> is_generic(Generic[T])
  112. False
  113. ```
  114. """
  115. def is_literal(obj: Any, /) -> bool:
  116. """
  117. Return whether the argument is the [`Literal`][typing.Literal] [special form][].
  118. ```pycon
  119. >>> is_literal(Literal)
  120. True
  121. >>> is_literal(Literal["a"])
  122. False
  123. ```
  124. """
  125. def is_paramspec(obj: Any, /) -> TypeIs[ParamSpec]:
  126. """
  127. Return whether the argument is an instance of [`ParamSpec`][typing.ParamSpec].
  128. ```pycon
  129. >>> P = ParamSpec('P')
  130. >>> is_paramspec(P)
  131. True
  132. ```
  133. """
  134. def is_typevar(obj: Any, /) -> TypeIs[TypeVar]:
  135. """
  136. Return whether the argument is an instance of [`TypeVar`][typing.TypeVar].
  137. ```pycon
  138. >>> T = TypeVar('T')
  139. >>> is_typevar(T)
  140. True
  141. ```
  142. """
  143. def is_typevartuple(obj: Any, /) -> TypeIs[TypeVarTuple]:
  144. """
  145. Return whether the argument is an instance of [`TypeVarTuple`][typing.TypeVarTuple].
  146. ```pycon
  147. >>> Ts = TypeVarTuple('Ts')
  148. >>> is_typevartuple(Ts)
  149. True
  150. ```
  151. """
  152. def is_union(obj: Any, /) -> bool:
  153. """
  154. Return whether the argument is the [`Union`][typing.Union] [special form][].
  155. This function can also be used to check for the [`Optional`][typing.Optional] [special form][],
  156. as at runtime, `Optional[int]` is equivalent to `Union[int, None]`.
  157. ```pycon
  158. >>> is_union(Union)
  159. True
  160. >>> is_union(Union[int, str])
  161. False
  162. ```
  163. !!! warning
  164. This does not check for unions using the [new syntax][types-union] (e.g. `int | str`).
  165. """
  166. def is_namedtuple(obj: Any, /) -> bool:
  167. """Return whether the argument is a named tuple type.
  168. This includes [`NamedTuple`][typing.NamedTuple] subclasses and classes created from the
  169. [`collections.namedtuple`][] factory function.
  170. ```pycon
  171. >>> class User(NamedTuple):
  172. ... name: str
  173. ...
  174. >>> is_namedtuple(User)
  175. True
  176. >>> City = collections.namedtuple('City', [])
  177. >>> is_namedtuple(City)
  178. True
  179. >>> is_namedtuple(NamedTuple)
  180. False
  181. ```
  182. """
  183. def is_literalstring(obj: Any, /) -> bool:
  184. """
  185. Return whether the argument is the [`LiteralString`][typing.LiteralString] [special form][].
  186. ```pycon
  187. >>> is_literalstring(LiteralString)
  188. True
  189. ```
  190. """
  191. def is_never(obj: Any, /) -> bool:
  192. """
  193. Return whether the argument is the [`Never`][typing.Never] [special form][].
  194. ```pycon
  195. >>> is_never(Never)
  196. True
  197. ```
  198. """
  199. def is_newtype(obj: Any, /) -> TypeIs[NewType]:
  200. """
  201. Return whether the argument is a [`NewType`][typing.NewType].
  202. ```pycon
  203. >>> UserId = NewType("UserId", int)
  204. >>> is_newtype(UserId)
  205. True
  206. ```
  207. """
  208. def is_nodefault(obj: Any, /) -> bool:
  209. """
  210. Return whether the argument is the [`NoDefault`][typing.NoDefault] sentinel object.
  211. ```pycon
  212. >>> is_nodefault(NoDefault)
  213. True
  214. ```
  215. """
  216. def is_noextraitems(obj: Any, /) -> bool:
  217. """
  218. Return whether the argument is the `NoExtraItems` sentinel object.
  219. ```pycon
  220. >>> is_noextraitems(NoExtraItems)
  221. True
  222. ```
  223. """
  224. def is_noreturn(obj: Any, /) -> bool:
  225. """
  226. Return whether the argument is the [`NoReturn`][typing.NoReturn] [special form][].
  227. ```pycon
  228. >>> is_noreturn(NoReturn)
  229. True
  230. >>> is_noreturn(Never)
  231. False
  232. ```
  233. """
  234. def is_notrequired(obj: Any, /) -> bool:
  235. """
  236. Return whether the argument is the [`NotRequired`][typing.NotRequired] [special form][].
  237. ```pycon
  238. >>> is_notrequired(NotRequired)
  239. True
  240. ```
  241. """
  242. def is_paramspecargs(obj: Any, /) -> TypeIs[ParamSpecArgs]:
  243. """
  244. Return whether the argument is an instance of [`ParamSpecArgs`][typing.ParamSpecArgs].
  245. ```pycon
  246. >>> P = ParamSpec('P')
  247. >>> is_paramspecargs(P.args)
  248. True
  249. ```
  250. """
  251. def is_paramspeckwargs(obj: Any, /) -> TypeIs[ParamSpecKwargs]:
  252. """
  253. Return whether the argument is an instance of [`ParamSpecKwargs`][typing.ParamSpecKwargs].
  254. ```pycon
  255. >>> P = ParamSpec('P')
  256. >>> is_paramspeckwargs(P.kwargs)
  257. True
  258. ```
  259. """
  260. def is_readonly(obj: Any, /) -> bool:
  261. """
  262. Return whether the argument is the [`ReadOnly`][typing.ReadOnly] [special form][].
  263. ```pycon
  264. >>> is_readonly(ReadOnly)
  265. True
  266. ```
  267. """
  268. def is_required(obj: Any, /) -> bool:
  269. """
  270. Return whether the argument is the [`Required`][typing.Required] [special form][].
  271. ```pycon
  272. >>> is_required(Required)
  273. True
  274. ```
  275. """
  276. def is_self(obj: Any, /) -> bool:
  277. """
  278. Return whether the argument is the [`Self`][typing.Self] [special form][].
  279. ```pycon
  280. >>> is_self(Self)
  281. True
  282. ```
  283. """
  284. def is_typealias(obj: Any, /) -> bool:
  285. """
  286. Return whether the argument is the [`TypeAlias`][typing.TypeAlias] [special form][].
  287. ```pycon
  288. >>> is_typealias(TypeAlias)
  289. True
  290. ```
  291. """
  292. def is_typeguard(obj: Any, /) -> bool:
  293. """
  294. Return whether the argument is the [`TypeGuard`][typing.TypeGuard] [special form][].
  295. ```pycon
  296. >>> is_typeguard(TypeGuard)
  297. True
  298. ```
  299. """
  300. def is_typeis(obj: Any, /) -> bool:
  301. """
  302. Return whether the argument is the [`TypeIs`][typing.TypeIs] [special form][].
  303. ```pycon
  304. >>> is_typeis(TypeIs)
  305. True
  306. ```
  307. """
  308. def is_typealiastype(obj: Any, /) -> TypeIs[TypeAliasType]:
  309. """
  310. Return whether the argument is a [`TypeAliasType`][typing.TypeAliasType] instance.
  311. ```pycon
  312. >>> type MyInt = int
  313. >>> is_typealiastype(MyInt)
  314. True
  315. >>> MyStr = TypeAliasType("MyStr", str)
  316. >>> is_typealiastype(MyStr):
  317. True
  318. >>> type MyList[T] = list[T]
  319. >>> is_typealiastype(MyList[int])
  320. False
  321. ```
  322. """
  323. def is_unpack(obj: Any, /) -> bool:
  324. """
  325. Return whether the argument is the [`Unpack`][typing.Unpack] [special form][].
  326. ```pycon
  327. >>> is_unpack(Unpack)
  328. True
  329. >>> is_unpack(Unpack[Ts])
  330. False
  331. ```
  332. """
  333. def is_deprecated(obj: Any, /) -> TypeIs[deprecated]:
  334. """
  335. Return whether the argument is a [`deprecated`][warnings.deprecated] instance.
  336. This also includes the [`typing_extensions` backport][typing_extensions.deprecated].
  337. ```pycon
  338. >>> is_deprecated(warnings.deprecated('message'))
  339. True
  340. >>> is_deprecated(typing_extensions.deprecated('deprecated'))
  341. True
  342. ```
  343. """
  344. DEPRECATED_ALIASES: Final[dict[Any, type[Any]]]
  345. """A mapping between the deprecated typing aliases to their replacement, as per [PEP 585](https://peps.python.org/pep-0585/)."""