Skip to content

Commit 321359c

Browse files
authored
Add _typeshed.(Opt)ExcInfo (#7645)
1 parent a24b765 commit 321359c

File tree

7 files changed

+26
-31
lines changed

7 files changed

+26
-31
lines changed

stdlib/_typeshed/__init__.pyi

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import ctypes
77
import mmap
88
import sys
99
from os import PathLike
10-
from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar
10+
from types import TracebackType
11+
from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar, Union
1112
from typing_extensions import Final, Literal, TypeAlias, final
1213

1314
_KT = TypeVar("_KT")
@@ -197,6 +198,9 @@ WriteableBuffer: TypeAlias = bytearray | memoryview | array.array[Any] | mmap.mm
197198
# Same as _WriteableBuffer, but also includes read-only buffer types (like bytes).
198199
ReadableBuffer: TypeAlias = ReadOnlyBuffer | WriteableBuffer # stable
199200

201+
ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
202+
OptExcInfo: TypeAlias = Union[ExcInfo, tuple[None, None, None]]
203+
200204
# stable
201205
if sys.version_info >= (3, 10):
202206
from types import NoneType as NoneType

stdlib/_typeshed/wsgi.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# See the README.md file in this directory for more information.
66

77
import sys
8-
from sys import _OptExcInfo
8+
from _typeshed import OptExcInfo
99
from typing import Any, Callable, Iterable, Protocol
1010
from typing_extensions import TypeAlias
1111

@@ -19,7 +19,7 @@ else:
1919
# stable
2020
class StartResponse(Protocol):
2121
def __call__(
22-
self, __status: str, __headers: list[tuple[str, str]], __exc_info: _OptExcInfo | None = ...
22+
self, __status: str, __headers: list[tuple[str, str]], __exc_info: OptExcInfo | None = ...
2323
) -> Callable[[bytes], object]: ...
2424

2525
WSGIEnvironment: TypeAlias = dict[str, Any] # stable

stdlib/bdb.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from _typeshed import ExcInfo
12
from types import CodeType, FrameType, TracebackType
23
from typing import IO, Any, Callable, Iterable, Mapping, SupportsInt, TypeVar
34
from typing_extensions import Literal, ParamSpec, TypeAlias
@@ -7,14 +8,12 @@ __all__ = ["BdbQuit", "Bdb", "Breakpoint"]
78
_T = TypeVar("_T")
89
_P = ParamSpec("_P")
910
_TraceDispatch: TypeAlias = Callable[[FrameType, str, Any], Any] # TODO: Recursive type
10-
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, FrameType]
1111

1212
GENERATOR_AND_COROUTINE_FLAGS: Literal[672]
1313

1414
class BdbQuit(Exception): ...
1515

1616
class Bdb:
17-
1817
skip: set[str] | None
1918
breaks: dict[str, list[int]]
2019
fncache: dict[str, str]
@@ -31,7 +30,7 @@ class Bdb:
3130
def dispatch_line(self, frame: FrameType) -> _TraceDispatch: ...
3231
def dispatch_call(self, frame: FrameType, arg: None) -> _TraceDispatch: ...
3332
def dispatch_return(self, frame: FrameType, arg: Any) -> _TraceDispatch: ...
34-
def dispatch_exception(self, frame: FrameType, arg: _ExcInfo) -> _TraceDispatch: ...
33+
def dispatch_exception(self, frame: FrameType, arg: ExcInfo) -> _TraceDispatch: ...
3534
def is_skipped_module(self, module_name: str) -> bool: ...
3635
def stop_here(self, frame: FrameType) -> bool: ...
3736
def break_here(self, frame: FrameType) -> bool: ...
@@ -40,7 +39,7 @@ class Bdb:
4039
def user_call(self, frame: FrameType, argument_list: None) -> None: ...
4140
def user_line(self, frame: FrameType) -> None: ...
4241
def user_return(self, frame: FrameType, return_value: Any) -> None: ...
43-
def user_exception(self, frame: FrameType, exc_info: _ExcInfo) -> None: ...
42+
def user_exception(self, frame: FrameType, exc_info: ExcInfo) -> None: ...
4443
def set_until(self, frame: FrameType, lineno: int | None = ...) -> None: ...
4544
def set_step(self) -> None: ...
4645
def set_next(self, frame: FrameType) -> None: ...

stdlib/cgitb.pyi

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
from _typeshed import StrOrBytesPath
1+
from _typeshed import OptExcInfo, StrOrBytesPath
22
from types import FrameType, TracebackType
33
from typing import IO, Any, Callable
4-
from typing_extensions import TypeAlias
5-
6-
_ExcInfo: TypeAlias = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]
74

85
__UNDEF__: object # undocumented sentinel
96

@@ -15,8 +12,8 @@ def lookup(name: str, frame: FrameType, locals: dict[str, Any]) -> tuple[str | N
1512
def scanvars(
1613
reader: Callable[[], bytes], frame: FrameType, locals: dict[str, Any]
1714
) -> list[tuple[str, str | None, Any]]: ... # undocumented
18-
def html(einfo: _ExcInfo, context: int = ...) -> str: ...
19-
def text(einfo: _ExcInfo, context: int = ...) -> str: ...
15+
def html(einfo: OptExcInfo, context: int = ...) -> str: ...
16+
def text(einfo: OptExcInfo, context: int = ...) -> str: ...
2017

2118
class Hook: # undocumented
2219
def __init__(
@@ -28,7 +25,7 @@ class Hook: # undocumented
2825
format: str = ...,
2926
) -> None: ...
3027
def __call__(self, etype: type[BaseException] | None, evalue: BaseException | None, etb: TracebackType | None) -> None: ...
31-
def handle(self, info: _ExcInfo | None = ...) -> None: ...
28+
def handle(self, info: OptExcInfo | None = ...) -> None: ...
3229

33-
def handler(info: _ExcInfo | None = ...) -> None: ...
30+
def handler(info: OptExcInfo | None = ...) -> None: ...
3431
def enable(display: int = ..., logdir: StrOrBytesPath | None = ..., context: int = ..., format: str = ...) -> None: ...

stdlib/doctest.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import types
22
import unittest
3+
from _typeshed import ExcInfo
34
from typing import Any, Callable, NamedTuple
45
from typing_extensions import TypeAlias
56

@@ -125,7 +126,6 @@ class DocTestFinder:
125126
) -> list[DocTest]: ...
126127

