Skip to content

Commit

Permalink
fix: make ReactionInfo hashable (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Feb 18, 2022
1 parent 43152f2 commit a7e2637
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/qrules/transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,14 +935,14 @@ def _to_tuple(
return tuple(iterable)


@attr.frozen(eq=False)
@attr.frozen(eq=False, hash=True)
class ReactionInfo:
"""`StateTransitionCollection` instances, grouped by `.Topology`."""

transition_groups: Tuple[StateTransitionCollection, ...] = attr.ib(
converter=_to_tuple
)
transitions: List[StateTransition] = attr.ib(
transitions: Tuple[StateTransition, ...] = attr.ib(
init=False, repr=False, eq=False
)
initial_state: FrozenDict[int, Particle] = attr.ib(init=False, repr=False)
Expand All @@ -958,7 +958,7 @@ def __attrs_post_init__(self) -> None:
for grouping in self.transition_groups:
transitions.extend(sorted(grouping))
first_grouping = self.transition_groups[0]
object.__setattr__(self, "transitions", transitions)
object.__setattr__(self, "transitions", tuple(transitions))
object.__setattr__(self, "final_state", first_grouping.final_state)
object.__setattr__(self, "initial_state", first_grouping.initial_state)

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_transition.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def test_from_to_graphs(self, reaction: ReactionInfo):
from_graphs = ReactionInfo.from_graphs(graphs, reaction.formalism)
assert from_graphs == reaction

def test_hash(self, reaction: ReactionInfo):
graphs = reaction.to_graphs()
from_graphs = ReactionInfo.from_graphs(graphs, reaction.formalism)
assert hash(from_graphs) == hash(reaction)


class TestState:
@pytest.mark.parametrize(
Expand Down

0 comments on commit a7e2637

Please sign in to comment.