Skip to content

fix: key instance states by state.value, not state.id (#624)#625

Open
AndreyNenashev wants to merge 1 commit into
fgmacedo:developfrom
AndreyNenashev:bugfix/issues-624
Open

fix: key instance states by state.value, not state.id (#624)#625
AndreyNenashev wants to merge 1 commit into
fgmacedo:developfrom
AndreyNenashev:bugfix/issues-624

Conversation

@AndreyNenashev
Copy link
Copy Markdown

@AndreyNenashev AndreyNenashev commented May 27, 2026

Summary

Restores the 3.0.0 active-state resolution path that PR #592 inadvertently changed. Configuration._instance_states is re-keyed by state.value (the canonical identifier already used by states_map) instead of state.id (the Python attribute name, which collides across sibling Compounds with same-named children).

Closes #624.

Change

  • statemachine/statemachine.py:211_build_configuration writes instance_states[state.value] = ist.
  • statemachine/configuration.py:87Configuration.states resolves directly: self._instance_states[v] (drops the redundant _states_map[v].id hop).
  • statemachine/configuration.py:36 — type hint loosened to Mapping[Any, State] to match the established dict[Any, State] convention used by states_map.

The vars(self)[state.id] = ist user-facing attribute shortcut at statemachine.py:213 is untouched — orthogonal concern.

Tests

New TestSiblingCompoundIdCollision class in tests/test_configuration.py:

Full suite: 1472 passed, 144 skipped, 44 xfailed in ~23s. Branch coverage on statemachine.configuration and statemachine.statemachine preserved.

Out of scope — deeper issue surfaced during review

Charts that rely on default value= and reuse Python attribute names across sibling Compounds were never supported, in any version:

class g1(State.Compound):
    a = State(initial=True)   # value defaults to "a"
class g2(State.Compound):
    a = State(initial=True)   # value defaults to "a" — overwrites g1.a in states_map

factory.py:341 (cls.states_map[state.value] = state) silently overwrites. The chart constructs successfully but states_map has fewer entries than declared states. This predates #592 and is not a regression — it cannot be reached or repaired at the _instance_states layer.

Tracking as separate follow-ups:

  • Validate uniqueness at class-build time — raise InvalidDefinition in factory.add_state when state.value collides. Surfaces the constraint instead of silently overwriting. Backward-incompatible (charts that "worked by accident" will refuse to construct); should land in a minor release with notes.
  • Auto-qualify nested state.id (SCXML-style globally-unique ids) — would also make the default-value case work, but touches __repr__, diagrams, and callback name resolution. Larger design discussion.

PR fgmacedo#592 introduced ``Configuration._instance_states`` as a perf
optimization but keyed it by ``state.id`` — the Python attribute
name, which is not unique across sibling Compounds. When two
``State.Compound`` regions declared inner states under the same
attribute name (e.g. ``g1.a`` and ``g2.a``), the second overwrote
the first in the dict, causing dispatch to resolve against the
wrong active state and raise ``TransitionNotAllowed``.

Re-key ``_instance_states`` by ``state.value`` — the canonical
identifier already used as the ``states_map`` key and guaranteed
unique by construction when users provide explicit ``value=``.
This restores the 3.0.0 resolution path that PR fgmacedo#592 inadvertently
changed.

Charts that rely on default ``value=`` and reuse Python attribute
names across sibling Compounds were never supported (silent
``states_map`` overwrite at class-build time, predates fgmacedo#592).
Surfacing that case as ``InvalidDefinition`` is tracked as a
separate follow-up.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Andrey Nenashev <anenashev90@gmail.com>
@sonarqubecloud
Copy link
Copy Markdown

@AndreyNenashev AndreyNenashev marked this pull request as ready for review May 27, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sibling Compound states with same-named inner attributes break in 3.1.0+ (regression bisected to #592)

1 participant