Skip to content

Commit 36002a2

Browse files
authored
Standardize all context manager __exit__ methods (#1194)
1 parent 54e75fb commit 36002a2

File tree

12 files changed

+60
-26
lines changed

12 files changed

+60
-26
lines changed

django-stubs/core/files/base.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import os
2-
import types
1+
from types import TracebackType
32
from typing import IO, AnyStr, Iterator, Optional, Type, TypeVar, Union, type_check_only
43

54
from django.core.files.utils import FileProxyMixin
@@ -26,7 +25,7 @@ class File(FileProxyMixin[AnyStr], IO[AnyStr]):
2625
self,
2726
exc_type: Optional[Type[BaseException]],
2827
exc_value: Optional[BaseException],
29-
tb: Optional[types.TracebackType],
28+
exc_tb: Optional[TracebackType],
3029
) -> None: ...
3130
def open(self: _T, mode: Optional[str] = ...) -> _T: ...
3231
def close(self) -> None: ...

django-stubs/core/mail/backends/base.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import types
1+
from types import TracebackType
22
from typing import Any, Optional, Sequence, Type, TypeVar
33

44
from django.core.mail.message import EmailMessage
@@ -12,6 +12,9 @@ class BaseEmailBackend:
1212
def close(self) -> None: ...
1313
def __enter__(self: _T) -> _T: ...
1414
def __exit__(
15-
self, exc_type: Type[BaseException], exc_value: BaseException, traceback: types.TracebackType
15+
self,
16+
exc_type: Optional[Type[BaseException]],
17+
exc_value: Optional[BaseException],
18+
exc_tb: Optional[TracebackType],
1619
) -> None: ...
1720
def send_messages(self, email_messages: Sequence[EmailMessage]) -> int: ...

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from logging import Logger
2+
from types import TracebackType
23
from typing import Any, ContextManager, List, Optional, Sequence, Tuple, Type, Union
34

