Skip to content

Commit

Permalink
Fixed some type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Dec 4, 2024
1 parent ea2225a commit 38e8905
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/HABApp/rule/scheduler/job_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions src/HABApp/util/rate_limiter/limits/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions src/HABApp/util/rate_limiter/limits/fixed_window.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from time import monotonic
from typing import override

from .base import BaseRateLimit, BaseRateLimitInfo

Expand All @@ -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()

Expand All @@ -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()

Expand Down
5 changes: 4 additions & 1 deletion src/HABApp/util/rate_limiter/limits/leaky_bucket.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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():
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/HABApp/util/rate_limiter/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand Down

0 comments on commit 38e8905

Please sign in to comment.