from typing import Optional
from typing import Sequence
from typing import Dict
import dataclasses


def asdict_filtered(obj, remove_keys: Optional[Sequence[str]] = None) -> Dict:
    """Returns the attributes of a dataclass in the form of a dict, with unwanted attributes removed.
    Each config group has the term 'name', which is helpful in identifying the node that was chosen
    in the config group (Eg. config group = optimizers, nodes = adam, sgd).
    However, the 'name' parameter is not required for initializing any dataclasses. Hence it needs to be removed.

    Args:
        obj: The dataclass whose atrributes will be converted to dict
        remove_keys: The keys to remove from the dict. The default is ['name'].
    """
    if not dataclasses.is_dataclass(obj):
        raise ValueError(f"Not a dataclass/dataclass instance")

    if remove_keys is None:
        remove_keys = ["name"]

    # Clean the arguments
    args = dataclasses.asdict(obj)
    for key in remove_keys:
        if key in args:
            args.pop(key)

    return args