45
from django.db.backends.base.base import BaseDatabaseWrapper
@@ -47,7 +48,12 @@ class BaseDatabaseSchemaEditor(ContextManager[Any]):
4748
deferred_sql: Any = ...
4849
atomic: Any = ...
4950
def __enter__(self) -> BaseDatabaseSchemaEditor: ...
50-
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: ...
51+
def __exit__(
52+
self,
53+
exc_type: Optional[Type[BaseException]],
54+
exc_value: Optional[BaseException],
55+
exc_tb: Optional[TracebackType],
56+
) -> None: ...
5157
def execute(self, sql: Union[Statement, str], params: Optional[Sequence[Any]] = ...) -> None: ...
5258
def quote_name(self, name: str) -> str: ...
5359
def column_sql(

django-stubs/db/backends/utils.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import datetime
2-
import types
32
from contextlib import contextmanager
43
from decimal import Decimal
54
from logging import Logger
5+
from types import TracebackType
66
from typing import (
77
Any,
88
Dict,
@@ -48,9 +48,9 @@ class CursorWrapper:
4848
def __enter__(self) -> CursorWrapper: ...
4949
def __exit__(
5050
self,
51-
type: Optional[Type[BaseException]],
52-
value: Optional[BaseException],
53-
traceback: Optional[types.TracebackType],
51+
exc_type: Optional[Type[BaseException]],
52+
exc_value: Optional[BaseException],
53+
exc_tb: Optional[TracebackType],
5454
) -> None: ...
5555
def callproc(
5656
self, procname: str, params: Optional[Sequence[Any]] = ..., kparams: Optional[Dict[str, int]] = ...

django-stubs/db/transaction.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from contextlib import ContextDecorator, contextmanager
1+
from contextlib import contextmanager
22
from types import TracebackType
33
from typing import Any, Callable, Iterator, Optional, Type, TypeVar, overload
44

@@ -35,7 +35,7 @@ class Atomic:
3535
self,
3636
exc_type: Optional[Type[BaseException]],
3737
exc_value: Optional[BaseException],
38-
traceback: Optional[TracebackType],
38+
exc_tb: Optional[TracebackType],
3939
) -> None: ...
4040

4141
# Bare decorator

django-stubs/db/utils.pyi

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from types import TracebackType
2-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Type
2+
from typing import Any, Dict, Iterable, List, Optional, Type
33

44
from django.apps import AppConfig
55
from django.db.backends.base.base import BaseDatabaseWrapper
@@ -24,7 +24,10 @@ class DatabaseErrorWrapper:
2424
def __init__(self, wrapper: Any) -> None: ...
2525
def __enter__(self) -> None: ...
2626
def __exit__(
27-
self, exc_type: Optional[Type[BaseException]], exc_value: Optional[BaseException], traceback: TracebackType
27+
self,
28+
exc_type: Optional[Type[BaseException]],
29+
exc_value: Optional[BaseException],
30+
exc_tb: Optional[TracebackType],
2831
) -> None: ...
2932

3033
def load_backend(backend_name: str) -> Any: ...

django-stubs/template/context.pyi

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from contextlib import contextmanager
2+
from types import TracebackType
23
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Type, TypeVar, Union
34

45
from django.http.request import HttpRequest
@@ -16,7 +17,12 @@ class ContextDict(dict):
1617
context: BaseContext = ...
1718
def __init__(self, context: BaseContext, *args: Any, **kwargs: Any) -> None: ...
1819
def __enter__(self) -> ContextDict: ...
19-
def __exit__(self, *args: Any, **kwargs: Any) -> None: ...
20+
def __exit__(
21+
self,
22+
exc_type: Optional[Type[BaseException]],
23+
exc_value: Optional[BaseException],
24+
exc_tb: Optional[TracebackType],
25+
) -> None: ...
2026

2127
class BaseContext(Iterable[Any]):
2228
def __init__(self, dict_: Any = ...) -> None: ...

django-stubs/test/testcases.pyi

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import threading
22
import unittest
33
from datetime import date
4+
from types import TracebackType
45
from typing import (
56
Any,
67
Callable,
@@ -50,10 +51,15 @@ class _AssertTemplateUsedContext:
5051
context: ContextList = ...
5152
def __init__(self, test_case: Any, template_name: Any) -> None: ...
5253
def on_template_render(self, sender: Any, signal: Any, template: Any, context: Any, **kwargs: Any) -> None: ...
53-
def test(self): ...
54-
def message(self): ...
55-
def __enter__(self): ...
56-
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any): ...
54+
def test(self) -> None: ...
55+
def message(self) -> str: ...
56+
def __enter__(self) -> _AssertTemplateUsedContext: ...
57+
def __exit__(
58+
self,
59+
exc_type: Optional[Type[BaseException]],
60+
exc_value: Optional[BaseException],
61+
exc_tb: Optional[TracebackType],
62+
) -> None: ...
5763

5864
class _AssertTemplateNotUsedContext(_AssertTemplateUsedContext): ...
5965

django-stubs/test/utils.pyi

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ from contextlib import contextmanager
33
from decimal import Decimal
44
from io import StringIO
55
from logging import Logger
6+
from types import TracebackType
67
from typing import (
78
Any,
89
Callable,
@@ -61,7 +62,12 @@ class TestContextDecorator:
6162
def enable(self) -> Any: ...
6263
def disable(self) -> None: ...
6364
def __enter__(self) -> Optional[Apps]: ...
64-
def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None: ...
65+
def __exit__(
66+
self,
67+
exc_type: Optional[Type[BaseException]],
68+
exc_value: Optional[BaseException],
69+
exc_tb: Optional[TracebackType],
70+
) -> None: ...
6571
def decorate_class(self, cls: _TestClass) -> _TestClass: ...
6672
def decorate_callable(self, func: _C) -> _C: ...
6773
def __call__(self, decorated: _DecoratedTest) -> Any: ...
@@ -100,7 +106,12 @@ class CaptureQueriesContext:
100106
@property
101107
def captured_queries(self) -> List[Dict[str, str]]: ...
102108
def __enter__(self) -> CaptureQueriesContext: ...
103-
def __exit__(self, exc_type: None, exc_value: None, traceback: None) -> None: ...
109+
def __exit__(
110+
self,
111+
exc_type: Optional[Type[BaseException]],
112+
exc_value: Optional[BaseException],
113+
exc_tb: Optional[TracebackType],
114+
) -> None: ...
104115

105116
class ignore_warnings(TestContextDecorator):
106117
ignore_kwargs: Dict[str, Any] = ...

django-stubs/utils/archive.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Archive:
1414
exc_type: Optional[Type[BaseException]],
1515
exc_value: Optional[BaseException],
1616
traceback: Optional[TracebackType],
17-
) -> Optional[bool]: ...
17+
) -> None: ...
1818
def extract(self, to_path: str) -> None: ...
1919
def list(self) -> None: ...
2020
def close(self) -> None: ...

django-stubs/utils/timezone.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import types
21
from contextlib import ContextDecorator
32
from datetime import date
43
from datetime import datetime as datetime
54
from datetime import time
65
from datetime import timedelta as timedelta
76
from datetime import timezone
87
from datetime import tzinfo as tzinfo
8+
from types import TracebackType
99
from typing import Any, Optional, Type, Union, overload
1010

1111
import pytz
@@ -38,7 +38,7 @@ class override(ContextDecorator):
3838
self,
3939
exc_type: Optional[Type[BaseException]],
4040
exc_value: Optional[BaseException],
41-
traceback: Optional[types.TracebackType],
41+
exc_tb: Optional[TracebackType],
4242
) -> None: ...
4343

4444
def localtime(value: Optional[datetime] = ..., timezone: Optional[_TzInfoT] = ...) -> datetime: ...

django-stubs/utils/translation/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import functools
2-
import types
32
from contextlib import ContextDecorator
3+
from types import TracebackType
44
from typing import Any, Callable, Optional, Type, Union
55

66
from django.http.request import HttpRequest
@@ -60,7 +60,7 @@ class override(ContextDecorator):
6060
self,
6161
exc_type: Optional[Type[BaseException]],
6262
exc_value: Optional[BaseException],
63-
traceback: Optional[types.TracebackType],
63+
exc_tb: Optional[TracebackType],
6464
) -> None: ...
6565

6666
def get_language() -> str: ...

0 commit comments

Comments
 (0)