1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
"""Custom type definitions and shortcuts for annotating ``friendly_traceback``."""
import os
import sys
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, TypeVar, Union
if TYPE_CHECKING:
from _typeshed import StrPath
from .tb_data import TracebackData
else:
StrPath = Union[str, os.PathLike]
# TODO see https://www.daan.fyi/writings/python-protocols and define
# a more general exception that could include all possible fields
# declared as optional
# including new ones for SyntaxError in Python 3.10+
_E = TypeVar("_E", bound=BaseException)
if sys.version_info >= (3, 8):
from typing import Literal, Protocol, TypedDict
InclusionChoice = Literal[
"message",
"hint",
"what",
"why",
"where",
"friendly_tb",
"python_tb",
"debug_tb",
"explain",
"no_tb",
]
class Info(TypedDict, total=False):
header: str
message: str
original_python_traceback: str
simulated_python_traceback: str
shortened_traceback: str
exception_notes_intro: List[str]
exception_notes: str
suggest: str
generic: str
parsing_error: str
parsing_error_source: str
cause: str
detailed_tb: List[str]
last_call_header: str
last_call_source: str
last_call_variables: str
exception_raised_header: str
exception_raised_source: str
exception_raised_variables: str
warning_message: str
warning_location_header: str
warning_source: str
warning_variables: str
additional_variable_warning: str
lang: str
_tb_data: "TracebackData"
class Formatter(Protocol):
def __call__(self, info: Info, include: InclusionChoice = ...) -> str:
...
class CauseInfo(TypedDict, total=False):
cause: str
suggest: str
Site = Literal["friendly", "python", "bug", "email"]
ScopeKind = Literal["local", "global", "nonlocal"]
ObjectsInfo = TypedDict(
"ObjectsInfo",
{
"locals": List[Tuple[str, str, Any]],
"globals": List[Tuple[str, str, Any]],
"builtins": List[Tuple[str, str, Any]],
"expressions": List[Tuple[str, Any]],
"name, obj": List[Tuple[str, Any]],
},
)
SimilarNamesInfo = TypedDict(
"SimilarNamesInfo",
{"locals": List[str], "globals": List[str], "builtins": List[str], "best": str},
)
else:
InclusionChoice = str
Info = Dict[str, str]
Formatter = Callable[[Info, InclusionChoice], str]
Site = str
CauseInfo = Dict[str, str]
ScopeKind = str
ObjectsInfo = Dict[str, List[Any]]
SimilarNamesInfo = Dict[str, List[str]]
GenericExplain = Callable[[], str]
Parser = Union[
Callable[[str, "TracebackData"], CauseInfo],
Callable[[_E, "TracebackData"], CauseInfo],
]
Translator = Callable[[str], str]
Writer = Callable[[str], None]
|