Skip to content

Commit 6588d35

Browse files
Fix __eq__ and __ne__. (#3765)
1 parent ee17fdc commit 6588d35

File tree

7 files changed

+76
-39
lines changed

7 files changed

+76
-39
lines changed

packages/google-cloud-bigtable/google/cloud/bigtable/cluster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def name(self):
159159

160160
def __eq__(self, other):
161161
if not isinstance(other, self.__class__):
162-
return False
162+
return NotImplemented
163163
# NOTE: This does not compare the configuration values, such as
164164
# the serve_nodes. Instead, it only compares
165165
# identifying values instance, cluster ID and client. This is
@@ -170,7 +170,7 @@ def __eq__(self, other):
170170
other._instance == self._instance)
171171

172172
def __ne__(self, other):
173-
return not self.__eq__(other)
173+
return not self == other
174174

175175
def reload(self):
176176
"""Reload the metadata for this cluster."""

packages/google-cloud-bigtable/google/cloud/bigtable/column_family.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class GarbageCollectionRule(object):
3939
don't support that feature and instead support via native classes.
4040
"""
4141

42-
def __ne__(self, other):
43-
return not self.__eq__(other)
44-
4542

4643
class MaxVersionsGCRule(GarbageCollectionRule):
4744
"""Garbage collection limiting the number of versions of a cell.
@@ -55,9 +52,12 @@ def __init__(self, max_num_versions):
5552

5653
def __eq__(self, other):
5754
if not isinstance(other, self.__class__):
58-
return False
55+
return NotImplemented
5956
return other.max_num_versions == self.max_num_versions
6057

58+
def __ne__(self, other):
59+
return not self == other
60+
6161
def to_pb(self):
6262
"""Converts the garbage collection rule to a protobuf.
6363
@@ -79,9 +79,12 @@ def __init__(self, max_age):
7979

8080
def __eq__(self, other):
8181
if not isinstance(other, self.__class__):
82-
return False
82+
return NotImplemented
8383
return other.max_age == self.max_age
8484

85+
def __ne__(self, other):
86+
return not self == other
87+
8588
def to_pb(self):
8689
"""Converts the garbage collection rule to a protobuf.
8790
@@ -104,9 +107,12 @@ def __init__(self, rules):
104107

105108
def __eq__(self, other):
106109
if not isinstance(other, self.__class__):
107-
return False
110+
return NotImplemented
108111
return other.rules == self.rules
109112

113+
def __ne__(self, other):
114+
return not self == other
115+
110116
def to_pb(self):
111117
"""Converts the union into a single GC rule as a protobuf.
112118
@@ -130,9 +136,12 @@ def __init__(self, rules):
130136

131137
def __eq__(self, other):
132138
if not isinstance(other, self.__class__):
133-
return False
139+
return NotImplemented
134140
return other.rules == self.rules
135141

142+
def __ne__(self, other):
143+
return not self == other
144+
136145
def to_pb(self):
137146
"""Converts the intersection into a single GC rule as a protobuf.
138147
@@ -190,13 +199,13 @@ def name(self):
190199

191200
def __eq__(self, other):
192201
if not isinstance(other, self.__class__):
193-
return False
202+
return NotImplemented
194203
return (other.column_family_id == self.column_family_id and
195204
other._table == self._table and
196205
other.gc_rule == self.gc_rule)
197206

198207
def __ne__(self, other):
199-
return not self.__eq__(other)
208+
return not self == other
200209

