]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
after all that, use pytest warnings plugin
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Jan 2022 15:51:55 +0000 (10:51 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 23 Jan 2022 17:58:38 +0000 (12:58 -0500)
The warnings plugin lets us set the filters up
in the config, and as our filter requirements are now
simple we can just set this up.

additionally pytest now recommends pyproject.toml, since
we fully include this now, let's move it there.

the pytest logging plugin seems to not be any problem either
at the moment, so re-enable that.  if it becomes apparent
whatever the problem was (which was probably that it was just
surprising, or something) we can disable it again and comment
what the reason was.

Change-Id: Ia9715533b01f72aa5fdcf6a27ce75b76f829fa43

lib/sqlalchemy/exc.py
lib/sqlalchemy/pool/base.py
lib/sqlalchemy/testing/warnings.py
pyproject.toml
setup.cfg
test/base/test_except.py
test/conftest.py

index f5b9f6e75bee64485a0f58bead645b3447534715..8fdacbdf2e54de50884391a288b82c8be0ddc1dc 100644 (file)
@@ -672,6 +672,15 @@ class NotSupportedError(DatabaseError):
 # Warnings
 
 
+class SATestSuiteWarning(Warning):
+    """warning for a condition detected during tests that is non-fatal
+
+    Currently outside of SAWarning so that we can work around tools like
+    Alembic doing the wrong thing with warnings.
+
+    """
+
+
 class SADeprecationWarning(HasDescriptionCode, DeprecationWarning):
     """Issued for usage of deprecated APIs."""
 
index 0512582f8da4ae605e02a3057ed0b1f259ea97d0..6c770e201c9589908396353da214652cb6f2773b 100644 (file)
@@ -757,13 +757,14 @@ def _finalize_fairy(
                 else:
                     message = (
                         "The garbage collector is trying to clean up "
-                        "connection %r. This feature is unsupported on async "
+                        f"connection {dbapi_connection!r}. This feature is "
+                        "unsupported on async "
                         "dbapi, since no IO can be performed at this stage to "
                         "reset the connection. Please close out all "
                         "connections when they are no longer used, calling "
                         "``close()`` or using a context manager to "
                         "manage their lifetime."
-                    ) % dbapi_connection
+                    )
                     pool.logger.error(message)
                     util.warn(message)
 
@@ -779,6 +780,14 @@ def _finalize_fairy(
     if connection_record and connection_record.fairy_ref is not None:
         connection_record.checkin()
 
+    # give gc some help.  See
+    # test/engine/test_pool.py::PoolEventsTest::test_checkin_event_gc[True]
+    # which actually started failing when pytest warnings plugin was
+    # turned on, due to util.warn() above
+    del dbapi_connection
+    del connection_record
+    del fairy
+
 
 # a dictionary of the _ConnectionFairy weakrefs to _ConnectionRecord, so that
 # GC under pypy will call ConnectionFairy finalizers.  linked directly to the
index 1c20396024792289fbdbf75cfea46db7021265aa..2d65e68ec4765170e6244250decf24223a8ede47 100644 (file)
@@ -4,54 +4,26 @@
 #
 # This module is part of SQLAlchemy and is released under
 # the MIT License: https://www.opensource.org/licenses/mit-license.php
-import warnings
-
 from . import assertions
 from .. import exc as sa_exc
+from ..exc import SATestSuiteWarning
 from ..util.langhelpers import _warnings_warn
 
 
-class SATestSuiteWarning(Warning):
-    """warning for a condition detected during tests that is non-fatal
-
-    Currently outside of SAWarning so that we can work around tools like
-    Alembic doing the wrong thing with warnings.
-
-    """
-
-
 def warn_test_suite(message):
     _warnings_warn(message, category=SATestSuiteWarning)
 
 
 def setup_filters():
-    """Set global warning behavior for the test suite."""
-
-    # TODO: at this point we can use the normal pytest warnings plugin,
-    # if we decide the test suite can be linked to pytest only
-
-    origin = r"^(?:test|sqlalchemy)\..*"
+    """hook for setting up warnings filters.
 
-    warnings.filterwarnings(
-        "ignore", category=sa_exc.SAPendingDeprecationWarning
-    )
-    warnings.filterwarnings("error", category=sa_exc.SADeprecationWarning)
-    warnings.filterwarnings("error", category=sa_exc.SAWarning)
+    Note that when the pytest warnings plugin is in place, that plugin
+    overwrites whatever happens here.
 
-    warnings.filterwarnings("always", category=SATestSuiteWarning)
+    Current SQLAlchemy 2.0 default is to use pytest warnings plugin
+    which is configured in pyproject.toml.
 
-    warnings.filterwarnings(
-        "error", category=DeprecationWarning, module=origin
-    )
-
-    try:
-        import pytest
-    except ImportError:
-        pass
-    else:
-        warnings.filterwarnings(
-            "once", category=pytest.PytestDeprecationWarning, module=origin
-        )
+    """
 
 
 def assert_warnings(fn, warning_msgs, regex=False):
index 9b0b3dfc72bb42217ae0b53957954337182b5e64..2707bea97c9438de6be3ca7bdcfa8ef8eed65a75 100644 (file)
@@ -9,3 +9,17 @@
 [tool.black]
 line-length = 79
 target-version = ['py37']
+
+
+[tool.pytest.ini_options]
+addopts = "--tb native -v -r sfxX --maxfail=250 -p warnings -p logging"
+python_files = "test/*test_*.py"
+minversion = "6.2"
+filterwarnings = [
+    "ignore::sqlalchemy.exc.SAPendingDeprecationWarning",
+    "error::sqlalchemy.exc.SADeprecationWarning",
+    "error::sqlalchemy.exc.SAWarning",
+    "always::sqlalchemy.exc.SATestSuiteWarning",
+    "error::DeprecationWarning:test",
+    "error::DeprecationWarning:sqlalchemy"
+]
index e841348cec13378528e63943650785ccc87fc06a..3f903eb62f6619fab6df13ef15e89077420064c4 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -85,9 +85,8 @@ tag_build = dev
 [options.packages.find]
 where = lib
 
-[tool:pytest]
-addopts = --tb native -v -r sfxX --maxfail=250 -p no:warnings -p no:logging
-python_files = test/*test_*.py
+# [tool:pytest]
+# pytest settings moved to pyproject.toml
 
 [upload]
 sign = 1
index c7ef304b3dd7f3e527c30553d0ac47e8fe9557c9..77f5c731acc8462772404cc1fb49292ef3903527 100644 (file)
@@ -511,6 +511,7 @@ ALL_EXC = [
         [lambda cls: cls("foo", code="42")],
     ),
     ([sa_exceptions.SAPendingDeprecationWarning], [lambda cls: cls(1, 2, 3)]),
+    ([sa_exceptions.SATestSuiteWarning], [lambda cls: cls()]),
 ]
 
 
index 921a4aadc55063aec7d655cff8518944ed80746d..d082f44a19a7ef87a825ac4f59aeb96407f9e54d 100755 (executable)
@@ -16,6 +16,8 @@ os.environ["SQLALCHEMY_WARN_20"] = "true"
 
 collect_ignore_glob = []
 
+# this requires that sqlalchemy.testing was not already
+# imported in order to work
 pytest.register_assert_rewrite("sqlalchemy.testing.assertions")