Skip to content

Commit bd62ee0

Browse files
authored
Type annotations for errors (#988)
1 parent ef4c318 commit bd62ee0

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ SCALA_VERSION?=2.13
55
KAFKA_VERSION?=2.8.1
66
DOCKER_IMAGE=aiolibs/kafka:$(SCALA_VERSION)_$(KAFKA_VERSION)
77
DIFF_BRANCH=origin/master
8-
FORMATTED_AREAS=aiokafka/util.py aiokafka/structs.py aiokafka/codec.py tests/test_codec.py
8+
FORMATTED_AREAS=\
9+
aiokafka/codec.py \
10+
aiokafka/errors.py \
11+
aiokafka/structs.py \
12+
aiokafka/util.py \
13+
tests/test_codec.py
914

1015
.PHONY: setup
1116
setup:

aiokafka/errors.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import inspect
2-
import sys
1+
from typing import Any, Iterable, Type, TypeVar
32

43
__all__ = [
54
# aiokafka custom errors
@@ -83,7 +82,7 @@ class KafkaError(RuntimeError):
8382
# whether metadata should be refreshed on error
8483
invalid_metadata = False
8584

86-
def __str__(self):
85+
def __str__(self) -> str:
8786
if not self.args:
8887
return self.__class__.__name__
8988
return f"{self.__class__.__name__}: {super().__str__()}"
@@ -140,7 +139,7 @@ class IncompatibleBrokerVersion(KafkaError):
140139

141140

142141
class CommitFailedError(KafkaError):
143-
def __init__(self, *args, **kwargs):
142+
def __init__(self, *args: Any, **kwargs: Any) -> None:
144143
super().__init__(
145144
"""Commit cannot be completed since the group has already
146145
rebalanced and assigned the partitions to another member.
@@ -223,19 +222,21 @@ class ProducerFenced(KafkaError):
223222

224223
def __init__(
225224
self,
226-
msg="There is a newer producer using the same transactional_id or"
227-
"transaction timeout occurred (check that processing time is "
228-
"below transaction_timeout_ms)",
229-
):
225+
msg: str = (
226+
"There is a newer producer using the same transactional_id or"
227+
"transaction timeout occurred (check that processing time is "
228+
"below transaction_timeout_ms)"
229+
),
230+
) -> None:
230231
super().__init__(msg)
231232

232233

233234
class BrokerResponseError(KafkaError):
234-
errno = None
235-
message = None
236-
description = None
235+
errno: int
236+
message: str
237+
description: str = ""
237238

238-
def __str__(self):
239+
def __str__(self) -> str:
239240
"""Add errno to standard KafkaError str"""
240241
return f"[Error {self.errno}] {super().__str__()}"
241242

@@ -859,18 +860,17 @@ class MemberIdRequired(BrokerResponseError):
859860
)
860861

861862

862-
def _iter_broker_errors():
863-
for _, obj in inspect.getmembers(sys.modules[__name__]):
864-
if (
865-
inspect.isclass(obj)
866-
and issubclass(obj, BrokerResponseError)
867-
and obj != BrokerResponseError
868-
):
869-
yield obj
863+
_T = TypeVar("_T", bound=type)
870864

871865

872-
kafka_errors = {x.errno: x for x in _iter_broker_errors()}
866+
def _iter_subclasses(cls: _T) -> Iterable[_T]:
867+
for subclass in cls.__subclasses__():
868+
yield subclass
869+
yield from _iter_subclasses(subclass)
873870

874871

875-
def for_code(error_code):
872+
kafka_errors = {x.errno: x for x in _iter_subclasses(BrokerResponseError)}
873+
874+
875+
def for_code(error_code: int) -> Type[BrokerResponseError]:
876876
return kafka_errors.get(error_code, UnknownError)

0 commit comments

Comments
 (0)