Debian Codegraph @

src/typing_inspection/typing_objects.pyi

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