201210
def to_pb(self):
202211
"""Converts the column family to a protobuf.

packages/google-cloud-bigtable/google/cloud/bigtable/instance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def name(self):
180180

181181
def __eq__(self, other):
182182
if not isinstance(other, self.__class__):
183-
return False
183+
return NotImplemented
184184
# NOTE: This does not compare the configuration values, such as
185185
# the display_name. Instead, it only compares
186186
# identifying values instance ID and client. This is
@@ -191,7 +191,7 @@ def __eq__(self, other):
191191
other._client == self._client)
192192

193193
def __ne__(self, other):
194-
return not self.__eq__(other)
194+
return not self == other
195195

196196
def reload(self):
197197
"""Reload the metadata for this instance."""

packages/google-cloud-bigtable/google/cloud/bigtable/row_data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ def from_pb(cls, cell_pb):
5858

5959
def __eq__(self, other):
6060
if not isinstance(other, self.__class__):
61-
return False
61+
return NotImplemented
6262
return (other.value == self.value and
6363
other.timestamp == self.timestamp and
6464
other.labels == self.labels)
6565

6666
def __ne__(self, other):
67-
return not self.__eq__(other)
67+
return not self == other
6868

6969

7070
class PartialCellData(object):
@@ -126,12 +126,12 @@ def __init__(self, row_key):
126126

127127
def __eq__(self, other):
128128
if not isinstance(other, self.__class__):
129-
return False
129+
return NotImplemented
130130
return (other._row_key == self._row_key and
131131
other._cells == self._cells)
132132

133133
def __ne__(self, other):
134-
return not self.__eq__(other)
134+
return not self == other
135135

136136
def to_dict(self):
137137
"""Convert the cells to a dictionary.
@@ -211,11 +211,11 @@ def __init__(self, response_iterator):
211211

212212
def __eq__(self, other):
213213
if not isinstance(other, self.__class__):
214-
return False
214+
return NotImplemented
215215
return other._response_iterator == self._response_iterator
216216

217217
def __ne__(self, other):
218-
return not self.__eq__(other)
218+
return not self == other
219219

220220
@property
221221
def state(self):

packages/google-cloud-bigtable/google/cloud/bigtable/row_filters.py

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class RowFilter(object):
3232
This class is a do-nothing base class for all row filters.
3333
"""
3434

35-
def __ne__(self, other):
36-
return not self.__eq__(other)
37-
3835

3936
class _BoolFilter(RowFilter):
4037
"""Row filter that uses a boolean flag.
@@ -48,9 +45,12 @@ def __init__(self, flag):
4845

4946
def __eq__(self, other):
5047
if not isinstance(other, self.__class__):
51-
return False
48+
return NotImplemented
5249
return other.flag == self.flag
5350

51+
def __ne__(self, other):
52+
return not self == other
53+
5454

5555
class SinkFilter(_BoolFilter):
5656
"""Advanced row filter to skip parent filters.
@@ -124,9 +124,12 @@ def __init__(self, regex):
124124

125125
def __eq__(self, other):
126126
if not isinstance(other, self.__class__):
127-
return False
127+
return NotImplemented
128128
return other.regex == self.regex
129129

130+
def __ne__(self, other):
131+
return not self == other
132+
130133

131134
class RowKeyRegexFilter(_RegexFilter):
132135
"""Row filter for a row key regular expression.
@@ -173,9 +176,12 @@ def __init__(self, sample):
173176

174177
def __eq__(self, other):
175178
if not isinstance(other, self.__class__):
176-
return False
179+
return NotImplemented
177180
return other.sample == self.sample
178181

182+
def __ne__(self, other):
183+
return not self == other
184+
179185
def to_pb(self):
180186
"""Converts the row filter to a protobuf.
181187
@@ -257,12 +263,12 @@ def __init__(self, start=None, end=None):
257263

258264
def __eq__(self, other):
259265
if not isinstance(other, self.__class__):
260-
return False
266+
return NotImplemented
261267
return (other.start == self.start and
262268
other.end == self.end)
263269

264270
def __ne__(self, other):
265-
return not self.__eq__(other)
271+
return not self == other
266272

267273
def to_pb(self):
268274
"""Converts the :class:`TimestampRange` to a protobuf.
@@ -292,9 +298,12 @@ def __init__(self, range_):
292298

293299
def __eq__(self, other):
294300
if not isinstance(other, self.__class__):
295-
return False
301+
return NotImplemented
296302
return other.range_ == self.range_
297303

304+
def __ne__(self, other):
305+
return not self == other
306+
298307
def to_pb(self):
299308
"""Converts the row filter to a protobuf.
300309
@@ -367,13 +376,16 @@ def __init__(self, column_family_id, start_column=None, end_column=None,
367376

368377
def __eq__(self, other):
369378
if not isinstance(other, self.__class__):
370-
return False
379+
return NotImplemented
371380
return (other.column_family_id == self.column_family_id and
372381
other.start_column == self.start_column and
373382
other.end_column == self.end_column and
374383
other.inclusive_start == self.inclusive_start and
375384
other.inclusive_end == self.inclusive_end)
376385

386+
def __ne__(self, other):
387+
return not self == other
388+
377389
def to_pb(self):
378390
"""Converts the row filter to a protobuf.
379391
@@ -485,12 +497,15 @@ def __init__(self, start_value=None, end_value=None,
485497

486498
def __eq__(self, other):
487499
if not isinstance(other, self.__class__):
488-
return False
500+
return NotImplemented
489501
return (other.start_value == self.start_value and
490502
other.end_value == self.end_value and
491503
other.inclusive_start == self.inclusive_start and
492504
other.inclusive_end == self.inclusive_end)
493505

506+
def __ne__(self, other):
507+
return not self == other
508+
494509
def to_pb(self):
495510
"""Converts the row filter to a protobuf.
496511
@@ -533,9 +548,12 @@ def __init__(self, num_cells):
533548

534549
def __eq__(self, other):
535550
if not isinstance(other, self.__class__):
536-
return False
551+
return NotImplemented
537552
return other.num_cells == self.num_cells
538553

554+
def __ne__(self, other):
555+
return not self == other
556+
539557

540558
class CellsRowOffsetFilter(_CellCountFilter):
541559
"""Row filter to skip cells in a row.
@@ -631,9 +649,12 @@ def __init__(self, label):
631649

632650
def __eq__(self, other):
633651
if not isinstance(other, self.__class__):
634-
return False
652+
return NotImplemented
635653
return other.label == self.label
636654

655+
def __ne__(self, other):
656+
return not self == other
657+
637658
def to_pb(self):
638659
"""Converts the row filter to a protobuf.
639660
@@ -661,9 +682,12 @@ def __init__(self, filters=None):
661682

662683
def __eq__(self, other):
663684
if not isinstance(other, self.__class__):
664-
return False
685+
return NotImplemented
665686
return other.filters == self.filters
666687

688+
def __ne__(self, other):
689+
return not self == other
690+
667691

668692
class RowFilterChain(_FilterCombination):
669693
"""Chain of row filters.
@@ -748,11 +772,14 @@ def __init__(self, base_filter, true_filter=None, false_filter=None):
748772

749773
def __eq__(self, other):
750774
if not isinstance(other, self.__class__):
751-
return False
775+
return NotImplemented
752776
return (other.base_filter == self.base_filter and
753777
other.true_filter == self.true_filter and
754778
other.false_filter == self.false_filter)
755779

780+
def __ne__(self, other):
781+
return not self == other
782+
756783
def to_pb(self):
757784
"""Converts the row filter to a protobuf.
758785

packages/google-cloud-bigtable/google/cloud/bigtable/table.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ def row(self, row_key, filter_=None, append=False):
142142

143143
def __eq__(self, other):
144144
if not isinstance(other, self.__class__):
145-
return False
145+
return NotImplemented
146146
return (other.table_id == self.table_id and
147147
other._instance == self._instance)
148148

149149
def __ne__(self, other):
150-
return not self.__eq__(other)
150+
return not self == other
151151

152152
def create(self, initial_split_keys=None, column_families=()):
153153
"""Creates this table.

packages/google-cloud-bigtable/tests/unit/test_column_family.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
1615
import unittest
1716

17+
import mock
18+
1819

1920
class TestMaxVersionsGCRule(unittest.TestCase):
2021

@@ -29,8 +30,8 @@ def _make_one(self, *args, **kwargs):
2930

3031
def test___eq__type_differ(self):
3132
gc_rule1 = self._make_one(10)
32-
gc_rule2 = object()
33-
self.assertNotEqual(gc_rule1, gc_rule2)
33+
self.assertNotEqual(gc_rule1, object())
34+
self.assertEqual(gc_rule1, mock.ANY)
3435

3536
def test___eq__same_value(self):
3637
gc_rule1 = self._make_one(2)

0 commit comments

Comments
 (0)