]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
use @classmethod per pytest guidance
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Jun 2026 15:36:02 +0000 (11:36 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Jun 2026 19:01:07 +0000 (15:01 -0400)
Fixed class-scoped pytest fixtures that were defined as instance methods
using ``self``, which is deprecated as of pytest 9.1 and will be removed in
pytest 10. Fixtures are now decorated with a compatibility ``@classmethod``
decorator and use ``cls`` as the first parameter.

Fixes: #13392
Change-Id: I866de54e22569c9d55eb97dce165670994ec4a57

doc/build/changelog/unreleased_20/13392.rst [new file with mode: 0644]
lib/sqlalchemy/testing/fixtures/mypy.py
lib/sqlalchemy/testing/fixtures/orm.py
lib/sqlalchemy/testing/fixtures/sql.py
lib/sqlalchemy/testing/plugin/pytestplugin.py
test/aaa_profiling/test_misc.py
test/ext/test_mutable.py

diff --git a/doc/build/changelog/unreleased_20/13392.rst b/doc/build/changelog/unreleased_20/13392.rst
new file mode 100644 (file)
index 0000000..cd9089b
--- /dev/null
@@ -0,0 +1,8 @@
+.. change::
+    :tags: bug, tests
+    :tickets: 13392
+
+    Fixed class-scoped pytest fixtures that were defined as instance methods
+    using ``self``, which is deprecated as of pytest 9.1 and will be removed in
+    pytest 10. Fixtures are now decorated with a compatibility ``@classmethod``
+    decorator and use ``cls`` as the first parameter.
index 44576b2a16048092e4450fab23e1336eff5f5557..cfcd3ab4261fbcbd469d6dc0221696457b2dd934 100644 (file)
@@ -39,10 +39,12 @@ class MypyTest(TestBase):
         yield from self._cachedir()
 
     @config.fixture(scope="class")
-    def cachedir(self):
-        yield from self._cachedir()
+    @classmethod
+    def cachedir(cls):
+        yield from cls._cachedir()
 
-    def _cachedir(self):
+    @classmethod
+    def _cachedir(cls):
         # as of mypy 0.971 i think we need to keep mypy_path empty
         mypy_path = ""
 
index 65693b719c6210ae17d45288625cce8ad35fe166..c42a19f259be24ef35097aa24cb7baa990a08cb4 100644 (file)
@@ -40,8 +40,8 @@ class MappedTest(ORMTest, TablesTest, assertions.AssertsExecutionResults):
     classes: Any = None
 
     @config.fixture(autouse=True, scope="class")
-    def _setup_tables_test_class(self):
-        cls = self.__class__
+    @classmethod
+    def _setup_tables_test_class(cls):
         cls._init_class()
 
         if cls.classes is None:
index eafab02865b6d44c76c11c25c5ff6205b99de941..8a2f08264e7e7fa661786ed98fa192d8d711cf11 100644 (file)
@@ -54,8 +54,8 @@ class TablesTest(TestBase):
     sequences = None
 
     @config.fixture(autouse=True, scope="class")
-    def _setup_tables_test_class(self):
-        cls = self.__class__
+    @classmethod
+    def _setup_tables_test_class(cls):
         cls._init_class()
 
         cls._setup_once_tables()
index 391b24576b140b58950060f4d16fde1a20ce1c57..77f6e96e2628ca22cc79d152c9dd5b5201d9955c 100644 (file)
@@ -862,10 +862,17 @@ class PytestFixtureFunctions(plugin_base.FixtureFunctions):
         # now apply wrappers to the function, including fixture itself
 
         def wrap(fn):
+            is_classmethod = isinstance(fn, classmethod)
+            if is_classmethod:
+                fn = fn.__func__
+
             if config.any_async:
                 fn = asyncio._maybe_async_wrapper(fn)
             # other wrappers may be added here
 
+            if is_classmethod:
+                fn = classmethod(fn)
+
             # now apply FixtureFunctionMarker
             fn = fixture(fn)
 
index c51648c677c22bdadb8ac7f1179f8ceff2120a81..b1423ddfaddc9f7400b8fcc49e6bd4daa0dbc09c 100644 (file)
@@ -56,7 +56,8 @@ class CacheKeyTest(fixtures.TestBase):
     __requires__ = ("cpython", "python_profiling_backend")
 
     @testing.fixture(scope="class")
-    def mapping_fixture(self):
+    @classmethod
+    def mapping_fixture(cls):
         # note in order to work nicely with "fixture" we are emerging
         # a whole new model of setup/teardown, since pytest "fixture"
         # sort of purposely works badly with setup/teardown
index c72f182f8fc5ccc0a33b262417d06f077915a1a0..216945450b5cbe5d8a62fc587726de38b8d10400 100644 (file)
@@ -1078,7 +1078,8 @@ class _MutableSetTestBase(_MutableSetTestFixture):
 
 class _MutableNoHashFixture:
     @testing.fixture(autouse=True, scope="class")
-    def set_class(self):
+    @classmethod
+    def set_class(cls):
         global Foo
 
         _replace_foo = Foo