Skip to content

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

Merged
fgmacedo merged 1 commit into
fgmacedo:developfrom
AndreyNenashev:bugfix/issues-624
Jun 12, 2026
Merged

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

Conversation

@AndreyNenashev

@AndreyNenashev AndreyNenashev commented May 27, 2026

Copy link
Copy Markdown
Contributor

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
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (34d51cf) to head (88a1e26).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #625   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           42        42           
  Lines         4976      4976           
  Branches       812       812           
=========================================
  Hits          4976      4976           
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fgmacedo

Copy link
Copy Markdown
Owner

Hi @AndreyNenashev, thanks a lot for the exceptionally detailed report and the fix! 🙏

The write-up in #624 was a model bug report: clean repro, correct bisect to #592, and a precise root-cause analysis. I reproduced it on develop and confirmed the fix resolves it on both engines, with the full suite green and no regressions.

The approach is spot on: re-keying _instance_states by state.value aligns it with the canonical identifier already used by states_map, restoring the 3.0.0 resolution path with a minimal, well-targeted change. I also really appreciate you surfacing the deeper default-value= collision as a separate, well-scoped follow-up instead of folding it into this bugfix.

Validating value uniqueness at class-build time (raising InvalidDefinition) is the natural next step, and your offer to put up a PR for it would be very welcome. Thanks again!

@fgmacedo fgmacedo merged commit 10b063f into fgmacedo:develop Jun 12, 2026
10 checks passed
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)

2 participants