mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-10 21:34:18 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
72ad6dc953
commit
f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions
|
|
@ -10,27 +10,29 @@ import textwrap
|
|||
import traceback
|
||||
from functools import singledispatch
|
||||
from types import TracebackType
|
||||
from typing import Any, List, Optional
|
||||
from typing import Any
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
|
||||
from ._exceptions import BaseExceptionGroup
|
||||
|
||||
max_group_width = 15
|
||||
max_group_depth = 10
|
||||
_cause_message = (
|
||||
"\nThe above exception was the direct cause of the following exception:\n\n"
|
||||
'\nThe above exception was the direct cause of the following exception:\n\n'
|
||||
)
|
||||
|
||||
_context_message = (
|
||||
"\nDuring handling of the above exception, another exception occurred:\n\n"
|
||||
'\nDuring handling of the above exception, another exception occurred:\n\n'
|
||||
)
|
||||
|
||||
|
||||
def _format_final_exc_line(etype, value):
|
||||
valuestr = _safe_string(value, "exception")
|
||||
valuestr = _safe_string(value, 'exception')
|
||||
if value is None or not valuestr:
|
||||
line = f"{etype}\n"
|
||||
line = f'{etype}\n'
|
||||
else:
|
||||
line = f"{etype}: {valuestr}\n"
|
||||
line = f'{etype}: {valuestr}\n'
|
||||
|
||||
return line
|
||||
|
||||
|
|
@ -39,7 +41,7 @@ def _safe_string(value, what, func=str):
|
|||
try:
|
||||
return func(value)
|
||||
except BaseException:
|
||||
return f"<{what} {func.__name__}() failed>"
|
||||
return f'<{what} {func.__name__}() failed>'
|
||||
|
||||
|
||||
class _ExceptionPrintContext:
|
||||
|
|
@ -49,14 +51,14 @@ class _ExceptionPrintContext:
|
|||
self.need_close = False
|
||||
|
||||
def indent(self):
|
||||
return " " * (2 * self.exception_group_depth)
|
||||
return ' ' * (2 * self.exception_group_depth)
|
||||
|
||||
def emit(self, text_gen, margin_char=None):
|
||||
if margin_char is None:
|
||||
margin_char = "|"
|
||||
margin_char = '|'
|
||||
indent_str = self.indent()
|
||||
if self.exception_group_depth:
|
||||
indent_str += margin_char + " "
|
||||
indent_str += margin_char + ' '
|
||||
|
||||
if isinstance(text_gen, str):
|
||||
yield textwrap.indent(text_gen, indent_str, lambda line: True)
|
||||
|
|
@ -66,9 +68,9 @@ class _ExceptionPrintContext:
|
|||
|
||||
|
||||
def exceptiongroup_excepthook(
|
||||
etype: type[BaseException], value: BaseException, tb: TracebackType | None
|
||||
etype: type[BaseException], value: BaseException, tb: TracebackType | None,
|
||||
) -> None:
|
||||
sys.stderr.write("".join(traceback.format_exception(etype, value, tb)))
|
||||
sys.stderr.write(''.join(traceback.format_exception(etype, value, tb)))
|
||||
|
||||
|
||||
class PatchedTracebackException(traceback.TracebackException):
|
||||
|
|
@ -86,7 +88,7 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
) -> None:
|
||||
kwargs: dict[str, Any] = {}
|
||||
if sys.version_info >= (3, 10):
|
||||
kwargs["compact"] = compact
|
||||
kwargs['compact'] = compact
|
||||
|
||||
is_recursive_call = _seen is not None
|
||||
if _seen is None:
|
||||
|
|
@ -102,13 +104,13 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
self.exc_type = exc_type
|
||||
# Capture now to permit freeing resources: only complication is in the
|
||||
# unofficial API _format_final_exc_line
|
||||
self._str = _safe_string(exc_value, "exception")
|
||||
self._str = _safe_string(exc_value, 'exception')
|
||||
try:
|
||||
self.__notes__ = getattr(exc_value, "__notes__", None)
|
||||
self.__notes__ = getattr(exc_value, '__notes__', None)
|
||||
except KeyError:
|
||||
# Workaround for https://github.com/python/cpython/issues/98778 on Python
|
||||
# <= 3.9, and some 3.10 and 3.11 patch versions.
|
||||
HTTPError = getattr(sys.modules.get("urllib.error", None), "HTTPError", ())
|
||||
HTTPError = getattr(sys.modules.get('urllib.error', None), 'HTTPError', ())
|
||||
if sys.version_info[:2] <= (3, 11) and isinstance(exc_value, HTTPError):
|
||||
self.__notes__ = None
|
||||
else:
|
||||
|
|
@ -127,9 +129,9 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
self.end_lineno = str(end_lno) if end_lno is not None else None
|
||||
self.end_offset = exc_value.end_offset
|
||||
elif (
|
||||
exc_type
|
||||
and issubclass(exc_type, (NameError, AttributeError))
|
||||
and getattr(exc_value, "name", None) is not None
|
||||
exc_type and
|
||||
issubclass(exc_type, (NameError, AttributeError)) and
|
||||
getattr(exc_value, 'name', None) is not None
|
||||
):
|
||||
suggestion = _compute_suggestion_error(exc_value, exc_traceback)
|
||||
if suggestion:
|
||||
|
|
@ -171,10 +173,10 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
else:
|
||||
need_context = True
|
||||
if (
|
||||
e
|
||||
and e.__context__ is not None
|
||||
and need_context
|
||||
and id(e.__context__) not in _seen
|
||||
e and
|
||||
e.__context__ is not None and
|
||||
need_context and
|
||||
id(e.__context__) not in _seen
|
||||
):
|
||||
context = PatchedTracebackException(
|
||||
type(e.__context__),
|
||||
|
|
@ -243,12 +245,12 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
yield from _ctx.emit(msg)
|
||||
if exc.exceptions is None:
|
||||
if exc.stack:
|
||||
yield from _ctx.emit("Traceback (most recent call last):\n")
|
||||
yield from _ctx.emit('Traceback (most recent call last):\n')
|
||||
yield from _ctx.emit(exc.stack.format())
|
||||
yield from _ctx.emit(exc.format_exception_only())
|
||||
elif _ctx.exception_group_depth > max_group_depth:
|
||||
# exception group, but depth exceeds limit
|
||||
yield from _ctx.emit(f"... (max_group_depth is {max_group_depth})\n")
|
||||
yield from _ctx.emit(f'... (max_group_depth is {max_group_depth})\n')
|
||||
else:
|
||||
# format exception group
|
||||
is_toplevel = _ctx.exception_group_depth == 0
|
||||
|
|
@ -257,8 +259,8 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
|
||||
if exc.stack:
|
||||
yield from _ctx.emit(
|
||||
"Exception Group Traceback (most recent call last):\n",
|
||||
margin_char="+" if is_toplevel else None,
|
||||
'Exception Group Traceback (most recent call last):\n',
|
||||
margin_char='+' if is_toplevel else None,
|
||||
)
|
||||
yield from _ctx.emit(exc.stack.format())
|
||||
|
||||
|
|
@ -279,24 +281,24 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
truncated = i >= max_group_width
|
||||
else:
|
||||
truncated = False
|
||||
title = f"{i + 1}" if not truncated else "..."
|
||||
title = f'{i + 1}' if not truncated else '...'
|
||||
yield (
|
||||
_ctx.indent()
|
||||
+ ("+-" if i == 0 else " ")
|
||||
+ f"+---------------- {title} ----------------\n"
|
||||
_ctx.indent() +
|
||||
('+-' if i == 0 else ' ') +
|
||||
f'+---------------- {title} ----------------\n'
|
||||
)
|
||||
_ctx.exception_group_depth += 1
|
||||
if not truncated:
|
||||
yield from exc.exceptions[i].format(chain=chain, _ctx=_ctx)
|
||||
else:
|
||||
remaining = num_excs - max_group_width
|
||||
plural = "s" if remaining > 1 else ""
|
||||
plural = 's' if remaining > 1 else ''
|
||||
yield from _ctx.emit(
|
||||
f"and {remaining} more exception{plural}\n"
|
||||
f'and {remaining} more exception{plural}\n',
|
||||
)
|
||||
|
||||
if last_exc and _ctx.need_close:
|
||||
yield _ctx.indent() + "+------------------------------------\n"
|
||||
yield _ctx.indent() + '+------------------------------------\n'
|
||||
_ctx.need_close = False
|
||||
_ctx.exception_group_depth -= 1
|
||||
|
||||
|
|
@ -320,10 +322,10 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
|
||||
stype = self.exc_type.__qualname__
|
||||
smod = self.exc_type.__module__
|
||||
if smod not in ("__main__", "builtins"):
|
||||
if smod not in ('__main__', 'builtins'):
|
||||
if not isinstance(smod, str):
|
||||
smod = "<unknown>"
|
||||
stype = smod + "." + stype
|
||||
smod = '<unknown>'
|
||||
stype = smod + '.' + stype
|
||||
|
||||
if not issubclass(self.exc_type, SyntaxError):
|
||||
yield _format_final_exc_line(stype, self._str)
|
||||
|
|
@ -334,10 +336,10 @@ class PatchedTracebackException(traceback.TracebackException):
|
|||
|
||||
if isinstance(self.__notes__, collections.abc.Sequence):
|
||||
for note in self.__notes__:
|
||||
note = _safe_string(note, "note")
|
||||
yield from [line + "\n" for line in note.split("\n")]
|
||||
note = _safe_string(note, 'note')
|
||||
yield from [line + '\n' for line in note.split('\n')]
|
||||
elif self.__notes__ is not None:
|
||||
yield _safe_string(self.__notes__, "__notes__", func=repr)
|
||||
yield _safe_string(self.__notes__, '__notes__', func=repr)
|
||||
|
||||
|
||||
traceback_exception_original_format = traceback.TracebackException.format
|
||||
|
|
@ -345,7 +347,7 @@ traceback_exception_original_format_exception_only = (
|
|||
traceback.TracebackException.format_exception_only
|
||||
)
|
||||
traceback_exception_format_syntax_error = getattr(
|
||||
traceback.TracebackException, "_format_syntax_error", None
|
||||
traceback.TracebackException, '_format_syntax_error', None,
|
||||
)
|
||||
if sys.excepthook is sys.__excepthook__:
|
||||
traceback.TracebackException.__init__ = ( # type: ignore[assignment]
|
||||
|
|
@ -371,10 +373,10 @@ if sys.excepthook is sys.__excepthook__:
|
|||
# hook.
|
||||
#
|
||||
# More details: https://github.com/python-trio/trio/issues/1065
|
||||
if getattr(sys.excepthook, "__name__", None) in (
|
||||
"apport_excepthook",
|
||||
if getattr(sys.excepthook, '__name__', None) in (
|
||||
'apport_excepthook',
|
||||
# on ubuntu 22.10 the hook was renamed to partial_apport_excepthook
|
||||
"partial_apport_excepthook",
|
||||
'partial_apport_excepthook',
|
||||
):
|
||||
# patch traceback like above
|
||||
traceback.TracebackException.__init__ = ( # type: ignore[assignment]
|
||||
|
|
@ -394,36 +396,36 @@ if getattr(sys.excepthook, "__name__", None) in (
|
|||
assert sys.excepthook is apport_python_hook.apport_excepthook
|
||||
|
||||
# monkeypatch the sys module that apport has imported
|
||||
fake_sys = ModuleType("exceptiongroup_fake_sys")
|
||||
fake_sys = ModuleType('exceptiongroup_fake_sys')
|
||||
fake_sys.__dict__.update(sys.__dict__)
|
||||
fake_sys.__excepthook__ = exceptiongroup_excepthook
|
||||
apport_python_hook.sys = fake_sys
|
||||
|
||||
|
||||
@singledispatch
|
||||
def format_exception_only(__exc: BaseException) -> List[str]:
|
||||
def format_exception_only(__exc: BaseException) -> list[str]:
|
||||
return list(
|
||||
PatchedTracebackException(
|
||||
type(__exc), __exc, None, compact=True
|
||||
).format_exception_only()
|
||||
type(__exc), __exc, None, compact=True,
|
||||
).format_exception_only(),
|
||||
)
|
||||
|
||||
|
||||
@format_exception_only.register
|
||||
def _(__exc: type, value: BaseException) -> List[str]:
|
||||
def _(__exc: type, value: BaseException) -> list[str]:
|
||||
return format_exception_only(value)
|
||||
|
||||
|
||||
@singledispatch
|
||||
def format_exception(
|
||||
__exc: BaseException,
|
||||
limit: Optional[int] = None,
|
||||
limit: int | None = None,
|
||||
chain: bool = True,
|
||||
) -> List[str]:
|
||||
) -> list[str]:
|
||||
return list(
|
||||
PatchedTracebackException(
|
||||
type(__exc), __exc, __exc.__traceback__, limit=limit, compact=True
|
||||
).format(chain=chain)
|
||||
type(__exc), __exc, __exc.__traceback__, limit=limit, compact=True,
|
||||
).format(chain=chain),
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -432,16 +434,16 @@ def _(
|
|||
__exc: type,
|
||||
value: BaseException,
|
||||
tb: TracebackType,
|
||||
limit: Optional[int] = None,
|
||||
limit: int | None = None,
|
||||
chain: bool = True,
|
||||
) -> List[str]:
|
||||
) -> list[str]:
|
||||
return format_exception(value, limit, chain)
|
||||
|
||||
|
||||
@singledispatch
|
||||
def print_exception(
|
||||
__exc: BaseException,
|
||||
limit: Optional[int] = None,
|
||||
limit: int | None = None,
|
||||
file: Any = None,
|
||||
chain: bool = True,
|
||||
) -> None:
|
||||
|
|
@ -449,9 +451,9 @@ def print_exception(
|
|||
file = sys.stderr
|
||||
|
||||
for line in PatchedTracebackException(
|
||||
type(__exc), __exc, __exc.__traceback__, limit=limit
|
||||
type(__exc), __exc, __exc.__traceback__, limit=limit,
|
||||
).format(chain=chain):
|
||||
print(line, file=file, end="")
|
||||
print(line, file=file, end='')
|
||||
|
||||
|
||||
@print_exception.register
|
||||
|
|
@ -459,7 +461,7 @@ def _(
|
|||
__exc: type,
|
||||
value: BaseException,
|
||||
tb: TracebackType,
|
||||
limit: Optional[int] = None,
|
||||
limit: int | None = None,
|
||||
file: Any = None,
|
||||
chain: bool = True,
|
||||
) -> None:
|
||||
|
|
@ -467,7 +469,7 @@ def _(
|
|||
|
||||
|
||||
def print_exc(
|
||||
limit: Optional[int] = None,
|
||||
limit: int | None = None,
|
||||
file: Any | None = None,
|
||||
chain: bool = True,
|
||||
) -> None:
|
||||
|
|
@ -494,11 +496,11 @@ def _substitution_cost(ch_a, ch_b):
|
|||
|
||||
|
||||
def _compute_suggestion_error(exc_value, tb):
|
||||
wrong_name = getattr(exc_value, "name", None)
|
||||
wrong_name = getattr(exc_value, 'name', None)
|
||||
if wrong_name is None or not isinstance(wrong_name, str):
|
||||
return None
|
||||
if isinstance(exc_value, AttributeError):
|
||||
obj = getattr(exc_value, "obj", _SENTINEL)
|
||||
obj = getattr(exc_value, 'obj', _SENTINEL)
|
||||
if obj is _SENTINEL:
|
||||
return None
|
||||
obj = exc_value.obj
|
||||
|
|
@ -532,7 +534,7 @@ def _compute_suggestion_error(exc_value, tb):
|
|||
# Don't take matches we've already beaten.
|
||||
max_distance = min(max_distance, best_distance - 1)
|
||||
current_distance = _levenshtein_distance(
|
||||
wrong_name, possible_name, max_distance
|
||||
wrong_name, possible_name, max_distance,
|
||||
)
|
||||
if current_distance > max_distance:
|
||||
continue
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue