from typing import Union
from typing import KeysView
from typing import Sequence
from typing import Optional


def _get_valid_keys(
    inputs_keys: Union[KeysView, Sequence[str]],
    use_keys: Optional[Union[KeysView, Sequence[str]]],
    ignore_keys: Optional[Union[KeysView, Sequence[str]]]
) -> Union[KeysView, Sequence[str]]:
    """Get only the valid keys from the input.

    Basically, it return use_keys, if not None. Otherwise, return inputs_keys after removing the keys which are in ignore_keys.

    Parameters
    ----------
    inputs_keys : Union[KeysView, Sequence[str]]
        All the keys available as input.
    use_keys : Optional[Union[KeysView, Sequence[str]]]
        If not None, then just use these keys.
    ignore_keys : Optional[Union[KeysView, Sequence[str]]]
        If not None, remove these keys from the inputs_keys.

    Returns
    -------
    Union[KeysView, Sequence[str]]
        The keys remaining after the validity checks.
    """
    if use_keys is not None:
        return use_keys
    if ignore_keys is None:
        return inputs_keys
    return [k for k in inputs_keys if k not in ignore_keys]