Skip to content

feat: propagate constructor kwargs to initial state callbacks#572

Merged
fgmacedo merged 1 commit intodevelopfrom
macedo/init-kwargs-propagation
Feb 19, 2026
Merged

feat: propagate constructor kwargs to initial state callbacks#572
fgmacedo merged 1 commit intodevelopfrom
macedo/init-kwargs-propagation

Conversation

@fgmacedo
Copy link
Owner

Summary

  • Forward **kwargs from StateChart.__init__() through the engine's __initial__ event, making them available to on_enter_<state>, invoke handlers, and all initial-entry callbacks via dependency injection
  • Self-contained machines can now receive context at creation time:
class Greeter(StateChart):
    idle = State(initial=True)
    done = State(final=True)
    idle.to(done)

    def on_enter_idle(self, name=None, **kwargs):
        self.greeting = f"Hello, {name}!"

sm = Greeter(name="Alice")
sm.greeting  # "Hello, Alice!"
  • Especially useful for invoke handlers on initial states:
class AppLoader(StateChart):
    loading = State(initial=True)
    ready = State(final=True)
    done_invoke_loading = loading.to(ready)

    def on_invoke_loading(self, config_path=None, **kwargs):
        return json.loads(Path(config_path).read_text())

sm = AppLoader(config_path="/etc/app.json")

Changes

  • StateChart.__init__() passes **kwargs to engine.start()
  • BaseEngine.start(), SyncEngine.start()/activate_initial_state(), and AsyncEngine.activate_initial_state() accept and forward **kwargs
  • kwargs are stored in TriggerData.kwargs for the __initial__ event, flowing naturally through EventData.extended_kwargs to all callbacks
  • Updated docs/invoke.md and docs/releases/3.0.0.md

Forward **kwargs from StateChart.__init__() through the engine's initial
event (TriggerData), making them available to on_enter_<initial_state>,
invoke handlers, and other initial-entry callbacks via dependency injection.

This enables self-contained machines to receive context at creation time,
e.g. `MyMachine(url="...", config=config)`.
@sonarqubecloud
Copy link

@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #572   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           33        33           
  Lines         4044      4044           
  Branches       635       635           
=========================================
  Hits          4044      4044           
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 Sentry.
📢 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 fgmacedo merged commit 6f2b617 into develop Feb 19, 2026
14 checks passed
@fgmacedo fgmacedo deleted the macedo/init-kwargs-propagation branch February 19, 2026 12:44
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.

1 participant

Comments