Skip to content

Commit 452a4a4

Browse files
authored
Use AbstractContextManager instead of @contextmanager (#2561)
1 parent cfcfc92 commit 452a4a4

File tree

11 files changed

+42
-64
lines changed

11 files changed

+42
-64
lines changed

django-stubs/contrib/admin/tests.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Callable, Generator
2-
from contextlib import contextmanager
1+
from collections.abc import Callable
2+
from contextlib import AbstractContextManager
33
from typing import Any
44

55
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
@@ -17,8 +17,7 @@ class AdminSeleniumTestCase(SeleniumTestCase, StaticLiveServerTestCase):
1717
def wait_until_visible(self, css_selector: str, timeout: int = ...) -> None: ...
1818
def wait_until_invisible(self, css_selector: str, timeout: int = ...) -> None: ...
1919
def wait_page_ready(self, timeout: int = ...) -> None: ...
20-
@contextmanager
21-
def wait_page_loaded(self, timeout: int = ...) -> Generator[None, None, None]: ...
20+
def wait_page_loaded(self, timeout: int = ...) -> AbstractContextManager[None]: ...
2221
def admin_login(self, username: str, password: str, login_url: str = ...) -> None: ...
2322
def select_option(self, selector: str, value: Any) -> None: ...
2423
def deselect_option(self, selector: str, value: Any) -> None: ...

django-stubs/db/backends/base/base.pyi

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Callable, Generator, Iterator, MutableMapping
2-
from contextlib import contextmanager
1+
from collections.abc import Callable, MutableMapping
2+
from contextlib import AbstractContextManager
33
from datetime import tzinfo
44
from logging import Logger
55
from typing import Any
@@ -99,8 +99,7 @@ class BaseDatabaseWrapper:
9999
def set_rollback(self, rollback: bool) -> None: ...
100100
def validate_no_atomic_block(self) -> None: ...
101101
def validate_no_broken_transaction(self) -> None: ...
102-
@contextmanager
103-
def constraint_checks_disabled(self) -> Iterator[None]: ...
102+
def constraint_checks_disabled(self) -> AbstractContextManager[None]: ...
104103
def disable_constraint_checking(self) -> bool: ...
105104
def enable_constraint_checking(self) -> None: ...
106105
def check_constraints(self, table_names: Any | None = None) -> None: ...
@@ -118,13 +117,10 @@ class BaseDatabaseWrapper:
118117
def chunked_cursor(self) -> CursorWrapper: ...
119118
def make_debug_cursor(self, cursor: CursorWrapper) -> CursorDebugWrapper: ...
120119
def make_cursor(self, cursor: CursorWrapper) -> CursorWrapper: ...
121-
@contextmanager
122-
def temporary_connection(self) -> Generator[CursorWrapper, None, None]: ...
123-
@contextmanager
124-
def _nodb_cursor(self) -> Generator[CursorWrapper, None, None]: ...
120+
def temporary_connection(self) -> AbstractContextManager[CursorWrapper]: ...
121+
def _nodb_cursor(self) -> AbstractContextManager[CursorWrapper]: ...
125122
def schema_editor(self, *args: Any, **kwargs: Any) -> BaseDatabaseSchemaEditor: ...
126123
def on_commit(self, func: Callable[[], object], robust: bool = False) -> None: ...
127124
def run_and_clear_commit_hooks(self) -> None: ...
128-
@contextmanager
129-
def execute_wrapper(self, wrapper: _ExecuteWrapper) -> Generator[None, None, None]: ...
125+
def execute_wrapper(self, wrapper: _ExecuteWrapper) -> AbstractContextManager[None]: ...
130126
def copy(self, alias: str | None = None) -> Self: ...

django-stubs/db/backends/oracle/base.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Generator, Iterator
2-
from contextlib import contextmanager
1+
from collections.abc import Iterator
2+
from contextlib import AbstractContextManager
33
from typing import Any
44

55
from django.db.backends.base.base import BaseDatabaseWrapper
@@ -11,8 +11,7 @@ from .introspection import DatabaseIntrospection
1111
from .operations import DatabaseOperations
1212
from .validation import DatabaseValidation
1313

14-
@contextmanager
15-
def wrap_oracle_errors() -> Generator[None, None, None]: ...
14+
def wrap_oracle_errors() -> AbstractContextManager[None]: ...
1615

1716
class _UninitializedOperatorsDescriptor:
1817
def __get__(self, instance: Any, cls: Any | None = None) -> Any: ...

django-stubs/db/backends/utils.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import datetime
2-
from collections.abc import Generator, Iterator, Mapping, Sequence
3-
from contextlib import contextmanager
2+
from collections.abc import Iterator, Mapping, Sequence
3+
from contextlib import AbstractContextManager
44
from decimal import Decimal
55
from logging import Logger
66
from types import TracebackType
@@ -62,14 +62,13 @@ class CursorWrapper:
6262
class CursorDebugWrapper(CursorWrapper):
6363
cursor: Any
6464
db: Any
65-
@contextmanager
6665
def debug_sql(
6766
self,
6867
sql: str | None = None,
6968
params: _ExecuteParameters | Sequence[_ExecuteParameters] | None = None,
7069
use_last_executed_query: bool = False,
7170
many: bool = False,
72-
) -> Generator[None, None, None]: ...
71+
) -> AbstractContextManager[None]: ...
7372

