remove redundant stuff

This commit is contained in:
Fengqing Liu
2025-10-19 17:08:18 +11:00
parent e8144e9591
commit d466f46d6f
39 changed files with 372 additions and 573 deletions

View File

@@ -37,7 +37,7 @@ def format_traceback(exc: BaseException, **kwargs: Any) -> str:
Like `traceback.print_exc` but returns a string. Uses the passed-in exception.
Any additional `**kwargs` are passed to the underlaying `traceback.format_exception`.
"""
return ''.join(traceback.format_exception(type(exc), exc, exc.__traceback__, **kwargs))
return "".join(traceback.format_exception(type(exc), exc, exc.__traceback__, **kwargs))
def task_wrapper(
@@ -53,8 +53,9 @@ def task_wrapper(
Handles ExitRequest and ReloadRequest silently, logs other exceptions.
Critical tasks will attempt to find and close the Twitch instance on failure.
"""
def decorator(
afunc: abc.Callable[_P, abc.Coroutine[Any, Any, _T]]
afunc: abc.Callable[_P, abc.Coroutine[Any, Any, _T]],
) -> abc.Callable[_P, abc.Coroutine[Any, Any, _T]]:
@wraps(afunc)
async def wrapper(*args: _P.args, **kwargs: _P.kwargs):
@@ -69,6 +70,7 @@ def task_wrapper(
# there isn't an easy and sure way to obtain the Twitch instance here,
# but we can improvise finding it
from src.core.client import Twitch # cyclic import
probe = args and args[0] or None # extract from 'self' arg
if isinstance(probe, Twitch):
probe.close()
@@ -77,7 +79,9 @@ def task_wrapper(
if isinstance(probe, Twitch):
probe.close()
raise # raise up to the wrapping task
return wrapper
if afunc is None:
return decorator
return decorator(afunc)

View File

@@ -69,8 +69,7 @@ class ExponentialBackoff:
def __next__(self) -> float:
"""Generate the next delay value."""
value: float = (
pow(self.base, self.steps)
* random.uniform(self.variance_min, self.variance_max)
pow(self.base, self.steps) * random.uniform(self.variance_min, self.variance_max)
+ self.shift
)
if value > self.maximum:

View File

@@ -28,7 +28,7 @@ SERIALIZE_ENV: dict[str, Callable[[Any], object]] = {
def json_minify(data: JsonType | list[JsonType]) -> str:
"""Return minified JSON string (no whitespace) for payload usage."""
return json.dumps(data, separators=(',', ':'))
return json.dumps(data, separators=(",", ":"))
def _serialize(obj: Any) -> Any:
@@ -155,5 +155,5 @@ def json_save(path: Path, contents: Mapping[Any, Any], *, sort: bool = False) ->
contents: Data to serialize
sort: If True, sort keys alphabetically
"""
with open(path, 'w', encoding="utf8") as file:
with open(path, "w", encoding="utf8") as file:
json.dump(contents, file, default=_serialize, sort_keys=sort, indent=4)

View File

@@ -18,14 +18,14 @@ _T = TypeVar("_T")
def create_nonce(chars: str, length: int) -> str:
"""Generate a random nonce string of specified length from given characters."""
return ''.join(random.choices(chars, k=length))
return "".join(random.choices(chars, k=length))
def chunk(to_chunk: abc.Iterable[_T], chunk_length: int) -> abc.Generator[list[_T], None, None]:
"""Split an iterable into chunks of a specified length."""
list_to_chunk: list[_T] = list(to_chunk)
for i in range(0, len(list_to_chunk), chunk_length):
yield list_to_chunk[i:i + chunk_length]
yield list_to_chunk[i : i + chunk_length]
def deduplicate(iterable: abc.Iterable[_T]) -> list[_T]: