warnings.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # testing/warnings.py
  2. # Copyright (C) 2005-2022 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(sa_exc.SAWarning):
  13. """warning for a condition detected during tests that is non-fatal"""
  14. def warn_test_suite(message):
  15. _warnings_warn(message, category=SATestSuiteWarning)
  16. def setup_filters():
  17. """Set global warning behavior for the test suite."""
  18. warnings.filterwarnings(
  19. "ignore", category=sa_exc.SAPendingDeprecationWarning
  20. )
  21. warnings.filterwarnings("error", category=sa_exc.SADeprecationWarning)
  22. warnings.filterwarnings("error", category=sa_exc.SAWarning)
  23. warnings.filterwarnings("always", category=SATestSuiteWarning)
  24. # some selected deprecations...
  25. warnings.filterwarnings("error", category=DeprecationWarning)
  26. warnings.filterwarnings(
  27. "ignore", category=DeprecationWarning, message=r".*StopIteration"
  28. )
  29. warnings.filterwarnings(
  30. "ignore",
  31. category=DeprecationWarning,
  32. message=r".*inspect.get.*argspec",
  33. )
  34. warnings.filterwarnings(
  35. "ignore",
  36. category=DeprecationWarning,
  37. message="The loop argument 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
  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()