open-mmlab/mmdetection · error · TypeError

dataset must a str, but got {type(dataset)}

Error message

dataset must a str, but got {type(dataset)}

What it means

get_classes requires the dataset argument to be a string; any other type (list handling happens earlier, so e.g. None, dict, int) raises TypeError.

Source

Thrown at mmdet/evaluation/functional/class_names.py:761

    'objects365v2': ['objects365v2', 'obj365v2'],
    'lvis': ['lvis', 'lvis_v1'],
}


def get_classes(dataset) -> list:
    """Get class names of a dataset."""
    alias2name = {}
    for name, aliases in dataset_aliases.items():
        for alias in aliases:
            alias2name[alias] = name

    if is_str(dataset):
        if dataset in alias2name:
            labels = eval(alias2name[dataset] + '_classes()')
        else:
            raise ValueError(f'Unrecognized dataset: {dataset}')
    else:
        raise TypeError(f'dataset must a str, but got {type(dataset)}')
    return labels

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Pass a str dataset name or the explicit class list according to the API signature
  2. Normalize config values: ensure classes is str or list[str]
  3. Add an assertion/log before calling get_classes to catch bad types early

Example fix

# before
get_classes(None)
# after
get_classes('coco') if isinstance(ds, str) else list(ds)
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(dataset, str), f'dataset must be str, got {type(dataset)}'

Type guard

def is_dataset_str(x) -> bool:
    return isinstance(x, str)

Try / catch

try:
    labels = get_classes(ds)
except TypeError:
    ds = str(ds); labels = get_classes(ds)

Prevention

When it happens

Trigger: Calling get_classes(None), get_classes(['a','b']) that escapes earlier branches, or a config feeding a non-str value (e.g. CLASSES=None) into get_classes.

Common situations: Config migrations where CLASSES/`classes` kwarg becomes None or a tuple; programmatic metric construction with dynamic dataset variables.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/b96fdb3c04fb4346. Report an issue: GitHub.