Skip to content

Add _typeshed.(Opt)ExcInfo #7645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion stdlib/_typeshed/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import ctypes
import mmap
import sys
from os import PathLike
from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar
from types import TracebackType
from typing import AbstractSet, Any, Awaitable, Container, Generic, Iterable, Protocol, TypeVar, Union
from typing_extensions import Final, Literal, TypeAlias, final

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

ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, TracebackType]
OptExcInfo: TypeAlias = Union[ExcInfo, tuple[None, None, None]]

# stable
if sys.version_info >= (3, 10):
from types import NoneType as NoneType
Expand Down
4 changes: 2 additions & 2 deletions stdlib/_typeshed/wsgi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# See the README.md file in this directory for more information.

import sys
from sys import _OptExcInfo
from _typeshed import OptExcInfo
from typing import Any, Callable, Iterable, Protocol
from typing_extensions import TypeAlias

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

WSGIEnvironment: TypeAlias = dict[str, Any] # stable
Expand Down
7 changes: 3 additions & 4 deletions stdlib/bdb.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from _typeshed import ExcInfo
from types import CodeType, FrameType, TracebackType
from typing import IO, Any, Callable, Iterable, Mapping, SupportsInt, TypeVar
from typing_extensions import Literal, ParamSpec, TypeAlias
Expand All @@ -7,14 +8,12 @@ __all__ = ["BdbQuit", "Bdb", "Breakpoint"]
_T = TypeVar("_T")
_P = ParamSpec("_P")
_TraceDispatch: TypeAlias = Callable[[FrameType, str, Any], Any] # TODO: Recursive type
_ExcInfo: TypeAlias = tuple[type[BaseException], BaseException, FrameType]

GENERATOR_AND_COROUTINE_FLAGS: Literal[672]

class BdbQuit(Exception): ...

class Bdb:

skip: set[str] | None
breaks: dict[str, list[int]]
fncache: dict[str, str]
Expand All @@ -31,7 +30,7 @@ class Bdb:
def dispatch_line(self, frame: FrameType) -> _TraceDispatch: ...
def dispatch_call(self, frame: FrameType, arg: None) -> _TraceDispatch: ...
def dispatch_return(self, frame: FrameType, arg: Any) -> _TraceDispatch: ...
def dispatch_exception(self, frame: FrameType, arg: _ExcInfo) -> _TraceDispatch: ...
def dispatch_exception(self, frame: FrameType, arg: ExcInfo) -> _TraceDispatch: ...
def is_skipped_module(self, module_name: str) -> bool: ...
def stop_here(self, frame: FrameType) -> bool: ...
def break_here(self, frame: FrameType) -> bool: ...
Expand All @@ -40,7 +39,7 @@ class Bdb:
def user_call(self, frame: FrameType, argument_list: None) -> None: ...
def user_line(self, frame: FrameType) -> None: ...
def user_return(self, frame: FrameType, return_value: Any) -> None: ...
def user_exception(self, frame: FrameType, exc_info: _ExcInfo) -> None: ...
def user_exception(self, frame: FrameType, exc_info: ExcInfo) -> None: ...
def set_until(self, frame: FrameType, lineno: int | None = ...) -> None: ...
def set_step(self) -> None: ...
def set_next(self, frame: FrameType) -> None: ...
Expand Down
13 changes: 5 additions & 8 deletions stdlib/cgitb.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from _typeshed import StrOrBytesPath
from _typeshed import OptExcInfo, StrOrBytesPath
from types import FrameType, TracebackType
from typing import IO, Any, Callable
from typing_extensions import TypeAlias

_ExcInfo: TypeAlias = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, tuple[type[BaseException], BaseException, TracebackType] | tuple[None, None, None] is a different type to tuple[type[BaseException] | None, BaseException | None, TracebackType | None], so this is a semantic change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but usually the latter was a shortcut used for the former. See the typical __exit__ annotation that should properly be annotated using an overload.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the latter is a supertype of the former

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's not strictly true that it's always either all-None or all-non-None, at least before 3.11. (Irit did some work in 3.11 that made it always true.) See here: https://github.com/python/cpython/blob/84c279b514141f608cf480905c87d48998e296d1/Python/ceval.c#L4474

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a remnant of Python 2 string exceptions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to err on the side of safety here and define _typeshed.OptExcInfo as tuple[type[BaseException] | None, BaseException | None, TracebackType | None] instead of tuple[type[BaseException], BaseException, TracebackType] | tuple[None, None, None]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primer output looks good and from a type safety perspective, I'd rather go with the more strict/correct annotation. Personally, I've not come across a situation where the type is None when the other fields are not, and allowing that could certainly introduce subtle bugs. It would also disagree with the documentation of both sys.exc_info and contextmanager.__exit__.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JelleZijlstra, do you know of any real-world examples where some (but not all) elements of the tuple are None?

