def _convert_nested_path_to_str(config: Any) -> Any:  # noqa: ANN401
    """Convert all path values to strings recursively in a configuration object.

    This function traverses a configuration object and converts any ``Path`` or
    ``JSONArgparsePath`` objects to string representations. It handles nested
    dictionaries and lists recursively.

    Args:
        config: Configuration object that may contain path values. Can be a
            dictionary, list, Path object, or other types.

    Returns:
        Any: Configuration with all path values converted to strings. The returned
            object maintains the same structure as the input, with only path
            values converted to strings.

    Examples:
        >>> from pathlib import Path
        >>> config = {
        ...     "model_path": Path("/path/to/model"),
        ...     "data": {
        ...         "train_path": Path("/data/train"),
        ...         "val_path": Path("/data/val")
        ...     }
        ... }
        >>> converted = _convert_nested_path_to_str(config)
        >>> print(converted["model_path"])
        /path/to/model
        >>> print(converted["data"]["train_path"])
        /data/train
    """
    if isinstance(config, dict):
        for key, value in config.items():
            config[key] = _convert_nested_path_to_str(value)
    elif isinstance(config, list):
        for i, item in enumerate(config):
            config[i] = _convert_nested_path_to_str(item)
    elif isinstance(config, Path | JSONArgparsePath):
        config = str(config)
    return config
Convert all path values to strings recursively in a configuration object.

    This function traverses a configuration object and converts any ``Path`` or
    ``JSONArgparsePath`` objects to string representations. It handles nested
    dictionaries and lists recursively.

    Args:
        config: Configuration object that may contain path values. Can be a
            dictionary, list, Path object, or other types.

    Returns:
        Any: Configuration with all path values converted to strings. The returned
            object maintains the same structure as the input, with only path
            values converted to strings.

    Examples:
        >>> from pathlib import Path
        >>> config = {
        ...     "model_path": Path("/path/to/model"),
        ...     "data": {
        ...         "train_path": Path("/data/train"),
        ...         "val_path": Path("/data/val")
        ...     }
        ... }
        >>> converted = _convert_nested_path_to_str(config)
        >>> print(converted["model_path"])
        /path/to/model
        >>> print(converted["data"]["train_path"])
        /data/train
    
src/anomalib/utils/config.py