from typing import List
from typing import Optional


def filter_test_candidates(
    test_candidates: List[str], ignore_paths: List[str], filter_: Optional[str]
) -> List[str]:
    """Filters a series of test candidates down to the final output

    Uses a number of user-provided criteria (filters and ignores) to narrow down the
    list of candidates.

    Args:
        tests_candidates: The output list of test files determined by `filter_test_candidates`
        ignore_paths: Any test files that starts with any paths in this collection are ignored in the output
        filter_: Only test paths that start with this value are included in the output

    Returns:
        The final list of tests to be run. This is the end result of the full dgtest algorithm.

    """
    filtered_tests = []
    for file in test_candidates:
        # Throw out files that are in our ignore list
        if any(file.startswith(path) for path in ignore_paths):
            continue
        # Throw out files that aren't explicitly part of a filter (if supplied)
        if filter_ and not file.startswith(filter_):
            continue
        filtered_tests.append(file)
    return filtered_tests