127128
_Out: TypeAlias = Callable[[str], Any]
128-
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, types.TracebackType]
129129

130130
class DocTestRunner:
131131
DIVIDER: str
@@ -138,7 +138,7 @@ class DocTestRunner:
138138
def report_start(self, out: _Out, test: DocTest, example: Example) -> None: ...
139139
def report_success(self, out: _Out, test: DocTest, example: Example, got: str) -> None: ...
140140
def report_failure(self, out: _Out, test: DocTest, example: Example, got: str) -> None: ...
141-
def report_unexpected_exception(self, out: _Out, test: DocTest, example: Example, exc_info: _ExcInfo) -> None: ...
141+
def report_unexpected_exception(self, out: _Out, test: DocTest, example: Example, exc_info: ExcInfo) -> None: ...
142142
def run(
143143
self, test: DocTest, compileflags: int | None = ..., out: _Out | None = ..., clear_globs: bool = ...
144144
) -> TestResults: ...
@@ -158,8 +158,8 @@ class DocTestFailure(Exception):
158158
class UnexpectedException(Exception):
159159
test: DocTest
160160
example: Example
161-
exc_info: _ExcInfo
162-
def __init__(self, test: DocTest, example: Example, exc_info: _ExcInfo) -> None: ...
161+
exc_info: ExcInfo
162+
def __init__(self, test: DocTest, example: Example, exc_info: ExcInfo) -> None: ...
163163

164164
class DebugRunner(DocTestRunner): ...
165165

stdlib/sys.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import sys
2-
from _typeshed import structseq
2+
from _typeshed import OptExcInfo, structseq
33
from builtins import object as _object
44
from importlib.abc import PathEntryFinder
55
from importlib.machinery import ModuleSpec
66
from io import TextIOWrapper
77
from types import FrameType, ModuleType, TracebackType
8-
from typing import Any, AsyncGenerator, Callable, Coroutine, NoReturn, Protocol, Sequence, TextIO, TypeVar, Union, overload
8+
from typing import Any, AsyncGenerator, Callable, Coroutine, NoReturn, Protocol, Sequence, TextIO, TypeVar, overload
99
from typing_extensions import Literal, TypeAlias, final
1010

1111
_T = TypeVar("_T")
1212

13-
# The following type alias are stub-only and do not exist during runtime
14-
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
15-
_OptExcInfo: TypeAlias = Union[_ExcInfo, tuple[None, None, None]]
13+
_OptExcInfo: TypeAlias = OptExcInfo # TODO: obsolete, remove fall 2022 or later
1614

1715
# Intentionally omits one deprecated and one optional method of `importlib.abc.MetaPathFinder`
1816
class _MetaPathFinder(Protocol):
@@ -217,7 +215,7 @@ def _getframe(__depth: int = ...) -> FrameType: ...
217215
def _debugmallocstats() -> None: ...
218216
def __displayhook__(__value: object) -> None: ...
219217
def __excepthook__(__exctype: type[BaseException], __value: BaseException, __traceback: TracebackType | None) -> None: ...
220-
def exc_info() -> _OptExcInfo: ...
218+
def exc_info() -> OptExcInfo: ...
221219

222220
# sys.exit() accepts an optional argument of anything printable
223221
def exit(__status: object = ...) -> NoReturn: ...

stdlib/wsgiref/handlers.pyi

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
from _typeshed import OptExcInfo
12
from _typeshed.wsgi import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment
23
from abc import abstractmethod
3-
from types import TracebackType
44
from typing import IO, Callable, MutableMapping
5-
from typing_extensions import TypeAlias
65

76
from .headers import Headers
87
from .util import FileWrapper
98

109
__all__ = ["BaseHandler", "SimpleHandler", "BaseCGIHandler", "CGIHandler", "IISCGIHandler", "read_environ"]
1110

12-
_exc_info: TypeAlias = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]
13-
1411
def format_date_time(timestamp: float | None) -> str: ... # undocumented
1512
def read_environ() -> dict[str, str]: ...
1613

@@ -40,7 +37,7 @@ class BaseHandler:
4037
def set_content_length(self) -> None: ...
4138
def cleanup_headers(self) -> None: ...
4239
def start_response(
43-
self, status: str, headers: list[tuple[str, str]], exc_info: _exc_info | None = ...
40+
self, status: str, headers: list[tuple[str, str]], exc_info: OptExcInfo | None = ...
4441
) -> Callable[[bytes], None]: ...
4542
def send_preamble(self) -> None: ...
4643
def write(self, data: bytes) -> None: ...
@@ -50,7 +47,7 @@ class BaseHandler:
5047
def send_headers(self) -> None: ...
5148
def result_is_file(self) -> bool: ...
5249
def client_is_modern(self) -> bool: ...
53-
def log_exception(self, exc_info: _exc_info) -> None: ...
50+
def log_exception(self, exc_info: OptExcInfo) -> None: ...
5451
def handle_error(self) -> None: ...
5552
def error_output(self, environ: WSGIEnvironment, start_response: StartResponse) -> list[bytes]: ...
5653
@abstractmethod

0 commit comments

Comments
 (0)