def _merge_dicts_and_lists_b_into_a(
    a: dict[Any, Any] | list[Any],
    b: dict[Any, Any] | list[Any],
    cur_key: int | str | None = None,
) -> dict[Any, Any] | list[Any]:
    """Recursively merge two configuration dictionaries or lists.

    This function implements the following merge behavior:
    - Works with standard dicts, lists and their derived types
    - Merges lists by concatenation
    - Performs recursive merging for nested dictionaries
    - Overwrites scalar values when merging

    Args:
        a (dict[Any, Any] | list[Any]): First dictionary or list to merge.
        b (dict[Any, Any] | list[Any]): Second dictionary or list to merge into first.
        cur_key (int | str | None, optional): Current key in recursive merge. Defaults to None.

    Returns:
        dict[Any, Any] | list[Any]: Merged configuration.

    Raises:
        TypeError: If inputs are not dictionaries or lists, or if types are incompatible.

    Example:
        >>> a = {"x": {"y": [1]}}
        >>> b = {"x": {"y": [2]}}
        >>> result = _merge_dicts_and_lists_b_into_a(a, b)
        >>> result["x"]["y"]
        [1, 2]
    """

    def _err_str(_a: dict | list, _b: dict | list, _key: int | str | None = None) -> str:
        _key_str = "of whole structures" if _key is None else f"during merging for key=`{_key}`"
        return (
            f"Error in merging parts of config: different types {_key_str}, type(a) = {type(_a)}, type(b) = {type(_b)}"
        )

    if not (isinstance(a, dict | list)):
        msg = f"Can merge only dicts and lists, whereas type(a)={type(a)}"
        raise TypeError(msg)

    if not (isinstance(b, dict | list)):
        raise TypeError(_err_str(a, b, cur_key))

    if (isinstance(a, list) and not isinstance(b, list)) or (isinstance(b, list) and not isinstance(a, list)):
        raise TypeError(_err_str(a, b, cur_key))

    if isinstance(a, list) and isinstance(b, list):
        # the main diff w.r.t. mmcf.Config -- merging of lists
        return a + b

    a = copy(a)
    for k in b:
        if k not in a:
            a[k] = copy(b[k])
            continue
        new_cur_key = str(cur_key) + "." + k if cur_key else k
        if isinstance(a[k], dict | list):
            a[k] = _merge_dicts_and_lists_b_into_a(a[k], b[k], new_cur_key)
            continue

        if any(isinstance(b[k], t) for t in [dict, list]):
            raise TypeError(_err_str(a[k], b[k], new_cur_key))

        # suppose here that a[k] and b[k] are scalars, just overwrite
        a[k] = b[k]
    return a
Recursively merge two configuration dictionaries or lists.

    This function implements the following merge behavior:
    - Works with standard dicts, lists and their derived types
    - Merges lists by concatenation
    - Performs recursive merging for nested dictionaries
    - Overwrites scalar values when merging

    Args:
        a (dict[Any, Any] | list[Any]): First dictionary or list to merge.
        b (dict[Any, Any] | list[Any]): Second dictionary or list to merge into first.
        cur_key (int | str | None, optional): Current key in recursive merge. Defaults to None.

    Returns:
        dict[Any, Any] | list[Any]: Merged configuration.

    Raises:
        TypeError: If inputs are not dictionaries or lists, or if types are incompatible.

    Example:
        >>> a = {"x": {"y": [1]}}
        >>> b = {"x": {"y": [2]}}
        >>> result = _merge_dicts_and_lists_b_into_a(a, b)
        >>> result["x"]["y"]
        [1, 2]
    
src/anomalib/callbacks/nncf/utils.py