handle non-true options in hookspec warning
This commit is contained in:
parent
8c52dc5b7e
commit
b1fb9a9c8d
|
@ -348,18 +348,21 @@ def _get_legacy_hook_marks(
|
||||||
hook_type: str,
|
hook_type: str,
|
||||||
opt_names: Tuple[str, ...],
|
opt_names: Tuple[str, ...],
|
||||||
) -> Dict[str, bool]:
|
) -> Dict[str, bool]:
|
||||||
known_marks = {m.name for m in getattr(method, "pytestmark", [])}
|
known_marks: set[str] = {m.name for m in getattr(method, "pytestmark", [])}
|
||||||
must_warn = False
|
must_warn: list[str] = []
|
||||||
opts = {}
|
opts: dict[str, bool] = {}
|
||||||
for opt_name in opt_names:
|
for opt_name in opt_names:
|
||||||
|
opt_attr = getattr(method, opt_name, AttributeError)
|
||||||
|
if opt_attr is not AttributeError:
|
||||||
|
must_warn.append(f"{opt_name}={opt_attr}")
|
||||||
|
elif opt_name in known_marks:
|
||||||
|
must_warn.append(f"{opt_name}=True")
|
||||||
if hasattr(method, opt_name) or opt_name in known_marks:
|
if hasattr(method, opt_name) or opt_name in known_marks:
|
||||||
opts[opt_name] = True
|
opts[opt_name] = True
|
||||||
must_warn = True
|
|
||||||
else:
|
else:
|
||||||
opts[opt_name] = False
|
opts[opt_name] = False
|
||||||
if must_warn:
|
if must_warn:
|
||||||
|
hook_opts = ", ".join(must_warn)
|
||||||
hook_opts = ", ".join(f"{name}=True" for name, val in opts.items() if val)
|
|
||||||
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
|
message = _pytest.deprecated.HOOK_LEGACY_MARKING.format(
|
||||||
type=hook_type,
|
type=hook_type,
|
||||||
fullname=method.__qualname__, # type: ignore
|
fullname=method.__qualname__, # type: ignore
|
||||||
|
|
|
@ -29,11 +29,11 @@ def test_hookspec_via_function_attributes_are_deprecated():
|
||||||
def pytest_bad_hook(self):
|
def pytest_bad_hook(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
pytest_bad_hook.historic = True # type: ignore[attr-defined]
|
pytest_bad_hook.historic = False # type: ignore[attr-defined]
|
||||||
|
|
||||||
with pytest.warns(
|
with pytest.warns(
|
||||||
PytestDeprecationWarning,
|
PytestDeprecationWarning,
|
||||||
match=r"Please use the pytest\.hookspec\(historic=True\) decorator",
|
match=r"Please use the pytest\.hookspec\(historic=False\) decorator",
|
||||||
) as recorder:
|
) as recorder:
|
||||||
pm.add_hookspecs(DeprecatedHookMarkerSpec)
|
pm.add_hookspecs(DeprecatedHookMarkerSpec)
|
||||||
(record,) = recorder
|
(record,) = recorder
|
||||||
|
|
Loading…
Reference in New Issue