From 7457dfc896b77e9d432fece453c8ef9cb8523578 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 19 Oct 2022 09:54:39 +0100 Subject: [PATCH] Standardize all context manager __exit__ methods --- django-stubs/core/files/base.pyi | 5 ++--- django-stubs/core/mail/backends/base.pyi | 7 +++++-- django-stubs/db/backends/base/schema.pyi | 8 +++++++- django-stubs/db/backends/utils.pyi | 8 ++++---- django-stubs/db/transaction.pyi | 4 ++-- django-stubs/db/utils.pyi | 7 +++++-- django-stubs/template/context.pyi | 8 +++++++- django-stubs/test/testcases.pyi | 14 ++++++++++---- django-stubs/test/utils.pyi | 15 +++++++++++++-- django-stubs/utils/archive.pyi | 2 +- django-stubs/utils/timezone.pyi | 4 ++-- django-stubs/utils/translation/__init__.pyi | 4 ++-- 12 files changed, 60 insertions(+), 26 deletions(-) diff --git a/django-stubs/core/files/base.pyi b/django-stubs/core/files/base.pyi index 24cae1c04..2b092ee35 100644 --- a/django-stubs/core/files/base.pyi +++ b/django-stubs/core/files/base.pyi @@ -1,5 +1,4 @@ -import os -import types +from types import TracebackType from typing import IO, AnyStr, Iterator, Optional, Type, TypeVar, Union, type_check_only from django.core.files.utils import FileProxyMixin @@ -26,7 +25,7 @@ class File(FileProxyMixin[AnyStr], IO[AnyStr]): self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], - tb: Optional[types.TracebackType], + exc_tb: Optional[TracebackType], ) -> None: ... def open(self: _T, mode: Optional[str] = ...) -> _T: ... def close(self) -> None: ... diff --git a/django-stubs/core/mail/backends/base.pyi b/django-stubs/core/mail/backends/base.pyi index f578abc0f..2e8653f01 100644 --- a/django-stubs/core/mail/backends/base.pyi +++ b/django-stubs/core/mail/backends/base.pyi @@ -1,4 +1,4 @@ -import types +from types import TracebackType from typing import Any, Optional, Sequence, Type, TypeVar from django.core.mail.message import EmailMessage @@ -12,6 +12,9 @@ class BaseEmailBackend: def close(self) -> None: ... def __enter__(self: _T) -> _T: ... def __exit__( - self, exc_type: Type[BaseException], exc_value: BaseException, traceback: types.TracebackType + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], ) -> None: ... def send_messages(self, email_messages: Sequence[EmailMessage]) -> int: ... diff --git a/django-stubs/db/backends/base/schema.pyi b/django-stubs/db/backends/base/schema.pyi index b2ed2c5c2..3424757f3 100644 --- a/django-stubs/db/backends/base/schema.pyi +++ b/django-stubs/db/backends/base/schema.pyi @@ -1,4 +1,5 @@ from logging import Logger +from types import TracebackType from typing import Any, ContextManager, List, Optional, Sequence, Tuple, Type, Union from django.db.backends.base.base import BaseDatabaseWrapper @@ -47,7 +48,12 @@ class BaseDatabaseSchemaEditor(ContextManager[Any]): deferred_sql: Any = ... atomic: Any = ... def __enter__(self) -> BaseDatabaseSchemaEditor: ... - def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: ... + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: ... def execute(self, sql: Union[Statement, str], params: Optional[Sequence[Any]] = ...) -> None: ... def quote_name(self, name: str) -> str: ... def column_sql( diff --git a/django-stubs/db/backends/utils.pyi b/django-stubs/db/backends/utils.pyi index f89b64ae4..ca3a4f23b 100644 --- a/django-stubs/db/backends/utils.pyi +++ b/django-stubs/db/backends/utils.pyi @@ -1,8 +1,8 @@ import datetime -import types from contextlib import contextmanager from decimal import Decimal from logging import Logger +from types import TracebackType from typing import ( Any, Dict, @@ -48,9 +48,9 @@ class CursorWrapper: def __enter__(self) -> CursorWrapper: ... def __exit__( self, - type: Optional[Type[BaseException]], - value: Optional[BaseException], - traceback: Optional[types.TracebackType], + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], ) -> None: ... def callproc( self, procname: str, params: Optional[Sequence[Any]] = ..., kparams: Optional[Dict[str, int]] = ... diff --git a/django-stubs/db/transaction.pyi b/django-stubs/db/transaction.pyi index b376fda85..0532cd111 100644 --- a/django-stubs/db/transaction.pyi +++ b/django-stubs/db/transaction.pyi @@ -1,4 +1,4 @@ -from contextlib import ContextDecorator, contextmanager +from contextlib import contextmanager from types import TracebackType from typing import Any, Callable, Iterator, Optional, Type, TypeVar, overload @@ -35,7 +35,7 @@ class Atomic: self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], - traceback: Optional[TracebackType], + exc_tb: Optional[TracebackType], ) -> None: ... # Bare decorator diff --git a/django-stubs/db/utils.pyi b/django-stubs/db/utils.pyi index 68820eba8..67d37e6dc 100644 --- a/django-stubs/db/utils.pyi +++ b/django-stubs/db/utils.pyi @@ -1,5 +1,5 @@ from types import TracebackType -from typing import Any, Dict, Iterable, Iterator, List, Optional, Type +from typing import Any, Dict, Iterable, List, Optional, Type from django.apps import AppConfig from django.db.backends.base.base import BaseDatabaseWrapper @@ -24,7 +24,10 @@ class DatabaseErrorWrapper: def __init__(self, wrapper: Any) -> None: ... def __enter__(self) -> None: ... def __exit__( - self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: TracebackType + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], ) -> None: ... def load_backend(backend_name: str) -> Any: ... diff --git a/django-stubs/template/context.pyi b/django-stubs/template/context.pyi index 0f5864d40..9dc1e04bc 100644 --- a/django-stubs/template/context.pyi +++ b/django-stubs/template/context.pyi @@ -1,4 +1,5 @@ from contextlib import contextmanager +from types import TracebackType from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Type, TypeVar, Union from django.http.request import HttpRequest @@ -16,7 +17,12 @@ class ContextDict(dict): context: BaseContext = ... def __init__(self, context: BaseContext, *args: Any, **kwargs: Any) -> None: ... def __enter__(self) -> ContextDict: ... - def __exit__(self, *args: Any, **kwargs: Any) -> None: ... + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: ... class BaseContext(Iterable[Any]): def __init__(self, dict_: Any = ...) -> None: ... diff --git a/django-stubs/test/testcases.pyi b/django-stubs/test/testcases.pyi index ddd03de03..353b32e63 100644 --- a/django-stubs/test/testcases.pyi +++ b/django-stubs/test/testcases.pyi @@ -1,6 +1,7 @@ import threading import unittest from datetime import date +from types import TracebackType from typing import ( Any, Callable, @@ -50,10 +51,15 @@ class _AssertTemplateUsedContext: context: ContextList = ... def __init__(self, test_case: Any, template_name: Any) -> None: ... def on_template_render(self, sender: Any, signal: Any, template: Any, context: Any, **kwargs: Any) -> None: ... - def test(self): ... - def message(self): ... - def __enter__(self): ... - def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any): ... + def test(self) -> None: ... + def message(self) -> str: ... + def __enter__(self) -> _AssertTemplateUsedContext: ... + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: ... class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext): ... diff --git a/django-stubs/test/utils.pyi b/django-stubs/test/utils.pyi index 37fe37ad6..df4f97e79 100644 --- a/django-stubs/test/utils.pyi +++ b/django-stubs/test/utils.pyi @@ -3,6 +3,7 @@ from contextlib import contextmanager from decimal import Decimal from io import StringIO from logging import Logger +from types import TracebackType from typing import ( Any, Callable, @@ -61,7 +62,12 @@ class TestContextDecorator: def enable(self) -> Any: ... def disable(self) -> None: ... def __enter__(self) -> Optional[Apps]: ... - def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None: ... + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: ... def decorate_class(self, cls: _TestClass) -> _TestClass: ... def decorate_callable(self, func: _C) -> _C: ... def __call__(self, decorated: _DecoratedTest) -> Any: ... @@ -100,7 +106,12 @@ class CaptureQueriesContext: @property def captured_queries(self) -> List[Dict[str, str]]: ... def __enter__(self) -> CaptureQueriesContext: ... - def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None: ... + def __exit__( + self, + exc_type: Optional[Type[BaseException]], + exc_value: Optional[BaseException], + exc_tb: Optional[TracebackType], + ) -> None: ... class ignore_warnings(TestContextDecorator): ignore_kwargs: Dict[str, Any] = ... diff --git a/django-stubs/utils/archive.pyi b/django-stubs/utils/archive.pyi index 44ab041a5..c18db0887 100644 --- a/django-stubs/utils/archive.pyi +++ b/django-stubs/utils/archive.pyi @@ -14,7 +14,7 @@ class Archive: exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: Optional[TracebackType], - ) -> Optional[bool]: ... + ) -> None: ... def extract(self, to_path: str) -> None: ... def list(self) -> None: ... def close(self) -> None: ... diff --git a/django-stubs/utils/timezone.pyi b/django-stubs/utils/timezone.pyi index 699ca9e53..9856ee2c0 100644 --- a/django-stubs/utils/timezone.pyi +++ b/django-stubs/utils/timezone.pyi @@ -1,4 +1,3 @@ -import types from contextlib import ContextDecorator from datetime import date from datetime import datetime as datetime @@ -6,6 +5,7 @@ from datetime import time from datetime import timedelta as timedelta from datetime import timezone from datetime import tzinfo as tzinfo +from types import TracebackType from typing import Any, Optional, Type, Union, overload import pytz @@ -38,7 +38,7 @@ class override(ContextDecorator): self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], - traceback: Optional[types.TracebackType], + exc_tb: Optional[TracebackType], ) -> None: ... def localtime(value: Optional[datetime] = ..., timezone: Optional[_TzInfoT] = ...) -> datetime: ... diff --git a/django-stubs/utils/translation/__init__.pyi b/django-stubs/utils/translation/__init__.pyi index d82de72c9..4b1bbb4a6 100644 --- a/django-stubs/utils/translation/__init__.pyi +++ b/django-stubs/utils/translation/__init__.pyi @@ -1,6 +1,6 @@ import functools -import types from contextlib import ContextDecorator +from types import TracebackType from typing import Any, Callable, Optional, Type, Union from django.http.request import HttpRequest @@ -60,7 +60,7 @@ class override(ContextDecorator): self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], - traceback: Optional[types.TracebackType], + exc_tb: Optional[TracebackType], ) -> None: ... def get_language() -> str: ...