Skip to content

Commit

Permalink
Make an empty DiffSync instance evaluate to True.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kircheneer committed Jun 29, 2022
1 parent 46af6c4 commit 8fdaad6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions diffsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ def __str__(self):
def __repr__(self):
return f"<{str(self)}>"

def __bool__(self):
"""Always evaluate DiffSync instances as True.
This is needed because without it the __len__ method would be used, which in turn would cause empty DiffSync
instances to evaluate as False."""
return True

def __len__(self):
"""Total number of elements stored."""
return self.store.count()
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_diffsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,3 +894,7 @@ class NoDeleteInterfaceDiffSync(BackendA):
diff = extra_models.diff_from(backend_a)
print(diff.str()) # for debugging of any failure
assert not diff.has_diffs()


def test_diffsync_empty_instance_is_truthy():
assert bool(DiffSync())

0 comments on commit 8fdaad6

Please sign in to comment.