Utils
Shared utility functions for indexer submodules.
dict_to_hashable(dict_obj)
Recursively convert a dictionary to a hashable frozenset.
Converts dicts to frozensets and lists to tuples, making the entire structure hashable. Items are sorted to ensure consistent hashable representation regardless of insertion order. This is useful for caching dictionaries with nested structures.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dict_obj
|
dict[str, Any]
|
Dictionary to convert. |
required |
Returns:
| Type | Description |
|---|---|
frozenset[tuple[str, Any]]
|
frozenset[tuple[str, Any]]: Hashable frozenset representation of the dictionary. Each element is a (key, value) pair where nested dicts and lists are recursively converted. Items are sorted by key for consistent hashing. |
filter_kwargs_by_signature(target, kwargs)
Filter kwargs to only include parameters that match the target's signature.
This function performs two operations: 1. Validation: Checks that all required parameters (without default values) are present in the provided kwargs. Raises ValueError if any are missing. 2. Filtering: Removes any kwargs that are not in the target's signature, returning only the valid parameters that can be passed to the target.
The function handles both classes and callables:
- For classes: inspects the __init__ method signature (excluding 'self')
- For callables: inspects the function/method signature directly
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
Type[Any] | Callable[..., Any]
|
The class or callable to inspect for its signature. |
required |
kwargs
|
dict[str, Any]
|
The keyword arguments to filter. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Filtered kwargs containing only parameters accepted by the target's signature. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required parameters (without defaults) are missing from kwargs. |
TypeError
|
If signature inspection fails. |
hashable_to_dict(hashable)
Recursively convert a hashable frozenset back to a dictionary.
Converts frozensets back to dicts and tuples back to lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hashable
|
frozenset[tuple[str, Any]]
|
Hashable frozenset representation to convert. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: Dictionary representation of the hashable structure. |