If not, I'm fine with merging this, as it's probably just a theoretical issue :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From python/cpython#89874 (comment), sounds like it could only happen with interesting use of the C API, so probably not worth worrying about.

__UNDEF__: object # undocumented sentinel

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

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

def handler(info: _ExcInfo | None = ...) -> None: ...
def handler(info: OptExcInfo | None = ...) -> None: ...
def enable(display: int = ..., logdir: StrOrBytesPath | None = ..., context: int = ..., format: str = ...) -> None: ...
8 changes: 4 additions & 4 deletions stdlib/doctest.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import types
import unittest
from _typeshed import ExcInfo
from typing import Any, Callable, NamedTuple
from typing_extensions import TypeAlias

Expand Down Expand Up @@ -125,7 +126,6 @@ class DocTestFinder:
) -> list[DocTest]: ...

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

class DocTestRunner:
DIVIDER: str
Expand All @@ -138,7 +138,7 @@ class DocTestRunner:
def report_start(self, out: _Out, test: DocTest, example: Example) -> None: ...
def report_success(self, out: _Out, test: DocTest, example: Example, got: str) -> None: ...
def report_failure(self, out: _Out, test: DocTest, example: Example, got: str) -> None: ...
def report_unexpected_exception(self, out: _Out, test: DocTest, example: Example, exc_info: _ExcInfo) -> None: ...
def report_unexpected_exception(self, out: _Out, test: DocTest, example: Example, exc_info: ExcInfo) -> None: ...
def run(
self, test: DocTest, compileflags: int | None = ..., out: _Out | None = ..., clear_globs: bool = ...
) -> TestResults: ...
Expand All @@ -158,8 +158,8 @@ class DocTestFailure(Exception):
class UnexpectedException(Exception):
test: DocTest
example: Example
exc_info: _ExcInfo
def __init__(self, test: DocTest, example: Example, exc_info: _ExcInfo) -> None: ...
exc_info: ExcInfo
def __init__(self, test: DocTest, example: Example, exc_info: ExcInfo) -> None: ...

class DebugRunner(DocTestRunner): ...

Expand Down
10 changes: 4 additions & 6 deletions stdlib/sys.pyi
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import sys
from _typeshed import structseq
from _typeshed import OptExcInfo, structseq
from builtins import object as _object
from importlib.abc import PathEntryFinder
from importlib.machinery import ModuleSpec
from io import TextIOWrapper
from types import FrameType, ModuleType, TracebackType
from typing import Any, AsyncGenerator, Callable, Coroutine, NoReturn, Protocol, Sequence, TextIO, TypeVar, Union, overload
from typing import Any, AsyncGenerator, Callable, Coroutine, NoReturn, Protocol, Sequence, TextIO, TypeVar, overload
from typing_extensions import Literal, TypeAlias, final

_T = TypeVar("_T")

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

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

# sys.exit() accepts an optional argument of anything printable
def exit(__status: object = ...) -> NoReturn: ...
Expand Down
9 changes: 3 additions & 6 deletions stdlib/wsgiref/handlers.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from _typeshed import OptExcInfo
from _typeshed.wsgi import ErrorStream, InputStream, StartResponse, WSGIApplication, WSGIEnvironment
from abc import abstractmethod
from types import TracebackType
from typing import IO, Callable, MutableMapping
from typing_extensions import TypeAlias

from .headers import Headers
from .util import FileWrapper

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

_exc_info: TypeAlias = tuple[type[BaseException] | None, BaseException | None, TracebackType | None]

def format_date_time(timestamp: float | None) -> str: ... # undocumented
def read_environ() -> dict[str, str]: ...

Expand Down Expand Up @@ -40,7 +37,7 @@ class BaseHandler:
def set_content_length(self) -> None: ...
def cleanup_headers(self) -> None: ...
def start_response(
self, status: str, headers: list[tuple[str, str]], exc_info: _exc_info | None = ...
self, status: str, headers: list[tuple[str, str]], exc_info: OptExcInfo | None = ...
) -> Callable[[bytes], None]: ...
def send_preamble(self) -> None: ...
def write(self, data: bytes) -> None: ...
Expand All @@ -50,7 +47,7 @@ class BaseHandler:
def send_headers(self) -> None: ...
def result_is_file(self) -> bool: ...
def client_is_modern(self) -> bool: ...
def log_exception(self, exc_info: _exc_info) -> None: ...
def log_exception(self, exc_info: OptExcInfo) -> None: ...
def handle_error(self) -> None: ...
def error_output(self, environ: WSGIEnvironment, start_response: StartResponse) -> list[bytes]: ...
@abstractmethod
Expand Down