[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-13 00:00:18 +00:00
parent 72ad6dc953
commit f4cd1ba0d6
813 changed files with 66015 additions and 58839 deletions

View file

@ -1,21 +1,28 @@
from __future__ import annotations
from collections.abc import Callable, Sequence
from collections.abc import Callable
from collections.abc import Sequence
from functools import partial
from inspect import getmro, isclass
from typing import TYPE_CHECKING, Generic, Type, TypeVar, cast, overload
from inspect import getmro
from inspect import isclass
from typing import cast
from typing import Generic
from typing import overload
from typing import Type
from typing import TYPE_CHECKING
from typing import TypeVar
if TYPE_CHECKING:
from typing import Self
_BaseExceptionT_co = TypeVar("_BaseExceptionT_co", bound=BaseException, covariant=True)
_BaseExceptionT = TypeVar("_BaseExceptionT", bound=BaseException)
_ExceptionT_co = TypeVar("_ExceptionT_co", bound=Exception, covariant=True)
_ExceptionT = TypeVar("_ExceptionT", bound=Exception)
_BaseExceptionT_co = TypeVar('_BaseExceptionT_co', bound=BaseException, covariant=True)
_BaseExceptionT = TypeVar('_BaseExceptionT', bound=BaseException)
_ExceptionT_co = TypeVar('_ExceptionT_co', bound=Exception, covariant=True)
_ExceptionT = TypeVar('_ExceptionT', bound=Exception)
def check_direct_subclass(
exc: BaseException, parents: tuple[type[BaseException]]
exc: BaseException, parents: tuple[type[BaseException]],
) -> bool:
for cls in getmro(exc.__class__)[:-1]:
if cls in parents:
@ -25,42 +32,42 @@ def check_direct_subclass(
def get_condition_filter(
condition: type[_BaseExceptionT]
| tuple[type[_BaseExceptionT], ...]
| Callable[[_BaseExceptionT_co], bool],
condition: type[_BaseExceptionT] |
tuple[type[_BaseExceptionT], ...] |
Callable[[_BaseExceptionT_co], bool],
) -> Callable[[_BaseExceptionT_co], bool]:
if isclass(condition) and issubclass(
cast(Type[BaseException], condition), BaseException
cast(Type[BaseException], condition), BaseException,
):
return partial(check_direct_subclass, parents=(condition,))
elif isinstance(condition, tuple):
if all(isclass(x) and issubclass(x, BaseException) for x in condition):
return partial(check_direct_subclass, parents=condition)
elif callable(condition):
return cast("Callable[[BaseException], bool]", condition)
return cast('Callable[[BaseException], bool]', condition)
raise TypeError("expected a function, exception type or tuple of exception types")
raise TypeError('expected a function, exception type or tuple of exception types')
class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
"""A combination of multiple unrelated exceptions."""
def __new__(
cls, __message: str, __exceptions: Sequence[_BaseExceptionT_co]
cls, __message: str, __exceptions: Sequence[_BaseExceptionT_co],
) -> Self:
if not isinstance(__message, str):
raise TypeError(f"argument 1 must be str, not {type(__message)}")
raise TypeError(f'argument 1 must be str, not {type(__message)}')
if not isinstance(__exceptions, Sequence):
raise TypeError("second argument (exceptions) must be a sequence")
raise TypeError('second argument (exceptions) must be a sequence')
if not __exceptions:
raise ValueError(
"second argument (exceptions) must be a non-empty sequence"
'second argument (exceptions) must be a non-empty sequence',
)
for i, exc in enumerate(__exceptions):
if not isinstance(exc, BaseException):
raise ValueError(
f"Item {i} of second argument (exceptions) is not an exception"
f'Item {i} of second argument (exceptions) is not an exception',
)
if cls is BaseExceptionGroup:
@ -72,11 +79,11 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
if not isinstance(exc, Exception):
if cls is ExceptionGroup:
raise TypeError(
"Cannot nest BaseExceptions in an ExceptionGroup"
'Cannot nest BaseExceptions in an ExceptionGroup',
)
else:
raise TypeError(
f"Cannot nest BaseExceptions in {cls.__name__!r}"
f'Cannot nest BaseExceptions in {cls.__name__!r}',
)
instance = super().__new__(cls, __message, __exceptions)
@ -87,10 +94,10 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
def add_note(self, note: str) -> None:
if not isinstance(note, str):
raise TypeError(
f"Expected a string, got note={note!r} (type {type(note).__name__})"
f'Expected a string, got note={note!r} (type {type(note).__name__})',
)
if not hasattr(self, "__notes__"):
if not hasattr(self, '__notes__'):
self.__notes__: list[str] = []
self.__notes__.append(note)
@ -107,27 +114,27 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
def subgroup(
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...],
) -> ExceptionGroup[_ExceptionT] | None:
...
@overload
def subgroup(
self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...]
self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...],
) -> BaseExceptionGroup[_BaseExceptionT] | None:
...
@overload
def subgroup(
self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
self, __condition: Callable[[_BaseExceptionT_co | Self], bool],
) -> BaseExceptionGroup[_BaseExceptionT_co] | None:
...
def subgroup(
self,
__condition: type[_BaseExceptionT]
| tuple[type[_BaseExceptionT], ...]
| Callable[[_BaseExceptionT_co | Self], bool],
__condition: type[_BaseExceptionT] |
tuple[type[_BaseExceptionT], ...] |
Callable[[_BaseExceptionT_co | Self], bool],
) -> BaseExceptionGroup[_BaseExceptionT] | None:
condition = get_condition_filter(__condition)
modified = False
@ -161,7 +168,7 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
def split(
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...],
) -> tuple[
ExceptionGroup[_ExceptionT] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
@ -170,7 +177,7 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
def split(
self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...]
self, __condition: type[_BaseExceptionT] | tuple[type[_BaseExceptionT], ...],
) -> tuple[
BaseExceptionGroup[_BaseExceptionT] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
@ -179,7 +186,7 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
def split(
self, __condition: Callable[[_BaseExceptionT_co | Self], bool]
self, __condition: Callable[[_BaseExceptionT_co | Self], bool],
) -> tuple[
BaseExceptionGroup[_BaseExceptionT_co] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
@ -188,19 +195,19 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
def split(
self,
__condition: type[_BaseExceptionT]
| tuple[type[_BaseExceptionT], ...]
| Callable[[_BaseExceptionT_co], bool],
__condition: type[_BaseExceptionT] |
tuple[type[_BaseExceptionT], ...] |
Callable[[_BaseExceptionT_co], bool],
) -> (
tuple[
ExceptionGroup[_ExceptionT] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
]
| tuple[
] |
tuple[
BaseExceptionGroup[_BaseExceptionT] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
]
| tuple[
] |
tuple[
BaseExceptionGroup[_BaseExceptionT_co] | None,
BaseExceptionGroup[_BaseExceptionT_co] | None,
]
@ -246,26 +253,26 @@ class BaseExceptionGroup(BaseException, Generic[_BaseExceptionT_co]):
@overload
def derive(
self, __excs: Sequence[_BaseExceptionT]
self, __excs: Sequence[_BaseExceptionT],
) -> BaseExceptionGroup[_BaseExceptionT]:
...
def derive(
self, __excs: Sequence[_BaseExceptionT]
self, __excs: Sequence[_BaseExceptionT],
) -> BaseExceptionGroup[_BaseExceptionT]:
eg = BaseExceptionGroup(self.message, __excs)
if hasattr(self, "__notes__"):
if hasattr(self, '__notes__'):
# Create a new list so that add_note() only affects one exceptiongroup
eg.__notes__ = list(self.__notes__)
return eg
def __str__(self) -> str:
suffix = "" if len(self._exceptions) == 1 else "s"
return f"{self.message} ({len(self._exceptions)} sub-exception{suffix})"
suffix = '' if len(self._exceptions) == 1 else 's'
return f'{self.message} ({len(self._exceptions)} sub-exception{suffix})'
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.message!r}, {self._exceptions!r})"
return f'{self.__class__.__name__}({self.message!r}, {self._exceptions!r})'
class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
@ -282,46 +289,46 @@ class ExceptionGroup(BaseExceptionGroup[_ExceptionT_co], Exception):
@overload # type: ignore[override]
def subgroup(
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...],
) -> ExceptionGroup[_ExceptionT] | None:
...
@overload
def subgroup(
self, __condition: Callable[[_ExceptionT_co | Self], bool]
self, __condition: Callable[[_ExceptionT_co | Self], bool],
) -> ExceptionGroup[_ExceptionT_co] | None:
...
def subgroup(
self,
__condition: type[_ExceptionT]
| tuple[type[_ExceptionT], ...]
| Callable[[_ExceptionT_co], bool],
__condition: type[_ExceptionT] |
tuple[type[_ExceptionT], ...] |
Callable[[_ExceptionT_co], bool],
) -> ExceptionGroup[_ExceptionT] | None:
return super().subgroup(__condition)
@overload
def split(
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...]
self, __condition: type[_ExceptionT] | tuple[type[_ExceptionT], ...],
) -> tuple[
ExceptionGroup[_ExceptionT] | None, ExceptionGroup[_ExceptionT_co] | None
ExceptionGroup[_ExceptionT] | None, ExceptionGroup[_ExceptionT_co] | None,
]:
...
@overload
def split(
self, __condition: Callable[[_ExceptionT_co | Self], bool]
self, __condition: Callable[[_ExceptionT_co | Self], bool],
) -> tuple[
ExceptionGroup[_ExceptionT_co] | None, ExceptionGroup[_ExceptionT_co] | None
ExceptionGroup[_ExceptionT_co] | None, ExceptionGroup[_ExceptionT_co] | None,
]:
...
def split(
self: Self,
__condition: type[_ExceptionT]
| tuple[type[_ExceptionT], ...]
| Callable[[_ExceptionT_co], bool],
__condition: type[_ExceptionT] |
tuple[type[_ExceptionT], ...] |
Callable[[_ExceptionT_co], bool],
) -> tuple[
ExceptionGroup[_ExceptionT_co] | None, ExceptionGroup[_ExceptionT_co] | None
ExceptionGroup[_ExceptionT_co] | None, ExceptionGroup[_ExceptionT_co] | None,
]:
return super().split(__condition)