From e2464f412fb865d72f10188df73231683713e69b Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Fri, 25 Sep 2026 10:02:18 -0700 Subject: [PATCH 1/2] declare BaseDatabaseWrapper.atomic_blocks and the thread-sharing counters atomic_blocks is set in __init__ and inc_thread_sharing/dec_thread_sharing are public methods; code that hands a connection to another thread (test helpers, mostly) couldn't reference them. --- django-stubs/db/backends/base/base.pyi | 4 ++++ tests/pyright/test_database_wrapper.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 tests/pyright/test_database_wrapper.py diff --git a/django-stubs/db/backends/base/base.pyi b/django-stubs/db/backends/base/base.pyi index 774230644..a64dff231 100644 --- a/django-stubs/db/backends/base/base.pyi +++ b/django-stubs/db/backends/base/base.pyi @@ -8,6 +8,7 @@ from django.db.backends.base.introspection import BaseDatabaseIntrospection from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.base.validation import BaseDatabaseValidation from django.db.backends.utils import CursorDebugWrapper, CursorWrapper +from django.db.transaction import Atomic NO_DB_ALIAS: str @@ -33,6 +34,7 @@ class BaseDatabaseWrapper: force_debug_cursor: bool = ... autocommit: bool = ... in_atomic_block: bool = ... + atomic_blocks: list[Atomic] savepoint_state: int = ... savepoint_ids: Any = ... commit_on_exit: bool = ... @@ -93,6 +95,8 @@ class BaseDatabaseWrapper: def check_constraints(self, table_names: Any | None = ...) -> None: ... def is_usable(self) -> None: ... def close_if_unusable_or_obsolete(self) -> None: ... + def inc_thread_sharing(self) -> None: ... + def dec_thread_sharing(self) -> None: ... def validate_thread_sharing(self) -> None: ... def prepare_database(self) -> None: ... def wrap_database_errors(self) -> Any: ... diff --git a/tests/pyright/test_database_wrapper.py b/tests/pyright/test_database_wrapper.py new file mode 100644 index 000000000..c432e3849 --- /dev/null +++ b/tests/pyright/test_database_wrapper.py @@ -0,0 +1,18 @@ +from .base import Result, run_pyright + + +def test_atomic_blocks_and_thread_sharing_are_declared() -> None: + """A test helper hands a connection to another thread and inspects the open atomic blocks.""" + results = run_pyright( + """\ +from django.db import connection + +connection.inc_thread_sharing() +connection.dec_thread_sharing() +reveal_type(connection.atomic_blocks) +""" + ) + assert [r for r in results if r.type == "error"] == [] + assert results == [ + Result(type="information", message='Type of "connection.atomic_blocks" is "list[Atomic]"', line=5, column=13), + ] From 16cf07d7c1ed961c9a0de10a222ebe2dae84632d Mon Sep 17 00:00:00 2001 From: Adam Hitchcock Date: Fri, 25 Sep 2026 15:35:07 -0700 Subject: [PATCH 2/2] declare the rest of BaseDatabaseWrapper's public members get_database_version, check_database_version_supported, close_if_health_check_failed, health_check_enabled, health_check_done and rollback_exc were missing too. The class now declares everything public that Django 6.0 defines on it. --- django-stubs/db/backends/base/base.pyi | 8 +++++++- tests/pyright/test_database_wrapper.py | 16 ++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/django-stubs/db/backends/base/base.pyi b/django-stubs/db/backends/base/base.pyi index a64dff231..1d37ce726 100644 --- a/django-stubs/db/backends/base/base.pyi +++ b/django-stubs/db/backends/base/base.pyi @@ -34,12 +34,15 @@ class BaseDatabaseWrapper: force_debug_cursor: bool = ... autocommit: bool = ... in_atomic_block: bool = ... - atomic_blocks: list[Atomic] + atomic_blocks: list[Atomic] = ... savepoint_state: int = ... savepoint_ids: Any = ... commit_on_exit: bool = ... needs_rollback: bool = ... + rollback_exc: Exception | None = ... close_at: Any | None = ... + health_check_enabled: bool = ... + health_check_done: bool = ... closed_in_transaction: bool = ... errors_occurred: bool = ... allow_thread_sharing: bool = ... @@ -65,6 +68,8 @@ class BaseDatabaseWrapper: @property def queries(self) -> list[dict[str, str]]: ... def get_connection_params(self) -> None: ... + def get_database_version(self) -> tuple[int, ...]: ... + def check_database_version_supported(self) -> None: ... def get_new_connection(self, conn_params: Any) -> None: ... def init_connection_state(self) -> None: ... def create_cursor(self, name: Any | None = ...) -> None: ... @@ -94,6 +99,7 @@ class BaseDatabaseWrapper: def enable_constraint_checking(self) -> None: ... def check_constraints(self, table_names: Any | None = ...) -> None: ... def is_usable(self) -> None: ... + def close_if_health_check_failed(self) -> None: ... def close_if_unusable_or_obsolete(self) -> None: ... def inc_thread_sharing(self) -> None: ... def dec_thread_sharing(self) -> None: ... diff --git a/tests/pyright/test_database_wrapper.py b/tests/pyright/test_database_wrapper.py index c432e3849..a36c3ebf8 100644 --- a/tests/pyright/test_database_wrapper.py +++ b/tests/pyright/test_database_wrapper.py @@ -1,18 +1,26 @@ from .base import Result, run_pyright -def test_atomic_blocks_and_thread_sharing_are_declared() -> None: - """A test helper hands a connection to another thread and inspects the open atomic blocks.""" +def test_base_database_wrapper_declares_its_public_members() -> None: + """Every public attribute and method BaseDatabaseWrapper defines is visible to the checker.""" results = run_pyright( """\ from django.db import connection connection.inc_thread_sharing() connection.dec_thread_sharing() +connection.check_database_version_supported() +connection.close_if_health_check_failed() +connection.get_database_version() reveal_type(connection.atomic_blocks) +reveal_type(connection.health_check_done) +reveal_type(connection.health_check_enabled) +reveal_type(connection.rollback_exc) """ ) - assert [r for r in results if r.type == "error"] == [] assert results == [ - Result(type="information", message='Type of "connection.atomic_blocks" is "list[Atomic]"', line=5, column=13), + Result(type="information", message='Type of "connection.atomic_blocks" is "list[Atomic]"', line=8, column=13), + Result(type="information", message='Type of "connection.health_check_done" is "bool"', line=9, column=13), + Result(type="information", message='Type of "connection.health_check_enabled" is "bool"', line=10, column=13), + Result(type="information", message='Type of "connection.rollback_exc" is "Exception | None"', line=11, column=13), ]