Coverage for src/retrospeedlab/search/environment/levels.py: 100%
18 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 02:12 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-12-04 02:12 +0000
1from collections.abc import Sequence
2from typing import Any
4from retrospeedlab.search.states.state import State
6Levels = tuple[tuple[str, tuple[type[State[Any]], ...]], ...]
9def level_map(levels: Levels, state_classes: Sequence[type[State[Any]]]) -> dict[str, tuple[str, ...]]:
10 names = [state_cls.__name__ for state_cls in state_classes]
11 mapped: dict[str, tuple[str, ...]] = {}
12 for level, classes in levels:
13 members = tuple(state_cls.__name__ for state_cls in classes)
14 if level in names:
15 raise ValueError(f"Level {level} has the name of a state")
16 if not members or any(member not in names for member in members):
17 raise ValueError(f"Every state of {level} must be one of state_classes")
18 start = names.index(members[0])
19 if tuple(names[start : start + len(members)]) != members:
20 raise ValueError(f"The states of {level} must follow each other in state_classes")
21 mapped[level] = members
22 return mapped