A potential bug in HierarchicalMachine's logic

Author: only1ahmedCreated Mar 10, 2026Updated Mar 10, 2026

Hello, can you please explain to me why the variable "remap" is being overwritten instead of creating a new one (e.g. state_remap)

on line 965 in "extensions/nesting.py"

    def _add_dict_state(self, state, ignore_invalid_triggers, remap, **kwargs):
        if remap is not None and state['name'] in remap:
            return
        state = state.copy()  # prevent messing with the initially passed dict
        remap = state.pop('remap', None)

Is this intended? the passed remap is the parent's remap that was "popped" out before looping thro states. on line 510 in "extensions/nesting.py"

        remap = kwargs.pop('remap', None)
        ignore = self.ignore_invalid_triggers if ignore_invalid_triggers is None else ignore_invalid_triggers

        for state in listify(states):
            if isinstance(state, Enum):
                if isinstance(state.value, EnumMeta):
                    state = {'name': state, 'children': state.value}
                elif isinstance(state.value, dict):
                    state = dict(name=state, **state.value)
            if isinstance(state, string_types):
                self._add_string_state(state, on_enter, on_exit, ignore, remap, **kwargs)
            elif isinstance(state, Enum):
                self._add_enum_state(state, on_enter, on_exit, ignore, remap, **kwargs)
            elif isinstance(state, dict):
                self._add_dict_state(state, ignore, remap, **kwargs)
            elif isinstance(state, NestedState):
                if state.name in self.states:
                    raise ValueError("State {0} cannot be added since it already exists.".format(state.name))
                self.states[state.name] = state
                self._init_state(state)
            elif isinstance(state, HierarchicalMachine):
                self._add_machine_states(state, remap)
            elif isinstance(state, State) and not isinstance(state, NestedState):
                raise ValueError("A passed state object must derive from NestedState! "
                                 "A default State object is not sufficient")
            else:
                raise ValueError("Cannot add state of type {0}. ".format(type(state).__name__))

Wouldn't this overwritting affect sibiling states and lead to unintended behaviour?

Source: pytransitions/transitions