apifrom.utils.type_utils

Type utilities for APIFromAnything.

This module provides utilities for working with Python types and type hints.

Overview

Functions

Functions

apifrom.utils.type_utils.extract_optional_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

Type

Extract the inner type from an Optional type hint.

For types like Optional[int], this returns int.

param type_hint:

The type hint to extract from.

returns:

The inner type.

apifrom.utils.type_utils.get_args(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

Tuple[Type, Ellipsis]

Get the arguments of a type hint.

For types like List[int], this returns (int,). For types like Dict[str, int], this returns (str, int).

param type_hint:

The type hint to get the arguments for.

returns:

A tuple of type arguments.

apifrom.utils.type_utils.get_inner_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

Type

Get the inner type of a generic type hint.

For types like List[int], this returns int. For types like Dict[str, int], this returns (str, int).

param type_hint:

The type hint to get the inner type for.

returns:

The inner type, or a tuple of inner types for Dict.

apifrom.utils.type_utils.get_origin(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

Optional[Type]

Get the origin of a type hint.

For types like List[int], this returns List. For types like Dict[str, int], this returns Dict.

param type_hint:

The type hint to get the origin for.

returns:

The origin type, or None if not a generic type.

apifrom.utils.type_utils.get_origin_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

Type

Get the origin type of a type hint.

For generic types like List[int], this returns List. For Optional types like Optional[int], this returns Union.

param type_hint:

The type hint to get the origin type for.

returns:

The origin type.

apifrom.utils.type_utils.get_type_hints_with_extras(obj)[source]
Parameters:

obj (Callable)

Return type:

Dict[str, Type]

Get type hints for a callable, including return type.

This function extends typing.get_type_hints to include additional information about the types, such as whether they are optional.

param obj:

The callable to get type hints for.

returns:

A dictionary mapping parameter names to their types.

apifrom.utils.type_utils.get_union_types(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

List[Type]

Get the types in a Union type hint.

param type_hint:

The type hint to get the types from.

returns:

A list of types in the Union.

apifrom.utils.type_utils.is_builtin_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is a builtin type.

param type_hint:

The type hint to check.

returns:

True if the type hint is a builtin type, False otherwise.

apifrom.utils.type_utils.is_dict_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is a dictionary type.

param type_hint:

The type hint to check.

returns:

True if the type hint is a dictionary type, False otherwise.

apifrom.utils.type_utils.is_json_serializable(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is JSON serializable.

param type_hint:

The type hint to check.

returns:

True if the type hint is JSON serializable, False otherwise.

apifrom.utils.type_utils.is_list_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is a list type.

param type_hint:

The type hint to check.

returns:

True if the type hint is a list type, False otherwise.

apifrom.utils.type_utils.is_optional(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is Optional.

param type_hint:

The type hint to check.

returns:

True if the type hint is Optional, False otherwise.

apifrom.utils.type_utils.is_optional_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is Optional.

param type_hint:

The type hint to check.

returns:

True if the type hint is Optional, False otherwise.

apifrom.utils.type_utils.is_union_type(type_hint)[source]
Parameters:

type_hint (Type)

Return type:

bool

Check if a type hint is a Union type.

param type_hint:

The type hint to check.

returns:

True if the type hint is a Union type, False otherwise.