Skip to content

Commit 20c799c

Browse files
Leo KirchnerLeo Kirchner
authored andcommitted
Make an empty DiffSync instance evaluate to True.
1 parent b6792f5 commit 20c799c

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
# E501: Line length is enforced by Black, so flake8 doesn't need to check it
33
# W503: Black disagrees with this rule, as does PEP 8; Black wins
44
ignore = E501, W503
5+
exclude = .venv

diffsync/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,13 @@ def __str__(self) -> StrType:
459459
def __repr__(self) -> StrType:
460460
return f"<{str(self)}>"
461461

462+
def __bool__(self) -> bool:
463+
"""Always evaluate DiffSync instances as True.
464+
465+
This is needed because without it the __len__ method would be used, which in turn would cause empty DiffSync
466+
instances to evaluate as False."""
467+
return True
468+
462469
def __len__(self) -> int:
463470
"""Total number of elements stored."""
464471
return self.store.count()

tests/unit/test_diffsync.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,10 @@ class NoDeleteInterfaceDiffSync(BackendA):
992992
assert not diff.has_diffs()
993993

994994

995+
def test_diffsync_empty_instance_is_truthy():
996+
assert bool(Adapter())
997+
998+
995999
def test_diffsync_tree_traversal():
9961000
assert BackendA.get_tree_traversal(True) == {"site": {"device": {"interface": {}}, "person": {}}, "unused": {}}
9971001
text = """\

0 commit comments

Comments
 (0)