Skip to content

Commit 8fdaad6

Browse files
author
Leo Kirchner
committed
Make an empty DiffSync instance evaluate to True.
1 parent 46af6c4 commit 8fdaad6

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

diffsync/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,13 @@ def __str__(self):
456456
def __repr__(self):
457457
return f"<{str(self)}>"
458458

459+
def __bool__(self):
460+
"""Always evaluate DiffSync instances as True.
461+
462+
This is needed because without it the __len__ method would be used, which in turn would cause empty DiffSync
463+
instances to evaluate as False."""
464+
return True
465+
459466
def __len__(self):
460467
"""Total number of elements stored."""
461468
return self.store.count()

tests/unit/test_diffsync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,7 @@ class NoDeleteInterfaceDiffSync(BackendA):
894894
diff = extra_models.diff_from(backend_a)
895895
print(diff.str()) # for debugging of any failure
896896
assert not diff.has_diffs()
897+
898+
899+
def test_diffsync_empty_instance_is_truthy():
900+
assert bool(DiffSync())

0 commit comments

Comments
 (0)