Skip to content

Commit a445d0f

Browse files
rearrange explicit hook warnings to pass in all data needed for warning registries
1 parent 680a8e2 commit a445d0f

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ filterwarnings = [
4040
# Those are caught/handled by pyupgrade, and not easy to filter with the
4141
# module being the filename (with .py removed).
4242
"default:invalid escape sequence:DeprecationWarning",
43+
# ignore not yet fixed warnings for hook markers
44+
"default:.*not marked using pytest.hook.*",
45+
"ignore:.*not marked using pytest.hook.*::xdist.*",
4346
# ignore use of unregistered marks, because we use many to test the implementation
4447
"ignore::_pytest.warning_types.PytestUnknownMarkWarning",
4548
# https://github.com/benjaminp/six/issues/341

src/_pytest/config/__init__.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
from _pytest.pathlib import resolve_package_path
5959
from _pytest.stash import Stash
6060
from _pytest.warning_types import PytestConfigWarning
61+
from _pytest.warning_types import warn_explicit_for
6162

6263
if TYPE_CHECKING:
6364

@@ -407,18 +408,12 @@ def parse_hookimpl_opts(self, plugin: _PluggyPlugin, name: str):
407408
name: hasattr(method, name) or name in known_marks
408409
for name in ("tryfirst", "trylast", "optionalhook", "hookwrapper")
409410
}
410-
if any(opts.values()) and not getattr(
411-
plugin, "__module__", getattr(plugin, "__name__")
412-
).startswith("xdist."):
411+
if any(opts.values()):
413412
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
414413
type="spec", fullname=method.__qualname__
415414
)
416-
warnings.warn_explicit(
417-
message,
418-
type(message),
419-
filename=inspect.getfile(method),
420-
lineno=method.__code__.co_firstlineno,
421-
)
415+
warn_explicit_for(method, message)
416+
422417
return opts
423418

424419
def parse_hookspec_opts(self, module_or_class, name: str):
@@ -436,16 +431,10 @@ def parse_hookspec_opts(self, module_or_class, name: str):
436431
}
437432
# hook from xdist, fixing in ...
438433
if any(opts.values()) and module_or_class.__name__ != "xdist.newhooks":
439-
440434
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
441435
type="spec", fullname=method.__qualname__
442436
)
443-
warnings.warn_explicit(
444-
message,
445-
type(message),
446-
filename=inspect.getfile(method),
447-
lineno=method.__code__.co_firstlineno,
448-
)
437+
warn_explicit_for(method, message)
449438
return opts
450439

451440
def register(

src/_pytest/warning_types.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import inspect
2+
import warnings
3+
from types import FunctionType
14
from typing import Any
25
from typing import Generic
36
from typing import Type
@@ -130,3 +133,19 @@ class UnformattedWarning(Generic[_W]):
130133
def format(self, **kwargs: Any) -> _W:
131134
"""Return an instance of the warning category, formatted with given kwargs."""
132135
return self.category(self.template.format(**kwargs))
136+
137+
138+
def warn_explicit_for(method: FunctionType, message: PytestWarning) -> None:
139+
lineno = method.__code__.co_firstlineno
140+
filename = inspect.getfile(method)
141+
module = method.__module__
142+
mod_globals = method.__globals__
143+
144+
warnings.warn_explicit(
145+
message,
146+
type(message),
147+
filename=filename,
148+
module=module,
149+
registry=mod_globals.setdefault("__warningregistry__", {}),
150+
lineno=lineno,
151+
)

0 commit comments

Comments
 (0)