Skip to content

Commit 95b03a0

Browse files
author
Lode Rosseel
committed
Wrap patched method with one containing explicit wrapped self to fix functionality for django<5.1
1 parent 2c1d21b commit 95b03a0

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

pytest_django/plugin.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ def _blocking_wrapper(self, *args, **kwargs) -> NoReturn:
842842
'"db" or "transactional_db" fixtures to enable it. '
843843
)
844844

845-
def _unblocked_async_only(self, *args, **kwargs):
845+
def _unblocked_async_only(self, wrapper_self, *args, **kwargs):
846846
__tracebackhide__ = True
847847
from asgiref.sync import SyncToAsync
848848

@@ -856,9 +856,9 @@ def _unblocked_async_only(self, *args, **kwargs):
856856
"See https://pytest-django.readthedocs.io/en/latest/database.html#db-thread-safeguards for more information."
857857
)
858858
elif self._real_ensure_connection is not None:
859-
self._real_ensure_connection(*args, **kwargs)
859+
self._real_ensure_connection(wrapper_self, *args, **kwargs)
860860

861-
def _unblocked_sync_only(self, *args, **kwargs):
861+
def _unblocked_sync_only(self, wrapper_self, *args, **kwargs):
862862
__tracebackhide__ = True
863863
if threading.current_thread() != threading.main_thread():
864864
raise RuntimeError(
@@ -867,17 +867,23 @@ def _unblocked_sync_only(self, *args, **kwargs):
867867
"See https://pytest-django.readthedocs.io/en/latest/database.html#db-thread-safeguards for more information."
868868
)
869869
elif self._real_ensure_connection is not None:
870-
self._real_ensure_connection(*args, **kwargs)
870+
self._real_ensure_connection(wrapper_self, *args, **kwargs)
871871

872872
def unblock(self, sync_only=False, async_only=False) -> AbstractContextManager[None]:
873873
"""Enable access to the Django database."""
874874
if sync_only and async_only:
875875
raise ValueError("Cannot use both sync_only and async_only. Choose at most one.")
876876
self._save_active_wrapper()
877877
if sync_only:
878-
self._dj_db_wrapper.ensure_connection = self._unblocked_sync_only
878+
def _method(wrapper_self, *args, **kwargs):
879+
return self._unblocked_sync_only(wrapper_self, *args, **kwargs)
880+
881+
self._dj_db_wrapper.ensure_connection = _method
879882
elif async_only:
880-
self._dj_db_wrapper.ensure_connection = self._unblocked_async_only
883+
def _method(wrapper_self, *args, **kwargs):
884+
return self._unblocked_async_only(wrapper_self, *args, **kwargs)
885+
886+
self._dj_db_wrapper.ensure_connection = _method
881887
else:
882888
self._dj_db_wrapper.ensure_connection = self._real_ensure_connection
883889
return _DatabaseBlockerContextManager(self)

0 commit comments

Comments
 (0)