Skip to content

Commit b1fb9a9

Browse files
handle non-true options in hookspec warning
1 parent 8c52dc5 commit b1fb9a9

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/_pytest/config/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,21 @@ def _get_legacy_hook_marks(
348348
hook_type: str,
349349
opt_names: Tuple[str, ...],
350350
) -> Dict[str, bool]:
351-
known_marks = {m.name for m in getattr(method, "pytestmark", [])}
352-
must_warn = False
353-
opts = {}
351+
known_marks: set[str] = {m.name for m in getattr(method, "pytestmark", [])}
352+
must_warn: list[str] = []
353+
opts: dict[str, bool] = {}
354354
for opt_name in opt_names:
355+
opt_attr = getattr(method, opt_name, AttributeError)
356+
if opt_attr is not AttributeError:
357+
must_warn.append(f"{opt_name}={opt_attr}")
358+
elif opt_name in known_marks:
359+
must_warn.append(f"{opt_name}=True")
355360
if hasattr(method, opt_name) or opt_name in known_marks:
356361
opts[opt_name] = True
357-
must_warn = True
358362
else:
359363
opts[opt_name] = False
360364
if must_warn:
361-
362-
hook_opts = ", ".join(f"{name}=True" for name, val in opts.items() if val)
365+
hook_opts = ", ".join(must_warn)
363366
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
364367
type=hook_type,
365368
fullname=method.__qualname__, # type: ignore

testing/deprecated_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class DeprecatedHookMarkerSpec:
2929
def pytest_bad_hook(self):
3030
pass
3131

32-
pytest_bad_hook.historic = True # type: ignore[attr-defined]
32+
pytest_bad_hook.historic = False # type: ignore[attr-defined]
3333

3434
with pytest.warns(
3535
PytestDeprecationWarning,
36-
match=r"Please use the pytest\.hookspec\(historic=True\) decorator",
36+
match=r"Please use the pytest\.hookspec\(historic=False\) decorator",
3737
) as recorder:
3838
pm.add_hookspecs(DeprecatedHookMarkerSpec)
3939
(record,) = recorder

0 commit comments

Comments
 (0)