Skip to content

Commit

Permalink
lint: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxuser committed Feb 7, 2025
1 parent 195718d commit 15d6bbb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
15 changes: 8 additions & 7 deletions tests/test_ratelimits.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import asyncio
from datetime import datetime, timedelta

from httpx import Response
import pytest
import asyncio

from tests.common import get_response_json
from xbox.webapi.api.provider.ratelimitedprovider import RateLimitedProvider

from xbox.webapi.common.exceptions import RateLimitExceededException, XboxException
from xbox.webapi.common.ratelimits import CombinedRateLimit
from xbox.webapi.common.ratelimits.models import TimePeriod

from tests.common import get_response_json


def helper_test_combinedratelimit(
crl: CombinedRateLimit, burstLimit: int, sustainLimit: int
Expand All @@ -18,8 +19,8 @@ def helper_test_combinedratelimit(
sustain = crl.get_limits_by_period(TimePeriod.SUSTAIN)

# These functions should return a list with one element
assert type(burst) == list
assert type(sustain) == list
assert isinstance(burst, list)
assert isinstance(sustain, list)

assert len(burst) == 1
assert len(sustain) == 1
Expand Down Expand Up @@ -111,7 +112,7 @@ async def make_request():
route = respx_mock.get("https://social.xboxlive.com").mock(
return_value=Response(200, json=get_response_json("people_summary_own"))
)
ret = await xbl_client.people.get_friends_summary_own()
await xbl_client.people.get_friends_summary_own()

assert route.called

Expand Down Expand Up @@ -175,7 +176,7 @@ async def make_request():
route = respx_mock.get("https://social.xboxlive.com").mock(
return_value=Response(200, json=get_response_json("people_summary_own"))
)
ret = await xbl_client.people.get_friends_summary_own()
await xbl_client.people.get_friends_summary_own()

assert route.called

Expand Down
9 changes: 5 additions & 4 deletions xbox/webapi/api/provider/ratelimitedprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
Subclassed by providers with rate limit support
"""

from typing import Union, Dict
from typing import Dict, Union

from xbox.webapi.api.provider.baseprovider import BaseProvider
from xbox.webapi.common.exceptions import XboxException
from xbox.webapi.common.ratelimits.models import LimitType, ParsedRateLimit, TimePeriod
from xbox.webapi.common.ratelimits import CombinedRateLimit
from xbox.webapi.common.ratelimits.models import LimitType, ParsedRateLimit, TimePeriod


class RateLimitedProvider(BaseProvider):
Expand Down Expand Up @@ -63,9 +64,9 @@ def __parse_rate_limit_key(
self, key: Union[int, Dict[str, int]], period: TimePeriod
) -> ParsedRateLimit:
key_type = type(key)
if key_type == int:
if isinstance(key_type, int):
return ParsedRateLimit(read=key, write=key, period=period)
elif key_type == dict:
elif isinstance(key_type, dict):
# TODO: schema here?
# Since the key-value pairs match we can just pass the dict to the model
return ParsedRateLimit(**key, period=period)
Expand Down
13 changes: 6 additions & 7 deletions xbox/webapi/common/ratelimits/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from abc import ABCMeta, abstractmethod
from datetime import datetime, timedelta
from typing import Union, List
from typing import List, Union

from xbox.webapi.common.ratelimits.models import (
IncrementResult,
LimitType,
ParsedRateLimit,
TimePeriod,
LimitType,
IncrementResult,
)

from abc import ABCMeta, abstractmethod


class RateLimit(metaclass=ABCMeta):
"""
Expand Down Expand Up @@ -209,7 +208,7 @@ def get_reset_after(self) -> Union[datetime, None]:

# Construct a new list with only elements of instance datetime
# (Effectively filtering out any None elements)
dates_valid = [elem for elem in dates if type(elem) == datetime]
dates_valid = [elem for elem in dates if isinstance(elem, datetime)]

# If dates_valid has any elements, return the one with the *later* timestamp.
# This means that if two or more limits have been exceeded, we wait for both to have reset (by returning the later timestamp)
Expand Down Expand Up @@ -238,7 +237,7 @@ def get_limits_by_period(self, period: TimePeriod) -> List[SingleRateLimit]:
def is_exceeded(self) -> bool:
"""
This function returns `True` if **any** rate limit has been exceeded.
It behaves like an OR logic gate.
"""

Expand Down

0 comments on commit 15d6bbb

Please sign in to comment.