some new features

This commit is contained in:
ilgazca
2025-07-30 17:09:11 +03:00
parent db5d46760a
commit 8019bd3b7c
20616 changed files with 4375466 additions and 8 deletions

View File

@ -0,0 +1,50 @@
from __future__ import annotations
import warnings
from _pytest.recwarn import WarningsChecker
from pytest import warns
__all__ = ["pytest_warns"]
class NoWarningsChecker:
def __init__(self):
self.cw = warnings.catch_warnings(record=True)
self.rec = []
def __enter__(self):
self.rec = self.cw.__enter__()
def __exit__(self, type, value, traceback):
if self.rec:
warnings = [w.category.__name__ for w in self.rec]
joined = "\\n".join(warnings)
raise AssertionError(
"Function is marked as not warning but the following "
"warnings were found: \n"
f"{joined}"
)
def pytest_warns(
warning: type[Warning] | tuple[type[Warning], ...] | None
) -> WarningsChecker | NoWarningsChecker:
"""
Parameters
----------
warning : {None, Warning, Tuple[Warning]}
None if no warning is produced, or a single or multiple Warnings
Returns
-------
cm
"""
if warning is None:
return NoWarningsChecker()
else:
assert warning is not None
return warns(warning)