Skip to content

A nested state body cannot bind class attributes, so explicit id and transition-less Event are dropped #656

Description

@fgmacedo

NestedStateFactory.__new__ runs while the State.Compound / State.Parallel class body is
evaluated, which is before the owning statechart class exists. It has no cls, so it cannot do
the setattr that the top-level path does in StateMachineMetaclass.add_from_attributes.

Two documented differences follow from that, both introduced with the #643 fix in #645:

class ExplicitId(StateChart):
    class shire(State.Compound):
        bag_end = State(initial=True)
        green_dragon = State(final=True)

        visit_pub = Event(bag_end.to(green_dragon), id="pub.visit")

ExplicitId.events        # ['pub.visit'] -- registered
sm.send("pub.visit")     # works
ExplicitId.visit_pub     # AttributeError -- the top level would bind this
class Placeholder(StateChart):
    class shire(State.Compound):
        bag_end = State(initial=True)
        green_dragon = State(final=True)

        visit_pub = bag_end.to(green_dragon)
        knock = Event(name="Knock on the door")

Placeholder.knock        # AttributeError -- at the top level it is a class attribute

Both are pinned by tests in tests/test_statechart_compound.py
(test_explicit_id_wins_over_the_attribute_name, test_transition_less_event_declares_nothing).

The fix is #657: the nested body stops translating and stashes its raw
declarations on the State, and the metaclass reads them from add_state, where it has both
cls and the owning state.

When fixing: remove the note at the end of the Event section in docs/events.md and the
"two differences from the top-level form remain" paragraph in docs/releases/3.2.2.md, invert
the two tests above, and add an entry to the open release notes.

Activity

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