From a7e26372d14c5114d9dd7c14484537fcad9b1203 Mon Sep 17 00:00:00 2001 From: Remco de Boer Date: Fri, 18 Feb 2022 10:25:43 +0100 Subject: [PATCH] fix: make ReactionInfo hashable (#151) --- src/qrules/transition.py | 6 +++--- tests/unit/test_transition.py | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/qrules/transition.py b/src/qrules/transition.py index f334216d..3a9e7a0a 100644 --- a/src/qrules/transition.py +++ b/src/qrules/transition.py @@ -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) @@ -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) diff --git a/tests/unit/test_transition.py b/tests/unit/test_transition.py index c79e94ae..02dc7b3c 100644 --- a/tests/unit/test_transition.py +++ b/tests/unit/test_transition.py @@ -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(