stouputils.typing module#

This module provides utilities for typing enhancements such as JSON type aliases:

type JsonDict = dict[str, Any]#

A type alias for JSON dictionaries

type JsonList = list[Any]#

A type alias for JSON lists

type JsonMap = Mapping[str, Any]#

A type alias for JSON mapping

type JsonMutMap = MutableMapping[str, Any]#

A type alias for mutable JSON mapping

type IterAny = Iterable[Any]#

A type alias for iterable of any type

type CallableAny = Callable[[...], Any]#

A type alias for any callable

type ClassInfo = type[Any] | UnionType | GenericAlias | tuple[ClassInfo, ...]#

A type alias for class information used in isinstance checks, including unions and tuples of classes

is_generic_instance(
obj: Any,
type_hint: type[T],
) TypeGuard[T][source]#
is_generic_instance(
obj: Any,
type_hint: UnionType,
) TypeGuard[Any]
is_generic_instance(
obj: Any,
type_hint: T,
) TypeGuard[T]
is_generic_instance(
obj: Any,
type_hint: T,
) TypeGuard[T]
is_generic_instance(
obj: Any,
type_hint: Any,
) TypeGuard[Any]

Runtime equivalent of isinstance() for generic type hints. If you want to check types in a dict, you can use this function is_generic_instance(my_dict, dict[str, int]) to check if my_dict is a dictionary with string keys and integer values.

### Note: this function is not a perfect replacement for static type checking and may not cover all edge cases or complex type hints.

Parameters:
  • obj (Any) – The object to check.

  • type_hint (Any) – The type hint to check against.

Returns:

True if obj matches type_hint, False otherwise.

Examples

>>> is_generic_instance(5, int)
True
>>> is_generic_instance("hello", str)
True
>>> is_generic_instance([1, 2, 3], list)
True
>>> is_generic_instance([1, 2, 3], list[int])
True
>>> is_generic_instance([1, 2, 3], list[str])
False
>>> is_generic_instance({"a": 1}, dict[str, int])
True
>>> is_generic_instance({"a": 1}, dict[str, int] | Mapping)
True
>>> is_generic_instance({"a": 1}, dict[str, int] | Mapping | JsonDict)
True
>>> is_generic_instance({"a": 1}, (dict[str, int], Mapping, JsonDict))
True
>>> is_generic_instance([1, 2, 3], dict[str, int])
False
>>> is_generic_instance({"a": 1}, "dict[str, int] | Mapping | JsonDict")
True
convert_to_serializable(obj: Any) Any[source]#

Recursively convert objects to JSON-serializable forms.

Objects with a to_dict() or asdict() method are converted to their dictionary representation. Dictionaries and lists are recursively processed.

Can also be used to convert nested structures containing custom objects, such as defaultdict, dataclasses, or other user-defined types.

Parameters:

obj (Any) – The object to convert

Returns:

The JSON-serializable version of the object

Return type:

Any

Examples

>>> from typing import defaultdict
>>> my_dict = defaultdict(lambda: defaultdict(int))
>>> my_dict['a']['b'] += 6
>>> my_dict['c']['d'] = 4
>>> my_dict['a']
defaultdict(<class 'int'>, {'b': 6})
>>> my_dict['c']
defaultdict(<class 'int'>, {'d': 4})
>>> convert_to_serializable(my_dict)
{'a': {'b': 6}, 'c': {'d': 4}}
>>> from dataclasses import dataclass
>>> @dataclass
... class Point:
...     x: int
...     y: int
...     some_list: list[int]
>>> convert_to_serializable(Point(3, 4, [1, 2, 3]))
{'x': 3, 'y': 4, 'some_list': [1, 2, 3]}