warnings.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # testing/warnings.py
  2. # Copyright (C) 2005-2024 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. from __future__ import absolute_import
  8. import warnings
  9. from . import assertions
  10. from .. import exc as sa_exc
  11. from ..util.langhelpers import _warnings_warn
  12. class SATestSuiteWarning(Warning):
  13. """warning for a condition detected during tests that is non-fatal
  14. Currently outside of SAWarning so that we can work around tools like
  15. Alembic doing the wrong thing with warnings.
  16. """
  17. def warn_test_suite(message):
  18. _warnings_warn(message, category=SATestSuiteWarning)
  19. def setup_filters():
  20. """Set global warning behavior for the test suite."""
  21. # TODO: at this point we can use the normal pytest warnings plugin,
  22. # if we decide the test suite can be linked to pytest only
  23. origin = r"^(?:test|sqlalchemy)\..*"
  24. warnings.filterwarnings(
  25. "ignore", category=sa_exc.SAPendingDeprecationWarning
  26. )
  27. warnings.filterwarnings("error", category=sa_exc.SADeprecationWarning)
  28. warnings.filterwarnings("error", category=sa_exc.SAWarning)
  29. warnings.filterwarnings("always", category=SATestSuiteWarning)
  30. warnings.filterwarnings(
  31. "error", category=DeprecationWarning, module=origin
  32. )
  33. warnings.filterwarnings(
  34. "ignore",
  35. category=DeprecationWarning,
  36. message=r".*The default (?:date)?(?:time)?(?:stamp)? "
  37. r"(adapter|converter) is deprecated",
  38. )
  39. # ignore things that are deprecated *as of* 2.0 :)
  40. warnings.filterwarnings(
  41. "ignore",
  42. category=sa_exc.SADeprecationWarning,
  43. message=r".*\(deprecated since: 2.0\)$",
  44. )
  45. warnings.filterwarnings(
  46. "ignore",
  47. category=sa_exc.SADeprecationWarning,
  48. message=r"^The (Sybase|firebird) dialect is deprecated and will be",
  49. )
  50. try:
  51. import pytest
  52. except ImportError:
  53. pass
  54. else:
  55. warnings.filterwarnings(
  56. "once", category=pytest.PytestDeprecationWarning, module=origin
  57. )
  58. def assert_warnings(fn, warning_msgs, regex=False):
  59. """Assert that each of the given warnings are emitted by fn.
  60. Deprecated. Please use assertions.expect_warnings().
  61. """
  62. with assertions._expect_warnings(
  63. sa_exc.SAWarning, warning_msgs, regex=regex
  64. ):
  65. return fn()