7473
@overload
7574
def typecast_date(s: None | Literal[""]) -> None: ... # type: ignore[overload-overlap]

django-stubs/db/migrations/state.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Iterable, Iterator, Sequence
2-
from contextlib import contextmanager
2+
from contextlib import AbstractContextManager
33
from typing import Any
44

55
from django.apps import AppConfig
@@ -92,8 +92,7 @@ class StateApps(Apps):
9292
def __init__(
9393
self, real_apps: list[str], models: dict[tuple[str, str], ModelState], ignore_swappable: bool = False
9494
) -> None: ...
95-
@contextmanager
96-
def bulk_update(self) -> Iterator[None]: ...
95+
def bulk_update(self) -> AbstractContextManager[None]: ...
9796
def clone(self) -> StateApps: ...
9897
def render_multiple(self, model_states: list[ModelState]) -> None: ...
9998
def register_model(self, app_label: str, model: type[Model]) -> None: ...

django-stubs/db/transaction.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from collections.abc import Callable, Iterator
2-
from contextlib import contextmanager
1+
from collections.abc import Callable
2+
from contextlib import AbstractContextManager
33
from types import TracebackType
44
from typing import Any, TypeVar, overload
55

@@ -18,8 +18,7 @@ def savepoint_commit(sid: str, using: str | None = None) -> None: ...
1818
def clean_savepoints(using: str | None = None) -> None: ...
1919
def get_rollback(using: str | None = None) -> bool: ...
2020
def set_rollback(rollback: bool, using: str | None = None) -> None: ...
21-
@contextmanager
22-
def mark_for_rollback_on_error(using: str | None = None) -> Iterator[None]: ...
21+
def mark_for_rollback_on_error(using: str | None = None) -> AbstractContextManager[None]: ...
2322
def on_commit(func: Callable[[], object], using: str | None = None, robust: bool = False) -> None: ...
2423

2524
_C = TypeVar("_C", bound=Callable) # Any callable

django-stubs/template/context.pyi

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from collections.abc import Callable, Iterable, Iterator
2-
from contextlib import contextmanager
2+
from contextlib import AbstractContextManager
33
from types import TracebackType
44
from typing import Any
55

@@ -56,15 +56,13 @@ class Context(BaseContext):
5656
use_l10n: bool | None = None,
5757
use_tz: bool | None = None,
5858
) -> None: ...
59-
@contextmanager
60-
def bind_template(self, template: Template) -> Iterator[None]: ...
59+
def bind_template(self, template: Template) -> AbstractContextManager[None]: ...
6160
def update(self, other_dict: dict[str, Any] | Context) -> ContextDict: ...
6261

