typeapi
typeapi
AnnotatedTypeHint
Bases: TypeHint
Represents the Annotated
type hint.
Source code in typeapi/typehint.py
metadata
property
Returns the metadata, i.e. all the parameters after the wrapped type.
>>> TypeHint(Annotated[int, "foobar"]).metadata
('foobar',)
ClassTypeHint
Bases: TypeHint
Represents a real, possibly parameterized, type. For example int
, list
, list[int]
or list[T]
.
Source code in typeapi/typehint.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
|
bases
property
Return the bases of the classes' types. If the type is a generic, the bases of the generic's origin are
returned in their parameterized form (e.g. Generic[T]
instead of Generic
is returned).
get_parameter_map
Returns a dictionary that maps generic parameters to their values.
>>> TypeHint(List[int]).type
<class 'list'>
>>> TypeHint(List[int]).args
(<class 'int'>,)
# NOTE(@niklas): This is a bug for built-in types, but it's not that big of a deal because we don't
# usually need to recursively expand forward references in these types.
>>> TypeHint(List[int]).parameters
()
>>> TypeHint(List[int]).get_parameter_map()
{}
>>> T = TypeVar("T")
>>> class A(Generic[T]): pass
>>> TypeHint(A[int]).get_parameter_map()
{~T: <class 'int'>}
Source code in typeapi/typehint.py
recurse_bases
recurse_bases(order: Literal['dfs', 'bfs'] = 'bfs') -> Generator[ClassTypeHint, Union[Literal['skip'], None], None]
Iterate over all base classes of this type hint, and continues recursively. The iteration order is
determined by the order parameter, which can be either depth-first or breadh-first. If the generator
receives the string "skip"
from the caller, it will skip the bases of the last yielded type.
Source code in typeapi/typehint.py
ClassVarTypeHint
Bases: TypeHint
Represents a ClassVar
type hint.
Source code in typeapi/typehint.py
ForwardRefTypeHint
Bases: TypeHint
Represents a forward reference, i.e. a string in the type annotation or an explicit ForwardRef
.
Source code in typeapi/typehint.py
expr
property
Returns the expression of the forward reference.
>>> TypeHint(ForwardRef("Foobar")).expr
'Foobar'
>>> TypeHint(TypeHint(List["Foobar"]).args[0]).expr
'Foobar'
LiteralTypeHint
Bases: TypeHint
Represents a literal type hint, e.g. Literal["a", 42]
.
Source code in typeapi/typehint.py
TupleTypeHint
Bases: ClassTypeHint
A special class to represent a type hint for a parameterized tuple. This class does not represent a plain tuple type without parameterization.
Source code in typeapi/typehint.py
TypeAliasTypeHint
TypeHint
Bases: object
Base class that provides an object-oriented interface to a Python type hint.
Source code in typeapi/typehint.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
args
property
Type hint arguments are the values passed into type hint subscripts, e.g. in Union[int, str]
, the
arguments are (int, str)
. We only return arguments that are expected to be types or other type hints.
For example, Literal["foo", 0]
has an empty tuple for its args
, and instead the values can be
retrievd using :attr:LiteralTypeHint.valuse
.
origin
property
The original type behind a type hint (e.g. the Generic.__origin__
). For example, for :class:typing.List
,
it is list
. For :class:typing.Sequence
, it is :class:collections.abc.Sequence
.
parameters
property
The parameters of a type hint is basically :attr:args
but filtered for #typing.TypeVar objects.
source
property
The object from which on which the type hint was found, for example a class or a function.
evaluate
evaluate(context: HasGetitem[str, Any] | None = None) -> TypeHint
Evaluate forward references in the type hint using the given context.
This method supports evaluating forward references that use PEP585 and PEP604 syntax even in older versions of Python that do not support the PEPs.
:param context: An object that supports __getitem__()
to retrieve a value by name. If this is
not specified, the globals of the __module__
of the type hint's source :attr:source
is
used instead. If no source exists, a :class:RuntimeError
is raised.
Source code in typeapi/typehint.py
get_context
Return the context for this type hint in which forward references must be evaluated.
The context is derived from the source
attribute, which can be either a ModuleType
, Mapping
or
type
. In case of a type
, the context is composed of the class scope (to resolve class-level members)
and the scope of the module that contains the type (looked up in sys.modules
).
Raises RuntimeError: If source
is None
or has not one of the three supported types.
Source code in typeapi/typehint.py
parameterize
parameterize(parameter_map: Mapping[object, Any]) -> TypeHint
Replace references to the type variables in the keys of parameter_map with the type hints of the associated values.
:param parameter_map: A dictionary that maps :class:TypeVar
to other
type hints.
Source code in typeapi/typehint.py
TypeVarTypeHint
Bases: TypeHint
Represents a TypeVar
type hint.
Source code in typeapi/typehint.py
bound
property
Returns what the TypeVar is bound to.
>>> TypeHint(TypeVar("T", bound=str)).bound
<class 'str'>
constraints
property
Returns the constraints of the TypeVar.
>>> TypeHint(TypeVar("T", int, str)).constraints
(<class 'int'>, <class 'str'>)
contravariant
property
Returns whether the TypeVar is contravariant.
>>> TypeHint(TypeVar("T")).contravariant
False
>>> TypeHint(TypeVar("T", contravariant=True)).contravariant
True
covariant
property
Returns whether the TypeVar is covariant.
>>> TypeHint(TypeVar("T")).covariant
False
>>> TypeHint(TypeVar("T", covariant=True)).covariant
True
TypedDictProtocol
Bases: Protocol
A protocol that describes #typing.TypedDict values (which are actually instances of the #typing._TypedDictMeta metaclass). Use #is_typed_dict() to check if a hint is matches this protocol.
Source code in typeapi/utils.py
UnionTypeHint
Bases: TypeHint
Represents a union of types, e.g. typing.Union[A, B]
or A | B
.
Source code in typeapi/typehint.py
get_annotations
get_annotations(obj: Union[Callable[..., Any], ModuleType, type], include_bases: bool = False, globalns: Optional[Dict[str, Any]] = None, localns: Optional[Dict[str, Any]] = None, eval_str: bool = True) -> Dict[str, Any]
Like #typing.get_type_hints(), but always includes extras. This is important when we want to inspect
typing.Annotated hints (without extras the annotations are removed). In Python 3.10 and onwards, this is
an alias for #inspect.get_annotations() with eval_str=True
.
If include_bases is set to True
, annotations from base classes are taken into account as well.
This function will take into account the locals and globals accessible through the frame associated with a function or type by the #scoped() decorator.
Source code in typeapi/utils.py
is_typed_dict
is_typed_dict(hint: Any) -> TypeGuard[TypedDictProtocol]
Returns:
Type | Description |
---|---|
TypeGuard[TypedDictProtocol]
|
|
Note
Typed dictionaries are actually just type objects. This means #typeapi.of() will represent them as