Skip to content

Commit 03898de

Browse files
wbarnhabradenneal1
authored andcommitted
Use monkeytype to create some semblance of typing (dpkp#173)
* Add typing * define types as Struct for simplicity's sake
1 parent 2199bf8 commit 03898de

File tree

13 files changed

+373
-363
lines changed

13 files changed

+373
-363
lines changed

kafka/coordinator/assignors/abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AbstractPartitionAssignor(object):
1212
partition counts which are always needed in assignors).
1313
"""
1414

15-
@abc.abstractproperty
15+
@abc.abstractmethod
1616
def name(self):
1717
""".name should be a string identifying the assignor"""
1818
pass

kafka/coordinator/assignors/sticky/sticky_assignor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from collections import defaultdict, namedtuple
33
from copy import deepcopy
44

5-
from kafka.cluster import ClusterMetadata
65
from kafka.coordinator.assignors.abstract import AbstractPartitionAssignor
76
from kafka.coordinator.assignors.sticky.partition_movements import PartitionMovements
87
from kafka.coordinator.assignors.sticky.sorted_set import SortedSet

kafka/errors.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import inspect
22
import sys
3+
from typing import Any
34

45

56
class KafkaError(RuntimeError):
67
retriable = False
78
# whether metadata should be refreshed on error
89
invalid_metadata = False
910

10-
def __str__(self):
11+
def __str__(self) -> str:
1112
if not self.args:
1213
return self.__class__.__name__
1314
return '{}: {}'.format(self.__class__.__name__,
@@ -65,7 +66,7 @@ class IncompatibleBrokerVersion(KafkaError):
6566

6667

6768
class CommitFailedError(KafkaError):
68-
def __init__(self, *args, **kwargs):
69+
def __init__(self, *args, **kwargs) -> None:
6970
super().__init__(
7071
"""Commit cannot be completed since the group has already
7172
rebalanced and assigned the partitions to another member.
@@ -92,7 +93,7 @@ class BrokerResponseError(KafkaError):
9293
message = None
9394
description = None
9495

