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

States lost when Subclassing a statemachine class with states #211

Closed
joshuacc1 opened this issue May 16, 2019 · 1 comment
Closed

States lost when Subclassing a statemachine class with states #211

joshuacc1 opened this issue May 16, 2019 · 1 comment

Comments

@joshuacc1
Copy link

joshuacc1 commented May 16, 2019

  • Python State Machine version:
  • Python version:
  • Operating System:

Description

Describe what you were trying to get done.
Tell us what happened, what went wrong, and what you expected to happen.

Say we have a statemachine class and we subclass it:

class TrafficLightMachine(StateMachine):
    def __init__(self):
        StateMachine.__init__(self)

    green = State('green', initial=True)
    yellow = State('yellow')
    red = State('red')

    slowdown = green.to(yellow)
    stop = yellow.to(red)
    go = red.to(green)

class ExtTrafficLightMachine(TrafficLightMachine):
    def __init__(self):
        TrafficLightMachine.__init__(self)

tlm = ExtTrafficLightMachine()

The code above would result in an error:
"statemachine.exceptions.InvalidDefinition: There are no states". Now the attributes in ExtTrafficLightMachine contain the three states and transitions, but some how the subclass initialization fails to varify it.

What I Did

Workaround:

class ExtTrafficLightMachine():
    def __init__(self):
        self.statemachine = TrafficLightMachine()

    def __getattr__(self,item):
        return getattr(self.statemachine,item) #I know this doesn't capture any attributes in the ExtTrafficLightMachine class and needs flow control, but this should be good enough to point out the issue.
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
rschrader pushed a commit to rschrader/python-statemachine that referenced this issue Oct 19, 2019
When defining states and transitions in a base class, they are not
detected by the sub class. Therefore The StateMachinMetaClass is
extended to also take care of the states and transitions of the base
classes

Issue: fgmacedo#211
rschrader pushed a commit to rschrader/python-statemachine that referenced this issue Oct 19, 2019
When defining states and transitions in a base class, they are not
detected by the sub class. Therefore The StateMachinMetaClass is
extended to also take care of the states and transitions of the base
classes

Issue: fgmacedo#211
rschrader pushed a commit to rschrader/python-statemachine that referenced this issue Oct 19, 2019
When defining states and transitions in a base class, they are not
detected by the sub class. Therefore The StateMachinMetaClass is
extended to also take care of the states and transitions of the base
classes

Issue: fgmacedo#211
fgmacedo added a commit that referenced this issue Jan 23, 2020
* Support inheritance for Statemachine classes

When defining states and transitions in a base class, they are not
detected by the sub class. Therefore The StateMachinMetaClass is
extended to also take care of the states and transitions of the base
classes

Issue: #211

* chore(inheritance) Add basic inheritance support. Closes #247

Co-authored-by: rschrader-kostal <58595523+rschrader-kostal@users.noreply.github.com>
@fgmacedo
Copy link
Owner

@joshuacc1 Thanks for your report and sorry for the late reply. This was fixed on #253.

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

No branches or pull requests

2 participants