diff --git a/diffsync/__init__.py b/diffsync/__init__.py index 2191961..5e9704c 100644 --- a/diffsync/__init__.py +++ b/diffsync/__init__.py @@ -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() diff --git a/tests/unit/test_diffsync.py b/tests/unit/test_diffsync.py index 9e8ccb3..fd36e09 100644 --- a/tests/unit/test_diffsync.py +++ b/tests/unit/test_diffsync.py @@ -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())