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 Jan 22, 2024
1 parent b6792f5 commit 7c8fb8e
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 @@ -459,6 +459,13 @@ def __str__(self) -> StrType:
def __repr__(self) -> StrType:
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) -> int:
"""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 @@ -992,6 +992,10 @@ class NoDeleteInterfaceDiffSync(BackendA):
assert not diff.has_diffs()


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


def test_diffsync_tree_traversal():
assert BackendA.get_tree_traversal(True) == {"site": {"device": {"interface": {}}, "person": {}}, "unused": {}}
text = """\
Expand Down

0 comments on commit 7c8fb8e

Please sign in to comment.