Skip to content

from_enum in Compound States #606

Closed

Description

@vincedbowen

Just wanted to first say thanks so much for making such an awesome library! 😄

I have been using version 3.0.0 a bunch, and I was wondering if it is possible to use from_enum() in a compound state?

Using an Enum in the compound state:

from enum import Enum, auto
from statemachine import State, StateChart
from statemachine.states import States

class outer_sts(Enum):
    FOO = auto()
    BAR = auto()

class inner_sts(Enum):
    FIZZ = auto()
    BUZZ = auto()

class SC(StateChart):
    baz = States.from_enum(outer_sts, initial = outer_sts.FOO, final = outer_sts.BAR)

    class inner(State.Compound):
        qux = States.from_enum(inner_sts, initial = inner_sts.FIZZ, final = inner_sts.BUZZ)
        fizz_to_buzz = qux.FIZZ.to(qux.BUZZ)
        
    baz_foo_to_inner = baz.FOO.to(inner)
    inner_to_baz_bar = inner.to(baz.BAR)

sm = SC()
print(sm.configuration)
sm.send("baz_foo_to_inner")
print(sm.configuration)
sm.send("fizz_to_buzz")
print(sm.configuration)
sm.send("inner_to_baz_bar")
print(sm.configuration)

Output:

{Foo}
{Inner}
{Inner}
{Bar}

Using Declarative states in the compound state

from enum import Enum, auto
from statemachine import State, StateChart
from statemachine.states import States

class outer_sts(Enum):
    FOO = auto()
    BAR = auto()

class inner_sts(Enum):
    FIZZ = auto()
    BUZZ = auto()

class SC(StateChart):
    baz = States.from_enum(outer_sts, initial = outer_sts.FOO, final = outer_sts.BAR)

    class inner(State.Compound):
        fizz = State("fizz", initial=True)
        buzz = State("buzz", final=True)
        fizz_to_buzz = fizz.to(buzz)
        
    baz_foo_to_inner = baz.FOO.to(inner)
    inner_to_baz_bar = inner.to(baz.BAR)

sm = SC()
print(sm.configuration)
sm.send("baz_foo_to_inner")
print(sm.configuration)
sm.send("fizz_to_buzz")
print(sm.configuration)
sm.send("inner_to_baz_bar")
print(sm.configuration)

Output:

{Foo}
{Inner, fizz}
{Inner, buzz}
{Bar}

This is the output I think I would expect from using an Enum with the child in the configuration. Is this something that is just not supported, or am I using it incorrectly?

Activity

  1. added this to the 3.1.0 milestone on Apr 2, 2026
  2. fgmacedo commented on Apr 2, 2026

    @fgmacedo
    Owner

    Hey @vincedbowen, thanks so much for the kind words and for taking the time to report this with such a clear reproduction! Really appreciate the sponsorship too 🙏

    The root cause was that NestedStateFactory (the metaclass behind State.Compound and State.Parallel) only looked for individual State instances when processing class bodies, so States.from_enum() containers were silently ignored. The fix is up at #607.

    This will be available in the 3.1 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions