From 11c9772855faedb7f56f6756bd03f2511d2e0b6e Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Wed, 15 Jul 2026 14:46:43 -0700 Subject: [PATCH 1/2] Fix BlackSheep cross-task database access BlackSheep initializes the ORM in its lifespan task, but request tasks could not see that context because the global fallback was disabled. Enable the same fallback used by the other ASGI integrations and add a regression test for the startup configuration. --- tests/contrib/test_blacksheep.py | 34 +++++++++++++++++++++++++ tortoise/contrib/blacksheep/__init__.py | 8 +++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/contrib/test_blacksheep.py diff --git a/tests/contrib/test_blacksheep.py b/tests/contrib/test_blacksheep.py new file mode 100644 index 000000000..2dab65fae --- /dev/null +++ b/tests/contrib/test_blacksheep.py @@ -0,0 +1,34 @@ +from unittest.mock import AsyncMock, patch + +import pytest +from blacksheep.server import Application + +from tortoise.contrib.blacksheep import register_tortoise + + +@pytest.mark.asyncio +async def test_register_tortoise_enables_global_fallback() -> None: + with ( + patch("tortoise.contrib.blacksheep.Tortoise") as mocked_tortoise, + patch("tortoise.contrib.blacksheep.get_connections"), + ): + mocked_tortoise.init = AsyncMock() + mocked_tortoise.close_connections = AsyncMock() + app = Application() + register_tortoise( + app, + db_url="sqlite://:memory:", + modules={"models": ["__main__"]}, + ) + + await app.start() + mocked_tortoise.init.assert_awaited_once_with( + config=None, + config_file=None, + db_url="sqlite://:memory:", + modules={"models": ["__main__"]}, + _enable_global_fallback=True, + ) + + await app.stop() + mocked_tortoise.close_connections.assert_awaited_once() diff --git a/tortoise/contrib/blacksheep/__init__.py b/tortoise/contrib/blacksheep/__init__.py index 015ac1907..cda20acc3 100644 --- a/tortoise/contrib/blacksheep/__init__.py +++ b/tortoise/contrib/blacksheep/__init__.py @@ -89,7 +89,13 @@ def register_tortoise( @app.on_start async def init_orm(context) -> None: # pylint: disable=W0612 - await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules) + await Tortoise.init( + config=config, + config_file=config_file, + db_url=db_url, + modules=modules, + _enable_global_fallback=True, + ) logger.info("Tortoise-ORM started, %s, %s", get_connections()._get_storage(), Tortoise.apps) if generate_schemas: logger.info("Tortoise-ORM generating schema") From 9d35ff7d25c8fa012616527c3036b90c292edc5c Mon Sep 17 00:00:00 2001 From: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com> Date: Sat, 1 Aug 2026 21:40:57 -0700 Subject: [PATCH 2/2] Silence mypy no-untyped-call on blacksheep app start/stop in test --- tests/contrib/test_blacksheep.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/contrib/test_blacksheep.py b/tests/contrib/test_blacksheep.py index 2dab65fae..df51bde0a 100644 --- a/tests/contrib/test_blacksheep.py +++ b/tests/contrib/test_blacksheep.py @@ -21,7 +21,7 @@ async def test_register_tortoise_enables_global_fallback() -> None: modules={"models": ["__main__"]}, ) - await app.start() + await app.start() # type: ignore[no-untyped-call] mocked_tortoise.init.assert_awaited_once_with( config=None, config_file=None, @@ -30,5 +30,5 @@ async def test_register_tortoise_enables_global_fallback() -> None: _enable_global_fallback=True, ) - await app.stop() + await app.stop() # type: ignore[no-untyped-call] mocked_tortoise.close_connections.assert_awaited_once()