pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Provide a better way to schedule hooks ordering

Open harmin-parra opened this issue 7 months ago • 3 comments

What's the problem this feature will solve?

Provide a better way to fine-tune the order in which different hooks are executed.

Currently, the only way to control the scheduling is through the following parameters of the @pytest.hookimpl marker:

- tryfirst=True
- tryfirst=False
- trylast=True
- trylast=False

What if I want my plugin hook to be executed after plugin-A and plugin-B but before plugin-X and plugin-Y hooks ?

Describe the solution you'd like

Difficult to say what the ideal solution might be. Maybe something like this ?

@pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y'])

Of course, ideally we should check if the other plugins are registered/installed. If a plugin is not registered, then the hook condition should be considered as being fulfilled.

example: If plugin-A is not registered/installed: @pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y']) should behave like @pytest.hookimpl(after=['plugin-B'], before=['plugin-X', 'plugin-Y'])

Another consideration: If there is an impossible order to achieve, let say plugin-X asks its hooks to be executed before plugin-A hooks, then @pytest.hookimpl(after=['plugin-A', 'plugin-B'], before=['plugin-X', 'plugin-Y']) cannot be honored and we should display a message in the stderr output. Maybe also abort the execution ? or let the execution but ignoring the hook ordering ?

Alternative Solutions

Additional context

harmin-parra avatar Apr 12 '25 23:04 harmin-parra

I don't think this is worth the complexity.

We have four describable cases: before, after, not-after, and not-before - where the first two require the referenced plugin to be loaded, and the latter do not.

  • a before b (or symetrically b after a): a must be doing some important setup for b (else this would be not-after); b should explicitly import and use that code if necessary (and arrange for idempotence or a setup_if_needed method).
  • a not-after b, and b not-before a: b is presumably using or polluting some state which a requires. IMO a should have better isolation, or b is being a poor ecosystem citizen and users should disable it.

Zac-HD avatar Apr 12 '25 23:04 Zac-HD

This os a issue for pluggy

Having more order controls is a long standing issue

RonnyPfannschmidt avatar Apr 13 '25 03:04 RonnyPfannschmidt

This may be a dupe of #2364

RonnyPfannschmidt avatar Apr 13 '25 03:04 RonnyPfannschmidt