from typing import Optional
from typing import Union
from typing import List


def _check_pattern_list(
    patterns: Optional[Union[List[str], str]],
    key: str,
    default: Optional[List[str]] = None,
) -> Optional[List[str]]:
    """Validate file search patterns from user configuration.

    Acceptable input is a string (which will be converted to a singleton list),
    a list of strings, or anything falsy (such as None or an empty dictionary).
    Empty or unset input will be converted to a default.

    Args:
        patterns: Input from user configuration (YAML).
        key (str): Name of the configuration key the input came from,
            used for error display purposes.
        default: Value to return in case the input is empty or unset.

    Returns:
        Validated list of patterns.

    Raises:
        ValueError: If the input is unacceptable.

    """
    if not patterns:
        return default

    if isinstance(patterns, str):
        return [patterns]

    if isinstance(patterns, list) and all(isinstance(p, str) for p in patterns):  # type: ignore
        return patterns

    raise ValueError(
        f"Invalid file patterns in key '{key}': must be a string or " "list of strings"
    )