6362
class RenderContext(BaseContext):
6463
dicts: list[dict[IncludeNode | str, str]]
6564
template: Template | None
66-
@contextmanager
67-
def push_state(self, template: Template, isolated_context: bool = True) -> Iterator[None]: ...
65+
def push_state(self, template: Template, isolated_context: bool = True) -> AbstractContextManager[None]: ...
6866

6967
class RequestContext(Context):
7068
autoescape: bool
@@ -84,8 +82,7 @@ class RequestContext(Context):
8482
autoescape: bool = True,
8583
) -> None: ...
8684
template: Template | None
87-
@contextmanager
88-
def bind_template(self, template: Template) -> Iterator[None]: ...
85+
def bind_template(self, template: Template) -> AbstractContextManager[None]: ...
8986
def new(self, values: _ContextValues | None = None) -> RequestContext: ...
9087

9188
def make_context(context: dict[str, Any] | None, request: HttpRequest | None = None, **kwargs: Any) -> Context: ...

django-stubs/test/runner.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from argparse import ArgumentParser
3-
from collections.abc import Iterator, Sequence
4-
from contextlib import contextmanager
3+
from collections.abc import Sequence
4+
from contextlib import AbstractContextManager
55
from io import StringIO
66
from typing import Any, Literal
77
from unittest import TestCase, TestLoader, TestSuite, TextTestResult, TextTestRunner
@@ -144,8 +144,7 @@ class DiscoverRunner:
144144
def log(self, msg: str, level: int | None) -> None: ...
145145
def setup_test_environment(self, **kwargs: Any) -> None: ...
146146
def setup_shuffler(self) -> None: ...
147-
@contextmanager
148-
def load_with_patterns(self) -> Iterator[None]: ...
147+
def load_with_patterns(self) -> AbstractContextManager[None]: ...
149148
def load_tests_for_label(self, label: str, discover_kwargs: dict[str, str]) -> TestSuite: ...
150149
def build_suite(self, test_labels: Sequence[str] | None = ..., **kwargs: Any) -> TestSuite: ...
151150
def setup_databases(self, **kwargs: Any) -> list[tuple[BaseDatabaseWrapper, str, bool]]: ...

django-stubs/test/selenium.pyi

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
from collections.abc import Generator
2-
from contextlib import contextmanager
1+
from contextlib import AbstractContextManager
32
from typing import Any
43

54
from django.test import LiveServerTestCase
@@ -14,5 +13,4 @@ class SeleniumTestCaseBase:
1413
class SeleniumTestCase(LiveServerTestCase):
1514
implicit_wait: int
1615
selenium: Any
17-
@contextmanager
18-
def disable_implicit_wait(self) -> Generator[None, None, None]: ...
16+
def disable_implicit_wait(self) -> AbstractContextManager[None]: ...

django-stubs/test/testcases.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import threading
22
import unittest
3-
from collections.abc import Callable, Collection, Generator, Iterable, Iterator, Mapping, Sequence
4-
from contextlib import contextmanager
3+
from collections.abc import Callable, Collection, Iterable, Iterator, Mapping, Sequence
4+
from contextlib import AbstractContextManager
55
from types import TracebackType
66
from typing import Any, overload
77

@@ -184,10 +184,9 @@ class TestCase(TransactionTestCase):
184184
@classmethod
185185
def setUpTestData(cls) -> None: ...
186186
@classmethod
187-
@contextmanager
188187
def captureOnCommitCallbacks(
189188
cls, *, using: str = ..., execute: bool = ...
190-
) -> Generator[list[Callable[[], Any]], None, None]: ...
189+
) -> AbstractContextManager[list[Callable[[], Any]]]: ...
191190

192191
class CheckCondition:
193192
conditions: Sequence[tuple[Callable, str]]

0 commit comments

Comments
 (0)