Files
pybind11/tests/test_warnings.py
pre-commit-ci[bot] cc69a3789c chore(deps): update pre-commit hooks (#5745)
* chore(deps): update pre-commit hooks

updates:
- [github.com/pre-commit/mirrors-clang-format: v20.1.5 → v20.1.7](https://github.com/pre-commit/mirrors-clang-format/compare/v20.1.5...v20.1.7)
- [github.com/astral-sh/ruff-pre-commit: v0.11.12 → v0.12.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.11.12...v0.12.2)
- [github.com/pre-commit/mirrors-mypy: v1.16.0 → v1.16.1](https://github.com/pre-commit/mirrors-mypy/compare/v1.16.0...v1.16.1)
- [github.com/python-jsonschema/check-jsonschema: 0.33.0 → 0.33.2](https://github.com/python-jsonschema/check-jsonschema/compare/0.33.0...0.33.2)

* chore: fix new ruff check warnings

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* style: pre-commit fixes

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
2025-07-11 15:36:38 -07:00

69 lines
2.1 KiB
Python

from __future__ import annotations
import warnings
import pytest
import pybind11_tests # noqa: F401
from pybind11_tests import warnings_ as m
@pytest.mark.parametrize(
("expected_category", "expected_message", "expected_value", "module_function"),
[
(Warning, "This is simple warning", 21, m.warn_and_return_value),
(RuntimeWarning, "This is RuntimeWarning", None, m.warn_with_default_category),
(FutureWarning, "This is FutureWarning", None, m.warn_with_different_category),
],
)
def test_warning_simple(
expected_category, expected_message, expected_value, module_function
):
with pytest.warns(Warning, match="This is") as excinfo:
value = module_function()
assert issubclass(excinfo[0].category, expected_category)
assert str(excinfo[0].message) == expected_message
assert value == expected_value
def test_warning_wrong_subclass_fail():
with pytest.raises(Exception) as excinfo:
m.warn_with_invalid_category()
assert issubclass(excinfo.type, RuntimeError)
assert (
str(excinfo.value)
== "pybind11::warnings::warn(): cannot raise warning, category must be a subclass of PyExc_Warning!"
)
def test_warning_double_register_fail():
with pytest.raises(Exception) as excinfo:
m.register_duplicate_warning()
assert issubclass(excinfo.type, RuntimeError)
assert (
str(excinfo.value)
== 'pybind11::warnings::new_warning_type(): an attribute with name "CustomWarning" exists already.'
)
def test_warning_register():
assert m.CustomWarning is not None
with pytest.warns(m.CustomWarning) as excinfo:
warnings.warn("This is warning from Python!", m.CustomWarning, stacklevel=1)
assert issubclass(excinfo[0].category, DeprecationWarning)
assert str(excinfo[0].message) == "This is warning from Python!"
def test_warning_custom():
with pytest.warns(m.CustomWarning) as excinfo:
value = m.warn_with_custom_type()
assert issubclass(excinfo[0].category, DeprecationWarning)
assert str(excinfo[0].message) == "This is CustomWarning"
assert value == 37