From 38e890584404164e1eca2afdd483c93c5957a255 Mon Sep 17 00:00:00 2001 From: spacemanspiff2007 <10754716+spacemanspiff2007@users.noreply.github.com> Date: Wed, 4 Dec 2024 07:39:24 +0100 Subject: [PATCH] Fixed some type hints --- src/HABApp/rule/scheduler/job_builder.py | 2 +- src/HABApp/util/rate_limiter/limits/base.py | 6 +++--- src/HABApp/util/rate_limiter/limits/fixed_window.py | 6 ++++++ src/HABApp/util/rate_limiter/limits/leaky_bucket.py | 5 ++++- src/HABApp/util/rate_limiter/registry.py | 4 ++-- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/HABApp/rule/scheduler/job_builder.py b/src/HABApp/rule/scheduler/job_builder.py index 67a37b4a..70468a87 100644 --- a/src/HABApp/rule/scheduler/job_builder.py +++ b/src/HABApp/rule/scheduler/job_builder.py @@ -67,7 +67,7 @@ def wrapped_func_executor(func: Any, args: Iterable = (), kwargs: Mapping[str, A class AsyncHABAppScheduler(AsyncScheduler): @override - def set_enabled(self, enabled: bool) -> Self: # noqa: FBT001 + def set_enabled(self, enabled: bool) -> Self: return run_func_from_async(super().set_enabled, enabled) diff --git a/src/HABApp/util/rate_limiter/limits/base.py b/src/HABApp/util/rate_limiter/limits/base.py index a7c058cd..af5f0858 100644 --- a/src/HABApp/util/rate_limiter/limits/base.py +++ b/src/HABApp/util/rate_limiter/limits/base.py @@ -35,13 +35,13 @@ def __repr__(self) -> str: f'{self.repr_text():s}>' ) - def do_test_allow(self): + def do_test_allow(self) -> None: raise NotImplementedError() - def do_allow(self): + def do_allow(self) -> None: raise NotImplementedError() - def do_deny(self): + def do_deny(self) -> None: raise NotImplementedError() def info(self) -> BaseRateLimitInfo: diff --git a/src/HABApp/util/rate_limiter/limits/fixed_window.py b/src/HABApp/util/rate_limiter/limits/fixed_window.py index 5aa46155..229b54e3 100644 --- a/src/HABApp/util/rate_limiter/limits/fixed_window.py +++ b/src/HABApp/util/rate_limiter/limits/fixed_window.py @@ -1,5 +1,6 @@ from dataclasses import dataclass from time import monotonic +from typing import override from .base import BaseRateLimit, BaseRateLimitInfo @@ -16,14 +17,17 @@ def __init__(self, allowed: int, interval: int, hits: int = 0) -> None: self.start: float = -1.0 self.stop: float = -1.0 + @override def repr_text(self) -> str: return f'window={self.stop - self.start:.0f}s' + @override def do_test_allow(self) -> None: if self.stop <= monotonic(): self.hits = 0 self.skips = 0 + @override def do_allow(self) -> None: now = monotonic() @@ -33,9 +37,11 @@ def do_allow(self) -> None: self.start = now self.stop = now + self.interval + @override def do_deny(self) -> None: self.stop = monotonic() + self.interval + @override def info(self) -> FixedWindowElasticExpiryLimitInfo: self.do_test_allow() diff --git a/src/HABApp/util/rate_limiter/limits/leaky_bucket.py b/src/HABApp/util/rate_limiter/limits/leaky_bucket.py index 650f6ac9..c08f83c8 100644 --- a/src/HABApp/util/rate_limiter/limits/leaky_bucket.py +++ b/src/HABApp/util/rate_limiter/limits/leaky_bucket.py @@ -1,6 +1,6 @@ from dataclasses import dataclass from time import monotonic -from typing import Final +from typing import Final, override from .base import BaseRateLimit, BaseRateLimitInfo @@ -17,9 +17,11 @@ def __init__(self, allowed: int, interval: int, hits: int = 0) -> None: self.drop_interval: Final = interval / allowed self.next_drop: float = monotonic() + self.drop_interval + @override def repr_text(self) -> str: return f'drop_interval={self.drop_interval:.1f}s' + @override def do_test_allow(self) -> None: while self.next_drop <= monotonic(): @@ -38,6 +40,7 @@ def do_test_allow(self) -> None: do_allow = do_test_allow do_deny = None + @override def info(self) -> LeakyBucketLimitInfo: self.do_test_allow() diff --git a/src/HABApp/util/rate_limiter/registry.py b/src/HABApp/util/rate_limiter/registry.py index 048c7e40..ff14c0a4 100644 --- a/src/HABApp/util/rate_limiter/registry.py +++ b/src/HABApp/util/rate_limiter/registry.py @@ -10,10 +10,10 @@ _LIMITERS: dict[str, Limiter] = {} -def RateLimiter(name: str) -> Limiter: +def RateLimiter(name: str) -> Limiter: # noqa: N802 """Create a new rate limiter or return an already existing one with a given name. - :param name: case insensitive name of limiter + :param name: case-insensitive name of limiter :return: Rate limiter object """