warnings.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # testing/warnings.py
  2. # Copyright (C) 2005-2025 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: ignore-errors
  8. from __future__ import annotations
  9. import warnings
  10. from . import assertions
  11. from .. import exc
  12. from .. import exc as sa_exc
  13. from ..exc import SATestSuiteWarning
  14. from ..util.langhelpers import _warnings_warn
  15. def warn_test_suite(message):
  16. _warnings_warn(message, category=SATestSuiteWarning)
  17. def setup_filters():
  18. """hook for setting up warnings filters.
  19. SQLAlchemy-specific classes must only be here and not in pytest config,
  20. as we need to delay importing SQLAlchemy until conftest.py has been
  21. processed.
  22. NOTE: filters on subclasses of DeprecationWarning or
  23. PendingDeprecationWarning have no effect if added here, since pytest
  24. will add at each test the following filters
  25. ``always::PendingDeprecationWarning`` and ``always::DeprecationWarning``
  26. that will take precedence over any added here.
  27. """
  28. warnings.filterwarnings("error", category=exc.SAWarning)
  29. warnings.filterwarnings("always", category=exc.SATestSuiteWarning)
  30. def assert_warnings(fn, warning_msgs, regex=False):
  31. """Assert that each of the given warnings are emitted by fn.
  32. Deprecated. Please use assertions.expect_warnings().
  33. """
  34. with assertions._expect_warnings(
  35. sa_exc.SAWarning, warning_msgs, regex=regex
  36. ):
  37. return fn()