Skip to content

Subclassing a StateChart whose nested body has callbacks raises AttributeError on _callbacks #658

Description

@fgmacedo

Measured on develop:

class Base(StateChart):
    class c(State.Compound):
        a = State(initial=True)
        b = State(final=True)
        go = a.to(b)

        def on_enter_b(self):
            pass

class Sub(Base):
    pass

# AttributeError: 'State' object has no attribute '_callbacks'

add_inherited reuses the same State objects from the base class (cls.add_state(state.id, state)
over base.states), while _unpack_builders_callbacks consumes them destructively:

for state in iterate_states(cls.states):
    if state._callbacks:
        callbacks.update(state._callbacks)
        del state._callbacks

So the base class deletes the attribute, and the subclass reads it on the shared object and
crashes. It only triggers when a nested body actually declares a callback, which is why it has
gone unnoticed.

The _callbacks bag exists because NestedStateFactory has no owning class to bind to when the
nested body is evaluated, so it hands everything back for the metaclass to flatten afterwards.
#657 removes that bag entirely, which removes this failure along with it. A narrower fix is to
stop mutating shared state: read _callbacks without deleting it, or give the subclass its own
copy of the inherited State.

Found by an adversarial review of #645, unrelated to that change.

When fixing: add a subclassing example to docs/statecharts.md or docs/models.md if none
covers inheritance with nested bodies, and add an entry to the open release notes.

Activity

  1. fgmacedo commented on Sep 13, 2026

    @fgmacedo
    OwnerAuthor

    Duplicate of #646, which reported this first and has the same reproduction. The mechanism analysis has been moved there.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions