typing around Node.location, reportinfo, repr_excinfo etc

This commit is contained in:
Daniel Hahler
2019-11-04 14:53:43 +01:00
parent d53794f916
commit 741f0fedd1
9 changed files with 100 additions and 52 deletions

View File

@@ -549,7 +549,7 @@ class ExceptionInfo(Generic[_E]):
funcargs: bool = False,
truncate_locals: bool = True,
chain: bool = True,
):
) -> Union["ReprExceptionInfo", "ExceptionChainRepr"]:
"""
Return str()able representation of this exception info.
@@ -818,19 +818,19 @@ class FormattedExcinfo:
return traceback, extraline
def repr_excinfo(self, excinfo):
def repr_excinfo(self, excinfo: ExceptionInfo) -> "ExceptionChainRepr":
repr_chain = (
[]
) # type: List[Tuple[ReprTraceback, Optional[ReprFileLocation], Optional[str]]]
e = excinfo.value
excinfo_ = excinfo # type: Optional[ExceptionInfo]
descr = None
seen = set() # type: Set[int]
while e is not None and id(e) not in seen:
seen.add(id(e))
if excinfo:
reprtraceback = self.repr_traceback(excinfo)
reprcrash = excinfo._getreprcrash()
if excinfo_:
reprtraceback = self.repr_traceback(excinfo_)
reprcrash = excinfo_._getreprcrash() # type: Optional[ReprFileLocation]
else:
# fallback to native repr if the exception doesn't have a traceback:
# ExceptionInfo objects require a full traceback to work
@@ -842,7 +842,7 @@ class FormattedExcinfo:
repr_chain += [(reprtraceback, reprcrash, descr)]
if e.__cause__ is not None and self.chain:
e = e.__cause__
excinfo = (
excinfo_ = (
ExceptionInfo((type(e), e, e.__traceback__))
if e.__traceback__
else None
@@ -852,7 +852,7 @@ class FormattedExcinfo:
e.__context__ is not None and not e.__suppress_context__ and self.chain
):
e = e.__context__
excinfo = (
excinfo_ = (
ExceptionInfo((type(e), e, e.__traceback__))
if e.__traceback__
else None
@@ -876,6 +876,9 @@ class TerminalRepr:
def __repr__(self):
return "<{} instance at {:0x}>".format(self.__class__, id(self))
def toterminal(self, tw) -> None:
raise NotImplementedError()
class ExceptionRepr(TerminalRepr):
def __init__(self) -> None:
@@ -884,7 +887,7 @@ class ExceptionRepr(TerminalRepr):
def addsection(self, name, content, sep="-"):
self.sections.append((name, content, sep))
def toterminal(self, tw):
def toterminal(self, tw) -> None:
for name, content, sep in self.sections:
tw.sep(sep, name)
tw.line(content)
@@ -899,7 +902,7 @@ class ExceptionChainRepr(ExceptionRepr):
self.reprtraceback = chain[-1][0]
self.reprcrash = chain[-1][1]
def toterminal(self, tw):
def toterminal(self, tw) -> None:
for element in self.chain:
element[0].toterminal(tw)
if element[2] is not None:
@@ -914,7 +917,7 @@ class ReprExceptionInfo(ExceptionRepr):
self.reprtraceback = reprtraceback
self.reprcrash = reprcrash
def toterminal(self, tw):
def toterminal(self, tw) -> None:
self.reprtraceback.toterminal(tw)
super().toterminal(tw)
@@ -927,7 +930,7 @@ class ReprTraceback(TerminalRepr):
self.extraline = extraline
self.style = style
def toterminal(self, tw):
def toterminal(self, tw) -> None:
# the entries might have different styles
for i, entry in enumerate(self.reprentries):
if entry.style == "long":
@@ -959,7 +962,7 @@ class ReprEntryNative(TerminalRepr):
def __init__(self, tblines):
self.lines = tblines
def toterminal(self, tw):
def toterminal(self, tw) -> None:
tw.write("".join(self.lines))
@@ -971,7 +974,7 @@ class ReprEntry(TerminalRepr):
self.reprfileloc = filelocrepr
self.style = style
def toterminal(self, tw):
def toterminal(self, tw) -> None:
if self.style == "short":
self.reprfileloc.toterminal(tw)
for line in self.lines:
@@ -1003,7 +1006,7 @@ class ReprFileLocation(TerminalRepr):
self.lineno = lineno
self.message = message
def toterminal(self, tw):
def toterminal(self, tw) -> None:
# filename and lineno output for each entry,
# using an output format that most editors unterstand
msg = self.message
@@ -1018,7 +1021,7 @@ class ReprLocals(TerminalRepr):
def __init__(self, lines):
self.lines = lines
def toterminal(self, tw):
def toterminal(self, tw) -> None:
for line in self.lines:
tw.line(line)
@@ -1027,7 +1030,7 @@ class ReprFuncArgs(TerminalRepr):
def __init__(self, args):
self.args = args
def toterminal(self, tw):
def toterminal(self, tw) -> None:
if self.args:
linesofar = ""
for name, value in self.args: