Skip to content

Commit

Permalink
BaseRestartWorkChain: Fix handler overrides used only first iterati…
Browse files Browse the repository at this point in the history
…on (#6308)

The handler overrides were only being used in the first iteration of the
`BaseRestartWorkChain`. The code was using `pop` to obtain the `enabled`
and `priority` attributes, thereby removing them from the overrides that
are stored in the context. In the following iteration, the original
values wouldn't be there anymore. The fix is to simply replace `pop`
with `get`.
  • Loading branch information
yakutovicha authored Mar 22, 2024
1 parent e665925 commit 65786a6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/aiida/engine/processes/workchains/restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ def get_process_handlers_by_priority(self) -> List[Tuple[int, FunctionType]]:
if isinstance(overrides, bool):
enabled = overrides
else:
enabled = overrides.pop('enabled', None)
priority = overrides.pop('priority', None)
enabled = overrides.get('enabled')
priority = overrides.get('priority')

if enabled is False or (enabled is None and not handler.enabled): # type: ignore[attr-defined]
continue
Expand Down
11 changes: 10 additions & 1 deletion tests/engine/processes/workchains/test_restart.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ def test_get_process_handlers_by_priority(generate_work_chain, inputs, prioritie
warnings.simplefilter('ignore')
process = generate_work_chain(SomeWorkChain, inputs)
process.setup()
assert sorted([priority for priority, handler in process.get_process_handlers_by_priority()]) == priorities
handlers = process.get_process_handlers_by_priority()
assert sorted([priority for priority, _ in handlers]) == priorities

# Verify the actual handlers on the class haven't been modified
assert getattr(SomeWorkChain, 'handler_a').priority == 200
Expand All @@ -82,6 +83,14 @@ def test_get_process_handlers_by_priority(generate_work_chain, inputs, prioritie
assert getattr(SomeWorkChain, 'handler_b').enabled
assert not getattr(SomeWorkChain, 'handler_c').enabled

# Validate that a second invocation return the exact same results. This is a regression test for
# https://github.com/aiidateam/aiida-core/issues/6307. Note that the handlers are compared by name, because the
# bound methods will be separate clones and the ``__eq__`` operation will return ``False`` even though it represents
# the same handler function.
assert [(p, h.__name__) for p, h in process.get_process_handlers_by_priority()] == [
(p, h.__name__) for p, h in handlers
]


@pytest.mark.requires_rmq
def test_excepted_process(generate_work_chain, generate_calculation_node):
Expand Down

0 comments on commit 65786a6

Please sign in to comment.