aiq.utils.type_converter#

Attributes#

Exceptions#

ConvertException

Common base class for all non-exit exceptions.

Classes#

Functions#

Module Contents#

logger#
_T#
exception ConvertException#

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.

class TypeConverter(
converters: list[collections.abc.Callable[[Any], Any]],
parent: TypeConverter | None = None,
)#
Parameters:
  • converters – A list of single-argument converter callables annotated with their input param and return type.

  • parent – An optional parent TypeConverter for fallback.

_global_initialized = False#
_converters: collections.OrderedDict[type, collections.OrderedDict[type, collections.abc.Callable]]#
_indirect_warnings_shown: set[tuple[type, type]]#
_parent = None#
add_converter(converter: collections.abc.Callable) None#

Registers a converter. Must have exactly one parameter and an annotated return type.

try_convert(data, to_type: type[_T]) _T | None#

Attempts to convert data into to_type. Returns None if no path is found.

convert(data, to_type: type[_T]) _T#

Converts or raises ValueError if no path is found. We also give the parent a chance if self fails.

_try_direct_conversion(data, target_root_type: type) Any | None#

Tries direct conversion in this converter’s registry. If no match here, we forward to parent’s direct conversion for recursion up the chain.

_try_indirect_convert(data, to_type: type[_T]) _T | None#

Attempt indirect conversion (DFS) in this converter. If no success, fallback to parent’s indirect attempt.

_try_indirect_conversion(
data: Any,
to_type: type[_T],
visited: set[type],
) _T | None#

DFS attempt to find a chain of conversions from type(data) to to_type, ignoring parent. If not found, returns None.

_maybe_warn_indirect(source_type: type, to_type: type)#

Warn once if an indirect path was used between these two types.

class GlobalTypeConverter#
_global_converter: TypeConverter#
static get() TypeConverter#
static register_converter(converter: collections.abc.Callable) None#
static convert(data, to_type: type[_T]) _T#
_text_io_wrapper_to_string(data: io.TextIOWrapper) str#