-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #464 from bluetech/hookwrapper-teardown-warning
Warn when old-style hookwrapper raises during teardown
- Loading branch information
Showing
7 changed files
with
130 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
A warning :class:`~pluggy.PluggyTeardownRaisedWarning` is now issued when an old-style hookwrapper raises an exception during teardown. | ||
See the warning documentation for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import final | ||
|
||
|
||
class PluggyWarning(UserWarning): | ||
"""Base class for all warnings emitted by pluggy.""" | ||
|
||
__module__ = "pluggy" | ||
|
||
|
||
@final | ||
class PluggyTeardownRaisedWarning(PluggyWarning): | ||
"""A plugin raised an exception during an :ref:`old-style hookwrapper | ||
<old_style_hookwrappers>` teardown. | ||
Such exceptions are not handled by pluggy, and may cause subsequent | ||
teardowns to be executed at unexpected times, or be skipped entirely. | ||
This is an issue in the plugin implementation. | ||
If the exception is unintended, fix the underlying cause. | ||
If the exception is intended, switch to :ref:`new-style hook wrappers | ||
<hookwrappers>`, or use :func:`result.force_exception() | ||
<pluggy.Result.force_exception>` to set the exception instead of raising. | ||
""" | ||
|
||
__module__ = "pluggy" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pluggy import HookimplMarker | ||
from pluggy import HookspecMarker | ||
from pluggy import PluggyTeardownRaisedWarning | ||
from pluggy import PluginManager | ||
|
||
|
||
hookspec = HookspecMarker("example") | ||
hookimpl = HookimplMarker("example") | ||
|
||
|
||
def test_teardown_raised_warning(pm: PluginManager) -> None: | ||
class Api: | ||
@hookspec | ||
def my_hook(self): | ||
raise NotImplementedError() | ||
|
||
pm.add_hookspecs(Api) | ||
|
||
class Plugin1: | ||
@hookimpl | ||
def my_hook(self): | ||
pass | ||
|
||
class Plugin2: | ||
@hookimpl(hookwrapper=True) | ||
def my_hook(self): | ||
yield | ||
1 / 0 | ||
|
||
class Plugin3: | ||
@hookimpl(hookwrapper=True) | ||
def my_hook(self): | ||
yield | ||
|
||
pm.register(Plugin1(), "plugin1") | ||
pm.register(Plugin2(), "plugin2") | ||
pm.register(Plugin3(), "plugin3") | ||
with pytest.warns( | ||
PluggyTeardownRaisedWarning, | ||
match=r"\bplugin2\b.*\bmy_hook\b.*\n.*ZeroDivisionError", | ||
) as wc: | ||
with pytest.raises(ZeroDivisionError): | ||
pm.hook.my_hook() | ||
assert len(wc.list) == 1 | ||
assert Path(wc.list[0].filename).name == "test_warnings.py" |