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