Skip to content

Commit 7b02f60

Browse files
Resolve "Timeout for clients can't be modified unless self.TIMEOUT is set" (#457)
Fix the timeout evaluation and testing it using a helper function. Closes #456
1 parent e59dd7b commit 7b02f60

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎src/kraken/base_api/__init__.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ def defined(value: Any) -> bool: # noqa: ANN401
4747
return value is not None
4848

4949

50+
def _resolve_timeout(timeout: int, default: int, class_timeout: int) -> int:
51+
"""
52+
Resolves the timeout to use for a request: the explicitly passed
53+
``timeout`` if it deviates from the ``request`` method's ``default``,
54+
otherwise the client's ``TIMEOUT`` class attribute.
55+
"""
56+
return class_timeout if timeout == default else timeout
57+
58+
5059
def ensure_string(parameter_name: str) -> Callable:
5160
"""
5261
This function is intended to be used as decorator
@@ -376,7 +385,7 @@ def request( # noqa: PLR0913 # pylint: disable=too-many-arguments
376385
extra_params=extra_params,
377386
)
378387

379-
timeout: int = self.TIMEOUT if timeout != 10 else timeout # type: ignore[no-redef]
388+
timeout = _resolve_timeout(timeout, 10, self.TIMEOUT)
380389
self.__check_renew_session()
381390

382391
if method in {"GET", "DELETE"}:
@@ -605,7 +614,7 @@ async def request( # type: ignore[override] # pylint: disable=invalid-overridde
605614
query_str=query_str,
606615
extra_params=extra_params,
607616
)
608-
timeout: int = self.TIMEOUT if timeout != 10 else timeout # type: ignore[no-redef]
617+
timeout = _resolve_timeout(timeout, 10, self.TIMEOUT)
609618
await self.__check_renew_session()
610619

611620
if method in {"GET", "DELETE"}:
@@ -884,7 +893,7 @@ def request( # pylint: disable=too-many-arguments
884893
auth=auth,
885894
extra_params=extra_params,
886895
)
887-
timeout: int = self.TIMEOUT if timeout == 10 else timeout # type: ignore[no-redef]
896+
timeout = _resolve_timeout(timeout, 10, self.TIMEOUT)
888897
self.__check_renew_session()
889898

890899
if method in {"GET", "DELETE"}:
@@ -1083,7 +1092,7 @@ async def request( # type: ignore[override] # pylint: disable=arguments-differ,
10831092
auth=auth,
10841093
)
10851094

1086-
timeout = self.TIMEOUT if timeout != 10 else timeout
1095+
timeout = _resolve_timeout(timeout, 10, self.TIMEOUT)
10871096
await self.__check_renew_session()
10881097

10891098
if method in {"GET", "DELETE"}:

‎tests/spot/test_spot_base_api_unit.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import pytest
2020

21+
from kraken import base_api
2122
from kraken.base_api import ErrorHandler, SpotClient, defined, ensure_string
2223
from kraken.exceptions import KrakenInsufficientFundsError
2324

@@ -38,6 +39,14 @@ def test_defined(self: Self) -> None:
3839
assert defined([]) is True
3940
assert defined(None) is False
4041

42+
def test_resolve_timeout_falls_back_to_class_timeout(self: Self) -> None:
43+
"""Leaving ``timeout`` at the ``request`` default uses ``class_timeout``."""
44+
assert base_api._resolve_timeout(timeout=10, default=10, class_timeout=42) == 42
45+
46+
def test_resolve_timeout_uses_explicit_value(self: Self) -> None:
47+
"""A ``timeout`` deviating from the default is used as-is."""
48+
assert base_api._resolve_timeout(timeout=5, default=10, class_timeout=42) == 5
49+
4150
def test_ensure_string_joins_list(self: Self) -> None:
4251
"""A list argument is collapsed into a comma-joined string."""
4352

0 commit comments

Comments
 (0)