Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add typing to aiokafka/protocol/* #999

Merged
merged 16 commits into from
Apr 21, 2024
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FORMATTED_AREAS=\
aiokafka/helpers.py \
aiokafka/structs.py \
aiokafka/util.py \
aiokafka/protocol/ \
tests/test_codec.py \
tests/test_helpers.py

Expand Down
12 changes: 8 additions & 4 deletions aiokafka/protocol/abstract.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import abc
from io import BytesIO
from typing import Generic, TypeVar

T = TypeVar("T")

class AbstractType(metaclass=abc.ABCMeta):

class AbstractType(Generic[T], metaclass=abc.ABCMeta):
@classmethod
@abc.abstractmethod
def encode(cls, value): ...
def encode(cls, value: T) -> bytes: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.

@classmethod
@abc.abstractmethod
def decode(cls, data): ...
def decode(cls, data: BytesIO) -> T: ...

Check notice

Code scanning / CodeQL

Statement has no effect Note

This statement has no effect.

@classmethod
def repr(cls, value):
def repr(cls, value: T) -> str:
return repr(value)
126 changes: 65 additions & 61 deletions aiokafka/protocol/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from collections.abc import Iterable
from typing import Dict, Optional, Tuple

from .api import Request, Response
from .types import (
Array,
Expand Down Expand Up @@ -68,16 +71,16 @@ class ApiVersionRequest_v2(Request):
SCHEMA = ApiVersionRequest_v0.SCHEMA


ApiVersionRequest = [
ApiVersionRequest = (
dimastbk marked this conversation as resolved.
Show resolved Hide resolved
ApiVersionRequest_v0,
ApiVersionRequest_v1,
ApiVersionRequest_v2,
]
ApiVersionResponse = [
)
ApiVersionResponse = (
ApiVersionResponse_v0,
ApiVersionResponse_v1,
ApiVersionResponse_v2,
]
)


class CreateTopicsResponse_v0(Response):
Expand Down Expand Up @@ -196,18 +199,18 @@ class CreateTopicsRequest_v3(Request):
SCHEMA = CreateTopicsRequest_v1.SCHEMA


CreateTopicsRequest = [
CreateTopicsRequest = (
CreateTopicsRequest_v0,
CreateTopicsRequest_v1,
CreateTopicsRequest_v2,
CreateTopicsRequest_v3,
]
CreateTopicsResponse = [
)
CreateTopicsResponse = (
CreateTopicsResponse_v0,
CreateTopicsResponse_v1,
CreateTopicsResponse_v2,
CreateTopicsResponse_v3,
]
)


class DeleteTopicsResponse_v0(Response):
Expand Down Expand Up @@ -267,18 +270,18 @@ class DeleteTopicsRequest_v3(Request):
SCHEMA = DeleteTopicsRequest_v0.SCHEMA


DeleteTopicsRequest = [
DeleteTopicsRequest = (
DeleteTopicsRequest_v0,
DeleteTopicsRequest_v1,
DeleteTopicsRequest_v2,
DeleteTopicsRequest_v3,
]
DeleteTopicsResponse = [
)
DeleteTopicsResponse = (
DeleteTopicsResponse_v0,
DeleteTopicsResponse_v1,
DeleteTopicsResponse_v2,
DeleteTopicsResponse_v3,
]
)


class ListGroupsResponse_v0(Response):
Expand Down Expand Up @@ -333,16 +336,16 @@ class ListGroupsRequest_v2(Request):
SCHEMA = ListGroupsRequest_v0.SCHEMA


ListGroupsRequest = [
ListGroupsRequest = (
ListGroupsRequest_v0,
ListGroupsRequest_v1,
ListGroupsRequest_v2,
]
ListGroupsResponse = [
)
ListGroupsResponse = (
ListGroupsResponse_v0,
ListGroupsResponse_v1,
ListGroupsResponse_v2,
]
)


class DescribeGroupsResponse_v0(Response):
Expand Down Expand Up @@ -429,8 +432,8 @@ class DescribeGroupsResponse_v3(Response):
("member_assignment", Bytes),
),
),
("authorized_operations", Int32),
),
("authorized_operations", Int32),
dimastbk marked this conversation as resolved.
Show resolved Hide resolved
),
)

Expand Down Expand Up @@ -465,18 +468,18 @@ class DescribeGroupsRequest_v3(Request):
)


DescribeGroupsRequest = [
DescribeGroupsRequest = (
DescribeGroupsRequest_v0,
DescribeGroupsRequest_v1,
DescribeGroupsRequest_v2,
DescribeGroupsRequest_v3,
]
DescribeGroupsResponse = [
)
DescribeGroupsResponse = (
DescribeGroupsResponse_v0,
DescribeGroupsResponse_v1,
DescribeGroupsResponse_v2,
DescribeGroupsResponse_v3,
]
)


class SaslHandShakeResponse_v0(Response):
Expand Down Expand Up @@ -507,8 +510,8 @@ class SaslHandShakeRequest_v1(Request):
SCHEMA = SaslHandShakeRequest_v0.SCHEMA


SaslHandShakeRequest = [SaslHandShakeRequest_v0, SaslHandShakeRequest_v1]
SaslHandShakeResponse = [SaslHandShakeResponse_v0, SaslHandShakeResponse_v1]
SaslHandShakeRequest = (SaslHandShakeRequest_v0, SaslHandShakeRequest_v1)
SaslHandShakeResponse = (SaslHandShakeResponse_v0, SaslHandShakeResponse_v1)


class DescribeAclsResponse_v0(Response):
Expand Down Expand Up @@ -610,8 +613,8 @@ class DescribeAclsRequest_v2(Request):
SCHEMA = DescribeAclsRequest_v1.SCHEMA


DescribeAclsRequest = [DescribeAclsRequest_v0, DescribeAclsRequest_v1]
DescribeAclsResponse = [DescribeAclsResponse_v0, DescribeAclsResponse_v1]
DescribeAclsRequest = (DescribeAclsRequest_v0, DescribeAclsRequest_v1)
DescribeAclsResponse = (DescribeAclsResponse_v0, DescribeAclsResponse_v1)


class CreateAclsResponse_v0(Response):
Expand Down Expand Up @@ -671,8 +674,8 @@ class CreateAclsRequest_v1(Request):
)


CreateAclsRequest = [CreateAclsRequest_v0, CreateAclsRequest_v1]
CreateAclsResponse = [CreateAclsResponse_v0, CreateAclsResponse_v1]
CreateAclsRequest = (CreateAclsRequest_v0, CreateAclsRequest_v1)
CreateAclsResponse = (CreateAclsResponse_v0, CreateAclsResponse_v1)


class DeleteAclsResponse_v0(Response):
Expand Down Expand Up @@ -771,8 +774,8 @@ class DeleteAclsRequest_v1(Request):
)


DeleteAclsRequest = [DeleteAclsRequest_v0, DeleteAclsRequest_v1]
DeleteAclsResponse = [DeleteAclsResponse_v0, DeleteAclsResponse_v1]
DeleteAclsRequest = (DeleteAclsRequest_v0, DeleteAclsRequest_v1)
DeleteAclsResponse = (DeleteAclsResponse_v0, DeleteAclsResponse_v1)


class AlterConfigsResponse_v0(Response):
Expand Down Expand Up @@ -828,8 +831,8 @@ class AlterConfigsRequest_v1(Request):
SCHEMA = AlterConfigsRequest_v0.SCHEMA


AlterConfigsRequest = [AlterConfigsRequest_v0, AlterConfigsRequest_v1]
AlterConfigsResponse = [AlterConfigsResponse_v0, AlterConfigsRequest_v1]
AlterConfigsRequest = (AlterConfigsRequest_v0, AlterConfigsRequest_v1)
AlterConfigsResponse = (AlterConfigsResponse_v0, AlterConfigsRequest_v1)


class DescribeConfigsResponse_v0(Response):
Expand Down Expand Up @@ -969,16 +972,16 @@ class DescribeConfigsRequest_v2(Request):
SCHEMA = DescribeConfigsRequest_v1.SCHEMA


DescribeConfigsRequest = [
DescribeConfigsRequest = (
DescribeConfigsRequest_v0,
DescribeConfigsRequest_v1,
DescribeConfigsRequest_v2,
]
DescribeConfigsResponse = [
)
DescribeConfigsResponse = (
DescribeConfigsResponse_v0,
DescribeConfigsResponse_v1,
DescribeConfigsResponse_v2,
]
)


class SaslAuthenticateResponse_v0(Response):
Expand Down Expand Up @@ -1016,14 +1019,14 @@ class SaslAuthenticateRequest_v1(Request):
SCHEMA = SaslAuthenticateRequest_v0.SCHEMA


SaslAuthenticateRequest = [
SaslAuthenticateRequest = (
SaslAuthenticateRequest_v0,
SaslAuthenticateRequest_v1,
]
SaslAuthenticateResponse = [
)
SaslAuthenticateResponse = (
SaslAuthenticateResponse_v0,
SaslAuthenticateResponse_v1,
]
)


class CreatePartitionsResponse_v0(Response):
Expand Down Expand Up @@ -1075,14 +1078,14 @@ class CreatePartitionsRequest_v1(Request):
RESPONSE_TYPE = CreatePartitionsResponse_v1


CreatePartitionsRequest = [
CreatePartitionsRequest = (
CreatePartitionsRequest_v0,
CreatePartitionsRequest_v1,
]
CreatePartitionsResponse = [
)
CreatePartitionsResponse = (
CreatePartitionsResponse_v0,
CreatePartitionsResponse_v1,
]
)


class DeleteGroupsResponse_v0(Response):
Expand Down Expand Up @@ -1114,12 +1117,12 @@ class DeleteGroupsRequest_v1(Request):
SCHEMA = DeleteGroupsRequest_v0.SCHEMA


DeleteGroupsRequest = [DeleteGroupsRequest_v0, DeleteGroupsRequest_v1]
DeleteGroupsRequest = (DeleteGroupsRequest_v0, DeleteGroupsRequest_v1)

DeleteGroupsResponse = [DeleteGroupsResponse_v0, DeleteGroupsResponse_v1]
DeleteGroupsResponse = (DeleteGroupsResponse_v0, DeleteGroupsResponse_v1)


class DescribeClientQuotasResponse_v0(Request):
class DescribeClientQuotasResponse_v0(Response):
API_KEY = 48
API_VERSION = 0
SCHEMA = Schema(
Expand Down Expand Up @@ -1159,13 +1162,9 @@ class DescribeClientQuotasRequest_v0(Request):
)


DescribeClientQuotasRequest = [
DescribeClientQuotasRequest_v0,
]
DescribeClientQuotasRequest = (DescribeClientQuotasRequest_v0,)

DescribeClientQuotasResponse = [
DescribeClientQuotasResponse_v0,
]
DescribeClientQuotasResponse = (DescribeClientQuotasResponse_v0,)


class AlterPartitionReassignmentsResponse_v0(Response):
Expand Down Expand Up @@ -1221,9 +1220,9 @@ class AlterPartitionReassignmentsRequest_v0(Request):
)


AlterPartitionReassignmentsRequest = [AlterPartitionReassignmentsRequest_v0]
AlterPartitionReassignmentsRequest = (AlterPartitionReassignmentsRequest_v0,)

AlterPartitionReassignmentsResponse = [AlterPartitionReassignmentsResponse_v0]
AlterPartitionReassignmentsResponse = (AlterPartitionReassignmentsResponse_v0,)


class ListPartitionReassignmentsResponse_v0(Response):
Expand Down Expand Up @@ -1273,9 +1272,9 @@ class ListPartitionReassignmentsRequest_v0(Request):
)


ListPartitionReassignmentsRequest = [ListPartitionReassignmentsRequest_v0]
ListPartitionReassignmentsRequest = (ListPartitionReassignmentsRequest_v0,)

ListPartitionReassignmentsResponse = [ListPartitionReassignmentsResponse_v0]
ListPartitionReassignmentsResponse = (ListPartitionReassignmentsResponse_v0,)


class DeleteRecordsResponse_v0(Response):
Expand Down Expand Up @@ -1385,7 +1384,12 @@ class DeleteRecordsRequest_v2(Request):
("tags", TaggedFields),
)

def __init__(self, topics, timeout_ms, tags=None):
def __init__(
self,
topics: Iterable[Tuple[str, Iterable[Tuple[int, int]]]],
timeout_ms: int,
tags: Optional[Dict[int, bytes]] = None,
) -> None:
super().__init__(
[
(
Expand All @@ -1403,14 +1407,14 @@ def __init__(self, topics, timeout_ms, tags=None):
)


DeleteRecordsRequest = [
DeleteRecordsRequest = (
DeleteRecordsRequest_v0,
DeleteRecordsRequest_v1,
DeleteRecordsRequest_v2,
]
)

DeleteRecordsResponse = [
DeleteRecordsResponse = (
DeleteRecordsResponse_v0,
DeleteRecordsResponse_v1,
DeleteRecordsResponse_v2,
]
)
Loading