Skip to content

Commit

Permalink
rearrange explicit hook warnings to pass in all data needed for warni…
Browse files Browse the repository at this point in the history
…ng registries
  • Loading branch information
RonnyPfannschmidt committed Sep 23, 2021
1 parent 680a8e2 commit a445d0f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ filterwarnings = [
# Those are caught/handled by pyupgrade, and not easy to filter with the
# module being the filename (with .py removed).
"default:invalid escape sequence:DeprecationWarning",
# ignore not yet fixed warnings for hook markers
"default:.*not marked using pytest.hook.*",
"ignore:.*not marked using pytest.hook.*::xdist.*",
# ignore use of unregistered marks, because we use many to test the implementation
"ignore::_pytest.warning_types.PytestUnknownMarkWarning",
# https://github.com/benjaminp/six/issues/341
Expand Down
21 changes: 5 additions & 16 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from _pytest.pathlib import resolve_package_path
from _pytest.stash import Stash
from _pytest.warning_types import PytestConfigWarning
from _pytest.warning_types import warn_explicit_for

if TYPE_CHECKING:

Expand Down Expand Up @@ -407,18 +408,12 @@ def parse_hookimpl_opts(self, plugin: _PluggyPlugin, name: str):
name: hasattr(method, name) or name in known_marks
for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper")
}
if any(opts.values()) and not getattr(
plugin, "__module__", getattr(plugin, "__name__")
).startswith("xdist."):
if any(opts.values()):
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
type="spec", fullname=method.__qualname__
)
warnings.warn_explicit(
message,
type(message),
filename=inspect.getfile(method),
lineno=method.__code__.co_firstlineno,
)
warn_explicit_for(method, message)

return opts

def parse_hookspec_opts(self, module_or_class, name: str):
Expand All @@ -436,16 +431,10 @@ def parse_hookspec_opts(self, module_or_class, name: str):
}
# hook from xdist, fixing in ...
if any(opts.values()) and module_or_class.__name__ != "xdist.newhooks":

message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
type="spec", fullname=method.__qualname__
)
warnings.warn_explicit(
message,
type(message),
filename=inspect.getfile(method),
lineno=method.__code__.co_firstlineno,
)
warn_explicit_for(method, message)
return opts

def register(
Expand Down
19 changes: 19 additions & 0 deletions src/_pytest/warning_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import inspect
import warnings
from types import FunctionType
from typing import Any
from typing import Generic
from typing import Type
Expand Down Expand Up @@ -130,3 +133,19 @@ class UnformattedWarning(Generic[_W]):
def format(self, **kwargs: Any) -> _W:
"""Return an instance of the warning category, formatted with given kwargs."""
return self.category(self.template.format(**kwargs))


def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
lineno = method.__code__.co_firstlineno
filename = inspect.getfile(method)
module = method.__module__
mod_globals = method.__globals__

warnings.warn_explicit(
message,
type(message),
filename=filename,
module=module,
registry=mod_globals.setdefault("__warningregistry__", {}),
lineno=lineno,
)

0 comments on commit a445d0f

Please sign in to comment.