Skip to content

Commit 2de1683

Browse files
authored
Prefer ABC subclass to ABCmeta metaclass (#3055)
1 parent 4bdaa42 commit 2de1683

18 files changed

Lines changed: 105 additions & 105 deletions

File tree

kafka/consumer/subscription_state.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22
from collections import OrderedDict
33
from collections.abc import Sequence
44
from enum import IntEnum
@@ -731,7 +731,7 @@ def complete_validation(self, validated_position=None):
731731
self._position = validated_position
732732

733733

734-
class ConsumerRebalanceListener(metaclass=abc.ABCMeta):
734+
class ConsumerRebalanceListener(ABC):
735735
"""
736736
A callback interface that the user can implement to trigger custom actions
737737
when the set of partitions assigned to the consumer changes.
@@ -779,7 +779,7 @@ class ConsumerRebalanceListener(metaclass=abc.ABCMeta):
779779
taking over that partition has their on_partitions_assigned() callback
780780
called to load the state.
781781
"""
782-
@abc.abstractmethod
782+
@abstractmethod
783783
def on_partitions_revoked(self, revoked):
784784
"""
785785
A callback method the user can implement to provide handling of offset
@@ -801,7 +801,7 @@ def on_partitions_revoked(self, revoked):
801801
"""
802802
pass
803803

804-
@abc.abstractmethod
804+
@abstractmethod
805805
def on_partitions_assigned(self, assigned):
806806
"""
807807
A callback method the user can implement to provide handling of
@@ -840,7 +840,7 @@ def on_partitions_lost(self, lost):
840840
return self.on_partitions_revoked(lost)
841841

842842

843-
class AsyncConsumerRebalanceListener(metaclass=abc.ABCMeta):
843+
class AsyncConsumerRebalanceListener(ABC):
844844
"""
845845
Async variant of :class:`ConsumerRebalanceListener`.
846846
@@ -856,7 +856,7 @@ class AsyncConsumerRebalanceListener(metaclass=abc.ABCMeta):
856856
invokes ``on_partitions_assigned``. Both methods must be defined as
857857
``async def``; otherwise use :class:`ConsumerRebalanceListener`.
858858
"""
859-
@abc.abstractmethod
859+
@abstractmethod
860860
async def on_partitions_revoked(self, revoked):
861861
"""Async-callback for the start of a rebalance operation.
862862
@@ -870,7 +870,7 @@ async def on_partitions_revoked(self, revoked):
870870
"""
871871
pass
872872

873-
@abc.abstractmethod
873+
@abstractmethod
874874
async def on_partitions_assigned(self, assigned):
875875
"""Async-callback for the completion of a partition re-assignment.
876876

kafka/coordinator/assignors/abstract.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import abc
1+
from abc import ABC, abstractmethod, abstractproperty
22
from enum import IntEnum
33

44
from kafka.protocol.consumer.metadata import (
@@ -25,13 +25,13 @@ class RebalanceProtocol(IntEnum):
2525
COOPERATIVE = 1
2626

2727

28-
class AbstractPartitionAssignor(metaclass=abc.ABCMeta):
28+
class AbstractPartitionAssignor(ABC):
2929
"""
3030
Abstract assignor implementation which does some common grunt work (in particular collecting
3131
partition counts which are always needed in assignors).
3232
"""
3333

34-
@abc.abstractproperty
34+
@abstractproperty
3535
def name(self):
3636
""".name should be a string identifying the assignor"""
3737
pass
@@ -48,7 +48,7 @@ def supported_protocols(self):
4848
"""
4949
return [RebalanceProtocol.EAGER]
5050

51-
@abc.abstractmethod
51+
@abstractmethod
5252
def assign(self, cluster, members):
5353
"""Perform group assignment given cluster metadata and member subscriptions
5454
@@ -64,7 +64,7 @@ def assign(self, cluster, members):
6464
"""
6565
pass
6666

67-
@abc.abstractmethod
67+
@abstractmethod
6868
def metadata(self, topics):
6969
"""Generate ProtocolMetadata to be submitted via JoinGroupRequest.
7070
@@ -76,7 +76,7 @@ def metadata(self, topics):
7676
"""
7777
pass
7878

79-
@abc.abstractmethod
79+
@abstractmethod
8080
def on_assignment(self, assignment, generation):
8181
"""Callback that runs on each assignment.
8282

kafka/coordinator/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22
import copy
33
import logging
44
import threading
@@ -70,7 +70,7 @@ class UnjoinedGroupException(Errors.RetriableError):
7070
pass
7171

7272

73-
class BaseCoordinator(metaclass=abc.ABCMeta):
73+
class BaseCoordinator(ABC):
7474
"""
7575
BaseCoordinator implements group management for a single group member
7676
by interacting with a designated Kafka broker (the coordinator). Group
@@ -225,7 +225,7 @@ def group_id(self):
225225
def group_instance_id(self):
226226
return self.config['group_instance_id']
227227

228-
@abc.abstractmethod
228+
@abstractmethod
229229
def protocol_type(self):
230230
"""
231231
Unique identifier for the class of supported protocols
@@ -236,7 +236,7 @@ def protocol_type(self):
236236
"""
237237
pass
238238

239-
@abc.abstractmethod
239+
@abstractmethod
240240
def group_protocols(self):
241241
"""Return the list of supported group protocols and metadata.
242242
@@ -270,7 +270,7 @@ async def _on_join_prepare_async(self, generation, member_id, timeout_ms=None):
270270
"""
271271
pass
272272

273-
@abc.abstractmethod
273+
@abstractmethod
274274
def _perform_assignment(self, leader_id, protocol, members):
275275
"""Perform assignment for the group.
276276

kafka/metrics/compound_stat.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33
from kafka.metrics.stat import AbstractStat
44

55

6-
class AbstractCompoundStat(AbstractStat, metaclass=abc.ABCMeta):
6+
class AbstractCompoundStat(AbstractStat, ABC):
77
"""
88
A compound stat is a stat where a single measurement and associated
99
data structure feeds many metrics. This is the example for a
1010
histogram which has many associated percentiles.
1111
"""
12+
@abstractmethod
1213
def stats(self):
1314
"""
1415
Return list of NamedMeasurable
1516
"""
16-
raise NotImplementedError
17+
pass
1718

1819

1920
class NamedMeasurable:

kafka/metrics/measurable.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33

4-
class AbstractMeasurable(metaclass=abc.ABCMeta):
4+
class AbstractMeasurable(ABC):
55
"""A measurable quantity that can be registered as a metric"""
6-
@abc.abstractmethod
6+
@abstractmethod
77
def measure(self, config, now):
88
"""
99
Measure this quantity and return the result
@@ -16,7 +16,7 @@ def measure(self, config, now):
1616
Returns:
1717
The measured value
1818
"""
19-
raise NotImplementedError
19+
pass
2020

2121

2222
class AnonMeasurable(AbstractMeasurable):

kafka/metrics/measurable_stat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import abc
1+
from abc import ABC
22

33
from kafka.metrics.measurable import AbstractMeasurable
44
from kafka.metrics.stat import AbstractStat
55

66

7-
class AbstractMeasurableStat(AbstractStat, AbstractMeasurable, metaclass=abc.ABCMeta):
7+
class AbstractMeasurableStat(AbstractStat, AbstractMeasurable, ABC):
88
"""
99
An AbstractMeasurableStat is an AbstractStat that is also
1010
an AbstractMeasurable (i.e. can produce a single floating point value).

kafka/metrics/metrics_reporter.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33

4-
class AbstractMetricsReporter(metaclass=abc.ABCMeta):
4+
class AbstractMetricsReporter(ABC):
55
"""
66
An abstract class to allow things to listen as new metrics
77
are created so they can be reported.
88
"""
9-
@abc.abstractmethod
9+
@abstractmethod
1010
def init(self, metrics):
1111
"""
1212
This is called when the reporter is first registered
@@ -15,39 +15,39 @@ def init(self, metrics):
1515
Arguments:
1616
metrics (list of KafkaMetric): All currently existing metrics
1717
"""
18-
raise NotImplementedError
18+
pass
1919

20-
@abc.abstractmethod
20+
@abstractmethod
2121
def metric_change(self, metric):
2222
"""
2323
This is called whenever a metric is updated or added
2424
2525
Arguments:
2626
metric (KafkaMetric)
2727
"""
28-
raise NotImplementedError
28+
pass
2929

30-
@abc.abstractmethod
30+
@abstractmethod
3131
def metric_removal(self, metric):
3232
"""
3333
This is called whenever a metric is removed
3434
3535
Arguments:
3636
metric (KafkaMetric)
3737
"""
38-
raise NotImplementedError
38+
pass
3939

40-
@abc.abstractmethod
40+
@abstractmethod
4141
def configure(self, configs):
4242
"""
4343
Configure this class with the given key-value pairs
4444
4545
Arguments:
4646
configs (dict of {str, ?})
4747
"""
48-
raise NotImplementedError
48+
pass
4949

50-
@abc.abstractmethod
50+
@abstractmethod
5151
def close(self):
5252
"""Called when the metrics repository is closed."""
53-
raise NotImplementedError
53+
pass

kafka/metrics/stat.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33

4-
class AbstractStat(metaclass=abc.ABCMeta):
4+
class AbstractStat(ABC):
55
"""
66
An AbstractStat is a quantity such as average, max, etc that is computed
77
off the stream of updates to a sensor
88
"""
9-
@abc.abstractmethod
9+
@abstractmethod
1010
def record(self, config, value, time_ms):
1111
"""
1212
Record the given value
@@ -16,4 +16,4 @@ def record(self, config, value, time_ms):
1616
value (float): The value to record
1717
timeMs (int): The POSIX time in milliseconds this value occurred
1818
"""
19-
raise NotImplementedError
19+
pass

kafka/metrics/stats/sampled_stat.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33
from kafka.metrics.measurable_stat import AbstractMeasurableStat
44

55

6-
class AbstractSampledStat(AbstractMeasurableStat, metaclass=abc.ABCMeta):
6+
class AbstractSampledStat(AbstractMeasurableStat, ABC):
77
"""
88
An AbstractSampledStat records a single scalar value measured over
99
one or more samples. Each sample is recorded over a configurable
@@ -25,13 +25,13 @@ def __init__(self, initial_value):
2525
self._samples = []
2626
self._current = 0
2727

28-
@abc.abstractmethod
28+
@abstractmethod
2929
def update(self, sample, config, value, time_ms):
30-
raise NotImplementedError
30+
pass
3131

32-
@abc.abstractmethod
32+
@abstractmethod
3333
def combine(self, samples, config, now):
34-
raise NotImplementedError
34+
pass
3535

3636
def record(self, config, value, time_ms):
3737
sample = self.current(time_ms)

kafka/partitioner/abc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import abc
1+
from abc import ABC, abstractmethod
22

33

4-
class Partitioner(metaclass=abc.ABCMeta):
5-
@abc.abstractmethod
4+
class Partitioner(ABC):
5+
@abstractmethod
66
def partition(self, topic, key, serialized_key, value, serialized_value, cluster):
77
pass
88

0 commit comments

Comments
 (0)