typing_objects.pyi 9.4 KB

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