codekingpro/portable-devtools
114k
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 6The provided functions in this module check against both the [`typing`][] and [`typing_extensions`][]7variants, if they exists and are different.8"""9 10import sys11from typing import Any, Final, ForwardRef, NewType, TypeVar12 13from typing_extensions import ParamSpec, ParamSpecArgs, ParamSpecKwargs, TypeAliasType, TypeIs, TypeVarTuple, deprecated14 15__all__ = [16 'DEPRECATED_ALIASES',17 'NoneType',18 'is_annotated',19 'is_any',20 'is_classvar',21 'is_concatenate',22 'is_deprecated',23 'is_final',24 'is_generic',25 'is_literal',26 'is_literalstring',27 'is_namedtuple',28 'is_never',29 'is_newtype',30 'is_nodefault',31 'is_noextraitems',32 'is_noreturn',33 'is_notrequired',34 'is_paramspec',35 'is_paramspecargs',36 'is_paramspeckwargs',37 'is_readonly',38 'is_required',39 'is_self',40 'is_typealias',41 'is_typealiastype',42 'is_typeguard',43 'is_typeis',44 'is_typevar',45 'is_typevartuple',46 'is_union',47 'is_unpack',48]49 50if sys.version_info >= (3, 10):51 from types import NoneType52else:53 NoneType = type(None)54 55def is_annotated(obj: Any, /) -> bool:56 """57 Return whether the argument is the [`Annotated`][typing.Annotated] [special form][].58 59 ```pycon60 >>> is_annotated(Annotated)61 True62 >>> is_annotated(Annotated[int, ...])63 False64 ```65 """66 67def is_any(obj: Any, /) -> bool:68 """69 Return whether the argument is the [`Any`][typing.Any] [special form][].70 71 ```pycon72 >>> is_any(Any)73 True74 ```75 """76 77def is_classvar(obj: Any, /) -> bool:78 """79 Return whether the argument is the [`ClassVar`][typing.ClassVar] [type qualifier][].80 81 ```pycon82 >>> is_classvar(ClassVar)83 True84 >>> is_classvar(ClassVar[int])85 >>> False86 ```87 """88 89def is_concatenate(obj: Any, /) -> bool:90 """91 Return whether the argument is the [`Concatenate`][typing.Concatenate] [special form][].92 93 ```pycon94 >>> is_concatenate(Concatenate)95 True96 >>> is_concatenate(Concatenate[int, P])97 False98 ```99 """100 101def is_final(obj: Any, /) -> bool:102 """103 Return whether the argument is the [`Final`][typing.Final] [type qualifier][].104 105 ```pycon106 >>> is_final(Final)107 True108 >>> is_final(Final[int])109 False110 ```111 """112 113def is_forwardref(obj: Any, /) -> TypeIs[ForwardRef]:114 """115 Return whether the argument is an instance of [`ForwardRef`][typing.ForwardRef].116 117 ```pycon118 >>> is_forwardref(ForwardRef('T'))119 True120 ```121 """122 123def is_generic(obj: Any, /) -> bool:124 """125 Return whether the argument is the [`Generic`][typing.Generic] [special form][].126 127 ```pycon128 >>> is_generic(Generic)129 True130 >>> is_generic(Generic[T])131 False132 ```133 """134 135def is_literal(obj: Any, /) -> bool:136 """137 Return whether the argument is the [`Literal`][typing.Literal] [special form][].138 139 ```pycon140 >>> is_literal(Literal)141 True142 >>> is_literal(Literal["a"])143 False144 ```145 """146 147def is_paramspec(obj: Any, /) -> TypeIs[ParamSpec]:148 """149 Return whether the argument is an instance of [`ParamSpec`][typing.ParamSpec].150 151 ```pycon152 >>> P = ParamSpec('P')153 >>> is_paramspec(P)154 True155 ```156 """157 158def is_typevar(obj: Any, /) -> TypeIs[TypeVar]:159 """160 Return whether the argument is an instance of [`TypeVar`][typing.TypeVar].161 162 ```pycon163 >>> T = TypeVar('T')164 >>> is_typevar(T)165 True166 ```167 """168 169def is_typevartuple(obj: Any, /) -> TypeIs[TypeVarTuple]:170 """171 Return whether the argument is an instance of [`TypeVarTuple`][typing.TypeVarTuple].172 173 ```pycon174 >>> Ts = TypeVarTuple('Ts')175 >>> is_typevartuple(Ts)176 True177 ```178 """179 180def is_union(obj: Any, /) -> bool:181 """182 Return whether the argument is the [`Union`][typing.Union] [special form][].183 184 This function can also be used to check for the [`Optional`][typing.Optional] [special form][],185 as at runtime, `Optional[int]` is equivalent to `Union[int, None]`.186 187 ```pycon188 >>> is_union(Union)189 True190 >>> is_union(Union[int, str])191 False192 ```193 194 !!! warning195 This does not check for unions using the [new syntax][types-union] (e.g. `int | str`).196 """197 198def is_namedtuple(obj: Any, /) -> bool:199 """Return whether the argument is a named tuple type.200 201 This includes [`NamedTuple`][typing.NamedTuple] subclasses and classes created from the202 [`collections.namedtuple`][] factory function.203 204 ```pycon205 >>> class User(NamedTuple):206 ... name: str207 ...208 >>> is_namedtuple(User)209 True210 >>> City = collections.namedtuple('City', [])211 >>> is_namedtuple(City)212 True213 >>> is_namedtuple(NamedTuple)214 False215 ```216 """217 218def is_literalstring(obj: Any, /) -> bool:219 """220 Return whether the argument is the [`LiteralString`][typing.LiteralString] [special form][].221 222 ```pycon223 >>> is_literalstring(LiteralString)224 True225 ```226 """227 228def is_never(obj: Any, /) -> bool:229 """230 Return whether the argument is the [`Never`][typing.Never] [special form][].231 232 ```pycon233 >>> is_never(Never)234 True235 ```236 """237 238def is_newtype(obj: Any, /) -> TypeIs[NewType]:239 """240 Return whether the argument is a [`NewType`][typing.NewType].241 242 ```pycon243 >>> UserId = NewType("UserId", int)244 >>> is_newtype(UserId)245 True246 ```247 """248 249def is_nodefault(obj: Any, /) -> bool:250 """251 Return whether the argument is the [`NoDefault`][typing.NoDefault] sentinel object.252 253 ```pycon254 >>> is_nodefault(NoDefault)255 True256 ```257 """258 259def is_noextraitems(obj: Any, /) -> bool:260 """261 Return whether the argument is the `NoExtraItems` sentinel object.262 263 ```pycon264 >>> is_noextraitems(NoExtraItems)265 True266 ```267 """268 269def is_noreturn(obj: Any, /) -> bool:270 """271 Return whether the argument is the [`NoReturn`][typing.NoReturn] [special form][].272 273 ```pycon274 >>> is_noreturn(NoReturn)275 True276 >>> is_noreturn(Never)277 False278 ```279 """280 281def is_notrequired(obj: Any, /) -> bool:282 """283 Return whether the argument is the [`NotRequired`][typing.NotRequired] [special form][].284 285 ```pycon286 >>> is_notrequired(NotRequired)287 True288 ```289 """290 291def is_paramspecargs(obj: Any, /) -> TypeIs[ParamSpecArgs]:292 """293 Return whether the argument is an instance of [`ParamSpecArgs`][typing.ParamSpecArgs].294 295 ```pycon296 >>> P = ParamSpec('P')297 >>> is_paramspecargs(P.args)298 True299 ```300 """301 302def is_paramspeckwargs(obj: Any, /) -> TypeIs[ParamSpecKwargs]:303 """304 Return whether the argument is an instance of [`ParamSpecKwargs`][typing.ParamSpecKwargs].305 306 ```pycon307 >>> P = ParamSpec('P')308 >>> is_paramspeckwargs(P.kwargs)309 True310 ```311 """312 313def is_readonly(obj: Any, /) -> bool:314 """315 Return whether the argument is the [`ReadOnly`][typing.ReadOnly] [special form][].316 317 ```pycon318 >>> is_readonly(ReadOnly)319 True320 ```321 """322 323def is_required(obj: Any, /) -> bool:324 """325 Return whether the argument is the [`Required`][typing.Required] [special form][].326 327 ```pycon328 >>> is_required(Required)329 True330 ```331 """332 333def is_self(obj: Any, /) -> bool:334 """335 Return whether the argument is the [`Self`][typing.Self] [special form][].336 337 ```pycon338 >>> is_self(Self)339 True340 ```341 """342 343def is_typealias(obj: Any, /) -> bool:344 """345 Return whether the argument is the [`TypeAlias`][typing.TypeAlias] [special form][].346 347 ```pycon348 >>> is_typealias(TypeAlias)349 True350 ```351 """352 353def is_typeguard(obj: Any, /) -> bool:354 """355 Return whether the argument is the [`TypeGuard`][typing.TypeGuard] [special form][].356 357 ```pycon358 >>> is_typeguard(TypeGuard)359 True360 ```361 """362 363def is_typeis(obj: Any, /) -> bool:364 """365 Return whether the argument is the [`TypeIs`][typing.TypeIs] [special form][].366 367 ```pycon368 >>> is_typeis(TypeIs)369 True370 ```371 """372 373def is_typealiastype(obj: Any, /) -> TypeIs[TypeAliasType]:374 """375 Return whether the argument is a [`TypeAliasType`][typing.TypeAliasType] instance.376 377 ```pycon378 >>> type MyInt = int379 >>> is_typealiastype(MyInt)380 True381 >>> MyStr = TypeAliasType("MyStr", str)382 >>> is_typealiastype(MyStr):383 True384 >>> type MyList[T] = list[T]385 >>> is_typealiastype(MyList[int])386 False387 ```388 """389 390def is_unpack(obj: Any, /) -> bool:391 """392 Return whether the argument is the [`Unpack`][typing.Unpack] [special form][].393 394 ```pycon395 >>> is_unpack(Unpack)396 True397 >>> is_unpack(Unpack[Ts])398 False399 ```400 """401 402def is_deprecated(obj: Any, /) -> TypeIs[deprecated]:403 """404 Return whether the argument is a [`deprecated`][warnings.deprecated] instance.405 406 This also includes the [`typing_extensions` backport][typing_extensions.deprecated].407 408 ```pycon409 >>> is_deprecated(warnings.deprecated('message'))410 True411 >>> is_deprecated(typing_extensions.deprecated('deprecated'))412 True413 ```414 """415 416DEPRECATED_ALIASES: Final[dict[Any, type[Any]]]417"""A mapping between the deprecated typing aliases to their replacement, as per [PEP 585](https://peps.python.org/pep-0585/)."""418 