95-
def __str__(self):
96+
def __str__(self) -> str:
9697
"""Add errno to standard KafkaError str"""
9798
return '[Error {}] {}'.format(
9899
self.errno,
@@ -509,7 +510,7 @@ def _iter_broker_errors():
509510
kafka_errors = {x.errno: x for x in _iter_broker_errors()}
510511

511512

512-
def for_code(error_code):
513+
def for_code(error_code: int) -> Any:
513514
return kafka_errors.get(error_code, UnknownError)
514515

515516

kafka/protocol/api.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@ class Request(Struct):
5252

5353
FLEXIBLE_VERSION = False
5454

55-
@abc.abstractproperty
55+
@abc.abstractmethod
5656
def API_KEY(self):
5757
"""Integer identifier for api request"""
5858
pass
5959

60-
@abc.abstractproperty
60+
@abc.abstractmethod
6161
def API_VERSION(self):
6262
"""Integer of api request version"""
6363
pass
6464

65-
@abc.abstractproperty
65+
@abc.abstractmethod
6666
def SCHEMA(self):
6767
"""An instance of Schema() representing the request structure"""
6868
pass
6969

70-
@abc.abstractproperty
70+
@abc.abstractmethod
7171
def RESPONSE_TYPE(self):
7272
"""The Response class associated with the api request"""
7373
pass
@@ -93,17 +93,17 @@ def parse_response_header(self, read_buffer):
9393
class Response(Struct):
9494
__metaclass__ = abc.ABCMeta
9595

96-
@abc.abstractproperty
96+
@abc.abstractmethod
9797
def API_KEY(self):
9898
"""Integer identifier for api request/response"""
9999
pass
100100

101-
@abc.abstractproperty
101+
@abc.abstractmethod
102102
def API_VERSION(self):
103103
"""Integer of api request/response version"""
104104
pass
105105

106-
@abc.abstractproperty
106+
@abc.abstractmethod
107107
def SCHEMA(self):
108108
"""An instance of Schema() representing the response structure"""
109109
pass

kafka/protocol/struct.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from io import BytesIO
2+
from typing import List, Union
23

34
from kafka.protocol.abstract import AbstractType
45
from kafka.protocol.types import Schema
56

7+
68
from kafka.util import WeakMethod
79

810

911
class Struct(AbstractType):
1012
SCHEMA = Schema()
1113

12-
def __init__(self, *args, **kwargs):
14+
def __init__(self, *args, **kwargs) -> None:
1315
if len(args) == len(self.SCHEMA.fields):
1416
for i, name in enumerate(self.SCHEMA.names):
1517
self.__dict__[name] = args[i]
@@ -36,23 +38,23 @@ def encode(cls, item): # pylint: disable=E0202
3638
bits.append(field.encode(item[i]))
3739
return b''.join(bits)
3840

39-
def _encode_self(self):
41+
def _encode_self(self) -> bytes:
4042
return self.SCHEMA.encode(
4143
[self.__dict__[name] for name in self.SCHEMA.names]
4244
)
4345

4446
@classmethod
45-
def decode(cls, data):
47+
def decode(cls, data: Union[BytesIO, bytes]) -> "Struct":
4648
if isinstance(data, bytes):
4749
data = BytesIO(data)
4850
return cls(*[field.decode(data) for field in cls.SCHEMA.fields])
4951

50-
def get_item(self, name):
52+
def get_item(self, name: str) -> Union[int, List[List[Union[int, str, bool, List[List[Union[int, List[int]]]]]]], str, List[List[Union[int, str]]]]:
5153
if name not in self.SCHEMA.names:
5254
raise KeyError("%s is not in the schema" % name)
5355
return self.__dict__[name]
5456

55-
def __repr__(self):
57+
def __repr__(self) -> str:
5658
key_vals = []
5759
for name, field in zip(self.SCHEMA.names, self.SCHEMA.fields):
5860
key_vals.append(f'{name}={field.repr(self.__dict__[name])}')
@@ -61,7 +63,7 @@ def __repr__(self):
6163
def __hash__(self):
6264
return hash(self.encode())
6365

64-
def __eq__(self, other):
66+
def __eq__(self, other: "Struct") -> bool:
6567
if self.SCHEMA != other.SCHEMA:
6668
return False
6769
for attr in self.SCHEMA.names:

kafka/record/_crc32c.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
_MASK = 0xFFFFFFFF
9898

9999

100-
def crc_update(crc, data):
100+
def crc_update(crc: int, data: bytes) -> int:
101101
"""Update CRC-32C checksum with data.
102102
Args:
103103
crc: 32-bit checksum to update as long.
@@ -116,7 +116,7 @@ def crc_update(crc, data):
116116
return crc ^ _MASK
117117

118118

119-
def crc_finalize(crc):
119+
def crc_finalize(crc: int) -> int:
120120
"""Finalize CRC-32C checksum.
121121
This function should be called as last step of crc calculation.
122122
Args:
@@ -127,7 +127,7 @@ def crc_finalize(crc):
127127
return crc & _MASK
128128

129129

130-
def crc(data):
130+
def crc(data: bytes) -> int:
131131
"""Compute CRC-32C checksum of the data.
132132
Args:
133133
data: byte array, string or iterable over bytes.

kafka/record/abc.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,38 @@ class ABCRecord:
55
__metaclass__ = abc.ABCMeta
66
__slots__ = ()
77

8-
@abc.abstractproperty
8+
@abc.abstractmethod
99
def offset(self):
1010
""" Absolute offset of record
1111
"""
1212

13-
@abc.abstractproperty
13+
@abc.abstractmethod
1414
def timestamp(self):
1515
""" Epoch milliseconds
1616
"""
1717

18-
@abc.abstractproperty
18+
@abc.abstractmethod
1919
def timestamp_type(self):
2020
""" CREATE_TIME(0) or APPEND_TIME(1)
2121
"""
2222

23-
@abc.abstractproperty
23+
@abc.abstractmethod
2424
def key(self):
2525
""" Bytes key or None
2626
"""
2727

28-
@abc.abstractproperty
28+
@abc.abstractmethod
2929
def value(self):
3030
""" Bytes value or None
3131
"""
3232

33-
@abc.abstractproperty
33+
@abc.abstractmethod
3434
def checksum(self):
3535
""" Prior to v2 format CRC was contained in every message. This will
3636
be the checksum for v0 and v1 and None for v2 and above.
3737
"""
3838

39-
@abc.abstractproperty
39+
@abc.abstractmethod
4040
def headers(self):
4141
""" If supported by version list of key-value tuples, or empty list if
4242
not supported by format.

0 commit comments

Comments
 (0)