Skip to content

Commit f365bdf

Browse files
Fix traceback.FrameSummary's "line" parameter
traceback.FrameSummary's "line" parameter is a string that is the text of a line of code, not an int that is the line number of a line of code. Also relax type of traceback.format_list's "extracted_list" parameter in 3.5-and-later from List[FrameSummary] to Iterable[FrameSummary]. Never ask for a List when asking for a Sequence will do, and never ask for a Sequence when asking for a Collection will do, and never ask for a Collection when asking for an Iterable will do (and so on in other cases, but this case stops at Iterable). Also add a note about deliberately not supporting for type-checking purposes the 3.4-and-earlier List[Tuple[str, int, str, Optional[str]]] semantics for traceback.format_list's "extracted_list" parameter. The traceback module currently supports the old type of parameter, but if authors are typechecking their 3.5-and-later code, it's reasonable to expect that they'll want to modernize their code to use FrameSummary objects rather than four-tuples.
1 parent c968124 commit f365bdf

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

stdlib/2and3/traceback.pyi

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Stubs for traceback
22

3-
from typing import Any, Dict, Generator, IO, Iterator, List, Mapping, Optional, Tuple, Type, Iterable
3+
from typing import Any, Dict, Generator, IO, Iterable, Iterator, List, Mapping, Optional, Tuple, Type
44
from types import FrameType, TracebackType
55
import sys
66

@@ -35,7 +35,12 @@ if sys.version_info >= (3, 5):
3535
def extract_tb(tb: Optional[TracebackType], limit: Optional[int] = ...) -> StackSummary: ...
3636
def extract_stack(f: Optional[FrameType] = ...,
3737
limit: Optional[int] = ...) -> StackSummary: ...
38-
def format_list(extracted_list: List[FrameSummary]) -> List[str]: ...
38+
# NOTE(nathaniel): extracted_list *could* be of type
39+
# "Iterable[Union[_PT, FrameSummary]]", but if authors are being diligent
40+
# enough to type-check their 3.5-or-later code, they're probably diligent
41+
# enough to pass FrameSummarys rather than make use of the
42+
# only-for-backwards-compatibility four-tuple parameter type semantics.
43+
def format_list(extracted_list: Iterable[FrameSummary]) -> List[str]: ...
3944
else:
4045
def extract_tb(tb: Optional[TracebackType], limit: Optional[int] = ...) -> List[_PT]: ...
4146
def extract_stack(f: Optional[FrameType] = ...,
@@ -90,8 +95,6 @@ if sys.version_info >= (3, 5):
9095
def format(self, *, chain: bool = ...) -> Generator[str, None, None]: ...
9196
def format_exception_only(self) -> Generator[str, None, None]: ...
9297

93-
94-
if sys.version_info >= (3, 5):
9598
class FrameSummary(Iterable):
9699
filename: str
97100
lineno: int
@@ -101,7 +104,7 @@ if sys.version_info >= (3, 5):
101104
def __init__(self, filename: str, lineno: int, name: str,
102105
lookup_line: bool = ...,
103106
locals: Optional[Mapping[str, str]] = ...,
104-
line: Optional[int] = ...) -> None: ...
107+
line: Optional[str] = ...) -> None: ...
105108
# TODO: more precise typing for __getitem__ and __iter__,
106109
# for a namedtuple-like view on (filename, lineno, name, str).
107110
def __getitem__(self, i: int) -> Any: ...

0 commit comments

Comments
 (0)