from __future__ import annotations

import importlib
from typing import Any


def optional_import(module_name: str, attribute: str | None = None) -> Any:
    try:
        module = importlib.import_module(module_name)
    except ImportError:
        return None

    if attribute is None:
        return module
    return getattr(module, attribute, None)


def compact_types(*maybe_types: Any) -> tuple[type[Any], ...]:
    return tuple(item for item in maybe_types if isinstance(item, type))
