Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: expect_override decorator to mark model methods safe for overriding #676

Merged
merged 2 commits into from
Jun 11, 2024

Conversation

aleneum
Copy link
Member

@aleneum aleneum commented Jun 11, 2024

As of now, transition will not decorate models with trigger and convenience functions if they are already defined on the model. The check is done in Machine._checked_assignment. Users had to override _checked_assignment if they wanted to define model methods (e.g. for type checking) and have them overridden by transitions during runtime. This approach is an all or nothing solution. With expect_override, transitions now features an approach where developers can explicitly mark model methods that are intended to be overriden.

from transitions import Machine
from transitions.experimental.decoration import expect_override

class Model:

    @expect_override
    def is_A(self) -> bool:  # will be overridden during initialisation of Machine
        raise RuntimeError("Should be overridden")

    def is_B(self) -> bool:  # will NOT be overridden
        return False

    @expect_override
    def is_C(self) -> bool:  # will be overridden when state C is added later on
        return False

model = Model()

machine = Machine(model, states=["A", "B"], initial="A")

This is a step in the effort to improve the typing support of transitions.

Some related issues:

@coveralls
Copy link

Coverage Status

coverage: 98.528% (+0.002%) from 98.526%
when pulling 17b4ca1 on dev-experimental-decorator
into 4a3f06a on master.

@aleneum aleneum merged commit 4a611dd into master Jun 11, 2024
9 checks passed
@aleneum aleneum deleted the dev-experimental-decorator branch June 11, 2024 16:33
aleneum added a commit that referenced this pull request Jun 12, 2024
…er` and `with_trigger_decorator`

expect_override (introduced recently in #676) was revoked as a heterogenous model structure is not to be expecred
aleneum added a commit that referenced this pull request Jun 12, 2024
…er` and `with_trigger_decorator`

expect_override (introduced recently in #676) was revoked as a heterogenous model structure is not to be expecred
aleneum added a commit that referenced this pull request Jun 18, 2024
Scope of expect_override was too narrow and does not solve the typing issue adequately
this reverts #676
aleneum added a commit that referenced this pull request Jun 18, 2024
Scope of expect_override was too narrow and does not solve the typing issue adequately
this reverts #676
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.

2 participants