Skip to content

Commit f6df80b

Browse files
fixed typing
1 parent c5e2fcf commit f6df80b

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

circuit_breaker_box/retryer_base.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class BaseRetrier(abc.ABC, typing.Generic[ResponseType]):
1818
max_retries: int
1919
reraise: bool = True
20-
exceptions_to_retry: tuple[type[Exception]]
20+
exceptions_to_retry: tuple[type[Exception], ...]
2121
stop: tenacity.stop.stop_after_attempt = dataclasses.field(init=False)
2222
wait_strategy: tenacity.wait.wait_exponential_jitter = dataclasses.field(init=False)
2323
retry_cause: tenacity.retry_if_exception_type = dataclasses.field(init=False)
@@ -27,14 +27,6 @@ def __post_init__(self) -> None:
2727
self.wait_strategy = tenacity.wait_exponential_jitter()
2828
self.retry_cause = tenacity.retry_if_exception_type(self.exceptions_to_retry)
2929

30-
@abc.abstractmethod
31-
async def retry(
32-
self,
33-
coroutine: typing.Callable[P, typing.Awaitable[ResponseType]],
34-
*args: P.args,
35-
**kwargs: P.kwargs,
36-
) -> ResponseType: ...
37-
3830
@staticmethod
3931
def _log_attempts(retry_state: tenacity.RetryCallState) -> None:
4032
logger.info(

circuit_breaker_box/retryers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Retrier(BaseRetrier[ResponseType]):
1717
async def retry( # type: ignore[return]
1818
self,
1919
coroutine: typing.Callable[P, typing.Awaitable[ResponseType]],
20+
/,
2021
*args: P.args,
2122
**kwargs: P.kwargs,
2223
) -> ResponseType:
@@ -38,10 +39,12 @@ class RetrierCircuitBreaker(BaseRetrier[ResponseType]):
3839
async def retry( # type: ignore[return]
3940
self,
4041
coroutine: typing.Callable[P, typing.Awaitable[ResponseType]],
42+
/,
43+
host: str,
4144
*args: P.args,
4245
**kwargs: P.kwargs,
4346
) -> ResponseType:
44-
if not (host := str(kwargs.get("host", ""))):
47+
if not host:
4548
msg = "'host' argument should be defined"
4649
raise ValueError(msg)
4750

examples/example_retry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async def main() -> None:
2424
async def foo(request: httpx.Request, host: str) -> httpx.Response: # noqa: ARG001
2525
raise ZeroDivisionError
2626

27-
await retryer.retry(coroutine=foo, request=example_request, host=example_request.url.host)
27+
await retryer.retry(foo, request=example_request, host=example_request.url.host)
2828

2929

3030
if __name__ == "__main__":

examples/example_retry_circuit_breaker.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ async def main() -> None:
3030
retryer = RetrierCircuitBreaker[httpx.Response](
3131
circuit_breaker=circuit_breaker,
3232
max_retries=MAX_RETRIES,
33-
exceptions_to_retry=(ZeroDivisionError,),
33+
exceptions_to_retry=(ZeroDivisionError, httpx.RequestError),
3434
)
3535
example_request = httpx.Request("GET", httpx.URL("http://example.com"))
3636

37-
async def foo(request: httpx.Request, host: str) -> httpx.Response: # noqa: ARG001
37+
async def foo(request: httpx.Request) -> httpx.Response: # noqa: ARG001
3838
raise ZeroDivisionError
3939

4040
# will raise exception from circuit_breaker.raise_host_unavailable_error
41-
await retryer.retry(coroutine=foo, request=example_request, host=example_request.url.host)
41+
await retryer.retry(foo, example_request.url.host, example_request)
4242

4343

4444
if __name__ == "__main__":

tests/test_retriers.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,36 @@ async def test_retry(
1616
async def bar(request: httpx.Request) -> httpx.Response: # noqa: ARG001
1717
return httpx.Response(status_code=httpx.codes.OK)
1818

19-
response = await test_retry_without_circuit_breaker.retry(coroutine=bar, request=test_request)
19+
response = await test_retry_without_circuit_breaker.retry(bar, request=test_request)
2020
assert response.status_code == httpx.codes.OK
2121

2222
async def foo(request: httpx.Request) -> typing.NoReturn: # noqa: ARG001
2323
raise ZeroDivisionError
2424

2525
with pytest.raises(ZeroDivisionError):
26-
await test_retry_without_circuit_breaker.retry(coroutine=foo, request=test_request)
26+
await test_retry_without_circuit_breaker.retry(foo, request=test_request)
2727

2828

2929
async def test_retry_custom_circuit_breaker(
3030
test_retry_custom_circuit_breaker_in_memory: RetrierCircuitBreaker[httpx.Response],
3131
) -> None:
3232
test_request = httpx.AsyncClient().build_request(method="GET", url=SOME_HOST)
3333

34-
async def bar(request: httpx.Request, host: str) -> httpx.Response: # noqa: ARG001
34+
async def bar(request: httpx.Request) -> httpx.Response: # noqa: ARG001
3535
return httpx.Response(status_code=httpx.codes.OK)
3636

37-
response = await test_retry_custom_circuit_breaker_in_memory.retry(
38-
coroutine=bar, request=test_request, host=test_request.url.host
39-
)
37+
response = await test_retry_custom_circuit_breaker_in_memory.retry(bar, test_request.url.host, request=test_request)
4038
assert response.status_code == httpx.codes.OK
4139

42-
async def foo(request: httpx.Request, host: str) -> typing.NoReturn: # noqa: ARG001
40+
async def foo(request: httpx.Request) -> typing.NoReturn: # noqa: ARG001
4341
raise ZeroDivisionError
4442

4543
with pytest.raises(fastapi.exceptions.HTTPException, match=f"500: Host: {test_request.url.host} is unavailable"):
46-
await test_retry_custom_circuit_breaker_in_memory.retry(
47-
coroutine=foo, request=test_request, host=test_request.url.host
48-
)
44+
await test_retry_custom_circuit_breaker_in_memory.retry(foo, host=test_request.url.host, request=test_request)
4945

5046
with pytest.raises(ValueError, match="'host' argument should be defined"):
51-
await test_retry_custom_circuit_breaker_in_memory.retry( # type: ignore[call-arg]
52-
coroutine=foo,
47+
await test_retry_custom_circuit_breaker_in_memory.retry(
48+
foo,
5349
request=test_request,
50+
host="",
5451
)

0 commit comments

Comments
 (0)