Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/806.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A historic hook call now remembers the arguments it was made with instead of the mapping the caller passed in, so mutating that mapping afterwards no longer changes what plugins registered later, or the result callback, receive (`#806 <https://github.com/pytest-dev/pluggy/pull/806>`_).
4 changes: 4 additions & 0 deletions src/pluggy/_caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ def call_historic(
assert self._call_history is not None
kwargs = kwargs or {}
kwargs = self._apply_defaults(kwargs)
# ``kwargs`` may be a mapping the caller keeps and mutates; the call is
# memorized to be replayed, so it has to keep the values it was made
# with.
kwargs = dict(kwargs)
self._verify_all_args_are_provided(kwargs)
self._call_history.append((kwargs, result_callback))
# Historizing hooks don't return results.
Expand Down
31 changes: 31 additions & 0 deletions testing/test_pluginmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,37 @@ def he_method1(self, arg):
assert out == [1, 10, 120, 12]


def test_call_historic_keeps_the_values_the_call_was_made_with(
pm: PluginManager,
) -> None:
"""The memorized call is replayed with the values it was made with, even
when the caller's mapping is mutated after the call."""

class Hooks:
@hookspec(historic=True)
def he_method1(self, arg): ...

pm.add_hookspecs(Hooks)

out = []
kwargs = {"arg": "value-at-call-time"}
pm.hook.he_method1.call_historic(lambda res: out.append(res), kwargs=kwargs)
kwargs["arg"] = "mutated-after-the-call"

seen = []

class Plugin:
@hookimpl
def he_method1(self, arg):
seen.append(arg)
return arg

pm.register(Plugin())

assert seen == ["value-at-call-time"]
assert out == ["value-at-call-time"]


def test_historic_with_subset_hook_caller(pm: PluginManager) -> None:
class Hooks:
@hookspec(historic=True)
Expand Down
Loading