From e68ece386c028269931d91ba8a1fabb7ef8a2a70 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 21:21:43 +0000 Subject: [PATCH 1/4] Improve `traceback.FrameSummary` --- stdlib/traceback.pyi | 54 +++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/stdlib/traceback.pyi b/stdlib/traceback.pyi index 0e946762fbb8..ebd386a82a96 100644 --- a/stdlib/traceback.pyi +++ b/stdlib/traceback.pyi @@ -2,6 +2,7 @@ import sys from _typeshed import Self, SupportsWrite from types import FrameType, TracebackType from typing import IO, Any, Generator, Iterable, Iterator, Mapping, Optional, overload +from typing_extensions import Literal _PT = tuple[str, int, str, Optional[str]] @@ -127,25 +128,52 @@ class TracebackException: def format_exception_only(self) -> Generator[str, None, None]: ... class FrameSummary(Iterable[Any]): + if sys.version_info >= (3, 11): + def __init__( + self, + filename: str, + lineno: int, + name: str, + *, + lookup_line: bool = ..., + locals: Mapping[str, str] | None = ..., + line: str | None = ..., + end_lineno: int | None = ..., + colno: int | None = ..., + end_colno: int | None = ... + ) -> None: ... + end_lineno: int | None + colno: int | None + end_colno: int | None + else: + def __init__( + self, + filename: str, + lineno: int, + name: str, + *, + lookup_line: bool = ..., + locals: Mapping[str, str] | None = ..., + line: str | None = ... + ) -> None: ... filename: str lineno: int name: str - line: str locals: dict[str, str] | None - def __init__( - self, - filename: str, - lineno: int, - name: str, - *, - lookup_line: bool = ..., - locals: Mapping[str, str] | None = ..., - line: str | None = ..., - ) -> None: ... - # TODO: more precise typing for __getitem__ and __iter__, - # for a namedtuple-like view on (filename, lineno, name, str). + @property + def line(self) -> str | None: ... + @overload + def __getitem__(self, i: Literal[0]) -> str: ... + @overload + def __getitem__(self, i: Literal[1]) -> int: ... + @overload + def __getitem__(self, i: Literal[2]) -> str: ... + @overload + def __getitem__(self, i: Literal[3]) -> str | None: ... + @overload def __getitem__(self, i: int) -> Any: ... def __iter__(self) -> Iterator[Any]: ... + def __len__(self) -> Literal[4]: ... class StackSummary(list[FrameSummary]): @classmethod From ac9e9db4c44d88d5bd9bef1fb7989539eafcadec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 21:25:14 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/traceback.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/traceback.pyi b/stdlib/traceback.pyi index ebd386a82a96..8ffb6d1a8996 100644 --- a/stdlib/traceback.pyi +++ b/stdlib/traceback.pyi @@ -140,7 +140,7 @@ class FrameSummary(Iterable[Any]): line: str | None = ..., end_lineno: int | None = ..., colno: int | None = ..., - end_colno: int | None = ... + end_colno: int | None = ..., ) -> None: ... end_lineno: int | None colno: int | None @@ -154,7 +154,7 @@ class FrameSummary(Iterable[Any]): *, lookup_line: bool = ..., locals: Mapping[str, str] | None = ..., - line: str | None = ... + line: str | None = ..., ) -> None: ... filename: str lineno: int From faf178bcf0edd94be70b641e2bb1daeca3f3739e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 21:27:57 +0000 Subject: [PATCH 3/4] Update traceback.pyi --- stdlib/traceback.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/traceback.pyi b/stdlib/traceback.pyi index 8ffb6d1a8996..675bdded5328 100644 --- a/stdlib/traceback.pyi +++ b/stdlib/traceback.pyi @@ -173,7 +173,8 @@ class FrameSummary(Iterable[Any]): @overload def __getitem__(self, i: int) -> Any: ... def __iter__(self) -> Iterator[Any]: ... - def __len__(self) -> Literal[4]: ... + if sys.version_info >= (3, 8): + def __len__(self) -> Literal[4]: ... class StackSummary(list[FrameSummary]): @classmethod From ff9d033a2bc2a76d04d393cd85ef7d126cbf6185 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 21:38:21 +0000 Subject: [PATCH 4/4] `lineno` can also be `None`, judging by the source code --- stdlib/traceback.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/traceback.pyi b/stdlib/traceback.pyi index 675bdded5328..ee6c0772edde 100644 --- a/stdlib/traceback.pyi +++ b/stdlib/traceback.pyi @@ -132,7 +132,7 @@ class FrameSummary(Iterable[Any]): def __init__( self, filename: str, - lineno: int, + lineno: int | None, name: str, *, lookup_line: bool = ..., @@ -149,7 +149,7 @@ class FrameSummary(Iterable[Any]): def __init__( self, filename: str, - lineno: int, + lineno: int | None, name: str, *, lookup_line: bool = ..., @@ -157,7 +157,7 @@ class FrameSummary(Iterable[Any]): line: str | None = ..., ) -> None: ... filename: str - lineno: int + lineno: int | None name: str locals: dict[str, str] | None @property