Skip to content

Commit ffdb73f

Browse files
authored
fix: Fix TransitionList.__or__ mutating itself instead of returning a new instance. Closes #308. (#310)
1 parent f9746c1 commit ffdb73f

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

statemachine/transition_list.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55

66
class TransitionList(object):
7-
def __init__(self, *transitions):
8-
self.transitions = list(*transitions)
7+
def __init__(self, transitions=None):
8+
self.transitions = list(transitions) if transitions else []
99

1010
def __repr__(self):
1111
return "{}({!r})".format(type(self).__name__, self.transitions)
1212

1313
def __or__(self, other):
14-
self.add_transitions(other)
15-
return self
14+
return TransitionList(self.transitions).add_transitions(other)
1615

1716
def add_transitions(self, transition):
1817
if isinstance(transition, TransitionList):

tests/test_transition_list.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from statemachine import State
2+
3+
4+
def test_transition_list_or_operator():
5+
s1 = State("s1", initial=True)
6+
s2 = State("s2")
7+
s3 = State("s3")
8+
s4 = State("s4", final=True)
9+
10+
t12 = s1.to(s2)
11+
t23 = s2.to(s3)
12+
t34 = s3.to(s4)
13+
14+
cycle = t12 | t23 | t34
15+
16+
assert [(t.source.name, t.target.name) for t in t12] == [("s1", "s2")]
17+
assert [(t.source.name, t.target.name) for t in t23] == [("s2", "s3")]
18+
assert [(t.source.name, t.target.name) for t in t34] == [("s3", "s4")]
19+
assert [(t.source.name, t.target.name) for t in cycle] == [
20+
("s1", "s2"),
21+
("s2", "s3"),
22+
("s3", "s4"),
23+
]

0 commit comments

Comments
 (0)