Skip to content

Commit 8c6c9da

Browse files
authored
Switch protocol code to getattr/setattr from __dict__ (#2654)
1 parent c8d6cda commit 8c6c9da

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

kafka/admin/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,7 @@ def _describe_consumer_groups_process_response(self, response):
12361236
for response_field, response_name in zip(response.SCHEMA.fields, response.SCHEMA.names):
12371237
if isinstance(response_field, Array):
12381238
described_groups_field_schema = response_field.array_of
1239-
described_group = response.__dict__[response_name][0]
1239+
described_group = getattr(response, response_name)[0]
12401240
described_group_information_list = []
12411241
protocol_type_is_consumer = False
12421242
for (described_group_information, group_information_name, group_information_field) in zip(described_group, described_groups_field_schema.names, described_groups_field_schema.fields):

kafka/protocol/struct.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class Struct(AbstractType):
1414
def __init__(self, *args, **kwargs):
1515
if len(args) == len(self.SCHEMA.fields):
1616
for i, name in enumerate(self.SCHEMA.names):
17-
self.__dict__[name] = args[i]
17+
setattr(self, name, args[i])
1818
elif len(args) > 0:
1919
raise ValueError('Args must be empty or mirror schema')
2020
else:
2121
for name in self.SCHEMA.names:
22-
self.__dict__[name] = kwargs.pop(name, None)
22+
setattr(self, name, kwargs.pop(name, None))
2323
if kwargs:
2424
raise ValueError('Keyword(s) not in schema %s: %s'
2525
% (list(self.SCHEMA.names),
@@ -30,7 +30,6 @@ def __init__(self, *args, **kwargs):
3030
# causes instances to "leak" to garbage
3131
self.encode = WeakMethod(self._encode_self)
3232

33-
3433
@classmethod
3534
def encode(cls, item): # pylint: disable=E0202
3635
bits = []
@@ -40,7 +39,7 @@ def encode(cls, item): # pylint: disable=E0202
4039

4140
def _encode_self(self):
4241
return self.SCHEMA.encode(
43-
[self.__dict__[name] for name in self.SCHEMA.names]
42+
[getattr(self, name) for name in self.SCHEMA.names]
4443
)
4544

4645
@classmethod
@@ -52,12 +51,12 @@ def decode(cls, data):
5251
def get_item(self, name):
5352
if name not in self.SCHEMA.names:
5453
raise KeyError("%s is not in the schema" % name)
55-
return self.__dict__[name]
54+
return getattr(self, name)
5655

5756
def __repr__(self):
5857
key_vals = []
5958
for name, field in zip(self.SCHEMA.names, self.SCHEMA.fields):
60-
key_vals.append('%s=%s' % (name, field.repr(self.__dict__[name])))
59+
key_vals.append('%s=%s' % (name, field.repr(getattr(self, name))))
6160
return self.__class__.__name__ + '(' + ', '.join(key_vals) + ')'
6261

6362
def __hash__(self):
@@ -67,6 +66,6 @@ def __eq__(self, other):
6766
if self.SCHEMA != other.SCHEMA:
6867
return False
6968
for attr in self.SCHEMA.names:
70-
if self.__dict__[attr] != other.__dict__[attr]:
69+
if getattr(self, attr) != getattr(other, attr):
7170
return False
7271
return True

test/test_object_conversion.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class TestClass(superclass):
2121
assert tc.get_item('myobject') == 0
2222
with pytest.raises(KeyError):
2323
tc.get_item('does-not-exist')
24-
24+
2525
def test_with_empty_schema(self, superclass):
2626
class TestClass(superclass):
2727
API_KEY = 0
@@ -86,7 +86,7 @@ class TestClass(superclass):
8686
('subobject', Int16),
8787
('othersubobject', String('utf-8')))),
8888
('notarray', Int16))
89-
89+
9090
tc = TestClass(
9191
myarray=[[10, 'hello']],
9292
notarray=42
@@ -185,7 +185,6 @@ def test_with_metadata_response():
185185
]]
186186
)
187187
tc.encode() # Make sure this object encodes successfully
188-
189188

190189
obj = tc.to_object()
191190

0 commit comments

Comments
 (0)